






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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												return None




class       __SCREAMING_SNAKE_CASE   :






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												return None




class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    List[str]						=   [
						    # (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				):






												"""simple docstring"""
												for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																		self._test_export(_UpperCamelCase       ,  'tf'       ,  12       ,  **_UpperCamelCase				)






						@require_torch
						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																		self._test_export(_UpperCamelCase       ,  'pt'       ,  12       ,  **_UpperCamelCase				)






						@require_torch
						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												from transformers import BertModel

												lowerCAmelCase__        =  ['[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__        =  BertTokenizerFast(vocab_file.name				)

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






						@require_tf
						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																		lowerCAmelCase__        =  self._test_export(_UpperCamelCase       ,  'tf'       ,  12       ,  **_UpperCamelCase				)
																		lowerCAmelCase__        =  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				):






												"""simple docstring"""
												for model, model_kwargs in OnnxExportTestCase.MODEL_TO_TEST:
																		lowerCAmelCase__        =  self._test_export(_UpperCamelCase       ,  'pt'       ,  12       ,  **_UpperCamelCase				)
																		lowerCAmelCase__        =  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				):






												"""simple docstring"""
												try:
																		# Compute path
																		with TemporaryDirectory() as tempdir:
																								lowerCAmelCase__        =  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				):






												"""simple docstring"""
												from transformers import BertModel

												lowerCAmelCase__        =  BertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'				)				)
												lowerCAmelCase__        =  BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'				)
												self._test_infer_dynamic_axis(_UpperCamelCase       ,  _UpperCamelCase       ,  'pt'				)






						@require_tf
						@require_tokenizers
						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												from transformers import TFBertModel

												lowerCAmelCase__        =  TFBertModel(BertConfig.from_pretrained('lysandre/tiny-bert-random'				)				)
												lowerCAmelCase__        =  BertTokenizerFast.from_pretrained('lysandre/tiny-bert-random'				)
												self._test_infer_dynamic_axis(_UpperCamelCase       ,  _UpperCamelCase       ,  'tf'				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  FeatureExtractionPipeline(_UpperCamelCase       ,  _UpperCamelCase				)

												lowerCAmelCase__        =  ['input_ids', 'token_type_ids', 'attention_mask', 'output_0', 'output_1']
												lowerCAmelCase__  ,    lowerCAmelCase__  ,    lowerCAmelCase__  ,    lowerCAmelCase__        =  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				):






												"""simple docstring"""
												lowerCAmelCase__        =  ['input_ids', 'attention_mask', 'token_type_ids']
												lowerCAmelCase__        =  {'input_ids': [1, 2, 3, 4], 'attention_mask': [0, 0, 0, 0], 'token_type_ids': [1, 1, 1, 1]}
												lowerCAmelCase__  ,    lowerCAmelCase__        =  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__        =  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				):






												"""simple docstring"""
												lowerCAmelCase__        =  generate_identified_filename(Path('/home/something/my_fake_model.onnx'				)       ,  '-test'				)
												self.assertEqual('/home/something/my_fake_model-test.onnx'       ,  generated.as_posix()				)













from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__snake_case      :      Dict             =       {"""configuration_focalnet""": ["""FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FocalNetConfig"""]}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__snake_case      :      str             =       [
					    """FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """FocalNetForImageClassification""",
					    """FocalNetForMaskedImageModeling""",
					    """FocalNetBackbone""",
					    """FocalNetModel""",
					    """FocalNetPreTrainedModel""",
					]

if TYPE_CHECKING:
					from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_focalnet import (
										    FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST,
										    FocalNetBackbone,
										    FocalNetForImageClassification,
										    FocalNetForMaskedImageModeling,
										    FocalNetModel,
										    FocalNetPreTrainedModel,
										)

else:
					import sys

					__snake_case      :      List[Any]             =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





