


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

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


a					:				Optional[Any]       =      logging.get_logger(__name__)

a					:				List[str]       =      {
    'facebook/m2m100_418M': 'https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}

class   lowerCamelCase						(	SCREAMING_SNAKE_CASE  ):
       _UpperCamelCase     		=   """m2m_100"""
       _UpperCamelCase     		=   ["""past_key_values"""]
       _UpperCamelCase     		=   {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

       def __init__(      self						,    a__=12_81_12						,    a__=10_24						,    a__=12						,    a__=40_96						,    a__=16						,    a__=12						,    a__=40_96						,    a__=16						,    a__=0.05						,    a__=0.05						,    a__=True						,    a__=True						,    a__="relu"						,    a__=10_24						,    a__=0.1						,    a__=0.1						,    a__=0.0						,    a__=0.02						,    a__=2						,    a__=True						,    a__=1						,    a__=0						,    a__=2						,    **a__						,    ):






        '''simple docstring'''






        snake_case :			List[str]							=   vocab_size
        snake_case :			Dict							=   max_position_embeddings
        snake_case :			int							=   d_model
        snake_case :			Union[str, Any]							=   encoder_ffn_dim
        snake_case :			Dict							=   encoder_layers
        snake_case :			int							=   encoder_attention_heads
        snake_case :			List[Any]							=   decoder_ffn_dim
        snake_case :			List[Any]							=   decoder_layers
        snake_case :			Optional[int]							=   decoder_attention_heads
        snake_case :			Tuple							=   dropout
        snake_case :			str							=   attention_dropout
        snake_case :			Union[str, Any]							=   activation_dropout
        snake_case :			Union[str, Any]							=   activation_function
        snake_case :			List[str]							=   init_std
        snake_case :			Dict							=   encoder_layerdrop
        snake_case :			Any							=   decoder_layerdrop
        snake_case :			int							=   use_cache
        snake_case :			str							=   encoder_layers
        snake_case :			Any							=   scale_embedding  # scale factor will be sqrt(d_model) if True

        super().__init__(
            pad_token_id=a__						,    bos_token_id=a__						,    eos_token_id=a__						,    is_encoder_decoder=a__						,    decoder_start_token_id=a__						,    **a__						,    )







class   lowerCamelCase						(	SCREAMING_SNAKE_CASE  ):

       @property
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   OrderedDict(
            [
                ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
            ]       )

        if self.use_past:
         snake_case :			Dict							=   {0: '''batch'''}
         snake_case :			List[str]							=   {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
        else:
         snake_case :			str							=   {0: '''batch''', 1: '''decoder_sequence'''}
         snake_case :			List[str]							=   {0: '''batch''', 1: '''decoder_sequence'''}

        if self.use_past:
         self.fill_with_past_key_values_(a__						,    direction='''inputs'''       )
        return common_inputs

       def 					UpperCAmelCase					(      self						,    a__						,    a__ = -1						,    a__ = -1						,    a__ = False						,    a__ = None						,    ):






        '''simple docstring'''






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

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

       def 					UpperCAmelCase					(      self						,    a__						,    a__ = -1						,    a__ = -1						,    a__ = False						,    a__ = None						,    ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            a__						,    a__						,    a__						,    a__						,    a__       )

        # Generate decoder inputs
        snake_case :			int							=   seq_length if not self.use_past else 1
        snake_case :			List[Any]							=   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            a__						,    a__						,    a__						,    a__						,    a__       )
        snake_case :			Union[str, Any]							=   {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
        snake_case :			Optional[int]							=   dict(**a__						,    **a__       )

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

         snake_case :			int							=   torch.cat(
             [common_inputs['''decoder_attention_mask'''], torch.ones(a__						,    a__       )]						,    dim=1       )

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

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



       _UpperCamelCase     		=   _generate_dummy_inputs_for_default_and_seqaseq_lm










import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=10			,  __lowerCamelCase=10			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Tuple:


 '''simple docstring'''
 snake_case :			Dict							=   []
 for i in range(image_processor_tester.num_frames							):
  video.append(np.random.randint(255			,  size=(image_processor_tester.num_channels, width, height)			,  dtype=np.uinta							)							)

 if not numpify and not torchify:
  # PIL expects the channel dimension as last dimension
  snake_case :			Optional[int]							=   [Image.fromarray(np.moveaxis(__lowerCamelCase			,  0			,  -1							)							) for frame in video]

 if torchify:
  snake_case :			int							=   [torch.from_numpy(__lowerCamelCase							) for frame in video]

 return video






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=False			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Optional[Any]:


 '''simple docstring'''
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

 snake_case :			Any							=   []
 for i in range(image_processor_tester.batch_size							):
  if equal_resolution:
   snake_case :			Optional[int]							=   image_processor_tester.max_resolution
  else:
   snake_case	,       snake_case :			str							=   np.random.choice(
       np.arange(image_processor_tester.min_resolution			,  image_processor_tester.max_resolution							)			,  2							)
   snake_case :			Any							=   prepare_video(
       image_processor_tester=__lowerCamelCase			,  width=__lowerCamelCase			,  height=__lowerCamelCase			,  numpify=__lowerCamelCase			,  torchify=__lowerCamelCase			,  )
  video_inputs.append(__lowerCamelCase							)

 return video_inputs

class   lowerCamelCase						(	unittest.TestCase  ):

       def __init__(      self						,    a__						,    a__=7						,    a__=3						,    a__=4						,    a__=18						,    a__=30						,    a__=4_00						,    a__=True						,    a__=None						,    a__=True						,    a__=[0.5, 0.5, 0.5]						,    a__=[0.5, 0.5, 0.5]						,    a__=True						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			Tuple							=   size if size is not None else {'''shortest_edge''': 18}
        snake_case :			List[str]							=   crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

        snake_case :			Optional[Any]							=   parent
        snake_case :			List[Any]							=   batch_size
        snake_case :			Optional[int]							=   num_channels
        snake_case :			Union[str, Any]							=   num_frames
        snake_case :			List[Any]							=   image_size
        snake_case :			Tuple							=   min_resolution
        snake_case :			List[Any]							=   max_resolution
        snake_case :			Tuple							=   do_resize
        snake_case :			List[Any]							=   size
        snake_case :			List[Any]							=   do_normalize
        snake_case :			List[Any]							=   image_mean
        snake_case :			List[str]							=   image_std
        snake_case :			Tuple							=   do_center_crop
        snake_case :			str							=   crop_size



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
        }







@require_torch
@require_vision
class   lowerCamelCase						(	SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _UpperCamelCase     		=   TvltImageProcessor if is_vision_available() else None

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   TvltImageProcessorTester(self       )

       @property
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        return self.image_processor_tester.prepare_image_processor_dict()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.image_processing_class(**self.image_processor_dict       )
        self.assertTrue(hasattr(a__						,    '''image_mean'''       )       )
        self.assertTrue(hasattr(a__						,    '''image_std'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_normalize'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_resize'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_center_crop'''       )       )
        self.assertTrue(hasattr(a__						,    '''size'''       )       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.image_processing_class(**self.image_processor_dict       )
        # create random PIL videos
        snake_case :			List[str]							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    Image.Image       )

        # Test not batched input
        snake_case :			int							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Optional[Any]							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.image_processing_class(**self.image_processor_dict       )
        # create random numpy tensors
        snake_case :			Union[str, Any]							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__						,    numpify=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    np.ndarray       )

        # Test not batched input
        snake_case :			List[str]							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Tuple							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			str							=   self.image_processing_class(**self.image_processor_dict       )
        # create random PyTorch tensors
        snake_case :			str							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__						,    torchify=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    torch.Tensor       )

        # Test not batched input
        snake_case :			Tuple							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Optional[int]							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )






