






import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
	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		,     __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.02		,     __UpperCAmelCase=None		,     __UpperCAmelCase=2		,     )->       Tuple:







							'''simple docstring'''





							lowerCAmelCase__			   =    parent
							lowerCAmelCase__			   =    batch_size
							lowerCAmelCase__			   =    image_size
							lowerCAmelCase__			   =    patch_size
							lowerCAmelCase__			   =    num_channels
							lowerCAmelCase__			   =    is_training
							lowerCAmelCase__			   =    use_labels
							lowerCAmelCase__			   =    hidden_size
							lowerCAmelCase__			   =    num_hidden_layers
							lowerCAmelCase__			   =    num_attention_heads
							lowerCAmelCase__			   =    intermediate_size
							lowerCAmelCase__			   =    hidden_act
							lowerCAmelCase__			   =    hidden_dropout_prob
							lowerCAmelCase__			   =    attention_probs_dropout_prob
							lowerCAmelCase__			   =    type_sequence_label_size
							lowerCAmelCase__			   =    initializer_range
							lowerCAmelCase__			   =    scope
							lowerCAmelCase__			   =    encoder_stride

							# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
							lowerCAmelCase__			   =    (image_size // patch_size) ** 2
							lowerCAmelCase__			   =    num_patches + 1

	def 		UpperCAmelCase	(				self							)->       Tuple:







							'''simple docstring'''





							lowerCAmelCase__			   =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							lowerCAmelCase__			   =    None
							if self.use_labels:
													lowerCAmelCase__			   =    ids_tensor([self.batch_size]		,     self.type_sequence_label_size							)

							lowerCAmelCase__			   =    self.get_config()

							return config, pixel_values, labels

	def 		UpperCAmelCase	(				self							)->       Union[str, Any]:







							'''simple docstring'''





							return ViTConfig(
							    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		,     encoder_stride=self.encoder_stride		,     )

	def 		UpperCAmelCase	(				self		,     __UpperCAmelCase		,     __UpperCAmelCase		,     __UpperCAmelCase							)->       int:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTModel(config=lowerCAmelCase__							)
							model.to(lowerCAmelCase__							)
							model.eval()
							lowerCAmelCase__			   =    model(lowerCAmelCase__							)
							self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)							)

	def 		UpperCAmelCase	(				self		,     __UpperCAmelCase		,     __UpperCAmelCase		,     __UpperCAmelCase							)->       Any:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTForMaskedImageModeling(config=lowerCAmelCase__							)
							model.to(lowerCAmelCase__							)
							model.eval()
							lowerCAmelCase__			   =    model(lowerCAmelCase__							)
							self.parent.assertEqual(
							    result.reconstruction.shape		,     (self.batch_size, self.num_channels, self.image_size, self.image_size)							)

							# test greyscale images
							lowerCAmelCase__			   =    1
							lowerCAmelCase__			   =    ViTForMaskedImageModeling(lowerCAmelCase__							)
							model.to(lowerCAmelCase__							)
							model.eval()

							lowerCAmelCase__			   =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
							lowerCAmelCase__			   =    model(lowerCAmelCase__							)
							self.parent.assertEqual(result.reconstruction.shape		,     (self.batch_size, 1, self.image_size, self.image_size)							)

	def 		UpperCAmelCase	(				self		,     __UpperCAmelCase		,     __UpperCAmelCase		,     __UpperCAmelCase							)->       Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.type_sequence_label_size
							lowerCAmelCase__			   =    ViTForImageClassification(lowerCAmelCase__							)
							model.to(lowerCAmelCase__							)
							model.eval()
							lowerCAmelCase__			   =    model(lowerCAmelCase__		,     labels=lowerCAmelCase__							)
							self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)							)

							# test greyscale images
							lowerCAmelCase__			   =    1
							lowerCAmelCase__			   =    ViTForImageClassification(lowerCAmelCase__							)
							model.to(lowerCAmelCase__							)
							model.eval()

							lowerCAmelCase__			   =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
							lowerCAmelCase__			   =    model(lowerCAmelCase__							)
							self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)							)


	def 		UpperCAmelCase	(				self							)->       Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.prepare_config_and_inputs()
							(
							    lowerCAmelCase__      
							)			   =    config_and_inputs
							lowerCAmelCase__			   =    {'pixel_values': pixel_values}
							return config, inputs_dict





@require_torch
class   lowercase__  (      __SCREAMING_SNAKE_CASE,       __SCREAMING_SNAKE_CASE,       unittest.TestCase						):
	a_       =(
	    (
	        ViTModel,
	        ViTForImageClassification,
	        ViTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	a_       =(
	    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
	    if is_torch_available()
	    else {}
	)
	a_       =True

	a_       =False
	a_       =False
	a_       =False

	def 		UpperCAmelCase	(				self							)->       List[Any]:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTModelTester(self							)
							lowerCAmelCase__			   =    ConfigTester(self		,     config_class=lowerCAmelCase__		,     has_text_modality=lowerCAmelCase__		,     hidden_size=37							)

	def 		UpperCAmelCase	(				self							)->       List[Any]:







							'''simple docstring'''





							self.config_tester.run_common_tests()

	@unittest.skip(reason="ViT does not use inputs_embeds"							)
	def 		UpperCAmelCase	(				self							)->       Tuple:







							'''simple docstring'''





							pass

	def 		UpperCAmelCase	(				self							)->       Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													lowerCAmelCase__			   =    model_class(lowerCAmelCase__							)
													self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module)							)
													lowerCAmelCase__			   =    model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCAmelCase__		,     nn.Linear							)							)

	def 		UpperCAmelCase	(				self							)->       Tuple:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.model_tester.prepare_config_and_inputs_for_common()

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

													lowerCAmelCase__			   =    ['pixel_values']
													self.assertListEqual(arg_names[:1]		,     lowerCAmelCase__							)

	def 		UpperCAmelCase	(				self							)->       Dict:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCAmelCase__							)

	def 		UpperCAmelCase	(				self							)->       Any:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__							)

	def 		UpperCAmelCase	(				self							)->       Tuple:







							'''simple docstring'''





							lowerCAmelCase__			   =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)


	@slow
	def 		UpperCAmelCase	(				self							)->       str:







							'''simple docstring'''





							for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCAmelCase__			   =    ViTModel.from_pretrained(lowerCAmelCase__							)
													self.assertIsNotNone(lowerCAmelCase__							)





def 							_a	(  )			-> Optional[int]:
						"""simple docstring"""







						lowerCAmelCase__			   =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image





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

	@cached_property
	def 		UpperCAmelCase	(				self							)->       str:







							'''simple docstring'''





							return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"							) if is_vision_available() else None

	@slow
	def 		UpperCAmelCase	(				self							)->       int:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"							).to(lowerCAmelCase__							)

							lowerCAmelCase__			   =    self.default_image_processor
							lowerCAmelCase__			   =    prepare_img()
							lowerCAmelCase__			   =    image_processor(images=lowerCAmelCase__		,     return_tensors="pt"							).to(lowerCAmelCase__							)

							# forward pass
							with torch.no_grad():
													lowerCAmelCase__			   =    model(**lowerCAmelCase__							)

							# verify the logits
							lowerCAmelCase__			   =    torch.Size((1, 1000)							)
							self.assertEqual(outputs.logits.shape		,     lowerCAmelCase__							)

							lowerCAmelCase__			   =    torch.tensor([-0.2_744, 0.8_215, -0.0_836]							).to(lowerCAmelCase__							)

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

	@slow
	def 		UpperCAmelCase	(				self							)->       int:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTModel.from_pretrained("facebook/dino-vits8"							).to(lowerCAmelCase__							)

							lowerCAmelCase__			   =    ViTImageProcessor.from_pretrained("facebook/dino-vits8"		,     size=480							)
							lowerCAmelCase__			   =    prepare_img()
							lowerCAmelCase__			   =    image_processor(images=lowerCAmelCase__		,     return_tensors="pt"							)
							lowerCAmelCase__			   =    inputs.pixel_values.to(lowerCAmelCase__							)

							# forward pass
							with torch.no_grad():
													lowerCAmelCase__			   =    model(lowerCAmelCase__		,     interpolate_pos_encoding=lowerCAmelCase__							)

							# verify the logits
							lowerCAmelCase__			   =    torch.Size((1, 3601, 384)							)
							self.assertEqual(outputs.last_hidden_state.shape		,     lowerCAmelCase__							)

							lowerCAmelCase__			   =    torch.tensor(
							    [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]							).to(lowerCAmelCase__							)

							self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]		,     lowerCAmelCase__		,     atol=1E-4							)							)


	@slow
	@require_accelerate
	@require_torch_gpu
	def 		UpperCAmelCase	(				self							)->       Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__			   =    ViTModel.from_pretrained("facebook/dino-vits8"		,     torch_dtype=torch.floataa		,     device_map="auto"							)
							lowerCAmelCase__			   =    self.default_image_processor

							lowerCAmelCase__			   =    prepare_img()
							lowerCAmelCase__			   =    image_processor(images=lowerCAmelCase__		,     return_tensors="pt"							)
							lowerCAmelCase__			   =    inputs.pixel_values.to(lowerCAmelCase__							)

							# forward pass to make sure inference works in fp16
							with torch.no_grad():
													lowerCAmelCase__			   =    model(lowerCAmelCase__							)










# 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.
import torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
				from datasets import load_dataset







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							_UpperCAmelCase      =						"""microsoft/speecht5_tts"""
							_UpperCAmelCase      =						(
							    """This is a tool that reads an English text out loud. It takes an input named `text` which should contain the """
							    """text to read (in English) and returns a waveform object containing the sound."""
							)
							_UpperCAmelCase      =						"""text_reader"""
							_UpperCAmelCase      =						SpeechTaProcessor
							_UpperCAmelCase      =						SpeechTaForTextToSpeech
							_UpperCAmelCase      =						SpeechTaHifiGan

							_UpperCAmelCase      =						["""text"""]
							_UpperCAmelCase      =						["""audio"""]






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										if self.post_processor is None:
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					'microsoft/speecht5_hifigan'
										super().setup()






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__=None       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     int					=					self.pre_processor(text=lowerCAmelCase__			,						return_tensors='pt'			,						truncation=lowerCAmelCase__       )

										if speaker_embeddings is None:
													if not is_datasets_available():
																raise ImportError('Datasets needs to be installed if not passing speaker embeddings.'       )

													SCREAMING_SNAKE_CASE_		:     List[Any]					=					load_dataset('Matthijs/cmu-arctic-xvectors'			,						split='validation'       )
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.tensor(embeddings_dataset[7_3_0_5]['xvector']       ).unsqueeze(0       )

										return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__       ):





										"""simple docstring"""




										with torch.no_grad():
													return self.model.generate_speech(**lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__       ):





										"""simple docstring"""




										with torch.no_grad():
													return self.post_processor(lowerCAmelCase__       ).cpu().detach()





