




'''simple docstring'''



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


UpperCAmelCase_							:	Any	     =		logging.get_logger(__name__)

UpperCAmelCase_							:	int	     =		{
    '''Helsinki-NLP/opus-mt-en-de''': '''https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json''',
    # See all Marian models at https://huggingface.co/models?filter=marian
}





class      lowerCAmelCase (			__lowerCAmelCase):
       __lowercase	:   int										=      '''marian'''
       __lowercase	:   Any										=      ['''past_key_values''']
       __lowercase	:   List[Any]										=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}


       def __init__( self     ,	__SCREAMING_SNAKE_CASE=5_8101     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=1024     ,	__SCREAMING_SNAKE_CASE=12     ,	__SCREAMING_SNAKE_CASE=4096     ,	__SCREAMING_SNAKE_CASE=16     ,	__SCREAMING_SNAKE_CASE=12     ,	__SCREAMING_SNAKE_CASE=4096     ,	__SCREAMING_SNAKE_CASE=16     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE="gelu"     ,	__SCREAMING_SNAKE_CASE=1024     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.02     ,	__SCREAMING_SNAKE_CASE=5_8100     ,	__SCREAMING_SNAKE_CASE=False     ,	__SCREAMING_SNAKE_CASE=5_8100     ,	__SCREAMING_SNAKE_CASE=0     ,	__SCREAMING_SNAKE_CASE=0     ,	__SCREAMING_SNAKE_CASE=True     ,	**__SCREAMING_SNAKE_CASE     ,	)   ->	str:



            '''simple docstring'''




            __snake_case            =	vocab_size
            __snake_case            =	decoder_vocab_size or vocab_size
            __snake_case            =	max_position_embeddings
            __snake_case            =	d_model
            __snake_case            =	encoder_ffn_dim
            __snake_case            =	encoder_layers
            __snake_case            =	encoder_attention_heads
            __snake_case            =	decoder_ffn_dim
            __snake_case            =	decoder_layers
            __snake_case            =	decoder_attention_heads
            __snake_case            =	dropout
            __snake_case            =	attention_dropout
            __snake_case            =	activation_dropout
            __snake_case            =	activation_function
            __snake_case            =	init_std
            __snake_case            =	encoder_layerdrop
            __snake_case            =	decoder_layerdrop
            __snake_case            =	use_cache
            __snake_case            =	encoder_layers
            __snake_case            =	scale_embedding  # scale factor will be sqrt(d_model) if True
            __snake_case            =	share_encoder_decoder_embeddings
            super().__init__(
                pad_token_id=__SCREAMING_SNAKE_CASE     ,	eos_token_id=__SCREAMING_SNAKE_CASE     ,	is_encoder_decoder=__SCREAMING_SNAKE_CASE     ,	decoder_start_token_id=__SCREAMING_SNAKE_CASE     ,	forced_eos_token_id=__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE     ,	)





class      lowerCAmelCase (			__lowerCAmelCase):


       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def    lowerCAmelCase   ( self	)   ->	Mapping[str, Mapping[int, str]]:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case            =	OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ]	)

                 if self.use_past:
                      __snake_case            =	{0: '''batch'''}
                      __snake_case            =	{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
                 else:
                      __snake_case            =	{0: '''batch''', 1: '''decoder_sequence'''}
                      __snake_case            =	{0: '''batch''', 1: '''decoder_sequence'''}

                 if self.use_past:
                      self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE     ,	direction='''inputs'''	)
            elif self.task == "causal-lm":
                 # TODO: figure this case out.
                 __snake_case            =	OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ]	)
                 if self.use_past:
                      __snake_case       ,     __snake_case            =	self.num_layers
                      for i in range(__SCREAMING_SNAKE_CASE	):
                           __snake_case            =	{0: '''batch''', 2: '''past_sequence + sequence'''}
                           __snake_case            =	{0: '''batch''', 2: '''past_sequence + sequence'''}
            else:
                 __snake_case            =	OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
                         ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
                     ]	)

            return common_inputs


       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
       def    lowerCAmelCase   ( self	)   ->	Mapping[str, Mapping[int, str]]:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case            =	super().outputs
            else:
                 __snake_case            =	super(__SCREAMING_SNAKE_CASE     ,	self	).outputs
                 if self.use_past:
                      __snake_case       ,     __snake_case            =	self.num_layers
                      for i in range(__SCREAMING_SNAKE_CASE	):
                           __snake_case            =	{0: '''batch''', 2: '''past_sequence + sequence'''}
                           __snake_case            =	{0: '''batch''', 2: '''past_sequence + sequence'''}
            return common_outputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Mapping[str, Any]:



            '''simple docstring'''




            __snake_case            =	self._generate_dummy_inputs_for_encoder_and_decoder(
                __SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)

            # Generate decoder inputs
            __snake_case            =	seq_length if not self.use_past else 1
            __snake_case            =	self._generate_dummy_inputs_for_encoder_and_decoder(
                __SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            __snake_case            =	{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
            __snake_case            =	dict(**__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
                 else:
                      import torch
                 __snake_case       ,     __snake_case            =	common_inputs['''input_ids'''].shape
                 __snake_case            =	common_inputs['''decoder_input_ids'''].shape[1]
                 __snake_case       ,     __snake_case            =	self.num_attention_heads
                 __snake_case            =	(
                     batch,
                     num_encoder_attention_heads,
                     encoder_seq_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )
                 __snake_case            =	decoder_seq_length + 3
                 __snake_case            =	(
                     batch,
                     num_decoder_attention_heads,
                     decoder_past_length,
                     self._config.hidden_size // num_decoder_attention_heads,
                 )

                 __snake_case            =	torch.cat(
                     [common_inputs['''decoder_attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)]     ,	dim=1	)

                 __snake_case            =	[]
                 # If the number of encoder and decoder layers are present in the model configuration, both are considered
                 __snake_case       ,     __snake_case            =	self.num_layers
                 __snake_case            =	min(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	max(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	) - min_num_layers
                 __snake_case            =	'''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

                 for _ in range(__SCREAMING_SNAKE_CASE	):
                      common_inputs["past_key_values"].append(
                          (
                              torch.zeros(__SCREAMING_SNAKE_CASE	),
                              torch.zeros(__SCREAMING_SNAKE_CASE	),
                              torch.zeros(__SCREAMING_SNAKE_CASE	),
                              torch.zeros(__SCREAMING_SNAKE_CASE	),
                          )	)
                 # TODO: test this.
                 __snake_case            =	encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
                 for _ in range(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                      common_inputs["past_key_values"].append((torch.zeros(__SCREAMING_SNAKE_CASE	), torch.zeros(__SCREAMING_SNAKE_CASE	))	)
            return common_inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Mapping[str, Any]:



            '''simple docstring'''




            __snake_case            =	self._generate_dummy_inputs_for_encoder_and_decoder(
                __SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
                 else:
                      import torch
                 __snake_case       ,     __snake_case            =	common_inputs['''input_ids'''].shape
                 # Not using the same length for past_key_values
                 __snake_case            =	seqlen + 2
                 __snake_case       ,     __snake_case            =	self.num_layers
                 __snake_case       ,     __snake_case            =	self.num_attention_heads
                 __snake_case            =	(
                     batch,
                     num_encoder_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )

                 __snake_case            =	common_inputs['''attention_mask'''].dtype
                 __snake_case            =	torch.cat(
                     [common_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	dtype=__SCREAMING_SNAKE_CASE	)]     ,	dim=1	)
                 __snake_case            =	[
                     (torch.zeros(__SCREAMING_SNAKE_CASE	), torch.zeros(__SCREAMING_SNAKE_CASE	)) for _ in range(__SCREAMING_SNAKE_CASE	)
                 ]
            return common_inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Mapping[str, Any]:



            '''simple docstring'''




            __snake_case            =	compute_effective_axis_dimension(
                __SCREAMING_SNAKE_CASE     ,	fixed_dimension=OnnxConfig.default_fixed_batch     ,	num_token_to_add=0	)

            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            __snake_case            =	tokenizer.num_special_tokens_to_add(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	compute_effective_axis_dimension(
                __SCREAMING_SNAKE_CASE     ,	fixed_dimension=OnnxConfig.default_fixed_sequence     ,	num_token_to_add=__SCREAMING_SNAKE_CASE	)

            # Generate dummy inputs according to compute batch and sequence
            __snake_case            =	[''' '''.join([tokenizer.unk_token]	) * seq_length] * batch_size
            __snake_case            =	dict(tokenizer(__SCREAMING_SNAKE_CASE     ,	return_tensors=__SCREAMING_SNAKE_CASE	)	)
            return common_inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = -1     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Mapping[str, Any]:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case            =	self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                     __SCREAMING_SNAKE_CASE     ,	batch_size=__SCREAMING_SNAKE_CASE     ,	seq_length=__SCREAMING_SNAKE_CASE     ,	is_pair=__SCREAMING_SNAKE_CASE     ,	framework=__SCREAMING_SNAKE_CASE	)

            else:
                 __snake_case            =	self._generate_dummy_inputs_for_causal_lm(
                     __SCREAMING_SNAKE_CASE     ,	batch_size=__SCREAMING_SNAKE_CASE     ,	seq_length=__SCREAMING_SNAKE_CASE     ,	is_pair=__SCREAMING_SNAKE_CASE     ,	framework=__SCREAMING_SNAKE_CASE	)

            return common_inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case            =	super()._flatten_past_key_values_(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            else:
                 __snake_case            =	super(__SCREAMING_SNAKE_CASE     ,	self	)._flatten_past_key_values_(
                     __SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)




       @property
       def    lowerCAmelCase   ( self	)   ->	float:



            '''simple docstring'''




            return 1E-4












'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from packaging import version

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


UpperCAmelCase_							:	int	     =		logging.get_logger(__name__)

UpperCAmelCase_							:	Dict	     =		{
    '''microsoft/swin-tiny-patch4-window7-224''': (
        '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'''
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}





class      lowerCAmelCase (			__lowerCAmelCase					, __lowerCAmelCase):
       __lowercase	:   List[Any]										=      '''swin'''

       __lowercase	:   str										=      {
           '''num_attention_heads''': '''num_heads''',
           '''num_hidden_layers''': '''num_layers''',
       }


       def __init__( self     ,	__SCREAMING_SNAKE_CASE=224     ,	__SCREAMING_SNAKE_CASE=4     ,	__SCREAMING_SNAKE_CASE=3     ,	__SCREAMING_SNAKE_CASE=96     ,	__SCREAMING_SNAKE_CASE=[2, 2, 6, 2]     ,	__SCREAMING_SNAKE_CASE=[3, 6, 12, 24]     ,	__SCREAMING_SNAKE_CASE=7     ,	__SCREAMING_SNAKE_CASE=4.0     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE="gelu"     ,	__SCREAMING_SNAKE_CASE=False     ,	__SCREAMING_SNAKE_CASE=0.02     ,	__SCREAMING_SNAKE_CASE=1E-5     ,	__SCREAMING_SNAKE_CASE=32     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE     ,	)   ->	int:



            '''simple docstring'''




            super().__init__(**__SCREAMING_SNAKE_CASE	)

            __snake_case            =	image_size
            __snake_case            =	patch_size
            __snake_case            =	num_channels
            __snake_case            =	embed_dim
            __snake_case            =	depths
            __snake_case            =	len(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	num_heads
            __snake_case            =	window_size
            __snake_case            =	mlp_ratio
            __snake_case            =	qkv_bias
            __snake_case            =	hidden_dropout_prob
            __snake_case            =	attention_probs_dropout_prob
            __snake_case            =	drop_path_rate
            __snake_case            =	hidden_act
            __snake_case            =	use_absolute_embeddings
            __snake_case            =	layer_norm_eps
            __snake_case            =	initializer_range
            __snake_case            =	encoder_stride
            # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
            # this indicates the channel dimension after the last stage of the model
            __snake_case            =	int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE	) - 1)	)
            __snake_case            =	['''stem'''] + [F'''stage{idx}''' for idx in range(1     ,	len(__SCREAMING_SNAKE_CASE	) + 1	)]
            __snake_case       ,     __snake_case            =	get_aligned_output_features_output_indices(
                out_features=__SCREAMING_SNAKE_CASE     ,	out_indices=__SCREAMING_SNAKE_CASE     ,	stage_names=self.stage_names	)





class      lowerCAmelCase (			__lowerCAmelCase):
       __lowercase	:   Optional[int]										=      version.parse('''1.11''')


       @property
       def    lowerCAmelCase   ( self	)   ->	Mapping[str, Mapping[int, str]]:



            '''simple docstring'''




            return OrderedDict(
                [
                    ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                ]	)




       @property
       def    lowerCAmelCase   ( self	)   ->	float:



            '''simple docstring'''




            return 1E-4






