




'''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


lowerCAmelCase_			:     Dict							    =     logging.get_logger(__name__)

lowerCAmelCase_			:     Union[str, Any]							    =     {
    '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     SCREAMING_SNAKE_CASE						(	snake_case_							):
      __magic_name__     :					Optional[int]             =				'''m2m_100'''
      __magic_name__     :					List[Any]             =				['''past_key_values''']
      __magic_name__     :					Optional[int]             =				{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






      def __init__(   self      :     Optional[int]	,	lowercase__      :     Optional[Any]=12_8112	,	lowercase__      :     List[str]=1024	,	lowercase__      :     Tuple=12	,	lowercase__      :     Optional[Any]=4096	,	lowercase__      :     Union[str, Any]=16	,	lowercase__      :     List[Any]=12	,	lowercase__      :     List[str]=4096	,	lowercase__      :     Optional[int]=16	,	lowercase__      :     List[Any]=0.05	,	lowercase__      :     str=0.05	,	lowercase__      :     str=True	,	lowercase__      :     Tuple=True	,	lowercase__      :     Optional[int]="relu"	,	lowercase__      :     Optional[Any]=1024	,	lowercase__      :     Union[str, Any]=0.1	,	lowercase__      :     str=0.1	,	lowercase__      :     Any=0.0	,	lowercase__      :     Optional[Any]=0.02	,	lowercase__      :     str=2	,	lowercase__      :     List[str]=True	,	lowercase__      :     List[Any]=1	,	lowercase__      :     List[str]=0	,	lowercase__      :     int=2	,	**lowercase__      :     Optional[int]	,	):


           '''simple docstring'''

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

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







class     SCREAMING_SNAKE_CASE						(	snake_case_							):






      @property
      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           a_    :       Union[str, Any]    	=     OrderedDict(
               [
                   ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                   ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
               ]					)

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

           if self.use_past:
                self.fill_with_past_key_values_(lowercase__	,	direction="""inputs"""					)
           return common_inputs






      def        lowercase_	(   self      :     Optional[Any]	,	lowercase__      :     PreTrainedTokenizer	,	lowercase__      :     int = -1	,	lowercase__      :     int = -1	,	lowercase__      :     bool = False	,	lowercase__      :     Optional[TensorType] = None	,	):


           '''simple docstring'''

           a_    :       Optional[int]    	=     compute_effective_axis_dimension(
               lowercase__	,	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(lowercase__					)
           a_    :       List[str]    	=     compute_effective_axis_dimension(
               lowercase__	,	fixed_dimension=OnnxConfig.default_fixed_sequence	,	num_token_to_add=lowercase__					)

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






      def        lowercase_	(   self      :     Optional[Any]	,	lowercase__      :     PreTrainedTokenizer	,	lowercase__      :     int = -1	,	lowercase__      :     int = -1	,	lowercase__      :     bool = False	,	lowercase__      :     Optional[TensorType] = None	,	):


           '''simple docstring'''

           a_    :       Dict    	=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               lowercase__	,	lowercase__	,	lowercase__	,	lowercase__	,	lowercase__					)

           # Generate decoder inputs
           a_    :       Any    	=     seq_length if not self.use_past else 1
           a_    :       str    	=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               lowercase__	,	lowercase__	,	lowercase__	,	lowercase__	,	lowercase__					)
           a_    :       Dict    	=     {F"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
           a_    :       int    	=     dict(**lowercase__	,	**lowercase__					)

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

                a_    :       Tuple    	=     torch.cat(
                    [common_inputs["""decoder_attention_mask"""], torch.ones(lowercase__	,	lowercase__					)]	,	dim=1					)

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

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





      __magic_name__     :					List[str]             =				_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 CLIPImageProcessor







class     SCREAMING_SNAKE_CASE						(	unittest.TestCase							):






      def __init__(   self      :     Tuple	,	lowercase__      :     Union[str, Any]	,	lowercase__      :     Optional[Any]=7	,	lowercase__      :     str=3	,	lowercase__      :     Tuple=18	,	lowercase__      :     Dict=30	,	lowercase__      :     List[Any]=400	,	lowercase__      :     Optional[Any]=True	,	lowercase__      :     Union[str, Any]=None	,	lowercase__      :     Union[str, Any]=True	,	lowercase__      :     Tuple=None	,	lowercase__      :     str=True	,	lowercase__      :     List[Any]=[0.48145466, 0.4578275, 0.40821073]	,	lowercase__      :     Optional[Any]=[0.26862954, 0.26130258, 0.27577711]	,	lowercase__      :     str=True	,	):


           '''simple docstring'''

           a_    :       Dict    	=     size if size is not None else {"""shortest_edge""": 20}
           a_    :       Any    	=     crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
           a_    :       List[str]    	=     parent
           a_    :       Dict    	=     batch_size
           a_    :       Any    	=     num_channels
           a_    :       Optional[int]    	=     image_size
           a_    :       Union[str, Any]    	=     min_resolution
           a_    :       Tuple    	=     max_resolution
           a_    :       int    	=     do_resize
           a_    :       List[str]    	=     size
           a_    :       Optional[int]    	=     do_center_crop
           a_    :       str    	=     crop_size
           a_    :       str    	=     do_normalize
           a_    :       Any    	=     image_mean
           a_    :       List[str]    	=     image_std
           a_    :       Any    	=     do_convert_rgb






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

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






      def        lowercase_	(   self      :     str	,	lowercase__      :     Union[str, Any]=False	,	lowercase__      :     Union[str, Any]=False	,	lowercase__      :     Union[str, Any]=False					):


           '''simple docstring'''

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

           if equal_resolution:
                a_    :       Union[str, Any]    	=     []
                for i in range(self.batch_size					):
                     image_inputs.append(
                         np.random.randint(
                             255	,	size=(self.num_channels, self.max_resolution, self.max_resolution)	,	dtype=np.uinta					)					)
           else:
                a_    :       int    	=     []
                for i in range(self.batch_size					):
                     a_	,    a_    :       Any    	=     np.random.choice(np.arange(self.min_resolution	,	self.max_resolution					)	,	2					)
                     image_inputs.append(np.random.randint(255	,	size=(self.num_channels, width, height)	,	dtype=np.uinta					)					)

           if not numpify and not torchify:
                # PIL expects the channel dimension as last dimension
                a_    :       Union[str, Any]    	=     [Image.fromarray(np.moveaxis(lowercase__	,	0	,	-1					)					) for x in image_inputs]

           if torchify:
                a_    :       List[Any]    	=     [torch.from_numpy(lowercase__					) for x in image_inputs]

           return image_inputs







@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					int             =				CLIPImageProcessor if is_vision_available() else None






      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           a_    :       Dict    	=     CLIPImageProcessingTester(self					)






      @property
      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       List[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           self.assertTrue(hasattr(lowercase__	,	"""do_resize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""size"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_normalize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_mean"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_std"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_convert_rgb"""					)					)






      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.image_processing_class.from_dict(self.image_processor_dict					)
           self.assertEqual(image_processor.size	,	{"""shortest_edge""": 20}					)
           self.assertEqual(image_processor.crop_size	,	{"""height""": 18, """width""": 18}					)

           a_    :       Optional[int]    	=     self.image_processing_class.from_dict(self.image_processor_dict	,	size=42	,	crop_size=84					)
           self.assertEqual(image_processor.size	,	{"""shortest_edge""": 42}					)
           self.assertEqual(image_processor.crop_size	,	{"""height""": 84, """width""": 84}					)






      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       str    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PIL images
           a_    :       str    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	Image.Image					)

           # Test not batched input
           a_    :       Optional[Any]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       str    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random numpy tensors
           a_    :       Optional[int]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__	,	numpify=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	np.ndarray					)

           # Test not batched input
           a_    :       Tuple    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       str    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)






      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           a_    :       Dict    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PyTorch tensors
           a_    :       Optional[int]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__	,	torchify=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	torch.Tensor					)

           # Test not batched input
           a_    :       Optional[int]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       int    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)







@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					List[str]             =				CLIPImageProcessor if is_vision_available() else None






      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           a_    :       str    	=     CLIPImageProcessingTester(self	,	num_channels=4					)
           a_    :       Dict    	=     3






      @property
      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()






      def        lowercase_	(   self      :     List[Any]					):


           '''simple docstring'''

           a_    :       int    	=     self.image_processing_class(**self.image_processor_dict					)
           self.assertTrue(hasattr(lowercase__	,	"""do_resize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""size"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""center_crop"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_normalize"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_mean"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""image_std"""					)					)
           self.assertTrue(hasattr(lowercase__	,	"""do_convert_rgb"""					)					)






      def        lowercase_	(   self      :     int					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           a_    :       List[Any]    	=     self.image_processing_class(**self.image_processor_dict					)
           # create random PIL images
           a_    :       Union[str, Any]    	=     self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__					)
           for image in image_inputs:
                self.assertIsInstance(lowercase__	,	Image.Image					)

           # Test not batched input
           a_    :       Optional[Any]    	=     image_processing(image_inputs[0]	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   1,
                   self.expected_encoded_image_num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)

           # Test batched
           a_    :       List[str]    	=     image_processing(lowercase__	,	return_tensors="""pt"""					).pixel_values
           self.assertEqual(
               encoded_images.shape	,	(
                   self.image_processor_tester.batch_size,
                   self.expected_encoded_image_num_channels,
                   self.image_processor_tester.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )	,	)
