


import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
			import os

			import jax.numpy as jnp
			from jax import jit

			from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
			from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

			lowerCAmelCase       =							'0.12'  # assumed parallelism: 8


if is_torch_available():
			import torch
def 			_a (      SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE=None     ):


		"""simple docstring"""




		if rng is None:
				lowercase__            =      random.Random()

		lowercase__            =      1
		for dim in shape:
				total_dims *= dim

		lowercase__            =      []
		for _ in range(SCREAMING_SNAKE_CASE     ):
				values.append(rng.randint(0						,				vocab_size - 1     )     )

		lowercase__            =      np.array(SCREAMING_SNAKE_CASE						,				dtype=jnp.intaa     ).reshape(SCREAMING_SNAKE_CASE     )

		return output
def 			_a (      SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE=None     ):


		"""simple docstring"""




		lowercase__            =      ids_tensor(SCREAMING_SNAKE_CASE						,				vocab_size=2						,				rng=SCREAMING_SNAKE_CASE     )
		# make sure that at least one token is attended to for each batch
		lowercase__            =      1
		return attn_mask


@require_flax
class        _a							:
							_lowercase	:			List[Any]        = None
							_lowercase	:			List[Any]        = ()

							def       lowerCamelCase_ (							self:	Union[str, Any]							)			->   Tuple:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs_for_common()

									# cut to half length & take max batch_size 3
									lowercase__            =      2
									lowercase__            =      inputs['''input_ids'''].shape[-1] // 2
									lowercase__            =      inputs['''input_ids'''][:max_batch_size, :sequence_length]

									lowercase__            =      jnp.ones_like(UpperCamelCase_							)
									lowercase__            =      attention_mask[:max_batch_size, :sequence_length]

									# generate max 5 tokens
									lowercase__            =      input_ids.shape[-1] + 5
									if config.eos_token_id is not None and config.pad_token_id is None:
											# hack to allow generate for models such as GPT2 as is done in `generate()`
											lowercase__            =      config.eos_token_id
									return config, input_ids, attention_mask, max_length

							@is_pt_flax_cross_test
							def       lowerCamelCase_ (							self:	Tuple							)			->   Dict:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      False
									lowercase__            =      max_length
									lowercase__            =      0

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model_class.__name__[4:]  # Skip the "Flax" at the beginning
											lowercase__            =      getattr(UpperCamelCase_    ,						UpperCamelCase_							)
											lowercase__            =      pt_model_class(UpperCamelCase_							).eval()
											lowercase__            =      load_flax_weights_in_pytorch_model(UpperCamelCase_    ,						flax_model.params							)

											lowercase__            =      flax_model.generate(UpperCamelCase_							).sequences
											lowercase__            =      pt_model.generate(torch.tensor(UpperCamelCase_    ,						dtype=torch.long							)							)

											if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
													lowercase__            =      flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

											self.assertListEqual(pt_generation_outputs.numpy().tolist()    ,						flax_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	str							)			->   int:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      False
									lowercase__            =      max_length

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	int							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      True
									lowercase__            =      max_length

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	Any							)			->   int:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      False
									lowercase__            =      max_length
									lowercase__            =      2

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	List[Any]							)			->   List[str]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      False
									lowercase__            =      max_length
									lowercase__            =      2
									lowercase__            =      2

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[0]    ,						input_ids.shape[0] * config.num_return_sequences							)

							def       lowerCamelCase_ (							self:	List[Any]							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      True
									lowercase__            =      max_length
									lowercase__            =      0.8
									lowercase__            =      10
									lowercase__            =      0.3
									lowercase__            =      1
									lowercase__            =      8
									lowercase__            =      9

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	List[str]							)			->   List[Any]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      max_length
									lowercase__            =      1
									lowercase__            =      8
									lowercase__            =      9

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	str							)			->   Optional[Any]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()
									lowercase__            =      max_length
									lowercase__            =      2
									lowercase__            =      1
									lowercase__            =      8
									lowercase__            =      9

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	str							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()

									# pad attention mask on the left
									lowercase__            =      attention_mask.at[(0, 0)].set(0							)

									lowercase__            =      False
									lowercase__            =      max_length

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)

							def       lowerCamelCase_ (							self:	List[Any]							)			->   List[str]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()

									# pad attention mask on the left
									lowercase__            =      attention_mask.at[(0, 0)].set(0							)

									lowercase__            =      True
									lowercase__            =      max_length

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)


							def       lowerCamelCase_ (							self:	Union[str, Any]							)			->   List[str]:





									"""simple docstring"""







									lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      self._get_input_ids_and_config()

									# pad attention mask on the left
									lowercase__            =      attention_mask.at[(0, 0)].set(0							)

									lowercase__            =      2
									lowercase__            =      max_length

									for model_class in self.all_generative_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)

											lowercase__            =      model.generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences
											self.assertEqual(generation_outputs.shape[-1]    ,						UpperCamelCase_							)

											lowercase__            =      jit(model.generate							)
											lowercase__            =      jit_generate(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							).sequences

											self.assertListEqual(generation_outputs.tolist()    ,						jit_generation_outputs.tolist()							)


@require_flax
class        _a							(			unittest.TestCase			):

							def       lowerCamelCase_ (							self:	List[str]							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__            =      AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert'''							)
									lowercase__            =      FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''							)

									lowercase__            =      '''Hello world'''
									lowercase__            =      tokenizer(UpperCamelCase_    ,						return_tensors='''np'''							).input_ids

									# typos are quickly detected (the correct argument is `do_sample`)
									with self.assertRaisesRegex(UpperCamelCase_    ,						'''do_samples'''							):
											model.generate(UpperCamelCase_    ,						do_samples=UpperCamelCase_							)

									# arbitrary arguments that will not be used anywhere are also not accepted
									with self.assertRaisesRegex(UpperCamelCase_    ,						'''foo'''							):
											lowercase__            =      {'''foo''': '''bar'''}
											model.generate(UpperCamelCase_    ,						**UpperCamelCase_							)






def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')     )
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      credit_card_number
		lowercase__            =      0
		lowercase__            =      len(SCREAMING_SNAKE_CASE     ) - 2
		for i in range(SCREAMING_SNAKE_CASE						,				-1						,				-2     ):
				#  double the value of every second digit
				lowercase__            =      int(cc_number[i]     )
				digit *= 2
				# If doubling of a number results in a two digit number
				# i.e greater than 9(e.g., 6 × 2 = 12),
				# then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6),
				# to get a single digit number.
				if digit > 9:
						digit %= 10
						digit += 1
				lowercase__            =      cc_number[:i] + str(SCREAMING_SNAKE_CASE     ) + cc_number[i + 1 :]
				total += digit

		# Sum up the remaining digits
		for i in range(len(SCREAMING_SNAKE_CASE     ) - 1						,				-1						,				-2     ):
				total += int(cc_number[i]     )

		return total % 10 == 0
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      f'{credit_card_number} is an invalid credit card number because'
		if not credit_card_number.isdigit():
				print(f'{error_message} it has nonnumerical characters.'     )
				return False

		if not 13 <= len(SCREAMING_SNAKE_CASE     ) <= 16:
				print(f'{error_message} of its length.'     )
				return False

		if not validate_initial_digits(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} of its first two digits.'     )
				return False

		if not luhn_validation(SCREAMING_SNAKE_CASE     ):
				print(f'{error_message} it fails the Luhn check.'     )
				return False

		print(f'{credit_card_number} is a valid credit card number.'     )
		return True


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			validate_credit_card_number('4111111111111111')
			validate_credit_card_number('32323')


