


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 A    :






      def 			lowerCamelCase			(   self		:	Union[str, Any]     ,  lowercase_		:	List[Any]     ,  lowercase_		:	List[str]     ,  lowercase_		:	Union[str, Any]					)   ->		Union[str, Any]:
             """simple docstring"""




             return None




class A    :






      def 			lowerCamelCase			(   self		:	Optional[int]     ,  lowercase_		:	Optional[int]     ,  lowercase_		:	str     ,  lowercase_		:	Optional[Any]     ,  lowercase_		:	Optional[int]					)   ->		List[Any]:
             """simple docstring"""




             return None




class A    (  unittest.TestCase	):
      UpperCamelCase__				:    Tuple =[
          # (model_name, model_kwargs)
          ('bert-base-cased', {}),
          ('gpt2', {'use_cache': False}),  # We don't support exporting GPT2 past keys anymore
      ]






      @require_tf
      @slow
      def 			lowerCamelCase			(   self		:	List[str]					)   ->		List[Any]:
             """simple docstring"""




             for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
                    self._test_export(lowercase_     ,  'tf'     ,  12     ,  **lowercase_					)






      @require_torch
      @slow
      def 			lowerCamelCase			(   self		:	List[str]					)   ->		Optional[int]:
             """simple docstring"""




             for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
                    self._test_export(lowercase_     ,  'pt'     ,  12     ,  **lowercase_					)






      @require_torch
      @slow
      def 			lowerCamelCase			(   self		:	Tuple					)   ->		List[str]:
             """simple docstring"""




             from transformers import BertModel

             _lowerCamelCase			:						List[Any]            =['[UNK]', '[SEP]', '[CLS]', '[PAD]', '[MASK]', 'some', 'other', 'words']
             with NamedTemporaryFile(mode='w+t'					) as vocab_file:
                    vocab_file.write('\n'.join(lowercase_					)					)
                    vocab_file.flush()
                    _lowerCamelCase			:						Optional[int]            =BertTokenizerFast(vocab_file.name					)

             with TemporaryDirectory() as bert_save_dir:
                    _lowerCamelCase			:						Any            =BertModel(BertConfig(vocab_size=len(lowercase_					)					)					)
                    model.save_pretrained(lowercase_					)
                    self._test_export(lowercase_     ,  'pt'     ,  12     ,  lowercase_					)






      @require_tf
      @slow
      def 			lowerCamelCase			(   self		:	str					)   ->		List[str]:
             """simple docstring"""




             for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
                    _lowerCamelCase			:						int            =self._test_export(lowercase_     ,  'tf'     ,  12     ,  **lowercase_					)
                    _lowerCamelCase			:						List[Any]            =quantize(Path(lowercase_					)					)

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






      @require_torch
      @slow
      def 			lowerCamelCase			(   self		:	List[Any]					)   ->		List[Any]:
             """simple docstring"""




             for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
                    _lowerCamelCase			:						List[Any]            =self._test_export(lowercase_     ,  'pt'     ,  12     ,  **lowercase_					)
                    _lowerCamelCase			:						Optional[Any]            =quantize(lowercase_					)

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






      def 			lowerCamelCase			(   self		:	Dict     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Union[str, Any]     ,  lowercase_		:	str     ,  lowercase_		:	List[str]=None     ,  **lowercase_		:	List[str]					)   ->		Optional[int]:
             """simple docstring"""




             try:
                    # Compute path
                    with TemporaryDirectory() as tempdir:
                           _lowerCamelCase			:						Any            =Path(lowercase_					).joinpath('model.onnx'					)

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

                    # Export
                    convert(lowercase_     ,  lowercase_     ,  lowercase_     ,  lowercase_     ,  lowercase_     ,  **lowercase_					)

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






      @require_torch
      @require_tokenizers
      @slow
      def 			lowerCamelCase			(   self		:	List[Any]					)   ->		List[Any]:
             """simple docstring"""




             from transformers import BertModel

             _lowerCamelCase			:						Optional[int]            =BertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'					)					)
             _lowerCamelCase			:						Optional[Any]            =BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'					)
             self._test_infer_dynamic_axis(lowercase_     ,  lowercase_     ,  'pt'					)






      @require_tf
      @require_tokenizers
      @slow
      def 			lowerCamelCase			(   self		:	str					)   ->		Dict:
             """simple docstring"""




             from transformers import TFBertModel

             _lowerCamelCase			:						List[Any]            =TFBertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'					)					)
             _lowerCamelCase			:						List[Any]            =BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'					)
             self._test_infer_dynamic_axis(lowercase_     ,  lowercase_     ,  'tf'					)






      def 			lowerCamelCase			(   self		:	List[Any]     ,  lowercase_		:	Optional[int]     ,  lowercase_		:	str     ,  lowercase_		:	int					)   ->		List[str]:
             """simple docstring"""




             _lowerCamelCase			:						Tuple            =FeatureExtractionPipeline(lowercase_     ,  lowercase_					)

             _lowerCamelCase			:						Dict            =['input_ids', 'token_type_ids', 'attention_mask', 'output_0', 'output_1']
             _lowerCamelCase      ,	_lowerCamelCase      ,	_lowerCamelCase      ,	_lowerCamelCase			:						Tuple            =infer_shapes(lowercase_     ,  lowercase_					)

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

             # 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 			lowerCamelCase			(   self		:	List[str]					)   ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase			:						List[str]            =['input_ids', 'attention_mask', 'token_type_ids']
             _lowerCamelCase			:						Optional[Any]            ={'input_ids': [1, 2, 3, 4], 'attention_mask': [0, 0, 0, 0], 'token_type_ids': [1, 1, 1, 1]}
             _lowerCamelCase      ,	_lowerCamelCase			:						Optional[int]            =ensure_valid_input(FuncContiguousArgs()     ,  lowercase_     ,  lowercase_					)

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

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

             # Parameter should be reordered according to their respective place in the function:
             # (input_ids, token_type_ids, attention_mask)
             self.assertEqual(lowercase_     ,  (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			:						List[Any]            =ensure_valid_input(FuncNonContiguousArgs()     ,  lowercase_     ,  lowercase_					)

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

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






      def 			lowerCamelCase			(   self		:	str					)   ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase			:						str            =generate_identified_filename(Path('/home/something/my_fake_model.onnx'					)     ,  '-test'					)
             self.assertEqual('/home/something/my_fake_model-test.onnx'     ,  generated.as_posix()					)






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase		   =			logging.get_logger(__name__)

lowerCamelCase		   =			{
    'microsoft/focalnet-tiny': 'https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json',
}




class A    (  UpperCamelCase_       ,				UpperCamelCase_	):
      UpperCamelCase__				:    str ='focalnet'






      def __init__(   self		:	Optional[int]     ,  lowercase_		:	List[Any]=224     ,  lowercase_		:	str=4     ,  lowercase_		:	Any=3     ,  lowercase_		:	Dict=96     ,  lowercase_		:	List[str]=False     ,  lowercase_		:	Any=[192, 384, 768, 768]     ,  lowercase_		:	Dict=[2, 2, 6, 2]     ,  lowercase_		:	Optional[int]=[2, 2, 2, 2]     ,  lowercase_		:	List[Any]=[3, 3, 3, 3]     ,  lowercase_		:	Any="gelu"     ,  lowercase_		:	str=4.0     ,  lowercase_		:	List[Any]=0.0     ,  lowercase_		:	Tuple=0.1     ,  lowercase_		:	Optional[Any]=False     ,  lowercase_		:	Optional[int]=1E-4     ,  lowercase_		:	Dict=False     ,  lowercase_		:	Optional[Any]=False     ,  lowercase_		:	Dict=False     ,  lowercase_		:	List[Any]=0.02     ,  lowercase_		:	Any=1E-5     ,  lowercase_		:	Any=32     ,  lowercase_		:	List[str]=None     ,  lowercase_		:	int=None     ,  **lowercase_		:	Optional[Any]     ,  )   ->		Optional[Any]:
             """simple docstring"""




             super().__init__(**lowercase_					)

             _lowerCamelCase			:						Union[str, Any]            =image_size
             _lowerCamelCase			:						str            =patch_size
             _lowerCamelCase			:						str            =num_channels
             _lowerCamelCase			:						Tuple            =embed_dim
             _lowerCamelCase			:						Dict            =use_conv_embed
             _lowerCamelCase			:						int            =hidden_sizes
             _lowerCamelCase			:						str            =depths
             _lowerCamelCase			:						int            =focal_levels
             _lowerCamelCase			:						List[Any]            =focal_windows
             _lowerCamelCase			:						List[Any]            =hidden_act
             _lowerCamelCase			:						int            =mlp_ratio
             _lowerCamelCase			:						Any            =hidden_dropout_prob
             _lowerCamelCase			:						Optional[Any]            =drop_path_rate
             _lowerCamelCase			:						int            =use_layerscale
             _lowerCamelCase			:						str            =layerscale_value
             _lowerCamelCase			:						Dict            =use_post_layernorm
             _lowerCamelCase			:						str            =use_post_layernorm_in_modulation
             _lowerCamelCase			:						List[Any]            =normalize_modulator
             _lowerCamelCase			:						Optional[Any]            =initializer_range
             _lowerCamelCase			:						List[str]            =layer_norm_eps
             _lowerCamelCase			:						Optional[int]            =encoder_stride
             _lowerCamelCase			:						List[str]            =['stem'] + [F'''stage{idx}''' for idx in range(1     ,  len(self.depths					) + 1					)]
             _lowerCamelCase      ,	_lowerCamelCase			:						Union[str, Any]            =get_aligned_output_features_output_indices(
                 out_features=lowercase_     ,  out_indices=lowercase_     ,  stage_names=self.stage_names					)


