






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






						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       ,  ):






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






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






												"""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=_UpperCamelCase       ,  initializer_range=self.initializer_range       ,  encoder_stride=self.encoder_stride       ,  )






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  ViTForMaskedImageModeling(config=_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												model.eval()
												lowerCAmelCase__        =  model(_UpperCamelCase				)
												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(_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												model.eval()

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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






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

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

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







						def  UpperCamelCase__ (		self				):






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




@require_torch
class       __SCREAMING_SNAKE_CASE   (				__lowercase						,   __lowercase						,   unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    Dict						=   (
						    (
						        ViTModel,
						        ViTForImageClassification,
						        ViTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						_SCREAMING_SNAKE_CASE		:    int						=   (
						    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
						    if is_torch_available()
						    else {}
						)
						_SCREAMING_SNAKE_CASE		:    str						=   True

						_SCREAMING_SNAKE_CASE		:    int						=   False
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   False
						_SCREAMING_SNAKE_CASE		:    Dict						=   False






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  ViTModelTester(self				)
												lowerCAmelCase__        =  ConfigTester(self       ,  config_class=_UpperCamelCase       ,  has_text_modality=_UpperCamelCase       ,  hidden_size=37				)






						def  UpperCamelCase__ (		self				):






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






						@unittest.skip(reason='ViT does not use inputs_embeds'				)
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs_for_common()

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






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowerCAmelCase__        =  model_class(_UpperCamelCase				)
																		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]       ,  _UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase				)







						@slow
						def  UpperCamelCase__ (		self				):






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







def 		_UpperCamelCase				(      )					->				Optional[int]:

						"""simple docstring"""



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


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






						@cached_property
						def  UpperCamelCase__ (		self				):






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






						@slow
						def  UpperCamelCase__ (		self				):






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

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

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

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

												lowerCAmelCase__        =  torch.tensor([-0.27_44, 0.82_15, -0.08_36]				).to(_UpperCamelCase				)

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






						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  ViTModel.from_pretrained('facebook/dino-vits8'				).to(_UpperCamelCase				)

												lowerCAmelCase__        =  ViTImageProcessor.from_pretrained('facebook/dino-vits8'       ,  size=4_80				)
												lowerCAmelCase__        =  prepare_img()
												lowerCAmelCase__        =  image_processor(images=_UpperCamelCase       ,  return_tensors='pt'				)
												lowerCAmelCase__        =  inputs.pixel_values.to(_UpperCamelCase				)

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

												# verify the logits
												lowerCAmelCase__        =  torch.Size((1, 36_01, 3_84)				)
												self.assertEqual(outputs.last_hidden_state.shape       ,  _UpperCamelCase				)

												lowerCAmelCase__        =  torch.tensor(
												    [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]				).to(_UpperCamelCase				)

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







						@slow
						@require_accelerate
						@require_torch_gpu
						def  UpperCamelCase__ (		self				):






												"""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=_UpperCamelCase       ,  return_tensors='pt'				)
												lowerCAmelCase__        =  inputs.pixel_values.to(_UpperCamelCase				)

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













# 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       __SCREAMING_SNAKE_CASE   (				__lowercase):
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   '''microsoft/speecht5_tts'''
						_SCREAMING_SNAKE_CASE		:    Any						=   (
						    '''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.'''
						)
						_SCREAMING_SNAKE_CASE		:    int						=   '''text_reader'''
						_SCREAMING_SNAKE_CASE		:    List[str]						=   SpeechTaProcessor
						_SCREAMING_SNAKE_CASE		:    Optional[int]						=   SpeechTaForTextToSpeech
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   SpeechTaHifiGan

						_SCREAMING_SNAKE_CASE		:    Optional[int]						=   ['''text''']
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   ['''audio''']






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												if self.post_processor is None:
																		lowerCAmelCase__        =  'microsoft/speecht5_hifigan'
												super().setup()






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






												"""simple docstring"""
												lowerCAmelCase__        =  self.pre_processor(text=_UpperCamelCase       ,  return_tensors='pt'       ,  truncation=_UpperCamelCase				)

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

																		lowerCAmelCase__        =  load_dataset('Matthijs/cmu-arctic-xvectors'       ,  split='validation'				)
																		lowerCAmelCase__        =  torch.tensor(embeddings_dataset[73_05]['xvector']				).unsqueeze(0				)

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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase				):






												"""simple docstring"""
												with torch.no_grad():
																		return self.model.generate_speech(**_UpperCamelCase				)







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase				):






												"""simple docstring"""
												with torch.no_grad():
																		return self.post_processor(_UpperCamelCase				).cpu().detach()





