"""simple docstring"""







from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_a=			logging.get_logger(__name__)


_a=			{
    "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json",
    "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json",
    "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json",
    "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json",
    "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json",
    "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json",
    "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json",
    "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json",
    "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json",
    "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json",
    "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json",
    "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json",
}

class 		UpperCamelCase      (   _SCREAMING_SNAKE_CASE						):
				UpperCAmelCase    :				Any   =		"""codegen"""
				UpperCAmelCase    :				Optional[int]   =		{
				    """max_position_embeddings""": """n_positions""",
				    """hidden_size""": """n_embd""",
				    """num_attention_heads""": """n_head""",
				    """num_hidden_layers""": """n_layer""",
				}



				def __init__(self    :				Union[str, Any]     ,  _A    :				Optional[Any]=5_04_00     ,  _A    :				Tuple=20_48     ,  _A    :				str=20_48     ,  _A    :				Tuple=40_96     ,  _A    :				str=28     ,  _A    :				Dict=16     ,  _A    :				Optional[Any]=64     ,  _A    :				Optional[int]=None     ,  _A    :				List[Any]="gelu_new"     ,  _A    :				str=0.0     ,  _A    :				str=0.0     ,  _A    :				int=0.0     ,  _A    :				List[str]=1E-5     ,  _A    :				Optional[Any]=0.02     ,  _A    :				Optional[Any]=True     ,  _A    :				str=5_02_56     ,  _A    :				int=5_02_56     ,  _A    :				Tuple=False     ,  **_A    :				List[Any]     ,  )   ->     Dict:
					__snake_case      :							Optional[Any]						=			vocab_size
					__snake_case      :							Union[str, Any]						=			n_ctx
					__snake_case      :							int						=			n_positions
					__snake_case      :							int						=			n_embd
					__snake_case      :							List[str]						=			n_layer
					__snake_case      :							str						=			n_head
					__snake_case      :							Tuple						=			n_inner
					__snake_case      :							Dict						=			rotary_dim
					__snake_case      :							Tuple						=			activation_function
					__snake_case      :							Union[str, Any]						=			resid_pdrop
					__snake_case      :							Optional[int]						=			embd_pdrop
					__snake_case      :							Optional[Any]						=			attn_pdrop
					__snake_case      :							Any						=			layer_norm_epsilon
					__snake_case      :							List[Any]						=			initializer_range
					__snake_case      :							Tuple						=			use_cache

					__snake_case      :							Tuple						=			bos_token_id
					__snake_case      :							Union[str, Any]						=			eos_token_id

					super().__init__(
					    bos_token_id=_A     ,  eos_token_id=_A     ,  tie_word_embeddings=_A     ,  **_A)







class 		UpperCamelCase      (   _SCREAMING_SNAKE_CASE						):



				def __init__(self    :				List[str]     ,  _A    :				PretrainedConfig     ,  _A    :				str = "default"     ,  _A    :				List[PatchingSpec] = None     ,  _A    :				bool = False     ,  )   ->     Union[str, Any]:
					super().__init__(_A     ,  task=_A     ,  patching_specs=_A     ,  use_past=_A)
					if not getattr(self._config     ,  'pad_token_id'     ,  _A):
						# TODO: how to do that better?
						__snake_case      :							Optional[Any]						=			0



				@property
				def  _lowercase					(self    :				List[str])   ->     Mapping[str, Mapping[int, str]]:
					__snake_case      :							Union[str, Any]						=			OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}})
					if self.use_past:
						self.fill_with_past_key_values_(_A     ,  direction='inputs')
						__snake_case      :							Optional[int]						=			{0: """batch""", 1: """past_sequence + sequence"""}
					else:
						__snake_case      :							int						=			{0: """batch""", 1: """sequence"""}

					return common_inputs



				@property
				def  _lowercase					(self    :				int)   ->     int:
					return self._config.n_layer



				@property
				def  _lowercase					(self    :				int)   ->     int:
					return self._config.n_head



				def  _lowercase					(self    :				int     ,  _A    :				PreTrainedTokenizer     ,  _A    :				int = -1     ,  _A    :				int = -1     ,  _A    :				bool = False     ,  _A    :				Optional[TensorType] = None     ,  )   ->     Mapping[str, Any]:
					__snake_case      :							Dict						=			super(_A     ,  self).generate_dummy_inputs(
					    _A     ,  batch_size=_A     ,  seq_length=_A     ,  is_pair=_A     ,  framework=_A)

					# We need to order the input in the way they appears in the forward()
					__snake_case      :							Tuple						=			OrderedDict({'input_ids': common_inputs['input_ids']})

					# Need to add the past_keys
					if self.use_past:
						if not is_torch_available():
							raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.')
						else:
							import torch

							__snake_case      :							Optional[int]						=			common_inputs["""input_ids"""].shape
							# Not using the same length for past_key_values
							__snake_case      :							Dict						=			seqlen + 2
							__snake_case      :							Optional[int]						=			(
							    batch,
							    self.num_attention_heads,
							    past_key_values_length,
							    self._config.hidden_size // self.num_attention_heads,
							)
							__snake_case      :							Optional[Any]						=			[
							    (torch.zeros(_A), torch.zeros(_A)) for _ in range(self.num_layers)
							]

					__snake_case      :							Union[str, Any]						=			common_inputs["""attention_mask"""]
					if self.use_past:
						__snake_case      :							Union[str, Any]						=			ordered_inputs["""attention_mask"""].dtype
						__snake_case      :							Any						=			torch.cat(
						    [ordered_inputs['attention_mask'], torch.ones(_A     ,  _A     ,  dtype=_A)]     ,  dim=1)

					return ordered_inputs






				@property
				def  _lowercase					(self    :				List[str])   ->     int:
					return 13






"""simple docstring"""







from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
	from transformers import (
	    TFAutoModel,
	    TFAutoModelForCausalLM,
	    TFAutoModelForMaskedLM,
	    TFAutoModelForPreTraining,
	    TFAutoModelForQuestionAnswering,
	    TFAutoModelForSeqaSeqLM,
	    TFAutoModelForSequenceClassification,
	    TFAutoModelForTableQuestionAnswering,
	    TFAutoModelForTokenClassification,
	    TFAutoModelWithLMHead,
	    TFBertForMaskedLM,
	    TFBertForPreTraining,
	    TFBertForQuestionAnswering,
	    TFBertForSequenceClassification,
	    TFBertModel,
	    TFFunnelBaseModel,
	    TFFunnelModel,
	    TFGPTaLMHeadModel,
	    TFRobertaForMaskedLM,
	    TFTaForConditionalGeneration,
	    TFTapasForQuestionAnswering,
	)
	from transformers.models.auto.modeling_tf_auto import (
	    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
	    TF_MODEL_FOR_MASKED_LM_MAPPING,
	    TF_MODEL_FOR_PRETRAINING_MAPPING,
	    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
	    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
	    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
	    TF_MODEL_MAPPING,
	)
	from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
	from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST

class 		UpperCamelCase      (   lowercase						):
				UpperCAmelCase    :				Optional[int]   =		"""new-model"""


if is_tf_available():

	class 		UpperCamelCase      (   lowercase						):
					UpperCAmelCase    :				List[str]   =		NewModelConfig

@require_tf
class 		UpperCamelCase      (   unittest.TestCase						):



				@slow
				def  _lowercase					(self    :				List[str])   ->     Dict:
					__snake_case      :							Any						=			'bert-base-cased'
					__snake_case      :							Optional[Any]						=			AutoConfig.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)

					__snake_case      :							Union[str, Any]						=			TFAutoModel.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				List[Any])   ->     str:
					__snake_case      :							Optional[int]						=			'bert-base-cased'
					__snake_case      :							List[Any]						=			AutoConfig.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)

					__snake_case      :							Dict						=			TFAutoModelForPreTraining.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Any)   ->     List[str]:
					for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							str						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							List[str]						=			TFAutoModelForCausalLM.from_pretrained(_A)
						__snake_case		,    __snake_case      :							List[str]						=			TFAutoModelForCausalLM.from_pretrained(_A     ,  output_loading_info=_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Tuple)   ->     Dict:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Union[str, Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Union[str, Any]						=			TFAutoModelWithLMHead.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[int]:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Union[str, Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Optional[Any]						=			TFAutoModelForMaskedLM.from_pretrained(_A)
						__snake_case		,    __snake_case      :							Optional[int]						=			TFAutoModelForMaskedLM.from_pretrained(_A     ,  output_loading_info=_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				str)   ->     Union[str, Any]:
					for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Dict						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							str						=			TFAutoModelForSeqaSeqLM.from_pretrained(_A)
						__snake_case		,    __snake_case      :							Union[str, Any]						=			TFAutoModelForSeqaSeqLM.from_pretrained(_A     ,  output_loading_info=_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				str)   ->     str:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						__snake_case      :							Tuple						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Tuple						=			TFAutoModelForSequenceClassification.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Optional[Any])   ->     Optional[int]:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						__snake_case      :							List[str]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Any						=			TFAutoModelForQuestionAnswering.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				@require_tensorflow_probability
				def  _lowercase					(self    :				List[Any])   ->     List[str]:
					for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
						__snake_case      :							Optional[Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							int						=			TFAutoModelForTableQuestionAnswering.from_pretrained(_A)
						__snake_case		,    __snake_case      :							Dict						=			TFAutoModelForTableQuestionAnswering.from_pretrained(
						    _A     ,  output_loading_info=_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				Optional[Any])   ->     Optional[Any]:
					__snake_case      :							Optional[int]						=			TFAutoModelWithLMHead.from_pretrained(_A)
					self.assertIsInstance(_A     ,  _A)
					self.assertEqual(model.num_parameters()     ,  1_44_10)
					self.assertEqual(model.num_parameters(only_trainable=_A)     ,  1_44_10)



				def  _lowercase					(self    :				Any)   ->     List[str]:
					__snake_case      :							List[Any]						=			TFAutoModelWithLMHead.from_pretrained(_A)
					self.assertIsInstance(_A     ,  _A)
					self.assertEqual(model.num_parameters()     ,  1_44_10)
					self.assertEqual(model.num_parameters(only_trainable=_A)     ,  1_44_10)



				def  _lowercase					(self    :				Optional[Any])   ->     str:
					# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
					__snake_case      :							Optional[Any]						=			TFAutoModel.from_pretrained('sgugger/funnel-random-tiny')
					self.assertIsInstance(_A     ,  _A)

					__snake_case      :							int						=			copy.deepcopy(model.config)
					__snake_case      :							int						=			['FunnelBaseModel']
					__snake_case      :							int						=			TFAutoModel.from_config(_A)
					self.assertIsInstance(_A     ,  _A)

					with tempfile.TemporaryDirectory() as tmp_dir:
						model.save_pretrained(_A)
						__snake_case      :							List[Any]						=			TFAutoModel.from_pretrained(_A)
						self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				List[Any])   ->     int:
					try:
						AutoConfig.register('new-model'     ,  _A)

						__snake_case      :							int						=			[
						    TFAutoModel,
						    TFAutoModelForCausalLM,
						    TFAutoModelForMaskedLM,
						    TFAutoModelForPreTraining,
						    TFAutoModelForQuestionAnswering,
						    TFAutoModelForSequenceClassification,
						    TFAutoModelForTokenClassification,
						]

						for auto_class in auto_classes:
							with self.subTest(auto_class.__name__):
								# Wrong config class will raise an error
								with self.assertRaises(_A):
									auto_class.register(_A     ,  _A)
								auto_class.register(_A     ,  _A)
								# Trying to register something existing in the Transformers library will raise an error
								with self.assertRaises(_A):
									auto_class.register(_A     ,  _A)

								# Now that the config is registered, it can be used as any other config with the auto-API
								__snake_case      :							Union[str, Any]						=			BertModelTester(self).get_config()
								__snake_case      :							Optional[int]						=			NewModelConfig(**tiny_config.to_dict())
								__snake_case      :							List[str]						=			auto_class.from_config(_A)
								self.assertIsInstance(_A     ,  _A)

								with tempfile.TemporaryDirectory() as tmp_dir:
									model.save_pretrained(_A)
									__snake_case      :							Tuple						=			auto_class.from_pretrained(_A)
									self.assertIsInstance(_A     ,  _A)

					finally:
						if "new-model" in CONFIG_MAPPING._extra_content:
							del CONFIG_MAPPING._extra_content["new-model"]
						for mapping in (
						    TF_MODEL_MAPPING,
						    TF_MODEL_FOR_PRETRAINING_MAPPING,
						    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
						    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
						    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
						    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
						    TF_MODEL_FOR_MASKED_LM_MAPPING,
						):
							if NewModelConfig in mapping._extra_content:
								del mapping._extra_content[NewModelConfig]



				def  _lowercase					(self    :				Optional[int])   ->     Union[str, Any]:
					with self.assertRaisesRegex(
					    _A     ,  'bert-base is not a local folder and is not a valid model identifier'):
						__snake_case      :							Any						=			TFAutoModel.from_pretrained('bert-base')



				def  _lowercase					(self    :				str)   ->     str:
					with self.assertRaisesRegex(
					    _A     ,  r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
						__snake_case      :							Optional[Any]						=			TFAutoModel.from_pretrained(_A     ,  revision='aaaaaa')



				def  _lowercase					(self    :				int)   ->     Any:
					with self.assertRaisesRegex(
					    _A     ,  'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'     ,  ):
						__snake_case      :							List[str]						=			TFAutoModel.from_pretrained('hf-internal-testing/config-no-model')



				def  _lowercase					(self    :				Optional[Any])   ->     Any:
					with self.assertRaisesRegex(_A     ,  'Use `from_pt=True` to load this model'):
						__snake_case      :							List[str]						=			TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only')






				def  _lowercase					(self    :				str)   ->     Any:
					# Make sure we have cached the model.
					__snake_case      :							str						=			TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert')
					with RequestCounter() as counter:
						__snake_case      :							List[str]						=			TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert')
						self.assertEqual(counter.get_request_count     ,  0)
						self.assertEqual(counter.head_request_count     ,  1)
						self.assertEqual(counter.other_request_count     ,  0)

					# With a sharded checkpoint
					__snake_case      :							Optional[int]						=			TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded')
					with RequestCounter() as counter:
						__snake_case      :							Any						=			TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded')
						self.assertEqual(counter.get_request_count     ,  0)
						self.assertEqual(counter.head_request_count     ,  1)
						self.assertEqual(counter.other_request_count     ,  0)





