






'''simple docstring'''


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


SCREAMING_SNAKE_CASE_				      =							logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_				      =							{
    "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					(							lowerCamelCase__   ):







   """simple docstring"""







   _A						=   'm2m_100'
   _A						=   ['past_key_values']
   _A						=   {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}



   def __init__( self   ,   _A=128112   ,   _A=1024   ,   _A=12   ,   _A=4096   ,   _A=16   ,   _A=12   ,   _A=4096   ,   _A=16   ,   _A=0.05   ,   _A=0.05   ,   _A=True   ,   _A=True   ,   _A="relu"   ,   _A=1024   ,   _A=0.1   ,   _A=0.1   ,   _A=0.0   ,   _A=0.02   ,   _A=2   ,   _A=True   ,   _A=1   ,   _A=0   ,   _A=2   ,   **_A   ,   )    ->   int:
    __a    :	Tuple									=						vocab_size
    __a    :	str									=						max_position_embeddings
    __a    :	int									=						d_model
    __a    :	Optional[Any]									=						encoder_ffn_dim
    __a    :	Tuple									=						encoder_layers
    __a    :	Dict									=						encoder_attention_heads
    __a    :	int									=						decoder_ffn_dim
    __a    :	List[Any]									=						decoder_layers
    __a    :	int									=						decoder_attention_heads
    __a    :	Optional[Any]									=						dropout
    __a    :	Dict									=						attention_dropout
    __a    :	Optional[Any]									=						activation_dropout
    __a    :	Union[str, Any]									=						activation_function
    __a    :	Dict									=						init_std
    __a    :	int									=						encoder_layerdrop
    __a    :	str									=						decoder_layerdrop
    __a    :	str									=						use_cache
    __a    :	Optional[Any]									=						encoder_layers
    __a    :	Optional[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					(							lowerCamelCase__   ):







   """simple docstring"""







   @property
   def    __magic_name__    ( self			)    ->   Mapping[str, Mapping[int, str]]:
    __a    :	Dict									=						OrderedDict(
        [
            ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
            ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
        ]			)

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

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



   def    __magic_name__    ( self   ,   _A   ,   _A = -1   ,   _A = -1   ,   _A = False   ,   _A = None   ,   )    ->   Mapping[str, Any]:
    # Copied from OnnxConfig.generate_dummy_inputs
    # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
    # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
    __a    :	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
    __a    :	Any									=						tokenizer.num_special_tokens_to_add(_A			)
    __a    :	Any									=						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
    __a    :	str									=						[' '.join([tokenizer.unk_token]			) * seq_length] * batch_size
    __a    :	Dict									=						dict(tokenizer(_A   ,   return_tensors=_A			)			)
    return common_inputs



   def    __magic_name__    ( self   ,   _A   ,   _A = -1   ,   _A = -1   ,   _A = False   ,   _A = None   ,   )    ->   Mapping[str, Any]:
    __a    :	Tuple									=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        _A   ,   _A   ,   _A   ,   _A   ,   _A			)

    # Generate decoder inputs
    __a    :	Optional[int]									=						seq_length if not self.use_past else 1
    __a    :	Optional[Any]									=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        _A   ,   _A   ,   _A   ,   _A   ,   _A			)
    __a    :	Any									=						{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
    __a    :	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
     __a			,    __a    :	Optional[int]									=						common_inputs['input_ids'].shape
     __a    :	List[Any]									=						common_inputs['decoder_input_ids'].shape[1]
     __a			,    __a    :	Tuple									=						self.num_attention_heads
     __a    :	int									=						(
         batch,
         num_encoder_attention_heads,
         encoder_seq_length,
         self._config.hidden_size // num_encoder_attention_heads,
     )
     __a    :	Any									=						decoder_seq_length + 3
     __a    :	Tuple									=						(
         batch,
         num_decoder_attention_heads,
         decoder_past_length,
         self._config.hidden_size // num_decoder_attention_heads,
     )

     __a    :	Optional[int]									=						torch.cat(
         [common_inputs['decoder_attention_mask'], torch.ones(_A   ,   _A			)]   ,   dim=1			)

     __a    :	Optional[int]									=						[]
     # If the number of encoder and decoder layers are present in the model configuration, both are considered
     __a			,    __a    :	Optional[Any]									=						self.num_layers
     __a    :	Optional[int]									=						min(_A   ,   _A			)
     __a    :	Optional[Any]									=						max(_A   ,   _A			) - min_num_layers
     __a    :	Optional[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.
     __a    :	Dict									=						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

   _A						=   _generate_dummy_inputs_for_default_and_seqaseq_lm












'''simple docstring'''


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   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__       ,		SCREAMING_SNAKE_CASE__=10       ,		SCREAMING_SNAKE_CASE__=10       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False		):
 __a    :	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
  __a    :	List[Any]									=						[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__       ,		0       ,		-1		)		) for frame in video]

 if torchify:
  __a    :	List[Any]									=						[torch.from_numpy(SCREAMING_SNAKE_CASE__		) for frame in video]

 return video






def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False       ,		SCREAMING_SNAKE_CASE__=False		):
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

 return video_inputs




class 							lowerCAmelCase					(							unittest.TestCase   ):







   """simple docstring"""







   def __init__( self   ,   _A   ,   _A=7   ,   _A=3   ,   _A=4   ,   _A=18   ,   _A=30   ,   _A=400   ,   _A=True   ,   _A=None   ,   _A=True   ,   _A=[0.5, 0.5, 0.5]   ,   _A=[0.5, 0.5, 0.5]   ,   _A=True   ,   _A=None   ,   )    ->   str:
    __a    :	Any									=						size if size is not None else {'shortest_edge': 18}
    __a    :	str									=						crop_size if crop_size is not None else {'height': 18, 'width': 18}

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



   def    __magic_name__    ( self			)    ->   Any:
    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					(							lowerCamelCase__							,      unittest.TestCase   ):







   """simple docstring"""







   _A						=   TvltImageProcessor if is_vision_available() else None



   def    __magic_name__    ( self			)    ->   int:
    __a    :	List[Any]									=						TvltImageProcessorTester(self			)



   @property
   def    __magic_name__    ( self			)    ->   Optional[Any]:
    return self.image_processor_tester.prepare_image_processor_dict()



   def    __magic_name__    ( self			)    ->   Optional[int]:
    __a    :	Any									=						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    __magic_name__    ( self			)    ->   str:
    # Initialize image_processor
    __a    :	str									=						self.image_processing_class(**self.image_processor_dict			)
    # create random PIL videos
    __a    :	Optional[Any]									=						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
    __a    :	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
    __a    :	Dict									=						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    __magic_name__    ( self			)    ->   Any:
    # Initialize image_processor
    __a    :	Union[str, Any]									=						self.image_processing_class(**self.image_processor_dict			)
    # create random numpy tensors
    __a    :	List[str]									=						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
    __a    :	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
    __a    :	Dict									=						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    __magic_name__    ( self			)    ->   Union[str, Any]:
    # Initialize image_processor
    __a    :	Optional[Any]									=						self.image_processing_class(**self.image_processor_dict			)
    # create random PyTorch tensors
    __a    :	Optional[Any]									=						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
    __a    :	Dict									=						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
    __a    :	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'],
        )   ,   )




