



from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class       a							(			__lowerCamelCase				):



		def 	__lowerCamelCase			(     self     :Optional[int]  ):
						snake_case__	:      Any     		=	SMALL_MODEL_IDENTIFIER
						snake_case__	:      Optional[int]     		=	'''pt'''
						snake_case__	:      Optional[Any]     		=	'''tf'''



		def 	__lowerCamelCase			(     self     :Dict   ,__lowercase     :Optional[Any]  ):
						snake_case__	:      int     		=	AutoModel.from_pretrained(self.test_model  )
						model_pt.save_pretrained(__lowercase  )



		def 	__lowerCamelCase			(     self     :Any   ,__lowercase     :Dict  ):
						snake_case__	:      List[str]     		=	TFAutoModel.from_pretrained(self.test_model   ,from_pt=__lowercase  )
						model_tf.save_pretrained(__lowercase  )



		def 	__lowerCamelCase			(     self     :str  ):
						snake_case__	:      Union[str, Any]     		=	'''mock_framework'''

						# Framework provided - return whatever the user provides
						snake_case__	:      int     		=	FeaturesManager.determine_framework(self.test_model   ,__lowercase  )
						self.assertEqual(__lowercase   ,__lowercase  )

						# Local checkpoint and framework provided - return provided framework
						# PyTorch checkpoint
						with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(__lowercase  )
										snake_case__	:      Dict     		=	FeaturesManager.determine_framework(__lowercase   ,__lowercase  )
										self.assertEqual(__lowercase   ,__lowercase  )

						# TensorFlow checkpoint
						with TemporaryDirectory() as local_tf_ckpt:
										self._setup_tf_ckpt(__lowercase  )
										snake_case__	:      Optional[Any]     		=	FeaturesManager.determine_framework(__lowercase   ,__lowercase  )
										self.assertEqual(__lowercase   ,__lowercase  )



		def 	__lowerCamelCase			(     self     :Dict  ):
						# PyTorch checkpoint
						with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(__lowercase  )
										snake_case__	:      Union[str, Any]     		=	FeaturesManager.determine_framework(__lowercase  )
										self.assertEqual(__lowercase   ,self.framework_pt  )

						# TensorFlow checkpoint
						with TemporaryDirectory() as local_tf_ckpt:
										self._setup_tf_ckpt(__lowercase  )
										snake_case__	:      Optional[Any]     		=	FeaturesManager.determine_framework(__lowercase  )
										self.assertEqual(__lowercase   ,self.framework_tf  )

						# Invalid local checkpoint
						with TemporaryDirectory() as local_invalid_ckpt:
										with self.assertRaises(__lowercase  ):
														snake_case__	:      List[str]     		=	FeaturesManager.determine_framework(__lowercase  )






		def 	__lowerCamelCase			(     self     :Optional[Any]  ):
						snake_case__	:      List[Any]     		=	MagicMock(return_value=__lowercase  )
						with patch('''transformers.onnx.features.is_tf_available'''   ,__lowercase  ):
										snake_case__	:      List[str]     		=	FeaturesManager.determine_framework(self.test_model  )
										self.assertEqual(__lowercase   ,self.framework_pt  )

						# PyTorch not in environment -> use TensorFlow
						snake_case__	:      Tuple     		=	MagicMock(return_value=__lowercase  )
						with patch('''transformers.onnx.features.is_torch_available'''   ,__lowercase  ):
										snake_case__	:      List[Any]     		=	FeaturesManager.determine_framework(self.test_model  )
										self.assertEqual(__lowercase   ,self.framework_tf  )

						# Both in environment -> use PyTorch
						snake_case__	:      Union[str, Any]     		=	MagicMock(return_value=__lowercase  )
						snake_case__	:      str     		=	MagicMock(return_value=__lowercase  )
						with patch('''transformers.onnx.features.is_tf_available'''   ,__lowercase  ), patch(
						    '''transformers.onnx.features.is_torch_available'''   ,__lowercase  ):
										snake_case__	:      Union[str, Any]     		=	FeaturesManager.determine_framework(self.test_model  )
										self.assertEqual(__lowercase   ,self.framework_pt  )

						# Both not in environment -> raise error
						snake_case__	:      Optional[Any]     		=	MagicMock(return_value=__lowercase  )
						snake_case__	:      List[str]     		=	MagicMock(return_value=__lowercase  )
						with patch('''transformers.onnx.features.is_tf_available'''   ,__lowercase  ), patch(
						    '''transformers.onnx.features.is_torch_available'''   ,__lowercase  ):
										with self.assertRaises(__lowercase  ):
														snake_case__	:      Optional[int]     		=	FeaturesManager.determine_framework(self.test_model  )






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A__								=						logging.get_logger(__name__)

A__								=						{
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}


class       a							(			__lowerCamelCase				):
		__lowerCAmelCase	:    str											=					"""distilbert"""
		__lowerCAmelCase	:    str											=					{
		    """hidden_size""": """dim""",
		    """num_attention_heads""": """n_heads""",
		    """num_hidden_layers""": """n_layers""",
		}



		def __init__(     self     :Dict   ,__lowercase     :Tuple=3_0_5_2_2   ,__lowercase     :Optional[Any]=5_1_2   ,__lowercase     :List[str]=False   ,__lowercase     :List[str]=6   ,__lowercase     :Optional[Any]=1_2   ,__lowercase     :Tuple=7_6_8   ,__lowercase     :int=4 * 7_6_8   ,__lowercase     :List[Any]=0.1   ,__lowercase     :List[str]=0.1   ,__lowercase     :Union[str, Any]="gelu"   ,__lowercase     :List[str]=0.02   ,__lowercase     :Optional[int]=0.1   ,__lowercase     :Dict=0.2   ,__lowercase     :Union[str, Any]=0   ,**__lowercase     :Optional[Any]   ,):
						snake_case__	:      List[str]     		=	vocab_size
						snake_case__	:      Tuple     		=	max_position_embeddings
						snake_case__	:      Optional[int]     		=	sinusoidal_pos_embds
						snake_case__	:      str     		=	n_layers
						snake_case__	:      List[Any]     		=	n_heads
						snake_case__	:      Tuple     		=	dim
						snake_case__	:      str     		=	hidden_dim
						snake_case__	:      int     		=	dropout
						snake_case__	:      Dict     		=	attention_dropout
						snake_case__	:      Tuple     		=	activation
						snake_case__	:      int     		=	initializer_range
						snake_case__	:      Optional[Any]     		=	qa_dropout
						snake_case__	:      Union[str, Any]     		=	seq_classif_dropout
						super().__init__(**__lowercase   ,pad_token_id=__lowercase  )


class       a							(			__lowerCamelCase				):



		@property
		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						if self.task == "multiple-choice":
										snake_case__	:      str     		=	{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
						else:
										snake_case__	:      Optional[int]     		=	{0: '''batch''', 1: '''sequence'''}
						return OrderedDict(
						    [
						        ('''input_ids''', dynamic_axis),
						        ('''attention_mask''', dynamic_axis),
						    ]  )

