





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


lowerCAmelCase_    =				logging.get_logger(__name__)

lowerCAmelCase_    =				{
    '''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       (			_a     ):
			lowerCamelCase_     :     List[Any]       			=      '''marian'''
			lowerCamelCase_     :     Optional[int]       			=      ['''past_key_values''']
			lowerCamelCase_     :     Tuple       			=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






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


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  vocab_size
							snake_case_						:    str				=  decoder_vocab_size or vocab_size
							snake_case_						:    Union[str, Any]				=  max_position_embeddings
							snake_case_						:    str				=  d_model
							snake_case_						:    Tuple				=  encoder_ffn_dim
							snake_case_						:    List[Any]				=  encoder_layers
							snake_case_						:    List[Any]				=  encoder_attention_heads
							snake_case_						:    List[Any]				=  decoder_ffn_dim
							snake_case_						:    List[str]				=  decoder_layers
							snake_case_						:    List[Any]				=  decoder_attention_heads
							snake_case_						:    str				=  dropout
							snake_case_						:    int				=  attention_dropout
							snake_case_						:    Dict				=  activation_dropout
							snake_case_						:    Union[str, Any]				=  activation_function
							snake_case_						:    Union[str, Any]				=  init_std
							snake_case_						:    List[Any]				=  encoder_layerdrop
							snake_case_						:    Optional[Any]				=  decoder_layerdrop
							snake_case_						:    Optional[int]				=  use_cache
							snake_case_						:    Union[str, Any]				=  encoder_layers
							snake_case_						:    List[str]				=  scale_embedding  # scale factor will be sqrt(d_model) if True
							snake_case_						:    Union[str, Any]				=  share_encoder_decoder_embeddings
							super().__init__(
							    pad_token_id=__magic_name__					,							eos_token_id=__magic_name__					,							is_encoder_decoder=__magic_name__					,							decoder_start_token_id=__magic_name__					,							forced_eos_token_id=__magic_name__					,							**__magic_name__					,							)




class 					__lowerCAmelCase       (			_a     ):






			@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_						:    str				=  OrderedDict(
											    [
											        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
											    ]    )

											if self.use_past:
															snake_case_						:    Optional[Any]				=  {0: '''batch'''}
															snake_case_						:    str				=  {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
											else:
															snake_case_						:    Optional[Any]				=  {0: '''batch''', 1: '''decoder_sequence'''}
															snake_case_						:    Optional[int]				=  {0: '''batch''', 1: '''decoder_sequence'''}

											if self.use_past:
															self.fill_with_past_key_values_(__magic_name__					,							direction='''inputs'''    )
							elif self.task == "causal-lm":
											# TODO: figure this case out.
											snake_case_						:    Any				=  OrderedDict(
											    [
											        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
											        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
											    ]    )
											if self.use_past:
															snake_case_			,							snake_case_						:    Any				=  self.num_layers
															for i in range(__magic_name__    ):
																			snake_case_						:    Dict				=  {0: '''batch''', 2: '''past_sequence + sequence'''}
																			snake_case_						:    Any				=  {0: '''batch''', 2: '''past_sequence + sequence'''}
							else:
											snake_case_						:    Optional[Any]				=  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_						:    int				=  super().outputs
							else:
											snake_case_						:    Tuple				=  super(__magic_name__					,							self    ).outputs
											if self.use_past:
															snake_case_			,							snake_case_						:    Dict				=  self.num_layers
															for i in range(__magic_name__    ):
																			snake_case_						:    Optional[int]				=  {0: '''batch''', 2: '''past_sequence + sequence'''}
																			snake_case_						:    int				=  {0: '''batch''', 2: '''past_sequence + sequence'''}
							return common_outputs






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


							'''simple docstring'''



							snake_case_						:    Tuple				=  self._generate_dummy_inputs_for_encoder_and_decoder(
							    __magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )

							# Generate decoder inputs
							snake_case_						:    Optional[Any]				=  seq_length if not self.use_past else 1
							snake_case_						:    Optional[Any]				=  self._generate_dummy_inputs_for_encoder_and_decoder(
							    __magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )
							snake_case_						:    List[str]				=  {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
							snake_case_						:    Optional[Any]				=  dict(**__magic_name__					,							**__magic_name__    )

							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_						:    Optional[int]				=  common_inputs['''input_ids'''].shape
											snake_case_						:    List[Any]				=  common_inputs['''decoder_input_ids'''].shape[1]
											snake_case_			,							snake_case_						:    List[str]				=  self.num_attention_heads
											snake_case_						:    Any				=  (
											    batch,
											    num_encoder_attention_heads,
											    encoder_seq_length,
											    self._config.hidden_size // num_encoder_attention_heads,
											)
											snake_case_						:    List[Any]				=  decoder_seq_length + 3
											snake_case_						:    Dict				=  (
											    batch,
											    num_decoder_attention_heads,
											    decoder_past_length,
											    self._config.hidden_size // num_decoder_attention_heads,
											)

											snake_case_						:    Dict				=  torch.cat(
											    [common_inputs['''decoder_attention_mask'''], torch.ones(__magic_name__					,							__magic_name__    )]					,							dim=1    )

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

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






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


							'''simple docstring'''



							snake_case_						:    int				=  self._generate_dummy_inputs_for_encoder_and_decoder(
							    __magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )

							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_						:    Any				=  common_inputs['''input_ids'''].shape
											# Not using the same length for past_key_values
											snake_case_						:    int				=  seqlen + 2
											snake_case_			,							snake_case_						:    Optional[int]				=  self.num_layers
											snake_case_			,							snake_case_						:    Union[str, Any]				=  self.num_attention_heads
											snake_case_						:    Tuple				=  (
											    batch,
											    num_encoder_attention_heads,
											    past_key_values_length,
											    self._config.hidden_size // num_encoder_attention_heads,
											)

											snake_case_						:    int				=  common_inputs['''attention_mask'''].dtype
											snake_case_						:    Optional[int]				=  torch.cat(
											    [common_inputs['''attention_mask'''], torch.ones(__magic_name__					,							__magic_name__					,							dtype=__magic_name__    )]					,							dim=1    )
											snake_case_						:    List[str]				=  [
											    (torch.zeros(__magic_name__    ), torch.zeros(__magic_name__    )) for _ in range(__magic_name__    )
											]
							return common_inputs






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


							'''simple docstring'''



							snake_case_						:    int				=  compute_effective_axis_dimension(
							    __magic_name__					,							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_						:    str				=  tokenizer.num_special_tokens_to_add(__magic_name__    )
							snake_case_						:    Optional[int]				=  compute_effective_axis_dimension(
							    __magic_name__					,							fixed_dimension=OnnxConfig.default_fixed_sequence					,							num_token_to_add=__magic_name__    )

							# Generate dummy inputs according to compute batch and sequence
							snake_case_						:    Tuple				=  [''' '''.join([tokenizer.unk_token]    ) * seq_length] * batch_size
							snake_case_						:    Optional[Any]				=  dict(tokenizer(__magic_name__					,							return_tensors=__magic_name__    )    )
							return common_inputs






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


							'''simple docstring'''



							if self.task in ["default", "seq2seq-lm"]:
											snake_case_						:    Optional[Any]				=  self._generate_dummy_inputs_for_default_and_seqaseq_lm(
											    __magic_name__					,							batch_size=__magic_name__					,							seq_length=__magic_name__					,							is_pair=__magic_name__					,							framework=__magic_name__    )

							else:
											snake_case_						:    Optional[Any]				=  self._generate_dummy_inputs_for_causal_lm(
											    __magic_name__					,							batch_size=__magic_name__					,							seq_length=__magic_name__					,							is_pair=__magic_name__					,							framework=__magic_name__    )

							return common_inputs






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Optional[Any]:


							'''simple docstring'''



							if self.task in ["default", "seq2seq-lm"]:
											snake_case_						:    Dict				=  super()._flatten_past_key_values_(__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )
							else:
											snake_case_						:    Tuple				=  super(__magic_name__					,							self    )._flatten_past_key_values_(
											    __magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )






			@property
			def 			lowerCamelCase  (self    )     ->   float:


							'''simple docstring'''



							return 1e-4







import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY



@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 					__lowerCAmelCase       (			unittest.TestCase     ):
			lowerCamelCase_     :     Any       			=      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Any				=  hf_hub_download(
							    repo_id='''nateraw/video-demo'''					,							filename='''archery.mp4'''					,							repo_type='''dataset'''    )
							snake_case_						:    List[Any]				=  VideoClassificationPipeline(model=__magic_name__					,							image_processor=__magic_name__					,							top_k=2    )
							snake_case_						:    str				=  [
							    example_video_filepath,
							    '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
							]
							return video_classifier, examples






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__    )     ->   Any:


							'''simple docstring'''



							for example in examples:
											snake_case_						:    Union[str, Any]				=  video_classifier(__magic_name__    )

											self.assertEqual(
											    __magic_name__					,							[
											        {'''score''': ANY(__magic_name__    ), '''label''': ANY(__magic_name__    )},
											        {'''score''': ANY(__magic_name__    ), '''label''': ANY(__magic_name__    )},
											    ]					,							)






			@require_torch
			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  '''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
							snake_case_						:    str				=  VideoMAEFeatureExtractor(
							    size={'''shortest_edge''': 10}					,							crop_size={'''height''': 10, '''width''': 10}    )
							snake_case_						:    int				=  pipeline(
							    '''video-classification'''					,							model=__magic_name__					,							feature_extractor=__magic_name__					,							frame_sampling_rate=4    )

							snake_case_						:    List[str]				=  hf_hub_download(repo_id='''nateraw/video-demo'''					,							filename='''archery.mp4'''					,							repo_type='''dataset'''    )
							snake_case_						:    Union[str, Any]				=  video_classifier(__magic_name__					,							top_k=2    )
							self.assertEqual(
							    nested_simplify(__magic_name__					,							decimals=4    )					,							[{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}]					,							)

							snake_case_						:    int				=  video_classifier(
							    [
							        video_file_path,
							        video_file_path,
							    ]					,							top_k=2					,							)
							self.assertEqual(
							    nested_simplify(__magic_name__					,							decimals=4    )					,							[
							        [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
							        [{'''score''': 0.5_199, '''label''': '''LABEL_0'''}, {'''score''': 0.4_801, '''label''': '''LABEL_1'''}],
							    ]					,							)






			@require_tf
			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							pass
