


import argparse
import glob
import inspect
import os
import re
import unittest

from get_test_info import get_test_classes

from tests.test_pipeline_mixin import pipeline_test_mapping


a					:				List[Any]       =      {}
for task, _ in pipeline_test_mapping.items():
  a					:				Any       =      {'pt': None, 'tf': None}


# DO **NOT** add item to this set (unless the reason is approved)
a					:				List[str]       =      {
    'tests/models/esm/test_modeling_esmfold.py',  # The pipeline test mapping is added to `test_modeling_esm.py`
}






def  _UpperCAmelCase						( __lowerCamelCase							)		->Dict:


 '''simple docstring'''

 if "ModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
  return "pt"
 elif "TFModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
  return "tf"
 elif "FlaxModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
  return "flax"
 else:
  return None






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase							)		->Union[str, Any]:


 '''simple docstring'''
 # Use the cached results
 if PIPELINE_TEST_MAPPING[task].get(__lowerCamelCase			,  __lowerCamelCase							) is not None:
  return PIPELINE_TEST_MAPPING[task][framework]

 snake_case :			List[str]							=   pipeline_test_mapping[task]['''test''']
 snake_case :			List[Any]							=   None

 if framework == "pt":
  snake_case :			List[Any]							=   getattr(__lowerCamelCase			,  '''model_mapping'''			,  __lowerCamelCase							)
 elif framework == "tf":
  snake_case :			str							=   getattr(__lowerCamelCase			,  '''tf_model_mapping'''			,  __lowerCamelCase							)

 if mapping is not None:
  snake_case :			Optional[int]							=   dict(mapping.items()							)

 # cache the results
 snake_case :			Tuple							=   mapping
 return mapping






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase							)		->Dict:


 '''simple docstring'''
 snake_case :			str							=   get_framework(__lowerCamelCase							)
 if framework is None:
  return None
 snake_case :			Optional[int]							=   get_mapping_for_task(__lowerCamelCase			,  __lowerCamelCase							)
 if mapping is None:
  return None

 snake_case :			int							=   list({model_class.config_class for model_class in test_class.all_model_classes}							)
 if len(__lowerCamelCase							) != 1:
  raise ValueError('''There should be exactly one configuration class from `test_class.all_model_classes`.'''							)

 # This could be a list/tuple of model classes, but it's rare.
 snake_case :			Tuple							=   mapping.get(config_classes[0]			,  __lowerCamelCase							)
 if isinstance(__lowerCamelCase			,  (tuple, list)							):
  snake_case :			Optional[Any]							=   sorted(__lowerCamelCase			,  key=lambda __lowerCamelCase							: x.__name__							)

 return model_class






def  _UpperCAmelCase						( __lowerCamelCase							)		->Union[str, Any]:


 '''simple docstring'''
 snake_case :			Optional[Any]							=   [(task, get_model_for_pipeline_test(__lowerCamelCase			,  __lowerCamelCase							)) for task in pipeline_test_mapping]
 snake_case :			str							=   sorted([(task, model) for task, model in mapping if model is not None]			,  key=lambda __lowerCamelCase							: x[0]							)

 return dict(__lowerCamelCase							)






def  _UpperCAmelCase						( __lowerCamelCase							)		->List[str]:


 '''simple docstring'''
 snake_case :			Union[str, Any]							=   get_framework(__lowerCamelCase							)
 if framework == "pt":
  snake_case :			List[Any]							=   '''torch'''
 snake_case :			Union[str, Any]							=   '''{}'''

 snake_case :			Union[str, Any]							=   get_pipeline_model_mapping(__lowerCamelCase							)
 if len(__lowerCamelCase							) == 0:
  return ""

 snake_case :			Tuple							=   []
 for task, model_classes in mapping.items():
  if isinstance(__lowerCamelCase			,  (tuple, list)							):
   # A list/tuple of model classes
   snake_case :			Tuple							=   '''(''' + ''', '''.join([x.__name__ for x in model_classes]							) + ''')'''
  else:
   # A single model class
   snake_case :			Optional[Any]							=   model_classes.__name__
  texts.append(f'''"{task}": {value}'''							)
 snake_case :			Optional[Any]							=   '''{''' + ''', '''.join(__lowerCamelCase							) + '''}'''
 snake_case :			int							=   f'''pipeline_model_mapping = {text} if is_{framework}_available() else {default_value}'''

 return text






def  _UpperCAmelCase						( __lowerCamelCase							)		->int:


 '''simple docstring'''
 snake_case :			Tuple							=   {'''ModelTesterMixin''', '''TFModelTesterMixin''', '''FlaxModelTesterMixin'''}
 if not issubclass(__lowerCamelCase			,  unittest.TestCase							):
  return False
 return len(base_class_names.intersection([x.__name__ for x in test_class.__bases__]							)							) > 0






def  _UpperCAmelCase						( __lowerCamelCase							)		->int:


 '''simple docstring'''
 snake_case :			Any							=   [x for x in get_test_classes(__lowerCamelCase							) if is_valid_test_class(__lowerCamelCase							)]

 snake_case :			int							=   None
 for test_class in test_classes:
  # If a test class has defined `pipeline_model_mapping`, let's take it
  if getattr(__lowerCamelCase			,  '''pipeline_model_mapping'''			,  __lowerCamelCase							) is not None:
   snake_case :			Dict							=   test_class
   break
    # Take the test class with the shortest name (just a heuristic)
 if target_test_class is None and len(__lowerCamelCase							) > 0:
  snake_case :			str							=   sorted(__lowerCamelCase			,  key=lambda __lowerCamelCase							: (len(x.__name__							), x.__name__)							)[0]

 return target_test_class






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)		->Optional[int]:


 '''simple docstring'''
 snake_case :			int							=   start_idx
 for idx, line in enumerate(lines[start_idx:]							):
  snake_case :			List[str]							=   len(__lowerCamelCase							) - len(line.lstrip()							)
  if idx == 0 or indent > indent_level or (indent == indent_level and line.strip() == ")"):
   snake_case :			Optional[int]							=   start_idx + idx
  elif idx > 0 and indent <= indent_level:
   # Outside the definition block of `pipeline_model_mapping`
   break

 return end_idx






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=False							)		->str:


 '''simple docstring'''
 if getattr(__lowerCamelCase			,  '''pipeline_model_mapping'''			,  __lowerCamelCase							) is not None:
  if not overwrite:
   return "", -1

 snake_case :			List[Any]							=   get_pipeline_model_mapping_string(__lowerCamelCase							)
 if len(__lowerCamelCase							) == 0:
  return "", -1
 snake_case :			Optional[int]							=   line_to_add + '''\n'''

 # The code defined the class `test_class`
 snake_case	,       snake_case :			Any							=   inspect.getsourcelines(__lowerCamelCase							)
 # `inspect` gives the code for an object, including decorator(s) if any.
 # We (only) need the exact line of the class definition.
 for idx, line in enumerate(__lowerCamelCase							):
  if line.lstrip().startswith('''class '''							):
   snake_case :			Any							=   class_lines[idx:]
   class_start_line_no += idx
   break
 snake_case :			Tuple							=   class_start_line_no + len(__lowerCamelCase							) - 1

 # The index in `class_lines` that starts the definition of `all_model_classes`, `all_generative_model_classes` or
 # `pipeline_model_mapping`. This assumes they are defined in such order, and we take the start index of the last
 # block that appears in a `test_class`.
 snake_case :			List[Any]							=   None
 # The indent level of the line at `class_lines[start_idx]` (if defined)
 snake_case :			List[str]							=   0
 # To record if `pipeline_model_mapping` is found in `test_class`.
 snake_case :			List[Any]							=   None
 for idx, line in enumerate(__lowerCamelCase							):
  if line.strip().startswith('''all_model_classes = '''							):
   snake_case :			List[str]							=   len(__lowerCamelCase							) - len(line.lstrip()							)
   snake_case :			int							=   idx
  elif line.strip().startswith('''all_generative_model_classes = '''							):
   snake_case :			Tuple							=   len(__lowerCamelCase							) - len(line.lstrip()							)
   snake_case :			Optional[int]							=   idx
  elif line.strip().startswith('''pipeline_model_mapping = '''							):
   snake_case :			Union[str, Any]							=   len(__lowerCamelCase							) - len(line.lstrip()							)
   snake_case :			List[str]							=   idx
   snake_case :			int							=   line
   break

 if start_idx is None:
  return "", -1
 # Find the ending index (inclusive) of the above found block.
 snake_case :			Dict							=   find_block_ending(__lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)

 # Extract `is_xxx_available()` from existing blocks: some models require specific libraries like `timm` and use
 # `is_timm_available()` instead of `is_torch_available()`.
 # Keep leading and trailing whitespaces
 snake_case :			Tuple							=   re.compile(R'''\s(is_\S+?_available\(\))\s'''							)
 for line in class_lines[start_idx : end_idx + 1]:
  snake_case :			List[str]							=   r.search(__lowerCamelCase							)
  if backend_condition is not None:
   # replace the leading and trailing whitespaces to the space character " ".
   snake_case :			Union[str, Any]							=   ''' ''' + backend_condition[0][1:-1] + ''' '''
   snake_case :			Tuple							=   r.sub(__lowerCamelCase			,  __lowerCamelCase							)
   break

 if def_line is None:
  # `pipeline_model_mapping` is not defined. The target index is set to the ending index (inclusive) of
  # `all_model_classes` or `all_generative_model_classes`.
  snake_case :			Union[str, Any]							=   end_idx
 else:
  # `pipeline_model_mapping` is defined. The target index is set to be one **BEFORE** its start index.
  snake_case :			str							=   start_idx - 1
  # mark the lines of the currently existing `pipeline_model_mapping` to be removed.
  for idx in range(__lowerCamelCase			,  end_idx + 1							):
   # These lines are going to be removed before writing to the test file.
   snake_case :			Union[str, Any]							=   None  # noqa

    # Make sure the test class is a subclass of `PipelineTesterMixin`.
 snake_case :			Union[str, Any]							=   [x.__name__ for x in test_class.__bases__]
 if "PipelineTesterMixin" not in parent_classes:
  # Put `PipelineTesterMixin` just before `unittest.TestCase`
  snake_case :			List[str]							=   [x for x in parent_classes if x != '''TestCase'''] + ['''PipelineTesterMixin''']
  if "TestCase" in parent_classes:
   # Here we **assume** the original string is always with `unittest.TestCase`.
   _parent_classes.append('''unittest.TestCase'''							)
  snake_case :			Any							=   ''', '''.join(_parent_classes							)
  for idx, line in enumerate(__lowerCamelCase							):
   # Find the ending of the declaration of `test_class`
   if line.strip().endswith('''):'''							):
    # mark the lines of the declaration of `test_class` to be removed
    for _idx in range(idx + 1							):
     snake_case :			List[str]							=   None  # noqa
    break
        # Add the new, one-line, class declaration for `test_class`
  snake_case :			List[Any]							=   f'''class {test_class.__name__}({parent_classes}):\n'''

 # Add indentation
 snake_case :			int							=   ''' ''' * indent_level + line_to_add
 # Insert `pipeline_model_mapping` to `class_lines`.
 # (The line at `target_idx` should be kept by definition!)
 snake_case :			List[Any]							=   class_lines[: target_idx + 1] + [line_to_add] + class_lines[target_idx + 1 :]
 # Remove the lines that are marked to be removed
 snake_case :			Optional[Any]							=   [x for x in class_lines if x is not None]

 # Move from test class to module (in order to write to the test file)
 snake_case :			int							=   inspect.getsourcelines(inspect.getmodule(__lowerCamelCase							)							)[0]
 # Be careful with the 1-off between line numbers and array indices
 snake_case :			List[Any]							=   module_lines[: class_start_line_no - 1] + class_lines + module_lines[class_end_line_no:]
 snake_case :			List[Any]							=   ''''''.join(__lowerCamelCase							)

 snake_case :			Union[str, Any]							=   inspect.getsourcefile(__lowerCamelCase							)
 with open(__lowerCamelCase			,  '''w'''			,  encoding='''UTF-8'''			,  newline='''\n'''							) as fp:
  fp.write(__lowerCamelCase							)

 return line_to_add






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=False							)		->Dict:


 '''simple docstring'''
 snake_case :			Optional[int]							=   find_test_class(__lowerCamelCase							)
 if test_class:
  add_pipeline_model_mapping(__lowerCamelCase			,  overwrite=__lowerCamelCase							)


if __name__ == "__main__":
  a					:				List[Any]       =      argparse.ArgumentParser()
  parser.add_argument(
      '--test_file', type=str, help='A path to the test file, starting with the repository\'s `tests` directory.'
  )
  parser.add_argument(
      '--all',
      action='store_true',
      help='If to check and modify all test files.',
  )
  parser.add_argument(
      '--overwrite',
      action='store_true',
      help='If to overwrite a test class if it has already defined `pipeline_model_mapping`.',
  )
  a					:				int       =      parser.parse_args()

  if not args.all and not args.test_file:
    raise ValueError('Please specify either `test_file` or pass `--all` to check/modify all test files.')
  elif args.all and args.test_file:
    raise ValueError('Only one of `--test_file` and `--all` could be specified.')

  a					:				Any       =      []
  if args.test_file:
    a					:				Optional[Any]       =      [args.test_file]
  else:
    a					:				int       =      os.path.join('tests', 'models', '**', 'test_modeling_*.py')
    for test_file in glob.glob(pattern):
      # `Flax` is not concerned at this moment
      if not test_file.startswith('test_modeling_flax_'):
        test_files.append(test_file)

  for test_file in test_files:
    if test_file in TEST_FILE_TO_IGNORE:
      print(F'''[SKIPPED] {test_file} is skipped as it is in `TEST_FILE_TO_IGNORE` in the file {__file__}.''')
      continue
    add_pipeline_model_mapping_to_test_file(test_file, overwrite=args.overwrite)










import tempfile
import unittest

import transformers
from transformers import XGLMConfig, XGLMTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, require_sentencepiece, slow

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


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

  from transformers.modeling_flax_pytorch_utils import (
      convert_pytorch_state_dict_to_flax,
      load_flax_weights_in_pytorch_model,
  )
  from transformers.models.xglm.modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel


if is_torch_available():
  import torch

@require_flax
class   lowerCamelCase						:

       def __init__(      self						,    a__						,    a__=14						,    a__=7						,    a__=True						,    a__=True						,    a__=True						,    a__=99						,    a__=32						,    a__=5						,    a__=4						,    a__=37						,    a__="gelu"						,    a__=0.1						,    a__=0.1						,    a__=5_12						,    a__=0.02						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			Any							=   parent
        snake_case :			str							=   batch_size
        snake_case :			str							=   seq_length
        snake_case :			str							=   is_training
        snake_case :			List[str]							=   use_input_mask
        snake_case :			str							=   use_labels
        snake_case :			Union[str, Any]							=   vocab_size
        snake_case :			Dict							=   d_model
        snake_case :			List[str]							=   num_hidden_layers
        snake_case :			Optional[int]							=   num_attention_heads
        snake_case :			Optional[Any]							=   ffn_dim
        snake_case :			Dict							=   activation_function
        snake_case :			List[str]							=   activation_dropout
        snake_case :			Any							=   attention_dropout
        snake_case :			str							=   max_position_embeddings
        snake_case :			Optional[int]							=   initializer_range
        snake_case :			Union[str, Any]							=   None
        snake_case :			int							=   0
        snake_case :			int							=   2
        snake_case :			Union[str, Any]							=   1

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   np.clip(ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size       )						,    3						,    self.vocab_size       )

        snake_case :			Union[str, Any]							=   None
        if self.use_input_mask:
         snake_case :			Tuple							=   random_attention_mask([self.batch_size, self.seq_length]       )

        snake_case :			Optional[Any]							=   XGLMConfig(
            vocab_size=self.vocab_size						,    d_model=self.hidden_size						,    num_layers=self.num_hidden_layers						,    attention_heads=self.num_attention_heads						,    ffn_dim=self.ffn_dim						,    activation_function=self.activation_function						,    activation_dropout=self.activation_dropout						,    attention_dropout=self.attention_dropout						,    max_position_embeddings=self.max_position_embeddings						,    initializer_range=self.initializer_range						,    use_cache=a__						,    bos_token_id=self.bos_token_id						,    eos_token_id=self.eos_token_id						,    pad_token_id=self.pad_token_id						,    )

        return (config, input_ids, input_mask)

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.prepare_config_and_inputs()
        snake_case	,       snake_case	,       snake_case :			List[str]							=   config_and_inputs
        snake_case :			List[str]							=   {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
        return config, inputs_dict

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case	,       snake_case :			List[str]							=   self.prepare_config_and_inputs()

        snake_case :			Tuple							=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
        snake_case :			Union[str, Any]							=   ids_tensor([self.batch_size, self.seq_length]						,    vocab_size=2       )

        return (
            config,
            input_ids,
            attention_mask,
            encoder_hidden_states,
            encoder_attention_mask,
        )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   20
        snake_case :			List[Any]							=   model_class_name(a__       )

        snake_case :			List[Any]							=   model.init_cache(input_ids.shape[0]						,    a__       )
        snake_case :			Optional[int]							=   jnp.ones((input_ids.shape[0], max_decoder_length)						,    dtype='''i4'''       )

        snake_case :			Optional[Any]							=   jnp.broadcast_to(
            jnp.arange(input_ids.shape[-1] - 1       )[None, :]						,    (input_ids.shape[0], input_ids.shape[-1] - 1)       )
        snake_case :			Tuple							=   model(
            input_ids[:, :-1]						,    attention_mask=a__						,    past_key_values=a__						,    position_ids=a__						,    )

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

        snake_case :			Dict							=   model(a__       )

        snake_case :			Union[str, 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}'''       )



       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   20
        snake_case :			Dict							=   model_class_name(a__       )

        snake_case :			Dict							=   jnp.concatenate(
            [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])       )]						,    axis=-1						,    )

        snake_case :			List[str]							=   model.init_cache(input_ids.shape[0]						,    a__       )
        snake_case :			Optional[int]							=   jnp.broadcast_to(
            jnp.arange(input_ids.shape[-1] - 1       )[None, :]						,    (input_ids.shape[0], input_ids.shape[-1] - 1)       )

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

        snake_case :			Optional[Any]							=   model(a__						,    attention_mask=a__       )
        snake_case :			Optional[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}'''       )







@require_sentencepiece
@require_flax
class   lowerCamelCase						(	SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _UpperCamelCase     		=   (FlaxXGLMModel, FlaxXGLMForCausalLM) if is_flax_available() else ()
       _UpperCamelCase     		=   (FlaxXGLMForCausalLM,) if is_flax_available() else ()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   FlaxXGLMModelTester(self       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_class_name in self.all_model_classes:
         snake_case	,       snake_case	,       snake_case :			str							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.check_use_cache_forward(a__						,    a__						,    a__						,    a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_class_name in self.all_model_classes:
         snake_case	,       snake_case	,       snake_case :			int							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.check_use_cache_forward_with_attn_mask(
             a__						,    a__						,    a__						,    a__       )

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   XGLMTokenizer.from_pretrained('''XGLM'''						,    padding_side='''left'''       )
        snake_case :			Optional[int]							=   tokenizer(['''Hello this is a long string''', '''Hey''']						,    return_tensors='''np'''						,    padding=a__						,    truncation=a__       )

        snake_case :			List[Any]							=   FlaxXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''       )
        snake_case :			Optional[int]							=   1
        snake_case :			str							=   False

        snake_case :			str							=   jax.jit(model.generate       )

        snake_case :			Dict							=   jit_generate(inputs['''input_ids''']						,    attention_mask=inputs['''attention_mask''']       ).sequences

        snake_case :			Optional[Any]							=   tokenizer.batch_decode(a__						,    skip_special_tokens=a__       )

        snake_case :			Tuple							=   [
            '''Hello this is a long string of questions, but I\'m not sure if I\'m''',
            '''Hey, I\'m a newbie to the forum and I\'''',
        ]

        self.assertListEqual(a__						,    a__       )

       @is_pt_flax_cross_test
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			Union[str, Any]							=   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
          snake_case :			Union[str, Any]							=   self._prepare_for_class(a__						,    a__       )
          snake_case :			int							=   {k: torch.tensor(v.tolist()       ) for k, v in prepared_inputs_dict.items()}

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

          snake_case	,       snake_case :			Optional[Any]							=   pt_inputs['''input_ids'''].shape
          snake_case :			Tuple							=   np.random.randint(0						,    seq_length - 1						,    size=(batch_size,)       )
          for batch_idx, start_index in enumerate(a__       ):
           snake_case :			Optional[Any]							=   0
           snake_case :			Optional[int]							=   1
           snake_case :			Optional[Any]							=   0
           snake_case :			Optional[Any]							=   1
          snake_case :			Dict							=   pt_model_class(a__       ).eval()
          # Flax models don't use the `use_cache` option and cache is not returned as a default.
          # So we disable `use_cache` here for PyTorch model.
          snake_case :			Any							=   False
          snake_case :			str							=   model_class(a__						,    dtype=jnp.floataa       )
          snake_case :			Optional[int]							=   convert_pytorch_state_dict_to_flax(pt_model.state_dict()						,    a__       )
          snake_case :			Optional[int]							=   fx_state

          with torch.no_grad():
           snake_case :			int							=   pt_model(**a__       ).to_tuple()

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

          with tempfile.TemporaryDirectory() as tmpdirname:
           pt_model.save_pretrained(a__       )
           snake_case :			Union[str, Any]							=   model_class.from_pretrained(a__						,    from_pt=a__       )

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

       @is_pt_flax_cross_test
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case	,       snake_case :			List[Any]							=   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
          snake_case :			str							=   self._prepare_for_class(a__						,    a__       )
          snake_case :			List[Any]							=   {k: torch.tensor(v.tolist()       ) for k, v in prepared_inputs_dict.items()}

          # load corresponding PyTorch class
          snake_case :			Any							=   model_class.__name__[4:]  # Skip the "Flax" at the beginning
          snake_case :			Union[str, Any]							=   getattr(a__						,    a__       )

          snake_case :			List[str]							=   pt_model_class(a__       ).eval()
          snake_case :			Tuple							=   False
          snake_case :			Optional[int]							=   model_class(a__						,    dtype=jnp.floataa       )

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

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

          with torch.no_grad():
           snake_case :			Optional[int]							=   pt_model(**a__       ).to_tuple()

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

          with tempfile.TemporaryDirectory() as tmpdirname:
           fx_model.save_pretrained(a__       )
           snake_case :			int							=   pt_model_class.from_pretrained(a__						,    from_flax=a__       )

          with torch.no_grad():
           snake_case :			int							=   pt_model_loaded(**a__       ).to_tuple()

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



       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_class_name in self.all_model_classes:
         snake_case :			str							=   model_class_name.from_pretrained('''facebook/xglm-564M'''       )
         snake_case :			List[Any]							=   model(np.ones((1, 1)       )       )
         self.assertIsNotNone(a__       )






