





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 :    List[Any]							=     logging.get_logger(__name__)

_lowerCamelCase :    Dict							=     {
    '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 					UpperCamelCase_    (       UpperCamelCase__      ):
							lowerCamelCase_          =   "m2m_100"
							lowerCamelCase_          =   ["past_key_values"]
							lowerCamelCase_          =   {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


							def __init__(							self      :List[str]							,			__A      :Dict=12_8112							,			__A      :List[Any]=1024							,			__A      :Dict=12							,			__A      :str=4096							,			__A      :List[str]=16							,			__A      :str=12							,			__A      :int=4096							,			__A      :List[str]=16							,			__A      :Union[str, Any]=0.0_5							,			__A      :Optional[int]=0.0_5							,			__A      :List[str]=True							,			__A      :str=True							,			__A      :str="relu"							,			__A      :List[str]=1024							,			__A      :Optional[int]=0.1							,			__A      :Optional[Any]=0.1							,			__A      :Dict=0.0							,			__A      :Optional[Any]=0.0_2							,			__A      :int=2							,			__A      :Union[str, Any]=True							,			__A      :Any=1							,			__A      :Any=0							,			__A      :int=2							,			**__A      :Optional[Any]							,			)				->				List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= vocab_size
									SCREAMING_SNAKE_CASE__								= max_position_embeddings
									SCREAMING_SNAKE_CASE__								= d_model
									SCREAMING_SNAKE_CASE__								= encoder_ffn_dim
									SCREAMING_SNAKE_CASE__								= encoder_layers
									SCREAMING_SNAKE_CASE__								= encoder_attention_heads
									SCREAMING_SNAKE_CASE__								= decoder_ffn_dim
									SCREAMING_SNAKE_CASE__								= decoder_layers
									SCREAMING_SNAKE_CASE__								= decoder_attention_heads
									SCREAMING_SNAKE_CASE__								= dropout
									SCREAMING_SNAKE_CASE__								= attention_dropout
									SCREAMING_SNAKE_CASE__								= activation_dropout
									SCREAMING_SNAKE_CASE__								= activation_function
									SCREAMING_SNAKE_CASE__								= init_std
									SCREAMING_SNAKE_CASE__								= encoder_layerdrop
									SCREAMING_SNAKE_CASE__								= decoder_layerdrop
									SCREAMING_SNAKE_CASE__								= use_cache
									SCREAMING_SNAKE_CASE__								= encoder_layers
									SCREAMING_SNAKE_CASE__								= 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 					UpperCamelCase_    (       UpperCamelCase__      ):


							@property
							def        _snake_case					(							self      :Optional[Any]						)				->				Mapping[str, Mapping[int, str]]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= OrderedDict(
									    [
									        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
									        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
									    ]						)

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

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


							def        _snake_case					(							self      :Dict							,			__A      :PreTrainedTokenizer							,			__A      :int = -1							,			__A      :int = -1							,			__A      :bool = False							,			__A      :Optional[TensorType] = None							,			)				->				Mapping[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= 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
									SCREAMING_SNAKE_CASE__								= tokenizer.num_special_tokens_to_add(__A						)
									SCREAMING_SNAKE_CASE__								= 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
									SCREAMING_SNAKE_CASE__								= [""" """.join([tokenizer.unk_token]						) * seq_length] * batch_size
									SCREAMING_SNAKE_CASE__								= dict(tokenizer(__A							,			return_tensors=__A						)						)
									return common_inputs


							def        _snake_case					(							self      :int							,			__A      :PreTrainedTokenizer							,			__A      :int = -1							,			__A      :int = -1							,			__A      :bool = False							,			__A      :Optional[TensorType] = None							,			)				->				Mapping[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    __A							,			__A							,			__A							,			__A							,			__A						)

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

											SCREAMING_SNAKE_CASE__								= torch.cat(
											    [common_inputs["""decoder_attention_mask"""], torch.ones(__A							,			__A						)]							,			dim=1						)

											SCREAMING_SNAKE_CASE__								= []
											# If the number of encoder and decoder layers are present in the model configuration, both are considered
											SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								= self.num_layers
											SCREAMING_SNAKE_CASE__								= min(__A							,			__A						)
											SCREAMING_SNAKE_CASE__								= max(__A							,			__A						) - min_num_layers
											SCREAMING_SNAKE_CASE__								= """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.
											SCREAMING_SNAKE_CASE__								= 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


							lowerCamelCase_          =   _generate_dummy_inputs_for_default_and_seqaseq_lm






import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:




	class 					UpperCamelCase_    :


								@staticmethod
								def        _snake_case					(							*__A      :Optional[Any]							,			**__A      :Any						)				->				str:



										"""simple docstring"""



										pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class 					UpperCamelCase_    (       unittest.TestCase      ):
							lowerCamelCase_          =   MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							lowerCamelCase_          =   TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING


							def        _snake_case					(							self      :Union[str, Any]							,			__A      :str							,			__A      :Dict							,			__A      :List[str]						)				->				Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= ImageClassificationPipeline(model=__A							,			image_processor=__A							,			top_k=2						)
									SCREAMING_SNAKE_CASE__								= [
									    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						),
									    """http://images.cocodataset.org/val2017/000000039769.jpg""",
									]
									return image_classifier, examples


							def        _snake_case					(							self      :List[Any]							,			__A      :Any							,			__A      :List[str]						)				->				Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= image_classifier("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)

									self.assertEqual(
									    __A							,			[
									        {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        {"""score""": ANY(__A						), """label""": ANY(__A						)},
									    ]							,			)

									import datasets

									SCREAMING_SNAKE_CASE__								= datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""							,			"""image"""							,			split="""test"""						)

									# Accepts URL + PIL.Image + lists
									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						),
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        # RGBA
									        dataset[0]["""file"""],
									        # LA
									        dataset[1]["""file"""],
									        # L
									        dataset[2]["""file"""],
									    ]						)
									self.assertEqual(
									    __A							,			[
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									        [
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									            {"""score""": ANY(__A						), """label""": ANY(__A						)},
									        ],
									    ]							,			)


							@require_torch
							def        _snake_case					(							self      :str						)				->				Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= """hf-internal-testing/tiny-random-vit"""
									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model=__A						)

									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}]							,			)

									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									    ]							,			top_k=2							,			)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									    ]							,			)


							@require_tf
							def        _snake_case					(							self      :Any						)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= """hf-internal-testing/tiny-random-vit"""
									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model=__A							,			framework="""tf"""						)

									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}]							,			)

									SCREAMING_SNAKE_CASE__								= image_classifier(
									    [
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									        """http://images.cocodataset.org/val2017/000000039769.jpg""",
									    ]							,			top_k=2							,			)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									        [{"""label""": """LABEL_1""", """score""": 0.5_7_4}, {"""label""": """LABEL_0""", """score""": 0.4_2_6}],
									    ]							,			)


							def        _snake_case					(							self      :Dict						)				->				str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= PreTrainedTokenizer()

									# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
									SCREAMING_SNAKE_CASE__								= pipeline(
									    """image-classification"""							,			model="""hf-internal-testing/tiny-random-vit"""							,			tokenizer=__A						)

									self.assertIs(image_classifier.tokenizer							,			__A						)


							@slow
							@require_torch
							def        _snake_case					(							self      :Optional[int]						)				->				Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-conv"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.4_3_8_5, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.3_2_1, """label""": """tiger cat"""},
									        {"""score""": 0.0_5_0_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.0_1_3_7, """label""": """crib, cot"""},
									        {"""score""": 0.0_0_7, """label""": """radiator"""},
									    ]							,			)

									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-fourier"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.5_6_5_8, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.1_3_0_9, """label""": """tiger cat"""},
									        {"""score""": 0.0_7_2_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.0_7_0_7, """label""": """remote control, remote"""},
									        {"""score""": 0.0_0_8_2, """label""": """computer keyboard, keypad"""},
									    ]							,			)

									SCREAMING_SNAKE_CASE__								= pipeline("""image-classification"""							,			model="""deepmind/vision-perceiver-learned"""						)
									SCREAMING_SNAKE_CASE__								= image_classifier("""http://images.cocodataset.org/val2017/000000039769.jpg"""						)
									self.assertEqual(
									    nested_simplify(__A							,			decimals=4						)							,			[
									        {"""score""": 0.3_0_2_2, """label""": """tabby, tabby cat"""},
									        {"""score""": 0.2_3_6_2, """label""": """Egyptian cat"""},
									        {"""score""": 0.1_8_5_6, """label""": """tiger cat"""},
									        {"""score""": 0.0_3_2_4, """label""": """remote control, remote"""},
									        {"""score""": 0.0_0_9_6, """label""": """quilt, comforter, comfort, puff"""},
									    ]							,			)