





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     :







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=13		,     _UpperCAmelCase=30		,     _UpperCAmelCase=2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=32		,     _UpperCAmelCase=5		,     _UpperCAmelCase=4		,     _UpperCAmelCase=37		,     _UpperCAmelCase="gelu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=10		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=0.6		,     _UpperCAmelCase=None		,     ):
									__a	:  int         =					parent
									__a	:  Union[str, Any]         =					batch_size
									__a	:  List[str]         =					image_size
									__a	:  Optional[int]         =					patch_size
									__a	:  str         =					num_channels
									__a	:  List[Any]         =					is_training
									__a	:  Any         =					use_labels
									__a	:  List[Any]         =					hidden_size
									__a	:  str         =					num_hidden_layers
									__a	:  List[str]         =					num_attention_heads
									__a	:  List[str]         =					intermediate_size
									__a	:  Optional[Any]         =					hidden_act
									__a	:  str         =					hidden_dropout_prob
									__a	:  str         =					attention_probs_dropout_prob
									__a	:  Tuple         =					type_sequence_label_size
									__a	:  Optional[Any]         =					initializer_range
									__a	:  int         =					mask_ratio
									__a	:  List[str]         =					scope

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

									__a	:  Union[str, Any]         =					self.get_config()

									return config, pixel_values, labels







					def 	_lowerCamelCase						(  self       ):
									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 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[int]         =					ViTMAEModel(config=_lowerCamelCase       )
									model.to(_lowerCamelCase       )
									model.eval()
									__a	:  Union[str, Any]         =					model(_lowerCamelCase       )
									self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[Any]         =					ViTMAEForPreTraining(_lowerCamelCase       )
									model.to(_lowerCamelCase       )
									model.eval()
									__a	:  Optional[Any]         =					model(_lowerCamelCase       )
									__a	:  List[str]         =					(self.image_size // self.patch_size) ** 2
									__a	:  int         =					self.patch_size**2 * self.num_channels
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, num_patches, expected_num_channels)       )

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







					def 	_lowerCamelCase						(  self       ):
									__a	:  Tuple         =					self.prepare_config_and_inputs()
									__a	:  Dict         =					config_and_inputs
									__a	:  Any         =					{'''pixel_values''': pixel_values}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     a__				, a__				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
					__lowerCAmelCase					=					{'feature-extraction': ViTMAEModel} if is_torch_available() else {}

					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					False







					def 	_lowerCamelCase						(  self       ):
									__a	:  Tuple         =					ViTMAEModelTester(self       )
									__a	:  List[str]         =					ConfigTester(self		,     config_class=_lowerCamelCase		,     has_text_modality=_lowerCamelCase		,     hidden_size=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					@unittest.skip(reason='''ViTMAE does not use inputs_embeds'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													__a	:  List[Any]         =					model_class(_lowerCamelCase       )
													self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module)       )
													__a	:  Union[str, Any]         =					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(_lowerCamelCase		,     nn.Linear       )       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs_for_common()

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

													__a	:  List[Any]         =					['''pixel_values''']
													self.assertListEqual(arg_names[:1]		,     _lowerCamelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowerCamelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*_lowerCamelCase       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									# make masks reproducible
									np.random.seed(2       )

									__a	:  Union[str, Any]         =					int((pt_model.config.image_size // pt_model.config.patch_size) ** 2       )
									__a	:  List[Any]         =					np.random.uniform(size=(self.model_tester.batch_size, num_patches)       )
									__a	:  int         =					torch.from_numpy(_lowerCamelCase       )

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

									super().check_pt_tf_models(_lowerCamelCase		,     _lowerCamelCase		,     _lowerCamelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													__a	:  Any         =					model_class(_lowerCamelCase       )
													model.to(_lowerCamelCase       )
													model.eval()
													# make random mask reproducible
													torch.manual_seed(2       )
													with torch.no_grad():
																	__a	:  List[Any]         =					model(**self._prepare_for_class(_lowerCamelCase		,     _lowerCamelCase       )       )

													__a	:  str         =					outputs[0].cpu().numpy()
													__a	:  str         =					0

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

																	# Make sure we don't have nans
																	__a	:  Dict         =					after_outputs[0].cpu().numpy()
																	__a	:  Tuple         =					0
																	__a	:  Dict         =					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
    to get deterministic results.'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







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







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







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







					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a	:  Any         =					ViTMAEModel.from_pretrained(_lowerCamelCase       )
													self.assertIsNotNone(_lowerCamelCase       )






def       __A (     )						->						int:
				__a	:  str         =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image






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







					'''simple docstring'''




					@cached_property
					def 	_lowerCamelCase						(  self       ):
									return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base'''       ) if is_vision_available() else None







					@slow
					def 	_lowerCamelCase						(  self       ):
									# make random mask reproducible across the PT and TF model
									np.random.seed(2       )

									__a	:  Union[str, Any]         =					ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base'''       ).to(_lowerCamelCase       )

									__a	:  List[str]         =					self.default_image_processor
									__a	:  List[str]         =					prepare_img()
									__a	:  List[str]         =					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)
									__a	:  List[str]         =					ViTMAEConfig()
									__a	:  Tuple         =					int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2       )
									__a	:  Optional[Any]         =					np.random.uniform(size=(1, num_patches)       )

									# forward pass
									with torch.no_grad():
													__a	:  Optional[int]         =					model(**_lowerCamelCase		,     noise=torch.from_numpy(_lowerCamelCase       ).to(device=_lowerCamelCase       )       )

									# verify the logits
									__a	:  List[Any]         =					torch.Size((1, 196, 768)       )
									self.assertEqual(outputs.logits.shape		,     _lowerCamelCase       )

									__a	:  List[Any]         =					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"""






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					'''openai/whisper-base'''
					__lowerCAmelCase					=					(
					    '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the '''
					    '''transcribed text.'''
					)
					__lowerCAmelCase					=					'''transcriber'''
					__lowerCAmelCase					=					WhisperProcessor
					__lowerCAmelCase					=					WhisperForConditionalGeneration

					__lowerCAmelCase					=					['''audio''']
					__lowerCAmelCase					=					['''text''']







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor(_UpperCAmelCase		,     return_tensors='''pt'''       ).input_features







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.model.generate(inputs=_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor.batch_decode(_UpperCAmelCase		,     skip_special_tokens=_UpperCAmelCase       )[0]