






"""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 							__lowercase:






												@staticmethod
												def 		lowerCAmelCase (       *_SCREAMING_SNAKE_CASE     ,    **_SCREAMING_SNAKE_CASE)      ->	Any:




																	"""simple docstring"""





																	pass




@is_pipeline_test
@require_torch_or_tf
@require_vision
class 							__lowercase(							unittest.TestCase	):
						lowerCAmelCase_		:				List[Any]=							MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
						lowerCAmelCase_		:				Union[str, Any]=							TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	str:




											"""simple docstring"""





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






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	List[Any]:




											"""simple docstring"""





											lowerCamelCase__					=image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png')

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

											import datasets

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

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






						@require_torch
						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					='hf-internal-testing/tiny-random-vit'
											lowerCamelCase__					=pipeline('image-classification'     ,    model=_SCREAMING_SNAKE_CASE)

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

											lowerCamelCase__					=image_classifier(
											    [
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											    ]     ,    top_k=2     ,    )
											self.assertEqual(
											    nested_simplify(_SCREAMING_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 		lowerCAmelCase (       self)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					='hf-internal-testing/tiny-random-vit'
											lowerCamelCase__					=pipeline('image-classification'     ,    model=_SCREAMING_SNAKE_CASE     ,    framework='tf')

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

											lowerCamelCase__					=image_classifier(
											    [
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											        'http://images.cocodataset.org/val2017/000000039769.jpg',
											    ]     ,    top_k=2     ,    )
											self.assertEqual(
											    nested_simplify(_SCREAMING_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 		lowerCAmelCase (       self)      ->	Optional[int]:




											"""simple docstring"""





											lowerCamelCase__					=PreTrainedTokenizer()

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

											self.assertIs(image_classifier.tokenizer     ,    _SCREAMING_SNAKE_CASE)






						@slow
						@require_torch
						def 		lowerCAmelCase (       self)      ->	Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__					=pipeline('image-classification'     ,    model='deepmind/vision-perceiver-conv')
											lowerCamelCase__					=image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg')
											self.assertEqual(
											    nested_simplify(_SCREAMING_SNAKE_CASE     ,    decimals=4)     ,    [
											        {'score': 0.4385, 'label': 'tabby, tabby cat'},
											        {'score': 0.321, 'label': 'tiger cat'},
											        {'score': 0.0502, 'label': 'Egyptian cat'},
											        {'score': 0.0137, 'label': 'crib, cot'},
											        {'score': 0.007, 'label': 'radiator'},
											    ]     ,    )

											lowerCamelCase__					=pipeline('image-classification'     ,    model='deepmind/vision-perceiver-fourier')
											lowerCamelCase__					=image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg')
											self.assertEqual(
											    nested_simplify(_SCREAMING_SNAKE_CASE     ,    decimals=4)     ,    [
											        {'score': 0.5658, 'label': 'tabby, tabby cat'},
											        {'score': 0.1309, 'label': 'tiger cat'},
											        {'score': 0.0722, 'label': 'Egyptian cat'},
											        {'score': 0.0707, 'label': 'remote control, remote'},
											        {'score': 0.0082, 'label': 'computer keyboard, keypad'},
											    ]     ,    )

											lowerCamelCase__					=pipeline('image-classification'     ,    model='deepmind/vision-perceiver-learned')
											lowerCamelCase__					=image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg')
											self.assertEqual(
											    nested_simplify(_SCREAMING_SNAKE_CASE     ,    decimals=4)     ,    [
											        {'score': 0.3022, 'label': 'tabby, tabby cat'},
											        {'score': 0.2362, 'label': 'Egyptian cat'},
											        {'score': 0.1856, 'label': 'tiger cat'},
											        {'score': 0.0324, 'label': 'remote control, remote'},
											        {'score': 0.0096, 'label': 'quilt, comforter, comfort, puff'},
											    ]     ,    )







"""simple docstring"""



import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
						import torch

						from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
						import pyanvml.pyanvml as nvml


_snake_case							:   str	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				PyTorchBenchmarkArguments
						lowerCAmelCase_		:				PretrainedConfig
						lowerCAmelCase_		:				str=							"PyTorch"






						@property
						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											return torch.__version__






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											if self.args.torchscript:
																lowerCamelCase__					=True

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											model.eval()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')
																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											if self.args.torchscript:
																with torch.no_grad():
																					lowerCamelCase__					=torch.jit.trace(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											else:
																lowerCamelCase__					=model

											def encoder_decoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE)
																return outputs

											def encoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE)
																return outputs

											lowerCamelCase__					=encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
											return _forward






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											if self.args.torchscript:
																raise NotImplementedError('Training for torchscript is currently not implemented')
											else:
																lowerCamelCase__					=model

											model.train()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')

																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											def compute_loss_and_backprob_encoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											def compute_loss_and_backprob_encoder_decoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											lowerCamelCase__					=(
											    compute_loss_and_backprob_encoder_decoder
											    if config.is_encoder_decoder
											    else compute_loss_and_backprob_encoder
											)
											return _train






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											try:
																if self.args.is_tpu or self.args.torchscript:
																					# run additional 10 times to stabilize compilation for tpu and torchscript
																					logger.info('Do inference on TPU or torchscript. Running model 5 times to stabilize compilation')
																					timeit.repeat(
																					    _SCREAMING_SNAKE_CASE     ,    repeat=1     ,    number=5     ,    )

																# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
																lowerCamelCase__					=timeit.repeat(
																    _SCREAMING_SNAKE_CASE     ,    repeat=self.args.repeat     ,    number=10     ,    )

																if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
																					import torch_xla.debug.metrics as met

																					self.print_fn(met.metrics_report())

																return min(_SCREAMING_SNAKE_CASE) / 10.0
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A"






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, MemorySummary]:




											"""simple docstring"""





											try:
																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=start_memory_tracing('transformers')

																if self.args.is_tpu:
																					# tpu
																					raise NotImplementedError(
																					    'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'
																					    ' `--no-memory` or `args.memory=False`')
																elif self.args.is_gpu:
																					if not is_pyanvml_available():
																										logger.warning(
																										    'py3nvml not installed, we won\'t log GPU memory usage. '
																										    'Install py3nvml (pip install py3nvml) to log information about GPU.')
																										lowerCamelCase__					='N/A'
																					else:
																										logger.info(
																										    'Measuring total GPU usage on GPU device. Make sure to not have additional processes running'
																										    ' on the same GPU.')
																										# init nvml
																										nvml.nvmlInit()
																										func()
																										lowerCamelCase__					=nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx)
																										lowerCamelCase__					=nvml.nvmlDeviceGetMemoryInfo(_SCREAMING_SNAKE_CASE)
																										lowerCamelCase__					=meminfo.used
																										lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE)
																										# shutdown nvml
																										nvml.nvmlShutdown()
																else:
																					# cpu
																					lowerCamelCase__					=measure_peak_memory_cpu(_SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE) if isinstance(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE) else memory_bytes

																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=stop_memory_tracing(_SCREAMING_SNAKE_CASE)
																else:
																					lowerCamelCase__					=None

																return memory, summary
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A", None