



"""simple docstring"""







import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    GPTNeoXForCausalLM,
			    GPTNeoXForQuestionAnswering,
			    GPTNeoXForSequenceClassification,
			    GPTNeoXForTokenClassification,
			    GPTNeoXModel,
			)

class       _a  :



			def __init__(			self							:				Any   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				List[Any]=13   ,	_lowercase							:				int=7   ,	_lowercase							:				Any=True   ,	_lowercase							:				Optional[Any]=True   ,	_lowercase							:				Union[str, Any]=True   ,	_lowercase							:				Dict=True   ,	_lowercase							:				Optional[Any]=99   ,	_lowercase							:				Optional[int]=64   ,	_lowercase							:				List[Any]=5   ,	_lowercase							:				int=4   ,	_lowercase							:				Any=37   ,	_lowercase							:				Dict="gelu"   ,	_lowercase							:				Optional[Any]=0.1   ,	_lowercase							:				List[Any]=0.1   ,	_lowercase							:				Any=512   ,	_lowercase							:				Dict=16   ,	_lowercase							:				int=2   ,	_lowercase							:				Any=0.02   ,	_lowercase							:				Tuple=3   ,	_lowercase							:				Optional[int]=4   ,	_lowercase							:				Any=None   ,	)							->					List[str]:
									snake_case   :			Optional[int]						    = parent
									snake_case   :			Optional[int]						    = batch_size
									snake_case   :			Optional[Any]						    = seq_length
									snake_case   :			Dict						    = is_training
									snake_case   :			Optional[Any]						    = use_input_mask
									snake_case   :			List[str]						    = use_token_type_ids
									snake_case   :			Any						    = use_labels
									snake_case   :			int						    = vocab_size
									snake_case   :			Any						    = hidden_size
									snake_case   :			Union[str, Any]						    = num_hidden_layers
									snake_case   :			Any						    = num_attention_heads
									snake_case   :			Any						    = intermediate_size
									snake_case   :			int						    = hidden_act
									snake_case   :			Dict						    = hidden_dropout_prob
									snake_case   :			Optional[int]						    = attention_probs_dropout_prob
									snake_case   :			Optional[int]						    = max_position_embeddings
									snake_case   :			List[Any]						    = type_vocab_size
									snake_case   :			str						    = type_sequence_label_size
									snake_case   :			Optional[Any]						    = initializer_range
									snake_case   :			Tuple						    = num_labels
									snake_case   :			Union[str, Any]						    = num_choices
									snake_case   :			Optional[Any]						    = scope
									snake_case   :			Dict						    = vocab_size - 1



			def 				__lowercase					(			self							:				List[str]		)							->					Tuple:
									snake_case   :			Optional[Any]						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size		)

									snake_case   :			Dict						    = None
									if self.use_input_mask:
															snake_case   :			int						    = random_attention_mask([self.batch_size, self.seq_length]		)

									snake_case   :			Dict						    = None
									if self.use_labels:
															snake_case   :			List[str]						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.num_labels		)

									snake_case   :			Any						    = self.get_config()

									return config, input_ids, input_mask, token_labels



			def 				__lowercase					(			self							:				Union[str, Any]		)							->					Optional[int]:
									return GPTNeoXConfig(
									    vocab_size=self.vocab_size   ,	hidden_size=self.hidden_size   ,	num_hidden_layers=self.num_hidden_layers   ,	num_attention_heads=self.num_attention_heads   ,	intermediate_size=self.intermediate_size   ,	hidden_act=self.hidden_act   ,	hidden_dropout_prob=self.hidden_dropout_prob   ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,	max_position_embeddings=self.max_position_embeddings   ,	type_vocab_size=self.type_vocab_size   ,	is_decoder=_lowercase   ,	initializer_range=self.initializer_range   ,	pad_token_id=self.pad_token_id   ,	)



			def 				__lowercase					(			self							:				List[str]		)							->					str:
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Any						    = self.prepare_config_and_inputs()

									snake_case   :			int						    = True

									return config, input_ids, input_mask, token_labels



			def 				__lowercase					(			self							:				List[Any]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any		)							->					Optional[int]:
									snake_case   :			int						    = GPTNeoXModel(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Any						    = model(_lowercase   ,	attention_mask=_lowercase		)
									snake_case   :			Tuple						    = model(_lowercase		)
									self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)		)



			def 				__lowercase					(			self							:				str   ,	_lowercase							:				str   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any		)							->					List[Any]:
									snake_case   :			Any						    = True
									snake_case   :			Tuple						    = GPTNeoXModel(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			int						    = model(_lowercase   ,	attention_mask=_lowercase		)
									self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)		)



			def 				__lowercase					(			self							:				Union[str, Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Optional[int]		)							->					Union[str, Any]:
									snake_case   :			Any						    = GPTNeoXForCausalLM(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Dict						    = model(_lowercase   ,	attention_mask=_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.vocab_size)		)



			def 				__lowercase					(			self							:				Optional[Any]   ,	_lowercase							:				Dict   ,	_lowercase							:				int   ,	_lowercase							:				str   ,	_lowercase							:				Tuple		)							->					str:
									snake_case   :			Optional[Any]						    = self.num_labels
									snake_case   :			Union[str, Any]						    = GPTNeoXForQuestionAnswering(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Any						    = model(_lowercase   ,	attention_mask=_lowercase		)
									self.parent.assertEqual(result.start_logits.shape   ,	(self.batch_size, self.seq_length)		)
									self.parent.assertEqual(result.end_logits.shape   ,	(self.batch_size, self.seq_length)		)



			def 				__lowercase					(			self							:				int   ,	_lowercase							:				List[Any]   ,	_lowercase							:				str   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Tuple		)							->					List[Any]:
									snake_case   :			Optional[Any]						    = self.num_labels
									snake_case   :			int						    = GPTNeoXForSequenceClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Optional[Any]						    = ids_tensor([self.batch_size]   ,	self.type_sequence_label_size		)
									snake_case   :			int						    = model(_lowercase   ,	attention_mask=_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.num_labels)		)



			def 				__lowercase					(			self							:				Tuple   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				int   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				int		)							->					str:
									snake_case   :			int						    = self.num_labels
									snake_case   :			Dict						    = GPTNeoXForTokenClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			List[str]						    = model(_lowercase   ,	attention_mask=_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.num_labels)		)



			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				str   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Optional[Any]		)							->					str:
									snake_case   :			Optional[int]						    = True
									snake_case   :			Optional[Any]						    = GPTNeoXForCausalLM(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()

									# first forward pass
									snake_case   :			Optional[int]						    = model(_lowercase   ,	attention_mask=_lowercase   ,	use_cache=_lowercase		)
									snake_case   :			List[Any]						    = outputs.past_key_values

									# create hypothetical multiple next token and extent to next_input_ids
									snake_case   :			Any						    = ids_tensor((self.batch_size, 3)   ,	config.vocab_size		)
									snake_case   :			Union[str, Any]						    = ids_tensor((self.batch_size, 3)   ,	vocab_size=2		)

									# append to next input_ids and
									snake_case   :			List[str]						    = torch.cat([input_ids, next_tokens]   ,	dim=-1		)
									snake_case   :			Optional[Any]						    = torch.cat([input_mask, next_mask]   ,	dim=-1		)

									snake_case   :			Dict						    = model(_lowercase   ,	attention_mask=_lowercase   ,	output_hidden_states=_lowercase		)
									snake_case   :			str						    = output_from_no_past["hidden_states"][0]
									snake_case   :			Any						    = model(
									    _lowercase   ,	attention_mask=_lowercase   ,	past_key_values=_lowercase   ,	output_hidden_states=_lowercase   ,	)["hidden_states"][0]

									# select random slice
									snake_case   :			Any						    = ids_tensor((1,)   ,	output_from_past.shape[-1]		).item()
									snake_case   :			List[str]						    = output_from_no_past[:, -3:, random_slice_idx].detach()
									snake_case   :			Tuple						    = output_from_past[:, :, random_slice_idx].detach()

									self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]		)

									# test that outputs are equal for slice
									self.parent.assertTrue(torch.allclose(_lowercase   ,	_lowercase   ,	atol=1E-3		)		)



			def 				__lowercase					(			self							:				Dict		)							->					Tuple:
									snake_case   :			List[str]						    = self.prepare_config_and_inputs()
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Any						    = config_and_inputs
									snake_case   :			Union[str, Any]						    = {"input_ids": input_ids, "attention_mask": input_mask}
									return config, inputs_dict



@require_torch
class       _a  ( SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	unittest.TestCase):
			__magic_name__     					=     (
			    (
			        GPTNeoXModel,
			        GPTNeoXForCausalLM,
			        GPTNeoXForQuestionAnswering,
			        GPTNeoXForSequenceClassification,
			        GPTNeoXForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			__magic_name__     					=     (GPTNeoXForCausalLM,) if is_torch_available() else ()
			__magic_name__     					=     (
			    {
			        """feature-extraction""": GPTNeoXModel,
			        """question-answering""": GPTNeoXForQuestionAnswering,
			        """text-classification""": GPTNeoXForSequenceClassification,
			        """text-generation""": GPTNeoXForCausalLM,
			        """token-classification""": GPTNeoXForTokenClassification,
			        """zero-shot""": GPTNeoXForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False



			def 				__lowercase					(			self							:				Tuple		)							->					Optional[Any]:
									snake_case   :			Union[str, Any]						    = GPTNeoXModelTester(self		)
									snake_case   :			int						    = ConfigTester(self   ,	config_class=_lowercase   ,	hidden_size=64   ,	num_attention_heads=8		)



			def 				__lowercase					(			self							:				List[Any]		)							->					Optional[Any]:
									self.config_tester.run_common_tests()



			def 				__lowercase					(			self							:				Optional[Any]		)							->					int:
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Optional[Any]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(_lowercase   ,	_lowercase   ,	_lowercase		)



			def 				__lowercase					(			self							:				List[str]		)							->					str:
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Any						    = self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_model_as_decoder(_lowercase   ,	_lowercase   ,	_lowercase		)



			def 				__lowercase					(			self							:				Any		)							->					List[str]:
									# This regression test was failing with PyTorch < 1.3
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Union[str, Any]						    = self.model_tester.prepare_config_and_inputs_for_decoder()

									snake_case   :			Optional[int]						    = None

									self.model_tester.create_and_check_model_as_decoder(_lowercase   ,	_lowercase   ,	_lowercase		)



			def 				__lowercase					(			self							:				int		)							->					Tuple:
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Union[str, Any]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowercase   ,	_lowercase   ,	_lowercase		)



			def 				__lowercase					(			self							:				Tuple		)							->					Optional[int]:
									snake_case   :			List[str]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_causal_lm(*_lowercase		)



			def 				__lowercase					(			self							:				List[str]		)							->					List[str]:
									snake_case   :			Any						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_lowercase		)



			def 				__lowercase					(			self							:				Any		)							->					Any:
									snake_case   :			List[str]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_lowercase		)



			def 				__lowercase					(			self							:				List[str]		)							->					Optional[int]:
									snake_case   :			int						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_lowercase		)



			@unittest.skip(reason="Feed forward chunking is not implemented"		)
			def 				__lowercase					(			self							:				Optional[int]		)							->					Tuple:
									pass



			@parameterized.expand([("linear",), ("dynamic",)]		)
			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				List[Any]		)							->					Optional[int]:
									snake_case    ,			snake_case   :			List[str]						    = self.model_tester.prepare_config_and_inputs_for_common()
									snake_case   :			Tuple						    = ids_tensor([1, 10]   ,	config.vocab_size		)
									snake_case   :			Tuple						    = ids_tensor([1, int(config.max_position_embeddings * 1.5		)]   ,	config.vocab_size		)

									set_seed(42		)  # Fixed seed at init time so the two models get the same random weights
									snake_case   :			int						    = GPTNeoXModel(_lowercase		)
									original_model.to(_lowercase		)
									original_model.eval()
									snake_case   :			Tuple						    = original_model(_lowercase		).last_hidden_state
									snake_case   :			List[Any]						    = original_model(_lowercase		).last_hidden_state

									set_seed(42		)  # Fixed seed at init time so the two models get the same random weights
									snake_case   :			Union[str, Any]						    = {"type": scaling_type, "factor": 10.0}
									snake_case   :			Optional[Any]						    = GPTNeoXModel(_lowercase		)
									scaled_model.to(_lowercase		)
									scaled_model.eval()
									snake_case   :			List[str]						    = scaled_model(_lowercase		).last_hidden_state
									snake_case   :			Any						    = scaled_model(_lowercase		).last_hidden_state

									# Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
									# maximum sequence length, so the outputs for the short input should match.
									if scaling_type == "dynamic":
															self.assertTrue(torch.allclose(_lowercase   ,	_lowercase   ,	atol=1E-5		)		)
									else:
															self.assertFalse(torch.allclose(_lowercase   ,	_lowercase   ,	atol=1E-5		)		)

									# The output should be different for long inputs
									self.assertFalse(torch.allclose(_lowercase   ,	_lowercase   ,	atol=1E-5		)		)



@require_torch
class       _a  ( unittest.TestCase):



			@slow
			def 				__lowercase					(			self							:				str		)							->					int:
									snake_case   :			int						    = AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped"		)
									for checkpointing in [True, False]:
															snake_case   :			Optional[int]						    = GPTNeoXForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped"		)

															if checkpointing:
																					model.gradient_checkpointing_enable()
															else:
																					model.gradient_checkpointing_disable()
															model.to(_lowercase		)

															snake_case   :			Any						    = tokenizer("My favorite food is"   ,	return_tensors="pt"		).to(_lowercase		)
															# The hub repo. is updated on 2023-04-04, resulting in poor outputs.
															# See: https://github.com/huggingface/transformers/pull/24193
															snake_case   :			Any						    = "My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure"

															snake_case   :			Any						    = model.generate(**_lowercase   ,	do_sample=_lowercase   ,	max_new_tokens=20		)
															snake_case   :			Union[str, Any]						    = tokenizer.batch_decode(_lowercase		)[0]

															self.assertEqual(_lowercase   ,	_lowercase		)








"""simple docstring"""







import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
A		=						'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
A		=						importlib.util.spec_from_file_location(
    'transformers',
    os.path.join(PATH_TO_TRANSFORMERS, '__init__.py'),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
A		=						spec.loader.load_module()

A		=						transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
A		=						re.compile('\[(.+?)\]\((https://huggingface\.co/.+?)\)')


A		=						{
    'CLIPConfigMixin',
    'DecisionTransformerConfigMixin',
    'EncoderDecoderConfigMixin',
    'RagConfigMixin',
    'SpeechEncoderDecoderConfigMixin',
    'VisionEncoderDecoderConfigMixin',
    'VisionTextDualEncoderConfigMixin',
}

def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						snake_case   :			Union[str, Any]						    = []

						for config_class in list(CONFIG_MAPPING.values() ):
												snake_case   :			List[str]						    = False

												# source code of `config_class`
												snake_case   :			Optional[int]						    = inspect.getsource(lowerCamelCase_ )
												snake_case   :			Optional[int]						    = _re_checkpoint.findall(lowerCamelCase_ )

												for checkpoint in checkpoints:
																		# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
																		# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
																		snake_case    ,			snake_case   :			Tuple						    = checkpoint

																		# verify the checkpoint name corresponds to the checkpoint link
																		snake_case   :			Any						    = f'''https://huggingface.co/{ckpt_name}'''
																		if ckpt_link == ckpt_link_from_name:
																								snake_case   :			List[str]						    = True
																								break

												snake_case   :			Any						    = config_class.__name__
												if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
																		configs_without_checkpoint.append(lowerCamelCase_ )

						if len(lowerCamelCase_ ) > 0:
												snake_case   :			List[str]						    = "\n".join(sorted(lowerCamelCase_ ) )
												raise ValueError(f'''The following configurations don\'t contain any valid checkpoint:\n{message}''' )


if __name__ == "__main__":
			check_config_docstrings_have_checkpoints()



