






"""simple docstring"""






import inspect
import os
import re

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import direct_transformers_import


# 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
UpperCAmelCase_     :				Any         =				'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
UpperCAmelCase_     :				Any         =				direct_transformers_import(PATH_TO_TRANSFORMERS)

UpperCAmelCase_     :				str         =				transformers.models.auto.configuration_auto.CONFIG_MAPPING

UpperCAmelCase_     :				Tuple         =				{
    # used to compute the property `self.chunk_length`
    'EncodecConfig': ['overlap'],
    # used as `self.bert_model = BertModel(config, ...)`
    'DPRConfig': True,
    # not used in modeling files, but it's an important information
    'FSMTConfig': ['langs'],
    # used internally in the configuration class file
    'GPTNeoConfig': ['attention_types'],
    # used internally in the configuration class file
    'EsmConfig': ['is_folding_model'],
    # used during training (despite we don't have training script for these models yet)
    'Mask2FormerConfig': ['ignore_value'],
    # `ignore_value` used during training (despite we don't have training script for these models yet)
    # `norm` used in conversion script (despite not using in the modeling file)
    'OneFormerConfig': ['ignore_value', 'norm'],
    # used during preprocessing and collation, see `collating_graphormer.py`
    'GraphormerConfig': ['spatial_pos_max'],
    # used internally in the configuration class file
    'T5Config': ['feed_forward_proj'],
    # used internally in the configuration class file
    # `tokenizer_class` get default value `T5Tokenizer` intentionally
    'MT5Config': ['feed_forward_proj', 'tokenizer_class'],
    'UMT5Config': ['feed_forward_proj', 'tokenizer_class'],
    # used internally in the configuration class file
    'LongT5Config': ['feed_forward_proj'],
    # used internally in the configuration class file
    'SwitchTransformersConfig': ['feed_forward_proj'],
    # having default values other than `1e-5` - we can't fix them without breaking
    'BioGptConfig': ['layer_norm_eps'],
    # having default values other than `1e-5` - we can't fix them without breaking
    'GLPNConfig': ['layer_norm_eps'],
    # having default values other than `1e-5` - we can't fix them without breaking
    'SegformerConfig': ['layer_norm_eps'],
    # having default values other than `1e-5` - we can't fix them without breaking
    'CvtConfig': ['layer_norm_eps'],
    # having default values other than `1e-5` - we can't fix them without breaking
    'PerceiverConfig': ['layer_norm_eps'],
    # used internally to calculate the feature size
    'InformerConfig': ['num_static_real_features', 'num_time_features'],
    # used internally to calculate the feature size
    'TimeSeriesTransformerConfig': ['num_static_real_features', 'num_time_features'],
    # used internally to calculate the feature size
    'AutoformerConfig': ['num_static_real_features', 'num_time_features'],
    # used internally to calculate `mlp_dim`
    'SamVisionConfig': ['mlp_ratio'],
    # For (head) training, but so far not implemented
    'ClapAudioConfig': ['num_classes'],
    # Not used, but providing useful information to users
    'SpeechT5HifiGanConfig': ['sampling_rate'],
}


# TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure
SPECIAL_CASES_TO_ALLOW.update(
    {
        """CLIPSegConfig""": True,
        """DeformableDetrConfig""": True,
        """DetaConfig""": True,
        """DinatConfig""": True,
        """DonutSwinConfig""": True,
        """EfficientFormerConfig""": True,
        """FSMTConfig""": True,
        """JukeboxConfig""": True,
        """LayoutLMv2Config""": True,
        """MaskFormerSwinConfig""": True,
        """MT5Config""": True,
        """NatConfig""": True,
        """OneFormerConfig""": True,
        """PerceiverConfig""": True,
        """RagConfig""": True,
        """SpeechT5Config""": True,
        """SwinConfig""": True,
        """Swin2SRConfig""": True,
        """Swinv2Config""": True,
        """SwitchTransformersConfig""": True,
        """TableTransformerConfig""": True,
        """TapasConfig""": True,
        """TransfoXLConfig""": True,
        """UniSpeechConfig""": True,
        """UniSpeechSatConfig""": True,
        """WavLMConfig""": True,
        """WhisperConfig""": True,
        # TODO: @Arthur (for `alignment_head` and `alignment_layer`)
        """JukeboxPriorConfig""": True,
        # TODO: @Younes (for `is_decoder`)
        """Pix2StructTextConfig""": True,
    }
)



def       _A    (__a					,       __a					,       __a					,       __a							)				-> Optional[Any]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						str							=	False
    for attribute in attributes:
        for modeling_source in source_strings:
            # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)`
            if (
                f'config.{attribute}' in modeling_source
                or f'getattr(config, \"{attribute}\"' in modeling_source
                or f'getattr(self.config, \"{attribute}\"' in modeling_source
            ):
                SCREAMING_SNAKE_CASE_   :						int							=	True
            # Deal with multi-line cases
            elif (
                re.search(
                    Rf'getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"'					,       __UpperCamelCase					,       )
                is not None
            ):
                SCREAMING_SNAKE_CASE_   :						Any							=	True
            # `SequenceSummary` is called with `SequenceSummary(config)`
            elif attribute in [
                "summary_type",
                "summary_use_proj",
                "summary_activation",
                "summary_last_dropout",
                "summary_proj_to_labels",
                "summary_first_dropout",
            ]:
                if "SequenceSummary" in modeling_source:
                    SCREAMING_SNAKE_CASE_   :						Any							=	True
            if attribute_used:
                break
        if attribute_used:
            break

    # common and important attributes, even if they do not always appear in the modeling files
    SCREAMING_SNAKE_CASE_   :						List[Any]							=	[
        '''bos_index''',
        '''eos_index''',
        '''pad_index''',
        '''unk_index''',
        '''mask_index''',
        '''image_size''',
        '''use_cache''',
        '''out_features''',
        '''out_indices''',
    ]
    SCREAMING_SNAKE_CASE_   :						List[str]							=	['''encoder_no_repeat_ngram_size''']

    # Special cases to be allowed
    SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	True
    if not attribute_used:
        SCREAMING_SNAKE_CASE_   :						int							=	False
        for attribute in attributes:
            # Allow if the default value in the configuration class is different from the one in `PretrainedConfig`
            if attribute in ["is_encoder_decoder"] and default_value is True:
                SCREAMING_SNAKE_CASE_   :						Any							=	True
            elif attribute in ["tie_word_embeddings"] and default_value is False:
                SCREAMING_SNAKE_CASE_   :						Dict							=	True

            # Allow cases without checking the default value in the configuration class
            elif attribute in attributes_to_allow + attributes_used_in_generation:
                SCREAMING_SNAKE_CASE_   :						Optional[int]							=	True
            elif attribute.endswith('''_token_id'''							):
                SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	True

            # configuration class specific cases
            if not case_allowed:
                SCREAMING_SNAKE_CASE_   :						Tuple							=	SPECIAL_CASES_TO_ALLOW.get(config_class.__name__					,       []							)
                SCREAMING_SNAKE_CASE_   :						Dict							=	allowed_cases is True or attribute in allowed_cases

    return attribute_used or case_allowed



def       _A    (__a							)				-> Optional[int]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						List[str]							=	dict(inspect.signature(config_class.__init__							).parameters							)
    SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	[x for x in list(signature.keys()							) if x not in ['''self''', '''kwargs''']]
    SCREAMING_SNAKE_CASE_   :						int							=	[signature[param].default for param in parameter_names]

    # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long
    # as one variant is used, the test should pass
    SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	{}
    if len(config_class.attribute_map							) > 0:
        SCREAMING_SNAKE_CASE_   :						List[Any]							=	{v: k for k, v in config_class.attribute_map.items()}

    # Get the path to modeling source files
    SCREAMING_SNAKE_CASE_   :						Optional[int]							=	inspect.getsourcefile(__UpperCamelCase							)
    SCREAMING_SNAKE_CASE_   :						int							=	os.path.dirname(__UpperCamelCase							)
    # Let's check against all frameworks: as long as one framework uses an attribute, we are good.
    SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	[os.path.join(__UpperCamelCase					,       __UpperCamelCase							) for fn in os.listdir(__UpperCamelCase							) if fn.startswith('''modeling_'''							)]

    # Get the source code strings
    SCREAMING_SNAKE_CASE_   :						int							=	[]
    for path in modeling_paths:
        if os.path.isfile(__UpperCamelCase							):
            with open(__UpperCamelCase							) as fp:
                modeling_sources.append(fp.read()							)

    SCREAMING_SNAKE_CASE_   :						Any							=	[]
    for config_param, default_value in zip(__UpperCamelCase					,       __UpperCamelCase							):
        # `attributes` here is all the variant names for `config_param`
        SCREAMING_SNAKE_CASE_   :						int							=	[config_param]
        # some configuration classes have non-empty `attribute_map`, and both names could be used in the
        # corresponding modeling files. As long as one of them appears, it is fine.
        if config_param in reversed_attribute_map:
            attributes.append(reversed_attribute_map[config_param]							)

        if not check_attribute_being_used(__UpperCamelCase					,       __UpperCamelCase					,       __UpperCamelCase					,       __UpperCamelCase							):
            unused_attributes.append(attributes[0]							)

    return sorted(__UpperCamelCase							)



def       _A    ()				-> Union[str, Any]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Dict							=	{}
    for _config_class in list(CONFIG_MAPPING.values()							):
        # Skip deprecated models
        if "models.deprecated" in _config_class.__module__:
            continue
        # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.)
        SCREAMING_SNAKE_CASE_   :						Tuple							=	[
            cls
            for name, cls in inspect.getmembers(
                inspect.getmodule(_config_class							)					,       lambda __a							: inspect.isclass(__UpperCamelCase							)
                and issubclass(__UpperCamelCase					,       __UpperCamelCase							)
                and inspect.getmodule(__UpperCamelCase							) == inspect.getmodule(_config_class							)					,       )
        ]
        for config_class in config_classes_in_module:
            SCREAMING_SNAKE_CASE_   :						List[str]							=	check_config_attributes_being_used(__UpperCamelCase							)
            if len(__UpperCamelCase							) > 0:
                SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	unused_attributes

    if len(__UpperCamelCase							) > 0:
        SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	'''The following configuration classes contain unused attributes in the corresponding modeling files:\n'''
        for name, attributes in configs_with_unused_attributes.items():
            error += f'{name}: {attributes}\n'

        raise ValueError(__UpperCamelCase							)


if __name__ == "__main__":
      check_config_attributes()










"""simple docstring"""






import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
  import jax
  import jax.numpy as jnp

  from transformers.modeling_flax_pytorch_utils import (
      convert_pytorch_state_dict_to_flax,
      load_flax_weights_in_pytorch_model,
  )
  from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel

if is_torch_available():
  import torch




class 							lowerCAmelCase__       :






      '''simple docstring'''

      def __init__(		self  :						int		,			lowercase_  :						List[Any]		,			lowercase_  :						int=14		,			lowercase_  :						Tuple=7		,			lowercase_  :						int=True		,			lowercase_  :						int=True		,			lowercase_  :						int=False		,			lowercase_  :						Optional[int]=True		,			lowercase_  :						Union[str, Any]=99		,			lowercase_  :						Optional[int]=32		,			lowercase_  :						List[Any]=4		,			lowercase_  :						Optional[int]=4		,			lowercase_  :						List[str]=4		,			lowercase_  :						Tuple=37		,			lowercase_  :						Any="gelu"		,			lowercase_  :						Dict=0.1		,			lowercase_  :						str=0.1		,			lowercase_  :						Optional[int]=512		,			lowercase_  :						Dict=0.02		,			):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	parent
          SCREAMING_SNAKE_CASE_   :						Any							=	batch_size
          SCREAMING_SNAKE_CASE_   :						Tuple							=	seq_length
          SCREAMING_SNAKE_CASE_   :						Any							=	is_training
          SCREAMING_SNAKE_CASE_   :						int							=	use_input_mask
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	use_token_type_ids
          SCREAMING_SNAKE_CASE_   :						Tuple							=	use_labels
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	vocab_size
          SCREAMING_SNAKE_CASE_   :						str							=	hidden_size
          SCREAMING_SNAKE_CASE_   :						Tuple							=	rotary_dim
          SCREAMING_SNAKE_CASE_   :						str							=	num_hidden_layers
          SCREAMING_SNAKE_CASE_   :						int							=	num_attention_heads
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	intermediate_size
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	hidden_act
          SCREAMING_SNAKE_CASE_   :						str							=	hidden_dropout_prob
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	attention_probs_dropout_prob
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	max_position_embeddings
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	initializer_range
          SCREAMING_SNAKE_CASE_   :						Dict							=	None
          SCREAMING_SNAKE_CASE_   :						str							=	vocab_size - 1
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	vocab_size - 1
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	vocab_size - 1
      def _SCREAMING_SNAKE_CASE				(		self  :						List[str]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Tuple							=	ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size)

          SCREAMING_SNAKE_CASE_   :						str							=	None
          if self.use_input_mask:
              SCREAMING_SNAKE_CASE_   :						str							=	random_attention_mask([self.batch_size, self.seq_length])

          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	GPTJConfig(
              vocab_size=self.vocab_size		,			n_embd=self.hidden_size		,			n_layer=self.num_hidden_layers		,			n_head=self.num_attention_heads		,			n_positions=self.max_position_embeddings		,			use_cache=lowercase_		,			bos_token_id=self.bos_token_id		,			eos_token_id=self.eos_token_id		,			pad_token_id=self.pad_token_id		,			rotary_dim=self.rotary_dim		,			)

          return (config, input_ids, input_mask)
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						int							=	self.prepare_config_and_inputs()
          SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Tuple							=	config_and_inputs
          SCREAMING_SNAKE_CASE_   :						int							=	{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
          return config, inputs_dict
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple		,			lowercase_  :						Tuple		,			lowercase_  :						Any		,			lowercase_  :						Any		,			lowercase_  :						Tuple):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	20
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	model_class_name(lowercase_)

          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	model.init_cache(input_ids.shape[0]		,			lowercase_)
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	jnp.ones((input_ids.shape[0], max_decoder_length)		,			dtype='''i4''')

          SCREAMING_SNAKE_CASE_   :						Any							=	jnp.broadcast_to(
              jnp.arange(input_ids.shape[-1] - 1)[None, :]		,			(input_ids.shape[0], input_ids.shape[-1] - 1))
          SCREAMING_SNAKE_CASE_   :						Any							=	model(
              input_ids[:, :-1]		,			attention_mask=lowercase_		,			past_key_values=lowercase_		,			position_ids=lowercase_		,			)

          SCREAMING_SNAKE_CASE_   :						str							=	jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		,			dtype='''i4''')
          SCREAMING_SNAKE_CASE_   :						int							=	model(
              input_ids[:, -1:]		,			attention_mask=lowercase_		,			past_key_values=outputs_cache.past_key_values		,			position_ids=lowercase_		,			)

          SCREAMING_SNAKE_CASE_   :						List[str]							=	model(lowercase_)

          SCREAMING_SNAKE_CASE_   :						int							=	np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
          self.parent.assertTrue(diff < 1e-3		,			msg=F'Max diff is {diff}')



      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple		,			lowercase_  :						Tuple		,			lowercase_  :						Tuple		,			lowercase_  :						Optional[int]		,			lowercase_  :						Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	20
          SCREAMING_SNAKE_CASE_   :						List[str]							=	model_class_name(lowercase_)

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	jnp.concatenate(
              [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]))]		,			axis=-1		,			)

          SCREAMING_SNAKE_CASE_   :						Tuple							=	model.init_cache(input_ids.shape[0]		,			lowercase_)
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	jnp.broadcast_to(
              jnp.arange(input_ids.shape[-1] - 1)[None, :]		,			(input_ids.shape[0], input_ids.shape[-1] - 1))

          SCREAMING_SNAKE_CASE_   :						Dict							=	model(
              input_ids[:, :-1]		,			attention_mask=lowercase_		,			past_key_values=lowercase_		,			position_ids=lowercase_		,			)
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		,			dtype='''i4''')
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	model(
              input_ids[:, -1:]		,			past_key_values=outputs_cache.past_key_values		,			attention_mask=lowercase_		,			position_ids=lowercase_		,			)

          SCREAMING_SNAKE_CASE_   :						Any							=	model(lowercase_		,			attention_mask=lowercase_)

          SCREAMING_SNAKE_CASE_   :						Any							=	np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
          self.parent.assertTrue(diff < 1e-3		,			msg=F'Max diff is {diff}')




@require_flax
class 							lowerCAmelCase__       (    UpperCAmelCase__       ,	UpperCAmelCase__       ,	unittest.TestCase				):






      '''simple docstring'''

      __UpperCamelCase							=						(FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else ()
      __UpperCamelCase							=						(FlaxGPTJForCausalLM,) if is_flax_available() else ()
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[str]							=	FlaxGPTJModelTester(self)
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          for model_class_name in self.all_model_classes:
              SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						int							=	self.model_tester.prepare_config_and_inputs()
              self.model_tester.check_use_cache_forward(lowercase_		,			lowercase_		,			lowercase_		,			lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          for model_class_name in self.all_model_classes:
              SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Optional[int]							=	self.model_tester.prepare_config_and_inputs()
              self.model_tester.check_use_cache_forward_with_attn_mask(
                  lowercase_		,			lowercase_		,			lowercase_		,			lowercase_)
      @tooslow
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	GPTaTokenizer.from_pretrained('''gpt2'''		,			pad_token='''<|endoftext|>'''		,			padding_side='''left''')
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	tokenizer(['''Hello this is a long string''', '''Hey''']		,			return_tensors='''np'''		,			padding=lowercase_		,			truncation=lowercase_)

          SCREAMING_SNAKE_CASE_   :						List[str]							=	FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''')
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	False
          SCREAMING_SNAKE_CASE_   :						str							=	model.config.eos_token_id

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	jax.jit(model.generate)

          SCREAMING_SNAKE_CASE_   :						Tuple							=	jit_generate(
              inputs['''input_ids''']		,			attention_mask=inputs['''attention_mask''']		,			pad_token_id=tokenizer.pad_token_id).sequences

          SCREAMING_SNAKE_CASE_   :						int							=	tokenizer.batch_decode(lowercase_		,			skip_special_tokens=lowercase_)

          SCREAMING_SNAKE_CASE_   :						int							=	[
              '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''',
              '''Hey, I\'m a little late to the party. I\'m going to''',
          ]

          self.assertListEqual(lowercase_		,			lowercase_)
      @is_pt_flax_cross_test
      def _SCREAMING_SNAKE_CASE				(		self  :						Union[str, Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Optional[int]							=	self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__):
                  # prepare inputs
                  SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	self._prepare_for_class(lowercase_		,			lowercase_)
                  SCREAMING_SNAKE_CASE_   :						Any							=	{k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

                  # load corresponding PyTorch class
                  SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	model_class.__name__[4:]  # Skip the "Flax" at the beginning
                  SCREAMING_SNAKE_CASE_   :						Tuple							=	getattr(lowercase_		,			lowercase_)

                  SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Optional[int]							=	pt_inputs['''input_ids'''].shape
                  SCREAMING_SNAKE_CASE_   :						List[str]							=	np.random.randint(0		,			seq_length - 1		,			size=(batch_size,))
                  for batch_idx, start_index in enumerate(lowercase_):
                      SCREAMING_SNAKE_CASE_   :						Optional[int]							=	0
                      SCREAMING_SNAKE_CASE_   :						List[Any]							=	1
                      SCREAMING_SNAKE_CASE_   :						List[Any]							=	0
                      SCREAMING_SNAKE_CASE_   :						List[str]							=	1
                  SCREAMING_SNAKE_CASE_   :						List[Any]							=	pt_model_class(lowercase_).eval()
                  SCREAMING_SNAKE_CASE_   :						Any							=	model_class(lowercase_		,			dtype=jnp.floataa)

                  SCREAMING_SNAKE_CASE_   :						List[str]							=	convert_pytorch_state_dict_to_flax(pt_model.state_dict()		,			lowercase_)
                  SCREAMING_SNAKE_CASE_   :						List[str]							=	fx_state

                  with torch.no_grad():
                      SCREAMING_SNAKE_CASE_   :						Dict							=	pt_model(**lowercase_).to_tuple()

                  SCREAMING_SNAKE_CASE_   :						List[Any]							=	fx_model(**lowercase_).to_tuple()
                  self.assertEqual(len(lowercase_)		,			len(lowercase_)		,			'''Output lengths differ between Flax and PyTorch''')
                  for fx_output, pt_output in zip(lowercase_		,			lowercase_):
                      self.assert_almost_equals(fx_output[:, -1]		,			pt_output[:, -1].numpy()		,			4e-2)

                  with tempfile.TemporaryDirectory() as tmpdirname:
                      pt_model.save_pretrained(lowercase_)
                      SCREAMING_SNAKE_CASE_   :						int							=	model_class.from_pretrained(lowercase_		,			from_pt=lowercase_)

                  SCREAMING_SNAKE_CASE_   :						str							=	fx_model_loaded(**lowercase_).to_tuple()
                  self.assertEqual(
                      len(lowercase_)		,			len(lowercase_)		,			'''Output lengths differ between Flax and PyTorch''')
                  for fx_output_loaded, pt_output in zip(lowercase_		,			lowercase_):
                      self.assert_almost_equals(fx_output_loaded[:, -1]		,			pt_output[:, -1].numpy()		,			4e-2)
      @is_pt_flax_cross_test
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						List[str]							=	self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__):
                  # prepare inputs
                  SCREAMING_SNAKE_CASE_   :						Tuple							=	self._prepare_for_class(lowercase_		,			lowercase_)
                  SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	{k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

                  # load corresponding PyTorch class
                  SCREAMING_SNAKE_CASE_   :						Optional[int]							=	model_class.__name__[4:]  # Skip the "Flax" at the beginning
                  SCREAMING_SNAKE_CASE_   :						Dict							=	getattr(lowercase_		,			lowercase_)

                  SCREAMING_SNAKE_CASE_   :						int							=	pt_model_class(lowercase_).eval()
                  SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	model_class(lowercase_		,			dtype=jnp.floataa)

                  SCREAMING_SNAKE_CASE_   :						int							=	load_flax_weights_in_pytorch_model(lowercase_		,			fx_model.params)
                  SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	pt_inputs['''input_ids'''].shape
                  SCREAMING_SNAKE_CASE_   :						Any							=	np.random.randint(0		,			seq_length - 1		,			size=(batch_size,))
                  for batch_idx, start_index in enumerate(lowercase_):
                      SCREAMING_SNAKE_CASE_   :						int							=	0
                      SCREAMING_SNAKE_CASE_   :						Optional[int]							=	1
                      SCREAMING_SNAKE_CASE_   :						str							=	0
                      SCREAMING_SNAKE_CASE_   :						List[str]							=	1

                  # make sure weights are tied in PyTorch
                  pt_model.tie_weights()

                  with torch.no_grad():
                      SCREAMING_SNAKE_CASE_   :						Optional[int]							=	pt_model(**lowercase_).to_tuple()

                  SCREAMING_SNAKE_CASE_   :						str							=	fx_model(**lowercase_).to_tuple()
                  self.assertEqual(len(lowercase_)		,			len(lowercase_)		,			'''Output lengths differ between Flax and PyTorch''')
                  for fx_output, pt_output in zip(lowercase_		,			lowercase_):
                      self.assert_almost_equals(fx_output[:, -1]		,			pt_output[:, -1].numpy()		,			4e-2)

                  with tempfile.TemporaryDirectory() as tmpdirname:
                      fx_model.save_pretrained(lowercase_)
                      SCREAMING_SNAKE_CASE_   :						List[str]							=	pt_model_class.from_pretrained(lowercase_		,			from_flax=lowercase_)

                  with torch.no_grad():
                      SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	pt_model_loaded(**lowercase_).to_tuple()

                  self.assertEqual(
                      len(lowercase_)		,			len(lowercase_)		,			'''Output lengths differ between Flax and PyTorch''')
                  for fx_output, pt_output in zip(lowercase_		,			lowercase_):
                      self.assert_almost_equals(fx_output[:, -1]		,			pt_output[:, -1].numpy()		,			4e-2)



      @tooslow
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[Any]):




          '''simple docstring'''


          for model_class_name in self.all_model_classes:
              SCREAMING_SNAKE_CASE_   :						Any							=	model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''')
              SCREAMING_SNAKE_CASE_   :						List[str]							=	model(np.ones((1, 1)))
              self.assertIsNotNone(lowercase_)


