



'''simple docstring'''
import inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch
					from torch import nn

					from transformers import ViTMAEForPreTraining, ViTMAEModel
					from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor






class 				lowercase__      :





	def __init__(					self   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Optional[int]=13					,lowerCamelCase__   :							List[Any]=30					,lowerCamelCase__   :							Union[str, Any]=2					,lowerCamelCase__   :							str=3					,lowerCamelCase__   :							Dict=True					,lowerCamelCase__   :							Optional[Any]=True					,lowerCamelCase__   :							Union[str, Any]=32					,lowerCamelCase__   :							List[Any]=5					,lowerCamelCase__   :							Optional[int]=4					,lowerCamelCase__   :							Union[str, Any]=37					,lowerCamelCase__   :							Dict="gelu"					,lowerCamelCase__   :							Any=0.1					,lowerCamelCase__   :							Dict=0.1					,lowerCamelCase__   :							List[Any]=10					,lowerCamelCase__   :							List[str]=0.0_2					,lowerCamelCase__   :							Optional[int]=3					,lowerCamelCase__   :							int=0.6					,lowerCamelCase__   :							Optional[Any]=None					,):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  parent
							_UpperCamelCase   :						Union[str, Any]												=  batch_size
							_UpperCamelCase   :						str												=  image_size
							_UpperCamelCase   :						List[Any]												=  patch_size
							_UpperCamelCase   :						Any												=  num_channels
							_UpperCamelCase   :						List[str]												=  is_training
							_UpperCamelCase   :						Tuple												=  use_labels
							_UpperCamelCase   :						int												=  hidden_size
							_UpperCamelCase   :						int												=  num_hidden_layers
							_UpperCamelCase   :						Union[str, Any]												=  num_attention_heads
							_UpperCamelCase   :						Optional[Any]												=  intermediate_size
							_UpperCamelCase   :						Any												=  hidden_act
							_UpperCamelCase   :						Any												=  hidden_dropout_prob
							_UpperCamelCase   :						Optional[Any]												=  attention_probs_dropout_prob
							_UpperCamelCase   :						int												=  type_sequence_label_size
							_UpperCamelCase   :						Tuple												=  initializer_range
							_UpperCamelCase   :						List[str]												=  mask_ratio
							_UpperCamelCase   :						Any												=  scope

							# in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
							# (we add 1 for the [CLS] token)
							_UpperCamelCase   :						Any												=  (image_size // patch_size) ** 2
							_UpperCamelCase   :						Tuple												=  int(math.ceil((1 - mask_ratio) * (num_patches + 1)					)					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

							_UpperCamelCase   :						Optional[int]												=  None
							if self.use_labels:
													_UpperCamelCase   :						str												=  ids_tensor([self.batch_size]					,self.type_sequence_label_size					)

							_UpperCamelCase   :						Dict												=  self.get_config()

							return config, pixel_values, labels





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							return ViTMAEConfig(
							    image_size=self.image_size					,patch_size=self.patch_size					,num_channels=self.num_channels					,hidden_size=self.hidden_size					,num_hidden_layers=self.num_hidden_layers					,num_attention_heads=self.num_attention_heads					,intermediate_size=self.intermediate_size					,hidden_act=self.hidden_act					,hidden_dropout_prob=self.hidden_dropout_prob					,attention_probs_dropout_prob=self.attention_probs_dropout_prob					,is_decoder=lowerCamelCase__					,initializer_range=self.initializer_range					,mask_ratio=self.mask_ratio					,)





	def       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  ViTMAEModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						List[Any]												=  model(lowerCamelCase__					)
							self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  ViTMAEForPreTraining(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					)
							_UpperCamelCase   :						int												=  (self.image_size // self.patch_size) ** 2
							_UpperCamelCase   :						Any												=  self.patch_size**2 * self.num_channels
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, num_patches, expected_num_channels)					)

							# test greyscale images
							_UpperCamelCase   :						Union[str, Any]												=  1
							_UpperCamelCase   :						List[Any]												=  ViTMAEForPreTraining(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Tuple												=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
							_UpperCamelCase   :						str												=  model(lowerCamelCase__					)
							_UpperCamelCase   :						str												=  self.patch_size**2
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, num_patches, expected_num_channels)					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self.prepare_config_and_inputs()
							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						Union[str, Any]												=  config_and_inputs
							_UpperCamelCase   :						List[Any]												=  {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 				lowercase__      (				lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
	lowercase__    							=     {"""feature-extraction""": ViTMAEModel} if is_torch_available() else {}

	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  ViTMAEModelTester(self					)
							_UpperCamelCase   :						Dict												=  ConfigTester(self					,config_class=lowerCamelCase__					,has_text_modality=lowerCamelCase__					,hidden_size=37					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	@unittest.skip(reason='ViTMAE does not use inputs_embeds'					)
	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Any												=  model_class(lowerCamelCase__					)
													self.assertIsInstance(model.get_input_embeddings()					,(nn.Module)					)
													_UpperCamelCase   :						Optional[int]												=  model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__					,nn.Linear					)					)





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						str												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						List[Any]												=  model_class(lowerCamelCase__					)
													_UpperCamelCase   :						str												=  inspect.signature(model.forward					)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase   :						List[Any]												=  [*signature.parameters.keys()]

													_UpperCamelCase   :						int												=  ['pixel_values']
													self.assertListEqual(arg_names[:1]					,lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							str					):







							'''simple docstring'''
							# make masks reproducible
							np.random.seed(2					)

							_UpperCamelCase   :						Optional[Any]												=  int((pt_model.config.image_size // pt_model.config.patch_size) ** 2					)
							_UpperCamelCase   :						Optional[Any]												=  np.random.uniform(size=(self.model_tester.batch_size, num_patches)					)
							_UpperCamelCase   :						Optional[Any]												=  torch.from_numpy(lowerCamelCase__					)

							# Add `noise` argument.
							# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
							_UpperCamelCase   :						Optional[int]												=  pt_noise

							super().check_pt_tf_models(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						str												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Tuple												=  model_class(lowerCamelCase__					)
													model.to(lowerCamelCase__					)
													model.eval()
													# make random mask reproducible
													torch.manual_seed(2					)
													with torch.no_grad():
																			_UpperCamelCase   :						Tuple												=  model(**self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)					)

													_UpperCamelCase   :						Tuple												=  outputs[0].cpu().numpy()
													_UpperCamelCase   :						List[str]												=  0

													with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(lowerCamelCase__					)
																			_UpperCamelCase   :						str												=  model_class.from_pretrained(lowerCamelCase__					)
																			model.to(lowerCamelCase__					)
																			# make random mask reproducible
																			torch.manual_seed(2					)
																			with torch.no_grad():
																									_UpperCamelCase   :						Any												=  model(**self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)					)

																			# Make sure we don't have nans
																			_UpperCamelCase   :						Dict												=  after_outputs[0].cpu().numpy()
																			_UpperCamelCase   :						List[str]												=  0
																			_UpperCamelCase   :						List[str]												=  np.amax(np.abs(out_a - out_a					)					)
																			self.assertLessEqual(lowerCamelCase__					,1E-5					)





	@unittest.skip(
	    reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'					)
	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							pass





	@unittest.skip(
	    reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'					)
	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							pass





	@unittest.skip(
	    reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'					)
	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'					)
	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							pass





	@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'					)
	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							pass





	@slow
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase   :						Optional[int]												=  ViTMAEModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)






def A__       (	):
						_UpperCamelCase   :						List[Any]												=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
						return image






@require_torch
@require_vision
class 				lowercase__      (				unittest.TestCase  ):





	@cached_property
	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'					) if is_vision_available() else None





	@slow
	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							# make random mask reproducible across the PT and TF model
							np.random.seed(2					)

							_UpperCamelCase   :						int												=  ViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'					).to(lowerCamelCase__					)

							_UpperCamelCase   :						Tuple												=  self.default_image_processor
							_UpperCamelCase   :						Tuple												=  prepare_img()
							_UpperCamelCase   :						Optional[int]												=  image_processor(images=lowerCamelCase__					,return_tensors='pt'					).to(lowerCamelCase__					)

							# prepare a noise vector that will be also used for testing the TF model
							# (this way we can ensure that the PT and TF models operate on the same inputs)
							_UpperCamelCase   :						Dict												=  ViTMAEConfig()
							_UpperCamelCase   :						Union[str, Any]												=  int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2					)
							_UpperCamelCase   :						Optional[Any]												=  np.random.uniform(size=(1, num_patches)					)

							# forward pass
							with torch.no_grad():
													_UpperCamelCase   :						int												=  model(**lowerCamelCase__					,noise=torch.from_numpy(lowerCamelCase__					).to(device=lowerCamelCase__					)					)

							# verify the logits
							_UpperCamelCase   :						Union[str, Any]												=  torch.Size((1, 196, 768)					)
							self.assertEqual(outputs.logits.shape					,lowerCamelCase__					)

							_UpperCamelCase   :						Optional[int]												=  torch.tensor(
							    [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]					)

							self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]					,expected_slice.to(lowerCamelCase__					)					,atol=1E-4					)					)







'''simple docstring'''
from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


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






@add_end_docstrings(lowercase  )
class 				lowercase__      (				lowercase  ):





	def __init__(					self   :							Tuple					,**lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							super().__init__(**lowerCamelCase__					)

							if self.framework != "pt":
													raise ValueError(F'The {self.__class__} is only available in PyTorch.'					)
							# No specific FOR_XXX available yet





	def __call__(					self   :							Dict					,lowerCamelCase__   :							Union[np.ndarray, bytes, str]					,**lowerCamelCase__   :							Any					):







							'''simple docstring'''
							return super().__call__(lowerCamelCase__					,**lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					,**lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  {}
							if "candidate_labels" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['candidate_labels']
							if "hypothesis_template" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['hypothesis_template']

							return preprocess_params, {}, {}





	def       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							str=None					,lowerCamelCase__   :							List[Any]="This is a sound of {}."					):







							'''simple docstring'''
							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													if audio.startswith('http://'					) or audio.startswith('https://'					):
																			# We need to actually check for a real protocol, otherwise it's impossible to use a local file
																			# like http_huggingface_co.png
																			_UpperCamelCase   :						Optional[Any]												=  requests.get(lowerCamelCase__					).content
													else:
																			with open(lowerCamelCase__					,'rb'					) as f:
																									_UpperCamelCase   :						List[str]												=  f.read()

							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													_UpperCamelCase   :						int												=  ffmpeg_read(lowerCamelCase__					,self.feature_extractor.sampling_rate					)

							if not isinstance(lowerCamelCase__					,np.ndarray					):
													raise ValueError('We expect a numpy ndarray as input'					)
							if len(audio.shape					) != 1:
													raise ValueError('We expect a single channel audio input for ZeroShotAudioClassificationPipeline'					)

							_UpperCamelCase   :						Dict												=  self.feature_extractor(
							    [audio]					,sampling_rate=self.feature_extractor.sampling_rate					,return_tensors='pt'					)
							_UpperCamelCase   :						List[Any]												=  candidate_labels
							_UpperCamelCase   :						Tuple												=  [hypothesis_template.format(lowerCamelCase__					) for x in candidate_labels]
							_UpperCamelCase   :						Dict												=  self.tokenizer(lowerCamelCase__					,return_tensors=self.framework					,padding=lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  [text_inputs]
							return inputs





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  model_inputs.pop('candidate_labels'					)
							_UpperCamelCase   :						str												=  model_inputs.pop('text_inputs'					)
							if isinstance(text_inputs[0]					,lowerCamelCase__					):
													_UpperCamelCase   :						Union[str, Any]												=  text_inputs[0]
							else:
													# Batching case.
													_UpperCamelCase   :						int												=  text_inputs[0][0]

							_UpperCamelCase   :						List[str]												=  self.model(**lowerCamelCase__					,**lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  {
							    'candidate_labels': candidate_labels,
							    'logits': outputs.logits_per_audio,
							}
							return model_outputs





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  model_outputs.pop('candidate_labels'					)
							_UpperCamelCase   :						int												=  model_outputs['logits'][0]

							if self.framework == "pt":
													_UpperCamelCase   :						Tuple												=  logits.softmax(dim=0					)
													_UpperCamelCase   :						str												=  probs.tolist()
							else:
													raise ValueError('`tf` framework not supported.'					)

							_UpperCamelCase   :						Union[str, Any]												=  [
							    {'score': score, 'label': candidate_label}
							    for score, candidate_label in sorted(zip(lowerCamelCase__					,lowerCamelCase__					)					,key=lambda lowerCamelCase__					: -x[0]					)
							]
							return result


