





'''simple docstring'''






from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A :			List[Any]							   =      logging.get_logger(__name__)

A :			str							   =      {
    'microsoft/prophetnet-large-uncased': (
        'https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'
    ),
}







class       lowerCamelCase       (    __UpperCAmelCase	):
					_SCREAMING_SNAKE_CASE				 =     "prophetnet"
					_SCREAMING_SNAKE_CASE				 =     ["past_key_values"]
					_SCREAMING_SNAKE_CASE				 =     {
					    "num_attention_heads": "num_encoder_attention_heads",
					}


					def __init__(       self						:	int							,  __snake_case						:	Optional[float] = 0.1							,  __snake_case						:	Optional[Union[str, Callable]] = "gelu"							,  __snake_case						:	Optional[int] = 3_05_22							,  __snake_case						:	Optional[int] = 10_24							,  __snake_case						:	Optional[int] = 40_96							,  __snake_case						:	Optional[int] = 12							,  __snake_case						:	Optional[int] = 16							,  __snake_case						:	Optional[int] = 40_96							,  __snake_case						:	Optional[int] = 12							,  __snake_case						:	Optional[int] = 16							,  __snake_case						:	Optional[float] = 0.1							,  __snake_case						:	Optional[float] = 0.1							,  __snake_case						:	Optional[int] = 5_12							,  __snake_case						:	Optional[float] = 0.02							,  __snake_case						:	Optional[bool] = True							,  __snake_case						:	Optional[bool] = True							,  __snake_case						:	Optional[int] = 0							,  __snake_case						:	Optional[int] = 2							,  __snake_case						:	Optional[int] = 32							,  __snake_case						:	Optional[int] = 1_28							,  __snake_case						:	Optional[bool] = False							,  __snake_case						:	Optional[float] = 0.0							,  __snake_case						:	Optional[bool] = True							,  __snake_case						:	Optional[int] = 0							,  __snake_case						:	Optional[int] = 1							,  __snake_case						:	Optional[int] = 2							,  **__snake_case						:	Dict							,  ):





											'''simple docstring'''





											_snake_case: str	       =		vocab_size
											_snake_case: Optional[int]	       =		hidden_size
											_snake_case: Union[str, Any]	       =		encoder_ffn_dim
											_snake_case: str	       =		num_encoder_layers
											_snake_case: Tuple	       =		num_encoder_attention_heads
											_snake_case: List[Any]	       =		decoder_ffn_dim
											_snake_case: Dict	       =		num_decoder_layers
											_snake_case: List[Any]	       =		num_decoder_attention_heads
											_snake_case: Any	       =		max_position_embeddings
											_snake_case: Tuple	       =		init_std  # Normal(0, this parameter)
											_snake_case: Optional[int]	       =		activation_function

											# parameters for prophetnet
											_snake_case: Union[str, Any]	       =		ngram
											_snake_case: int	       =		num_buckets
											_snake_case: Dict	       =		relative_max_distance
											_snake_case: Dict	       =		disable_ngram_loss
											_snake_case: List[str]	       =		eps

											# 3 Types of Dropout
											_snake_case: Union[str, Any]	       =		attention_dropout
											_snake_case: List[Any]	       =		activation_dropout
											_snake_case: Optional[Any]	       =		dropout

											_snake_case: Dict	       =		use_cache

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


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





											'''simple docstring'''





											return self.num_encoder_layers + self.num_decoder_layers




					@num_hidden_layers.setter
					def  SCREAMING_SNAKE_CASE_		(       self						:	List[Any]							,  __snake_case						:	List[Any]							):





											'''simple docstring'''





											raise NotImplementedError(
											    'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'
											    ' `num_decoder_layers`.'							)












'''simple docstring'''






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       lowerCamelCase       :


							@staticmethod
							def  SCREAMING_SNAKE_CASE_		(       *__snake_case						:	Optional[Any]							,  **__snake_case						:	str							):





													'''simple docstring'''





													pass







@is_pipeline_test
@require_torch_or_tf
@require_vision
class       lowerCamelCase       (    unittest.TestCase	):
					_SCREAMING_SNAKE_CASE				 =     MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
					_SCREAMING_SNAKE_CASE				 =     TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING


					def  SCREAMING_SNAKE_CASE_		(       self						:	Union[str, Any]							,  __snake_case						:	Any							,  __snake_case						:	Tuple							,  __snake_case						:	Union[str, Any]							):





											'''simple docstring'''





											_snake_case: Union[str, Any]	       =		ImageClassificationPipeline(model=__snake_case							,  image_processor=__snake_case							,  top_k=2							)
											_snake_case: Tuple	       =		[
											    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							),
											    'http://images.cocodataset.org/val2017/000000039769.jpg',
											]
											return image_classifier, examples


					def  SCREAMING_SNAKE_CASE_		(       self						:	Dict							,  __snake_case						:	Any							,  __snake_case						:	str							):





											'''simple docstring'''





											_snake_case: Dict	       =		image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png'							)

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

											import datasets

											_snake_case: str	       =		datasets.load_dataset('hf-internal-testing/fixtures_image_utils'							,  'image'							,  split='test'							)

											# Accepts URL + PIL.Image + lists
											_snake_case: List[str]	       =		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(
											    __snake_case							,  [
											        [
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											        ],
											        [
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											        ],
											        [
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											        ],
											        [
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											        ],
											        [
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											            {'score': ANY(__snake_case							), 'label': ANY(__snake_case							)},
											        ],
											    ]							,  )


					@require_torch
					def  SCREAMING_SNAKE_CASE_		(       self						:	Optional[int]							):





											'''simple docstring'''





											_snake_case: Any	       =		'hf-internal-testing/tiny-random-vit'
											_snake_case: List[str]	       =		pipeline('image-classification'							,  model=__snake_case							)

											_snake_case: List[str]	       =		image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'							)
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}]							,  )

											_snake_case: Dict	       =		image_classifier(
											    [
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											    ]							,  top_k=2							,  )
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [
											        [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}],
											        [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}],
											    ]							,  )


					@require_tf
					def  SCREAMING_SNAKE_CASE_		(       self						:	List[str]							):





											'''simple docstring'''





											_snake_case: List[str]	       =		'hf-internal-testing/tiny-random-vit'
											_snake_case: List[str]	       =		pipeline('image-classification'							,  model=__snake_case							,  framework='tf'							)

											_snake_case: Any	       =		image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'							)
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}]							,  )

											_snake_case: str	       =		image_classifier(
											    [
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											    ]							,  top_k=2							,  )
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [
											        [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}],
											        [{'label': 'LABEL_1', 'score': 0.574}, {'label': 'LABEL_0', 'score': 0.426}],
											    ]							,  )


					def  SCREAMING_SNAKE_CASE_		(       self						:	Tuple							):





											'''simple docstring'''





											_snake_case: Union[str, Any]	       =		PreTrainedTokenizer()

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

											self.assertIs(image_classifier.tokenizer							,  __snake_case							)




					@slow
					@require_torch
					def  SCREAMING_SNAKE_CASE_		(       self						:	List[str]							):





											'''simple docstring'''





											_snake_case: Any	       =		pipeline('image-classification'							,  model='deepmind/vision-perceiver-conv'							)
											_snake_case: int	       =		image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'							)
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [
											        {'score': 0.4_385, 'label': 'tabby, tabby cat'},
											        {'score': 0.321, 'label': 'tiger cat'},
											        {'score': 0.0_502, 'label': 'Egyptian cat'},
											        {'score': 0.0_137, 'label': 'crib, cot'},
											        {'score': 0.007, 'label': 'radiator'},
											    ]							,  )

											_snake_case: Union[str, Any]	       =		pipeline('image-classification'							,  model='deepmind/vision-perceiver-fourier'							)
											_snake_case: int	       =		image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'							)
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [
											        {'score': 0.5_658, 'label': 'tabby, tabby cat'},
											        {'score': 0.1_309, 'label': 'tiger cat'},
											        {'score': 0.0_722, 'label': 'Egyptian cat'},
											        {'score': 0.0_707, 'label': 'remote control, remote'},
											        {'score': 0.0_082, 'label': 'computer keyboard, keypad'},
											    ]							,  )

											_snake_case: Optional[int]	       =		pipeline('image-classification'							,  model='deepmind/vision-perceiver-learned'							)
											_snake_case: str	       =		image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'							)
											self.assertEqual(
											    nested_simplify(__snake_case							,  decimals=4							)							,  [
											        {'score': 0.3_022, 'label': 'tabby, tabby cat'},
											        {'score': 0.2_362, 'label': 'Egyptian cat'},
											        {'score': 0.1_856, 'label': 'tiger cat'},
											        {'score': 0.0_324, 'label': 'remote control, remote'},
											        {'score': 0.0_096, 'label': 'quilt, comforter, comfort, puff'},
											    ]							,  )





