"""simple docstring"""
import subprocess
import sys
import warnings
from argparse import ArgumentParser
from pathlib import Path

from packaging import version

from .. import AutoFeatureExtractor, AutoImageProcessor, AutoProcessor, AutoTokenizer
from ..utils import logging
from ..utils.import_utils import is_optimum_available
from .convert import export, validate_model_outputs
from .features import FeaturesManager
from .utils import get_preprocessor


lowercase__   			=      '1.5.0'

lowercase__   			=      ['vision-encoder-decoder']







def 		__a  (      _SCREAMING_SNAKE_CASE  )      ->int:
  if is_optimum_available():
    from optimum.version import __version__ as optimum_version

    a__:	int     				=      version.parse(_SCREAMING_SNAKE_CASE  )
    if parsed_optimum_version < version.parse(_SCREAMING_SNAKE_CASE  ):
      raise RuntimeError(
          F'transformers.onnx requires optimum >= {MIN_OPTIMUM_VERSION} but {optimum_version} is installed. You '
          'can upgrade optimum by running: pip install -U optimum[exporters]'  )
  else:
    raise RuntimeError(
        'transformers.onnx requires optimum to run, you can install the library by running: pip install '
        'optimum[exporters]'  )
  a__:	Dict     				=      [
      sys.executable,
      '-m',
      'optimum.exporters.onnx',
      F'--model {args.model}',
      F'--task {args.feature}',
      F'--framework {args.framework}' if args.framework is not None else '',
      F'{args.output}',
  ]
  a__:	Union[str, Any]     				=      subprocess.Popen(' '.join(_SCREAMING_SNAKE_CASE  )						,	stdout=subprocess.PIPE						,	shell=_SCREAMING_SNAKE_CASE  )
  proc.wait()

  logger.info(
      'The export was done by optimum.exporters.onnx. We recommend using to use this package directly in future, as '
      'transformers.onnx is deprecated, and will be removed in v5. You can find more information here: '
      'https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model.'  )







def 		__a  (      _SCREAMING_SNAKE_CASE  )      ->str:
  a__:	Dict     				=      args.output if args.output.is_file() else args.output.joinpath('model.onnx'  )
  if not args.output.parent.exists():
    args.output.parent.mkdir(parents=_SCREAMING_SNAKE_CASE  )

  # Allocate the model
  a__:	Union[str, Any]     				=      FeaturesManager.get_model_from_feature(
      args.feature						,	args.model						,	framework=args.framework						,	cache_dir=args.cache_dir  )

  a__				,       a__:	str     				=      FeaturesManager.check_supported_model_or_raise(_SCREAMING_SNAKE_CASE						,	feature=args.feature  )
  a__:	Tuple     				=      model_onnx_config(model.config  )

  if model_kind in ENCODER_DECODER_MODELS:
    a__:	Any     				=      model.get_encoder()
    a__:	Tuple     				=      model.get_decoder()

    a__:	str     				=      onnx_config.get_encoder_config(encoder_model.config  )
    a__:	Any     				=      onnx_config.get_decoder_config(
        encoder_model.config						,	decoder_model.config						,	feature=args.feature  )

    if args.opset is None:
      a__:	int     				=      max(encoder_onnx_config.default_onnx_opset						,	decoder_onnx_config.default_onnx_opset  )

    if args.opset < min(encoder_onnx_config.default_onnx_opset						,	decoder_onnx_config.default_onnx_opset  ):
      raise ValueError(
          F'Opset {args.opset} is not sufficient to export {model_kind}. At least '
          F' {min(encoder_onnx_config.default_onnx_opset						,	decoder_onnx_config.default_onnx_opset  )} is required.'  )

    a__:	Optional[Any]     				=      AutoFeatureExtractor.from_pretrained(args.model  )

    a__				,       a__:	str     				=      export(
        _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.opset						,	args.output.parent.joinpath('encoder_model.onnx'  )						,	)

    validate_model_outputs(
        _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.output.parent.joinpath('encoder_model.onnx'  )						,	_SCREAMING_SNAKE_CASE						,	args.atol if args.atol else encoder_onnx_config.atol_for_validation						,	)

    a__:	Any     				=      AutoTokenizer.from_pretrained(args.model  )

    a__				,       a__:	List[str]     				=      export(
        _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.opset						,	args.output.parent.joinpath('decoder_model.onnx'  )						,	)

    validate_model_outputs(
        _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.output.parent.joinpath('decoder_model.onnx'  )						,	_SCREAMING_SNAKE_CASE						,	args.atol if args.atol else decoder_onnx_config.atol_for_validation						,	)
    logger.info(
        F'All good, model saved at: {args.output.parent.joinpath("encoder_model.onnx"  ).as_posix()},'
        F' {args.output.parent.joinpath("decoder_model.onnx"  ).as_posix()}'  )

  else:
    # Instantiate the appropriate preprocessor
    if args.preprocessor == "auto":
      a__:	Union[str, Any]     				=      get_preprocessor(args.model  )
    elif args.preprocessor == "tokenizer":
      a__:	Union[str, Any]     				=      AutoTokenizer.from_pretrained(args.model  )
    elif args.preprocessor == "image_processor":
      a__:	List[Any]     				=      AutoImageProcessor.from_pretrained(args.model  )
    elif args.preprocessor == "feature_extractor":
      a__:	Dict     				=      AutoFeatureExtractor.from_pretrained(args.model  )
    elif args.preprocessor == "processor":
      a__:	List[Any]     				=      AutoProcessor.from_pretrained(args.model  )
    else:
      raise ValueError(F'Unknown preprocessor type \'{args.preprocessor}\''  )

    # Ensure the requested opset is sufficient
    if args.opset is None:
      a__:	Tuple     				=      onnx_config.default_onnx_opset

    if args.opset < onnx_config.default_onnx_opset:
      raise ValueError(
          F'Opset {args.opset} is not sufficient to export {model_kind}. '
          F'At least  {onnx_config.default_onnx_opset} is required.'  )

    a__				,       a__:	List[str]     				=      export(
        _SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.opset						,	args.output						,	)

    if args.atol is None:
      a__:	str     				=      onnx_config.atol_for_validation

    validate_model_outputs(_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	_SCREAMING_SNAKE_CASE						,	args.output						,	_SCREAMING_SNAKE_CASE						,	args.atol  )
    logger.info(F'All good, model saved at: {args.output.as_posix()}'  )
    warnings.warn(
        'The export was done by transformers.onnx which is deprecated and will be removed in v5. We recommend'
        ' using optimum.exporters.onnx in future. You can find more information here:'
        ' https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model.'						,	_SCREAMING_SNAKE_CASE						,	)







def 		__a  (      )      ->Tuple:
  a__:	Tuple     				=      ArgumentParser('Hugging Face Transformers ONNX exporter'  )
  parser.add_argument(
      '-m'						,	'--model'						,	type=_SCREAMING_SNAKE_CASE						,	required=_SCREAMING_SNAKE_CASE						,	help='Model ID on huggingface.co or path on disk to load model from.'  )
  parser.add_argument(
      '--feature'						,	default='default'						,	help='The type of features to export the model with.'						,	)
  parser.add_argument('--opset'						,	type=_SCREAMING_SNAKE_CASE						,	default=_SCREAMING_SNAKE_CASE						,	help='ONNX opset version to export the model with.'  )
  parser.add_argument(
      '--atol'						,	type=_SCREAMING_SNAKE_CASE						,	default=_SCREAMING_SNAKE_CASE						,	help='Absolute difference tolerance when validating the model.'  )
  parser.add_argument(
      '--framework'						,	type=_SCREAMING_SNAKE_CASE						,	choices=['pt', 'tf']						,	default=_SCREAMING_SNAKE_CASE						,	help=(
          'The framework to use for the ONNX export.'
          ' If not provided, will attempt to use the local checkpoint\'s original framework'
          ' or what is available in the environment.'
      )						,	)
  parser.add_argument('output'						,	type=_SCREAMING_SNAKE_CASE						,	help='Path indicating where to store generated ONNX model.'  )
  parser.add_argument('--cache_dir'						,	type=_SCREAMING_SNAKE_CASE						,	default=_SCREAMING_SNAKE_CASE						,	help='Path indicating where to store cache.'  )
  parser.add_argument(
      '--preprocessor'						,	type=_SCREAMING_SNAKE_CASE						,	choices=['auto', 'tokenizer', 'feature_extractor', 'image_processor', 'processor']						,	default='auto'						,	help='Which type of preprocessor to use. \'auto\' tries to automatically detect it.'						,	)
  parser.add_argument(
      '--export_with_transformers'						,	action='store_true'						,	help=(
          'Whether to use transformers.onnx instead of optimum.exporters.onnx to perform the ONNX export. It can be '
          'useful when exporting a model supported in transformers but not in optimum, otherwise it is not '
          'recommended.'
      )						,	)

  a__:	int     				=      parser.parse_args()
  if args.export_with_transformers or not is_optimum_available():
    export_with_transformers(_SCREAMING_SNAKE_CASE  )
  else:
    export_with_optimum(_SCREAMING_SNAKE_CASE  )


if __name__ == "__main__":
 lowercase__   			=      logging.get_logger('transformers.onnx')  # pylint: disable=invalid-name
 logger.setLevel(logging.INFO)
 main()










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							, _UpperCAmelCase							, _UpperCAmelCase=14							, _UpperCAmelCase=7							, _UpperCAmelCase=True							, _UpperCAmelCase=True							, _UpperCAmelCase=True							, _UpperCAmelCase=99							, _UpperCAmelCase=32							, _UpperCAmelCase=5							, _UpperCAmelCase=4							, _UpperCAmelCase=37							, _UpperCAmelCase="gelu"							, _UpperCAmelCase=0.1							, _UpperCAmelCase=0.1							, _UpperCAmelCase=512							, _UpperCAmelCase=0.02							, _UpperCAmelCase=None							, )		->				str:
        lowerCamelCase									=parent
        lowerCamelCase									=batch_size
        lowerCamelCase									=seq_length
        lowerCamelCase									=is_training
        lowerCamelCase									=use_input_mask
        lowerCamelCase									=use_labels
        lowerCamelCase									=vocab_size
        lowerCamelCase									=d_model
        lowerCamelCase									=num_hidden_layers
        lowerCamelCase									=num_attention_heads
        lowerCamelCase									=ffn_dim
        lowerCamelCase									=activation_function
        lowerCamelCase									=activation_dropout
        lowerCamelCase									=attention_dropout
        lowerCamelCase									=max_position_embeddings
        lowerCamelCase									=initializer_range
        lowerCamelCase									=None
        lowerCamelCase									=0
        lowerCamelCase									=2
        lowerCamelCase									=1





     def 						lowerCamelCase							(	self  )		->				List[str]:
        lowerCamelCase									=np.clip(ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size  )							, 3							, self.vocab_size  )

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

        lowerCamelCase									=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=_UpperCAmelCase							, 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 						lowerCamelCase							(	self  )		->				Tuple:
        lowerCamelCase									=self.prepare_config_and_inputs()
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=config_and_inputs
        lowerCamelCase									={"""input_ids""": input_ids, """attention_mask""": attention_mask}
        return config, inputs_dict





     def 						lowerCamelCase							(	self  )		->				int:
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=self.prepare_config_and_inputs()

        lowerCamelCase									=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]  )
        lowerCamelCase									=ids_tensor([self.batch_size, self.seq_length]							, vocab_size=2  )

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





     def 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )		->				Optional[Any]:
        lowerCamelCase									=20
        lowerCamelCase									=model_class_name(_UpperCAmelCase  )

        lowerCamelCase									=model.init_cache(input_ids.shape[0]							, _UpperCAmelCase  )
        lowerCamelCase									=jnp.ones((input_ids.shape[0], max_decoder_length)							, dtype="""i4"""  )

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

        lowerCamelCase									=jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]							, dtype="""i4"""  )
        lowerCamelCase									=model(
            input_ids[:, -1:]							, attention_mask=_UpperCAmelCase							, past_key_values=outputs_cache.past_key_values							, position_ids=_UpperCAmelCase							, )

        lowerCamelCase									=model(_UpperCAmelCase  )

        lowerCamelCase									=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 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )		->				List[Any]:
        lowerCamelCase									=20
        lowerCamelCase									=model_class_name(_UpperCAmelCase  )

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

        lowerCamelCase									=model.init_cache(input_ids.shape[0]							, _UpperCAmelCase  )
        lowerCamelCase									=jnp.broadcast_to(
            jnp.arange(input_ids.shape[-1] - 1  )[None, :]							, (input_ids.shape[0], input_ids.shape[-1] - 1)  )

        lowerCamelCase									=model(
            input_ids[:, :-1]							, attention_mask=_UpperCAmelCase							, past_key_values=_UpperCAmelCase							, position_ids=_UpperCAmelCase							, )
        lowerCamelCase									=jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]							, dtype="""i4"""  )
        lowerCamelCase									=model(
            input_ids[:, -1:]							, past_key_values=outputs_cache.past_key_values							, attention_mask=_UpperCAmelCase							, position_ids=_UpperCAmelCase							, )

        lowerCamelCase									=model(_UpperCAmelCase							, attention_mask=_UpperCAmelCase  )
        lowerCamelCase									=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_				(  UpperCamelCase				,     UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    Tuple 	=     (FlaxXGLMModel, FlaxXGLMForCausalLM) if is_flax_available() else ()
     __lowerCamelCase       :    Dict 	=     (FlaxXGLMForCausalLM,) if is_flax_available() else ()





     def 						lowerCamelCase							(	self  )		->				List[str]:
        lowerCamelCase									=FlaxXGLMModelTester(self  )





     def 						lowerCamelCase							(	self  )		->				List[str]:
        for model_class_name in self.all_model_classes:
           lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
           self.model_tester.check_use_cache_forward(_UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )





     def 						lowerCamelCase							(	self  )		->				Tuple:
        for model_class_name in self.all_model_classes:
           lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
           self.model_tester.check_use_cache_forward_with_attn_mask(
               _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )





     @slow
     def 						lowerCamelCase							(	self  )		->				Tuple:
        lowerCamelCase									=XGLMTokenizer.from_pretrained("""XGLM"""							, padding_side="""left"""  )
        lowerCamelCase									=tokenizer(["""Hello this is a long string""", """Hey"""]							, return_tensors="""np"""							, padding=_UpperCAmelCase							, truncation=_UpperCAmelCase  )

        lowerCamelCase									=FlaxXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""  )
        lowerCamelCase									=1
        lowerCamelCase									=False

        lowerCamelCase									=jax.jit(model.generate  )

        lowerCamelCase									=jit_generate(inputs["""input_ids"""]							, attention_mask=inputs["""attention_mask"""]  ).sequences

        lowerCamelCase									=tokenizer.batch_decode(_UpperCAmelCase							, skip_special_tokens=_UpperCAmelCase  )

        lowerCamelCase									=[
            """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(_UpperCAmelCase							, _UpperCAmelCase  )





     @is_pt_flax_cross_test
     def 						lowerCamelCase							(	self  )		->				Dict:
        lowerCamelCase				,       lowerCamelCase									=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
              lowerCamelCase									=self._prepare_for_class(_UpperCAmelCase							, _UpperCAmelCase  )
              lowerCamelCase									={k: torch.tensor(v.tolist()  ) for k, v in prepared_inputs_dict.items()}

              # load corresponding PyTorch class
              lowerCamelCase									=model_class.__name__[4:]  # Skip the "Flax" at the beginning
              lowerCamelCase									=getattr(_UpperCAmelCase							, _UpperCAmelCase  )

              lowerCamelCase				,       lowerCamelCase									=pt_inputs["""input_ids"""].shape
              lowerCamelCase									=np.random.randint(0							, seq_length - 1							, size=(batch_size,)  )
              for batch_idx, start_index in enumerate(_UpperCAmelCase  ):
                 lowerCamelCase									=0
                 lowerCamelCase									=1
                 lowerCamelCase									=0
                 lowerCamelCase									=1
              lowerCamelCase									=pt_model_class(_UpperCAmelCase  ).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.
              lowerCamelCase									=False
              lowerCamelCase									=model_class(_UpperCAmelCase							, dtype=jnp.floataa  )
              lowerCamelCase									=convert_pytorch_state_dict_to_flax(pt_model.state_dict()							, _UpperCAmelCase  )
              lowerCamelCase									=fx_state

              with torch.no_grad():
                 lowerCamelCase									=pt_model(**_UpperCAmelCase  ).to_tuple()

              lowerCamelCase									=fx_model(**_UpperCAmelCase  ).to_tuple()
              self.assertEqual(len(_UpperCAmelCase  )							, len(_UpperCAmelCase  )							, """Output lengths differ between Flax and PyTorch"""  )
              for fx_output, pt_output in zip(_UpperCAmelCase							, _UpperCAmelCase  ):
                 self.assert_almost_equals(fx_output[:, -1]							, pt_output[:, -1].numpy()							, 4E-2  )

              with tempfile.TemporaryDirectory() as tmpdirname:
                 pt_model.save_pretrained(_UpperCAmelCase  )
                 lowerCamelCase									=model_class.from_pretrained(_UpperCAmelCase							, from_pt=_UpperCAmelCase  )

              lowerCamelCase									=fx_model_loaded(**_UpperCAmelCase  ).to_tuple()
              self.assertEqual(
                  len(_UpperCAmelCase  )							, len(_UpperCAmelCase  )							, """Output lengths differ between Flax and PyTorch"""  )
              for fx_output_loaded, pt_output in zip(_UpperCAmelCase							, _UpperCAmelCase  ):
                 self.assert_almost_equals(fx_output_loaded[:, -1]							, pt_output[:, -1].numpy()							, 4E-2  )





     @is_pt_flax_cross_test
     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        lowerCamelCase				,       lowerCamelCase									=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
              lowerCamelCase									=self._prepare_for_class(_UpperCAmelCase							, _UpperCAmelCase  )
              lowerCamelCase									={k: torch.tensor(v.tolist()  ) for k, v in prepared_inputs_dict.items()}

              # load corresponding PyTorch class
              lowerCamelCase									=model_class.__name__[4:]  # Skip the "Flax" at the beginning
              lowerCamelCase									=getattr(_UpperCAmelCase							, _UpperCAmelCase  )

              lowerCamelCase									=pt_model_class(_UpperCAmelCase  ).eval()
              lowerCamelCase									=False
              lowerCamelCase									=model_class(_UpperCAmelCase							, dtype=jnp.floataa  )

              lowerCamelCase									=load_flax_weights_in_pytorch_model(_UpperCAmelCase							, fx_model.params  )
              lowerCamelCase				,       lowerCamelCase									=pt_inputs["""input_ids"""].shape
              lowerCamelCase									=np.random.randint(0							, seq_length - 1							, size=(batch_size,)  )
              for batch_idx, start_index in enumerate(_UpperCAmelCase  ):
                 lowerCamelCase									=0
                 lowerCamelCase									=1
                 lowerCamelCase									=0
                 lowerCamelCase									=1

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

              with torch.no_grad():
                 lowerCamelCase									=pt_model(**_UpperCAmelCase  ).to_tuple()

              lowerCamelCase									=fx_model(**_UpperCAmelCase  ).to_tuple()
              self.assertEqual(len(_UpperCAmelCase  )							, len(_UpperCAmelCase  )							, """Output lengths differ between Flax and PyTorch"""  )
              for fx_output, pt_output in zip(_UpperCAmelCase							, _UpperCAmelCase  ):
                 self.assert_almost_equals(fx_output[:, -1]							, pt_output[:, -1].numpy()							, 4E-2  )

              with tempfile.TemporaryDirectory() as tmpdirname:
                 fx_model.save_pretrained(_UpperCAmelCase  )
                 lowerCamelCase									=pt_model_class.from_pretrained(_UpperCAmelCase							, from_flax=_UpperCAmelCase  )

              with torch.no_grad():
                 lowerCamelCase									=pt_model_loaded(**_UpperCAmelCase  ).to_tuple()

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





     @slow
     def 						lowerCamelCase							(	self  )		->				int:
        for model_class_name in self.all_model_classes:
           lowerCamelCase									=model_class_name.from_pretrained("""facebook/xglm-564M"""  )
           lowerCamelCase									=model(np.ones((1, 1)  )  )
           self.assertIsNotNone(_UpperCAmelCase  )