
"""simple docstring"""





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

							_a														=		"""0.12"""  # assumed parallelism: 8


if is_torch_available():
							import torch



def 				lowerCamelCase__		(      __snake_case,					__snake_case,					__snake_case=None     )							->		Union[str, Any]:


					"""simple docstring"""





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

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

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

					_UpperCamelCase										=  np.array(_lowerCAmelCase,					dtype=jnp.intaa     ).reshape(_lowerCAmelCase     )

					return output



def 				lowerCamelCase__		(      __snake_case,					__snake_case=None     )							->		Any:


					"""simple docstring"""





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






@require_flax
class    _UpperCAmelCase:
		lowercase__      =				None
		lowercase__      =				()



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs_for_common()

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

							_UpperCamelCase										=  jnp.ones_like(snake_case_)
							_UpperCamelCase										=  attention_mask[:max_batch_size, :sequence_length]

							# generate max 5 tokens
							_UpperCamelCase										=  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()`
												_UpperCamelCase										=  config.eos_token_id
							return config, input_ids, attention_mask, max_length



		@is_pt_flax_cross_test
		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  False
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  0

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  flax_model.generate(snake_case_).sequences
												_UpperCamelCase										=  pt_model.generate(torch.tensor(snake_case_    ,		dtype=torch.long))

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

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



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  False
							_UpperCamelCase										=  max_length

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



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






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  True
							_UpperCamelCase										=  max_length

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  False
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  2

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  False
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  2
							_UpperCamelCase										=  2

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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



		def 						UpperCAmelCase							( self)	->	int:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  True
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  0.8
							_UpperCamelCase										=  10
							_UpperCamelCase										=  0.3
							_UpperCamelCase										=  1
							_UpperCamelCase										=  8
							_UpperCamelCase										=  9

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  1
							_UpperCamelCase										=  8
							_UpperCamelCase										=  9

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()
							_UpperCamelCase										=  max_length
							_UpperCamelCase										=  2
							_UpperCamelCase										=  1
							_UpperCamelCase										=  8
							_UpperCamelCase										=  9

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()

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

							_UpperCamelCase										=  False
							_UpperCamelCase										=  max_length

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_    ,		attention_mask=snake_case_).sequences

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



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()

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

							_UpperCamelCase										=  True
							_UpperCamelCase										=  max_length

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_    ,		attention_mask=snake_case_).sequences

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




		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self._get_input_ids_and_config()

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

							_UpperCamelCase										=  2
							_UpperCamelCase										=  max_length

							for model_class in self.all_generative_model_classes:
												_UpperCamelCase										=  model_class(snake_case_)

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

												_UpperCamelCase										=  jit(model.generate)
												_UpperCamelCase										=  jit_generate(snake_case_    ,		attention_mask=snake_case_).sequences

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






@require_flax
class    _UpperCAmelCase(   unittest.TestCase     ):



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






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

							_UpperCamelCase										=  """Hello world"""
							_UpperCamelCase										=  tokenizer(snake_case_    ,		return_tensors='''np''').input_ids

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

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








"""simple docstring"""





import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402


_a														=		get_tests_dir("""fixtures/dummy-config.json""")






class    _UpperCAmelCase(   unittest.TestCase     ):



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  0



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							self.assertIsNotNone(transformers.models.auto.__spec__)
							self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''))



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained('''bert-base-uncased''')
							self.assertIsInstance(__a    ,		__a)



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
							self.assertIsInstance(__a    ,		__a)



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
							self.assertIsInstance(__a    ,		__a)



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






							'''simple docstring'''
							_UpperCamelCase										=  AutoConfig.for_model('''roberta''')
							self.assertIsInstance(__a    ,		__a)



		def 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							with tempfile.TemporaryDirectory() as tmp_dir:
												# This model name contains bert and roberta, but roberta ends up being picked.
												_UpperCamelCase										=  os.path.join(__a    ,		'''fake-roberta''')
												os.makedirs(__a    ,		exist_ok=__a)
												with open(os.path.join(__a    ,		'''config.json''')    ,		'''w''') as f:
																	f.write(json.dumps({}))
												_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
												self.assertEqual(type(__a)    ,		__a)



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							try:
												AutoConfig.register('''custom'''    ,		__a)
												# Wrong model type will raise an error
												with self.assertRaises(__a):
																	AutoConfig.register('''model'''    ,		__a)
												# Trying to register something existing in the Transformers library will raise an error
												with self.assertRaises(__a):
																	AutoConfig.register('''bert'''    ,		__a)

												# Now that the config is registered, it can be used as any other config with the auto-API
												_UpperCamelCase										=  CustomConfig()
												with tempfile.TemporaryDirectory() as tmp_dir:
																	config.save_pretrained(__a)
																	_UpperCamelCase										=  AutoConfig.from_pretrained(__a)
																	self.assertIsInstance(__a    ,		__a)

							finally:
												if "custom" in CONFIG_MAPPING._extra_content:
																	del CONFIG_MAPPING._extra_content["custom"]



		def 						UpperCAmelCase							( self)	->	List[Any]:






							'''simple docstring'''
							with self.assertRaisesRegex(
							    __a    ,		'''bert-base is not a local folder and is not a valid model identifier'''):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''bert-base''')



		def 						UpperCAmelCase							( self)	->	List[Any]:






							'''simple docstring'''
							with self.assertRaisesRegex(
							    __a    ,		R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''):
												_UpperCamelCase										=  AutoConfig.from_pretrained(__a    ,		revision='''aaaaaa''')



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							with self.assertRaisesRegex(
							    __a    ,		'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''    ,		):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo''')



		def 						UpperCAmelCase							( self)	->	int:






							'''simple docstring'''
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(__a):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''')
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(__a):
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,		trust_remote_code=__a)

							_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,		trust_remote_code=__a)
							self.assertEqual(config.__class__.__name__    ,		'''NewModelConfig''')

							# Test config can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
												config.save_pretrained(__a)
												_UpperCamelCase										=  AutoConfig.from_pretrained(__a    ,		trust_remote_code=__a)
							self.assertEqual(reloaded_config.__class__.__name__    ,		'''NewModelConfig''')




		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''






							class    _UpperCAmelCase(   lowerCamelCase     ):
									lowercase__      =				'new-model'

							try:
												AutoConfig.register('''new-model'''    ,		__a)
												# If remote code is not set, the default is to use local
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''')
												self.assertEqual(config.__class__.__name__    ,		'''NewModelConfigLocal''')

												# If remote code is disabled, we load the local one.
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,		trust_remote_code=__a)
												self.assertEqual(config.__class__.__name__    ,		'''NewModelConfigLocal''')

												# If remote is enabled, we load from the Hub
												_UpperCamelCase										=  AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,		trust_remote_code=__a)
												self.assertEqual(config.__class__.__name__    ,		'''NewModelConfig''')

							finally:
												if "new-model" in CONFIG_MAPPING._extra_content:
																	del CONFIG_MAPPING._extra_content["new-model"]






