import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
      from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor


class      lowerCamelCase    :
  """simple docstring"""







  def __init__(							self       :				List[str]     ,   __magic_name__       :				str     ,   __magic_name__       :				Any=13     ,   __magic_name__       :				int=32     ,   __magic_name__       :				str=2     ,   __magic_name__       :				Optional[Any]=3     ,   __magic_name__       :				List[Any]=16     ,   __magic_name__       :				int=[1, 2, 1]     ,   __magic_name__       :				Any=[2, 2, 4]     ,   __magic_name__       :				Dict=2     ,   __magic_name__       :				Dict=2.0     ,   __magic_name__       :				Union[str, Any]=True     ,   __magic_name__       :				Optional[int]=0.0     ,   __magic_name__       :				Union[str, Any]=0.0     ,   __magic_name__       :				Any=0.1     ,   __magic_name__       :				Union[str, Any]="gelu"     ,   __magic_name__       :				int=False     ,   __magic_name__       :				str=True     ,   __magic_name__       :				str=0.02     ,   __magic_name__       :				List[Any]=1e-5     ,   __magic_name__       :				str=True     ,   __magic_name__       :				Optional[int]=None     ,   __magic_name__       :				List[Any]=True     ,   __magic_name__       :				int=10     ,   __magic_name__       :				int=8     ,   )     ->		List[Any]:
        SCREAMING_SNAKE_CASE_      =     parent
        SCREAMING_SNAKE_CASE_      =     batch_size
        SCREAMING_SNAKE_CASE_      =     image_size
        SCREAMING_SNAKE_CASE_      =     patch_size
        SCREAMING_SNAKE_CASE_      =     num_channels
        SCREAMING_SNAKE_CASE_      =     embed_dim
        SCREAMING_SNAKE_CASE_      =     depths
        SCREAMING_SNAKE_CASE_      =     num_heads
        SCREAMING_SNAKE_CASE_      =     window_size
        SCREAMING_SNAKE_CASE_      =     mlp_ratio
        SCREAMING_SNAKE_CASE_      =     qkv_bias
        SCREAMING_SNAKE_CASE_      =     hidden_dropout_prob
        SCREAMING_SNAKE_CASE_      =     attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_      =     drop_path_rate
        SCREAMING_SNAKE_CASE_      =     hidden_act
        SCREAMING_SNAKE_CASE_      =     use_absolute_embeddings
        SCREAMING_SNAKE_CASE_      =     patch_norm
        SCREAMING_SNAKE_CASE_      =     layer_norm_eps
        SCREAMING_SNAKE_CASE_      =     initializer_range
        SCREAMING_SNAKE_CASE_      =     is_training
        SCREAMING_SNAKE_CASE_      =     scope
        SCREAMING_SNAKE_CASE_      =     use_labels
        SCREAMING_SNAKE_CASE_      =     type_sequence_label_size
        SCREAMING_SNAKE_CASE_      =     encoder_stride







  def       __A   (							self       :				Optional[Any]      )     ->		List[Any]:
        SCREAMING_SNAKE_CASE_      =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

        SCREAMING_SNAKE_CASE_      =     None
        if self.use_labels:
              SCREAMING_SNAKE_CASE_      =     ids_tensor([self.batch_size]     ,   self.type_sequence_label_size      )

        SCREAMING_SNAKE_CASE_      =     self.get_config()

        return config, pixel_values, labels







  def       __A   (							self       :				List[str]      )     ->		int:
        return SwinvaConfig(
            image_size=self.image_size     ,   patch_size=self.patch_size     ,   num_channels=self.num_channels     ,   embed_dim=self.embed_dim     ,   depths=self.depths     ,   num_heads=self.num_heads     ,   window_size=self.window_size     ,   mlp_ratio=self.mlp_ratio     ,   qkv_bias=self.qkv_bias     ,   hidden_dropout_prob=self.hidden_dropout_prob     ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,   drop_path_rate=self.drop_path_rate     ,   hidden_act=self.hidden_act     ,   use_absolute_embeddings=self.use_absolute_embeddings     ,   path_norm=self.patch_norm     ,   layer_norm_eps=self.layer_norm_eps     ,   initializer_range=self.initializer_range     ,   encoder_stride=self.encoder_stride     ,   )







  def       __A   (							self       :				List[str]     ,   __magic_name__       :				Union[str, Any]     ,   __magic_name__       :				int     ,   __magic_name__       :				int      )     ->		List[Any]:
        SCREAMING_SNAKE_CASE_      =     SwinvaModel(config=__UpperCAmelCase      )
        model.to(__UpperCAmelCase      )
        model.eval()
        SCREAMING_SNAKE_CASE_      =     model(__UpperCAmelCase      )

        SCREAMING_SNAKE_CASE_      =     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths      ) - 1))
        SCREAMING_SNAKE_CASE_      =     int(config.embed_dim * 2 ** (len(config.depths      ) - 1)      )

        self.parent.assertEqual(result.last_hidden_state.shape     ,   (self.batch_size, expected_seq_len, expected_dim)      )







  def       __A   (							self       :				List[Any]     ,   __magic_name__       :				Optional[int]     ,   __magic_name__       :				List[str]     ,   __magic_name__       :				List[Any]      )     ->		List[str]:
        SCREAMING_SNAKE_CASE_      =     SwinvaForMaskedImageModeling(config=__UpperCAmelCase      )
        model.to(__UpperCAmelCase      )
        model.eval()
        SCREAMING_SNAKE_CASE_      =     model(__UpperCAmelCase      )
        self.parent.assertEqual(
            result.logits.shape     ,   (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

        # test greyscale images
        SCREAMING_SNAKE_CASE_      =     1
        SCREAMING_SNAKE_CASE_      =     SwinvaForMaskedImageModeling(__UpperCAmelCase      )
        model.to(__UpperCAmelCase      )
        model.eval()

        SCREAMING_SNAKE_CASE_      =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
        SCREAMING_SNAKE_CASE_      =     model(__UpperCAmelCase      )
        self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, 1, self.image_size, self.image_size)      )







  def       __A   (							self       :				int     ,   __magic_name__       :				Dict     ,   __magic_name__       :				str     ,   __magic_name__       :				Any      )     ->		Union[str, Any]:
        SCREAMING_SNAKE_CASE_      =     self.type_sequence_label_size
        SCREAMING_SNAKE_CASE_      =     SwinvaForImageClassification(__UpperCAmelCase      )
        model.to(__UpperCAmelCase      )
        model.eval()
        SCREAMING_SNAKE_CASE_      =     model(__UpperCAmelCase     ,   labels=__UpperCAmelCase      )
        self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.type_sequence_label_size)      )







  def       __A   (							self       :				List[Any]      )     ->		Optional[int]:
        SCREAMING_SNAKE_CASE_      =     self.prepare_config_and_inputs()
        SCREAMING_SNAKE_CASE_      =     config_and_inputs
        SCREAMING_SNAKE_CASE_      =     {"""pixel_values""": pixel_values}
        return config, inputs_dict



@require_torch
class      lowerCamelCase    (SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_     ,					unittest.TestCase    ):
  """simple docstring"""







  lowerCamelCase__   =  (
      (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
  )
  lowerCamelCase__   =  (
      {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
      if is_torch_available()
      else {}
  )

  lowerCamelCase__   =  False
  lowerCamelCase__   =  False
  lowerCamelCase__   =  False
  lowerCamelCase__   =  False







  def       __A   (							self       :				Tuple      )     ->		Optional[int]:
        SCREAMING_SNAKE_CASE_      =     SwinvaModelTester(self      )
        SCREAMING_SNAKE_CASE_      =     ConfigTester(self     ,   config_class=__UpperCAmelCase     ,   embed_dim=37      )







  def       __A   (							self       :				List[Any]      )     ->		Union[str, Any]:
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()







  def       __A   (							self       :				Optional[int]      )     ->		str:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCAmelCase      )







  @unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."      )
  def       __A   (							self       :				Tuple      )     ->		int:
        pass







  @unittest.skip(reason="Swinv2 does not use inputs_embeds"      )
  def       __A   (							self       :				int      )     ->		int:
        pass







  def       __A   (							self       :				Optional[Any]      )     ->		Any:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
              self.assertIsInstance(model.get_input_embeddings()     ,   (nn.Module)      )
              SCREAMING_SNAKE_CASE_      =     model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(__UpperCAmelCase     ,   nn.Linear      )      )







  def       __A   (							self       :				List[str]      )     ->		str:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
              SCREAMING_SNAKE_CASE_      =     inspect.signature(model.forward      )
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              SCREAMING_SNAKE_CASE_      =     [*signature.parameters.keys()]

              SCREAMING_SNAKE_CASE_      =     ["""pixel_values"""]
              self.assertListEqual(arg_names[:1]     ,   __UpperCAmelCase      )







  def       __A   (							self       :				Dict      )     ->		int:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_      =     True

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     True
              SCREAMING_SNAKE_CASE_      =     False
              SCREAMING_SNAKE_CASE_      =     True
              SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
              model.to(__UpperCAmelCase      )
              model.eval()
              with torch.no_grad():
                    SCREAMING_SNAKE_CASE_      =     model(**self._prepare_for_class(__UpperCAmelCase     ,   __UpperCAmelCase      )      )
              SCREAMING_SNAKE_CASE_      =     outputs.attentions
              SCREAMING_SNAKE_CASE_      =     len(self.model_tester.depths      )
              self.assertEqual(len(__UpperCAmelCase      )     ,   __UpperCAmelCase      )

              # check that output_attentions also work using config
              del inputs_dict["output_attentions"]
              SCREAMING_SNAKE_CASE_      =     True
              SCREAMING_SNAKE_CASE_      =     config.window_size**2
              SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
              model.to(__UpperCAmelCase      )
              model.eval()
              with torch.no_grad():
                    SCREAMING_SNAKE_CASE_      =     model(**self._prepare_for_class(__UpperCAmelCase     ,   __UpperCAmelCase      )      )
              SCREAMING_SNAKE_CASE_      =     outputs.attentions
              self.assertEqual(len(__UpperCAmelCase      )     ,   __UpperCAmelCase      )

              self.assertListEqual(
                  list(attentions[0].shape[-3:]      )     ,   [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,   )
              SCREAMING_SNAKE_CASE_      =     len(__UpperCAmelCase      )

              # Check attention is always last and order is fine
              SCREAMING_SNAKE_CASE_      =     True
              SCREAMING_SNAKE_CASE_      =     True
              SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
              model.to(__UpperCAmelCase      )
              model.eval()
              with torch.no_grad():
                    SCREAMING_SNAKE_CASE_      =     model(**self._prepare_for_class(__UpperCAmelCase     ,   __UpperCAmelCase      )      )

              if hasattr(self.model_tester     ,   "num_hidden_states_types"      ):
                    SCREAMING_SNAKE_CASE_      =     self.model_tester.num_hidden_states_types
              else:
                    # also another +1 for reshaped_hidden_states
                    SCREAMING_SNAKE_CASE_      =     2
              self.assertEqual(out_len + added_hidden_states     ,   len(__UpperCAmelCase      )      )

              SCREAMING_SNAKE_CASE_      =     outputs.attentions

              self.assertEqual(len(__UpperCAmelCase      )     ,   __UpperCAmelCase      )

              self.assertListEqual(
                  list(self_attentions[0].shape[-3:]      )     ,   [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,   )







  def       __A   (							self       :				Tuple     ,   __magic_name__       :				Dict     ,   __magic_name__       :				List[str]     ,   __magic_name__       :				Any     ,   __magic_name__       :				List[Any]      )     ->		Tuple:
        SCREAMING_SNAKE_CASE_      =     model_class(__UpperCAmelCase      )
        model.to(__UpperCAmelCase      )
        model.eval()

        with torch.no_grad():
              SCREAMING_SNAKE_CASE_      =     model(**self._prepare_for_class(__UpperCAmelCase     ,   __UpperCAmelCase      )      )

        SCREAMING_SNAKE_CASE_      =     outputs.hidden_states

        SCREAMING_SNAKE_CASE_      =     getattr(
            self.model_tester     ,   "expected_num_hidden_layers"     ,   len(self.model_tester.depths      ) + 1      )
        self.assertEqual(len(__UpperCAmelCase      )     ,   __UpperCAmelCase      )

        # Swinv2 has a different seq_length
        SCREAMING_SNAKE_CASE_      =     (
            config.patch_size
            if isinstance(config.patch_size     ,   collections.abc.Iterable      )
            else (config.patch_size, config.patch_size)
        )

        SCREAMING_SNAKE_CASE_      =     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

        self.assertListEqual(
            list(hidden_states[0].shape[-2:]      )     ,   [num_patches, self.model_tester.embed_dim]     ,   )

        SCREAMING_SNAKE_CASE_      =     outputs.reshaped_hidden_states
        self.assertEqual(len(__UpperCAmelCase      )     ,   __UpperCAmelCase      )

        SCREAMING_SNAKE_CASE_      =     reshaped_hidden_states[0].shape
        SCREAMING_SNAKE_CASE_      =     (
            reshaped_hidden_states[0].view(__UpperCAmelCase     ,   __UpperCAmelCase     ,   height * width      ).permute(0     ,   2     ,   1      )
        )
        self.assertListEqual(
            list(reshaped_hidden_states.shape[-2:]      )     ,   [num_patches, self.model_tester.embed_dim]     ,   )







  def       __A   (							self       :				str      )     ->		List[str]:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()

        SCREAMING_SNAKE_CASE_      =     (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size     ,   collections.abc.Iterable      )
            else (self.model_tester.image_size, self.model_tester.image_size)
        )

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     True
              self.check_hidden_states_output(__UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase      )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              SCREAMING_SNAKE_CASE_      =     True

              self.check_hidden_states_output(__UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase      )







  def       __A   (							self       :				str      )     ->		Union[str, Any]:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_      =     3

        SCREAMING_SNAKE_CASE_      =     (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size     ,   collections.abc.Iterable      )
            else (self.model_tester.image_size, self.model_tester.image_size)
        )
        SCREAMING_SNAKE_CASE_      =     (
            config.patch_size
            if isinstance(config.patch_size     ,   collections.abc.Iterable      )
            else (config.patch_size, config.patch_size)
        )

        SCREAMING_SNAKE_CASE_      =     image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        SCREAMING_SNAKE_CASE_      =     image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     True
              self.check_hidden_states_output(__UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase     ,   (padded_height, padded_width)      )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              SCREAMING_SNAKE_CASE_      =     True
              self.check_hidden_states_output(__UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase     ,   (padded_height, padded_width)      )







  def       __A   (							self       :				Union[str, Any]      )     ->		int:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase      )







  def       __A   (							self       :				Any      )     ->		Tuple:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase      )







  @slow
  def       __A   (							self       :				Optional[Any]      )     ->		Any:
        for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_      =     SwinvaModel.from_pretrained(__UpperCAmelCase      )
              self.assertIsNotNone(__UpperCAmelCase      )







  def       __A   (							self       :				List[str]      )     ->		Any:
        SCREAMING_SNAKE_CASE_      =     self.model_tester.prepare_config_and_inputs_for_common()

        SCREAMING_SNAKE_CASE_      =     _config_zero_init(__UpperCAmelCase      )
        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE_      =     model_class(config=__UpperCAmelCase      )
              for name, param in model.named_parameters():
                    if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
                          self.assertIn(
                              ((param.data.mean() * 1e9).round() / 1e9).item()     ,   [0.0, 1.0]     ,   msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''     ,   )



@require_vision
@require_torch
class      lowerCamelCase    (unittest.TestCase    ):
  """simple docstring"""







  @cached_property
  def       __A   (							self       :				int      )     ->		Any:
        return (
            AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"      )
            if is_vision_available()
            else None
        )







  @slow
  def       __A   (							self       :				Optional[int]      )     ->		List[str]:
        SCREAMING_SNAKE_CASE_      =     SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"      ).to(
            __UpperCAmelCase      )
        SCREAMING_SNAKE_CASE_      =     self.default_image_processor

        SCREAMING_SNAKE_CASE_      =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
        SCREAMING_SNAKE_CASE_      =     image_processor(images=__UpperCAmelCase     ,   return_tensors="pt"      ).to(__UpperCAmelCase      )

        # forward pass
        with torch.no_grad():
              SCREAMING_SNAKE_CASE_      =     model(**__UpperCAmelCase      )

        # verify the logits
        SCREAMING_SNAKE_CASE_      =     torch.Size((1, 1_000)      )
        self.assertEqual(outputs.logits.shape     ,   __UpperCAmelCase      )
        SCREAMING_SNAKE_CASE_      =     torch.tensor([-0.3947, -0.4306, 0.0026]      ).to(__UpperCAmelCase      )
        self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,   __UpperCAmelCase     ,   atol=1e-4      )      )







'''simple docstring'''





import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory

from transformers import BertConfig, BertTokenizerFast, FeatureExtractionPipeline
from transformers.convert_graph_to_onnx import (
    convert,
    ensure_valid_input,
    generate_identified_filename,
    infer_shapes,
    quantize,
)
from transformers.testing_utils import require_tf, require_tokenizers, require_torch, slow






class        lowerCAmelCase_:
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Optional[Any]:
      return None







class        lowerCAmelCase_:
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Tuple:
      return None







class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  __lowercase							:      Dict				=      [
      # (model_name, model_kwargs)
      ('''bert-base-cased''', {}),
      ('''gpt2''', {'''use_cache''': False}),  # We don't support exporting GPT2 past keys anymore
  ]




  @require_tf
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		int:
      for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
          self._test_export(__UpperCAmelCase  ,"""tf"""  ,12  ,**__UpperCAmelCase      )




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
          self._test_export(__UpperCAmelCase  ,"""pt"""  ,12  ,**__UpperCAmelCase      )




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Any:
      from transformers import BertModel

      lowerCAmelCase__ :  Optional[int]							      =					["""[UNK]""", """[SEP]""", """[CLS]""", """[PAD]""", """[MASK]""", """some""", """other""", """words"""]
      with NamedTemporaryFile(mode="""w+t"""      ) as vocab_file:
          vocab_file.write("""\n""".join(__UpperCAmelCase      )      )
          vocab_file.flush()
          lowerCAmelCase__ :  Dict							      =					BertTokenizerFast(vocab_file.name      )

      with TemporaryDirectory() as bert_save_dir:
          lowerCAmelCase__ :  Tuple							      =					BertModel(BertConfig(vocab_size=len(__UpperCAmelCase      )      )      )
          model.save_pretrained(__UpperCAmelCase      )
          self._test_export(__UpperCAmelCase  ,"""pt"""  ,12  ,__UpperCAmelCase      )




  @require_tf
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
          lowerCAmelCase__ :  Dict							      =					self._test_export(__UpperCAmelCase  ,"""tf"""  ,12  ,**__UpperCAmelCase      )
          lowerCAmelCase__ :  List[str]							      =					quantize(Path(__UpperCAmelCase      )      )

          # Ensure the actual quantized model is not bigger than the original one
          if quantized_path.stat().st_size >= Path(__UpperCAmelCase      ).stat().st_size:
              self.fail("""Quantized model is bigger than initial ONNX model"""      )




  @require_torch
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		List[Any]:
      for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
          lowerCAmelCase__ :  Any							      =					self._test_export(__UpperCAmelCase  ,"""pt"""  ,12  ,**__UpperCAmelCase      )
          lowerCAmelCase__ :  Dict							      =					quantize(__UpperCAmelCase      )

          # Ensure the actual quantized model is not bigger than the original one
          if quantized_path.stat().st_size >= Path(__UpperCAmelCase      ).stat().st_size:
              self.fail("""Quantized model is bigger than initial ONNX model"""      )




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase=None  ,**__UpperCAmelCase      )    ->		Optional[Any]:
      try:
          # Compute path
          with TemporaryDirectory() as tempdir:
              lowerCAmelCase__ :  Optional[int]							      =					Path(__UpperCAmelCase      ).joinpath("""model.onnx"""      )

          # Remove folder if exists
          if path.parent.exists():
              path.parent.rmdir()

          # Export
          convert(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,**__UpperCAmelCase      )

          return path
      except Exception as e:
          self.fail(__UpperCAmelCase      )




  @require_torch
  @require_tokenizers
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      from transformers import BertModel

      lowerCAmelCase__ :  List[Any]							      =					BertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""      )      )
      lowerCAmelCase__ :  Union[str, Any]							      =					BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""      )
      self._test_infer_dynamic_axis(__UpperCAmelCase  ,__UpperCAmelCase  ,"""pt"""      )




  @require_tf
  @require_tokenizers
  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      from transformers import TFBertModel

      lowerCAmelCase__ :  int							      =					TFBertModel(BertConfig.from_pretrained("""lysandre/tiny-bert-random"""      )      )
      lowerCAmelCase__ :  Optional[int]							      =					BertTokenizerFast.from_pretrained("""lysandre/tiny-bert-random"""      )
      self._test_infer_dynamic_axis(__UpperCAmelCase  ,__UpperCAmelCase  ,"""tf"""      )




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )    ->		Tuple:
      lowerCAmelCase__ :  Any							      =					FeatureExtractionPipeline(__UpperCAmelCase  ,__UpperCAmelCase      )

      lowerCAmelCase__ :  List[str]							      =					["""input_ids""", """token_type_ids""", """attention_mask""", """output_0""", """output_1"""]
      lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__      ,   lowerCAmelCase__ :  Union[str, Any]							      =					infer_shapes(__UpperCAmelCase  ,__UpperCAmelCase      )

      # Assert all variables are present
      self.assertEqual(len(__UpperCAmelCase      )  ,len(__UpperCAmelCase      )      )
      self.assertTrue(all(var_name in shapes for var_name in variable_names      )      )
      self.assertSequenceEqual(variable_names[:3]  ,__UpperCAmelCase      )
      self.assertSequenceEqual(variable_names[3:]  ,__UpperCAmelCase      )

      # Assert inputs are {0: batch, 1: sequence}
      for var_name in ["input_ids", "token_type_ids", "attention_mask"]:
          self.assertDictEqual(shapes[var_name]  ,{0: """batch""", 1: """sequence"""}      )

      # Assert outputs are {0: batch, 1: sequence} and {0: batch}
      self.assertDictEqual(shapes["""output_0"""]  ,{0: """batch""", 1: """sequence"""}      )
      self.assertDictEqual(shapes["""output_1"""]  ,{0: """batch"""}      )




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  List[str]							      =					["""input_ids""", """attention_mask""", """token_type_ids"""]
      lowerCAmelCase__ :  Union[str, Any]							      =					{"""input_ids""": [1, 2, 3, 4], """attention_mask""": [0, 0, 0, 0], """token_type_ids""": [1, 1, 1, 1]}
      lowerCAmelCase__      ,   lowerCAmelCase__ :  List[Any]							      =					ensure_valid_input(FuncContiguousArgs()  ,__UpperCAmelCase  ,__UpperCAmelCase      )

      # Should have exactly the same number of args (all are valid)
      self.assertEqual(len(__UpperCAmelCase      )  ,3      )

      # Should have exactly the same input names
      self.assertEqual(set(__UpperCAmelCase      )  ,set(__UpperCAmelCase      )      )

      # Parameter should be reordered according to their respective place in the function:
      # (input_ids, token_type_ids, attention_mask)
      self.assertEqual(__UpperCAmelCase  ,(tokens["""input_ids"""], tokens["""token_type_ids"""], tokens["""attention_mask"""])      )

      # Generated args are interleaved with another args (for instance parameter "past" in GPT2)
      lowerCAmelCase__      ,   lowerCAmelCase__ :  int							      =					ensure_valid_input(FuncNonContiguousArgs()  ,__UpperCAmelCase  ,__UpperCAmelCase      )

      # Should have exactly the one arg (all before the one not provided "some_other_args")
      self.assertEqual(len(__UpperCAmelCase      )  ,1      )
      self.assertEqual(len(__UpperCAmelCase      )  ,1      )

      # Should have only "input_ids"
      self.assertEqual(inputs_args[0]  ,tokens["""input_ids"""]      )
      self.assertEqual(ordered_input_names[0]  ,"""input_ids"""      )






  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      lowerCAmelCase__ :  Dict							      =					generate_identified_filename(Path("""/home/something/my_fake_model.onnx"""      )  ,"""-test"""      )
      self.assertEqual("""/home/something/my_fake_model-test.onnx"""  ,generated.as_posix()      )


