





import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
     import torch

     from transformers import (
         SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
         SqueezeBertModel,
     )



class   _SCREAMING_SNAKE_CASE		(      __SCREAMING_SNAKE_CASE       ):







 '''simple docstring'''

 def __init__(self							:  Any			,					UpperCAmelCase_							:  Tuple			,					UpperCAmelCase_							:  List[Any]=13			,					UpperCAmelCase_							:  Any=7			,					UpperCAmelCase_							:  str=True			,					UpperCAmelCase_							:  List[str]=True			,					UpperCAmelCase_							:  Union[str, Any]=False			,					UpperCAmelCase_							:  Dict=True			,					UpperCAmelCase_							:  Union[str, Any]=99			,					UpperCAmelCase_							:  Union[str, Any]=32			,					UpperCAmelCase_							:  Any=5			,					UpperCAmelCase_							:  Optional[Any]=4			,					UpperCAmelCase_							:  int=64			,					UpperCAmelCase_							:  Optional[Any]="gelu"			,					UpperCAmelCase_							:  Union[str, Any]=0.1			,					UpperCAmelCase_							:  Union[str, Any]=0.1			,					UpperCAmelCase_							:  Any=512			,					UpperCAmelCase_							:  Optional[int]=16			,					UpperCAmelCase_							:  Tuple=2			,					UpperCAmelCase_							:  Union[str, Any]=0.02			,					UpperCAmelCase_							:  Dict=3			,					UpperCAmelCase_							:  Dict=4			,					UpperCAmelCase_							:  List[Any]=None			,					UpperCAmelCase_							:  Dict=2			,					UpperCAmelCase_							:  Tuple=2			,					UpperCAmelCase_							:  Union[str, Any]=2			,					UpperCAmelCase_							:  Dict=2			,					UpperCAmelCase_							:  Dict=4			,					UpperCAmelCase_							:  Tuple=1			,					)			->Any:



   '''simple docstring'''



   lowerCamelCase__: Optional[Any]         =parent
   lowerCamelCase__: Optional[int]         =batch_size
   lowerCamelCase__: Dict         =seq_length
   lowerCamelCase__: Union[str, Any]         =is_training
   lowerCamelCase__: int         =use_input_mask
   lowerCamelCase__: int         =use_token_type_ids
   lowerCamelCase__: Dict         =use_labels
   lowerCamelCase__: List[Any]         =vocab_size
   lowerCamelCase__: Tuple         =hidden_size
   lowerCamelCase__: List[Any]         =num_hidden_layers
   lowerCamelCase__: str         =num_attention_heads
   lowerCamelCase__: List[Any]         =intermediate_size
   lowerCamelCase__: Optional[int]         =hidden_act
   lowerCamelCase__: List[Any]         =hidden_dropout_prob
   lowerCamelCase__: Optional[int]         =attention_probs_dropout_prob
   lowerCamelCase__: List[Any]         =max_position_embeddings
   lowerCamelCase__: List[str]         =type_vocab_size
   lowerCamelCase__: Dict         =type_sequence_label_size
   lowerCamelCase__: int         =initializer_range
   lowerCamelCase__: Any         =num_labels
   lowerCamelCase__: List[Any]         =num_choices
   lowerCamelCase__: Optional[Any]         =scope
   lowerCamelCase__: str         =q_groups
   lowerCamelCase__: Optional[int]         =k_groups
   lowerCamelCase__: List[Any]         =v_groups
   lowerCamelCase__: Tuple         =post_attention_groups
   lowerCamelCase__: Tuple         =intermediate_groups
   lowerCamelCase__: str         =output_groups

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



   '''simple docstring'''



   lowerCamelCase__: Optional[Any]         =ids_tensor([self.batch_size, self.seq_length]			,					self.vocab_size)

   lowerCamelCase__: str         =None
   if self.use_input_mask:
     lowerCamelCase__: Optional[int]         =random_attention_mask([self.batch_size, self.seq_length])

   lowerCamelCase__: int         =None
   lowerCamelCase__: Optional[int]         =None
   lowerCamelCase__: Optional[int]         =None
   if self.use_labels:
     lowerCamelCase__: Optional[int]         =ids_tensor([self.batch_size]			,					self.type_sequence_label_size)
     lowerCamelCase__: Optional[int]         =ids_tensor([self.batch_size, self.seq_length]			,					self.num_labels)
     lowerCamelCase__: Any         =ids_tensor([self.batch_size]			,					self.num_choices)

   lowerCamelCase__: List[str]         =self.get_config()

   return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

 def 				SCREAMING_SNAKE_CASE_   (self							:  Tuple)			->Dict:



   '''simple docstring'''



   return SqueezeBertConfig(
       embedding_size=self.hidden_size			,					vocab_size=self.vocab_size			,					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			,					attention_probs_dropout_prob=self.hidden_dropout_prob			,					attention_dropout=self.attention_probs_dropout_prob			,					max_position_embeddings=self.max_position_embeddings			,					initializer_range=self.initializer_range			,					q_groups=self.q_groups			,					k_groups=self.k_groups			,					v_groups=self.v_groups			,					post_attention_groups=self.post_attention_groups			,					intermediate_groups=self.intermediate_groups			,					output_groups=self.output_groups			,					)

 def 				SCREAMING_SNAKE_CASE_   (self							:  Any			,					UpperCAmelCase_							:  Optional[int]			,					UpperCAmelCase_							:  Optional[int]			,					UpperCAmelCase_							:  Union[str, Any]			,					UpperCAmelCase_							:  int			,					UpperCAmelCase_							:  Optional[Any]			,					UpperCAmelCase_							:  List[str])			->str:



   '''simple docstring'''



   lowerCamelCase__: Dict         =SqueezeBertModel(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__: Optional[Any]         =model(UpperCAmelCase_			,					UpperCAmelCase_)
   lowerCamelCase__: Optional[Any]         =model(UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape			,					(self.batch_size, self.seq_length, self.hidden_size))

 def 				SCREAMING_SNAKE_CASE_   (self							:  List[Any]			,					UpperCAmelCase_							:  str			,					UpperCAmelCase_							:  List[str]			,					UpperCAmelCase_							:  Optional[Any]			,					UpperCAmelCase_							:  List[Any]			,					UpperCAmelCase_							:  Tuple			,					UpperCAmelCase_							:  str)			->Dict:



   '''simple docstring'''



   lowerCamelCase__: Optional[int]         =SqueezeBertForMaskedLM(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__: Union[str, Any]         =model(UpperCAmelCase_			,					attention_mask=UpperCAmelCase_			,					labels=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.vocab_size))

 def 				SCREAMING_SNAKE_CASE_   (self							:  str			,					UpperCAmelCase_							:  int			,					UpperCAmelCase_							:  Optional[int]			,					UpperCAmelCase_							:  List[str]			,					UpperCAmelCase_							:  List[str]			,					UpperCAmelCase_							:  Tuple			,					UpperCAmelCase_							:  List[str])			->Tuple:



   '''simple docstring'''



   lowerCamelCase__: Tuple         =SqueezeBertForQuestionAnswering(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__: Tuple         =model(
       UpperCAmelCase_			,					attention_mask=UpperCAmelCase_			,					start_positions=UpperCAmelCase_			,					end_positions=UpperCAmelCase_)
   self.parent.assertEqual(result.start_logits.shape			,					(self.batch_size, self.seq_length))
   self.parent.assertEqual(result.end_logits.shape			,					(self.batch_size, self.seq_length))

 def 				SCREAMING_SNAKE_CASE_   (self							:  str			,					UpperCAmelCase_							:  Any			,					UpperCAmelCase_							:  Union[str, Any]			,					UpperCAmelCase_							:  Optional[int]			,					UpperCAmelCase_							:  List[str]			,					UpperCAmelCase_							:  List[str]			,					UpperCAmelCase_							:  List[str])			->str:



   '''simple docstring'''



   lowerCamelCase__: Optional[int]         =self.num_labels
   lowerCamelCase__: Any         =SqueezeBertForSequenceClassification(UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__: List[str]         =model(UpperCAmelCase_			,					attention_mask=UpperCAmelCase_			,					labels=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels))

 def 				SCREAMING_SNAKE_CASE_   (self							:  int			,					UpperCAmelCase_							:  int			,					UpperCAmelCase_							:  Union[str, Any]			,					UpperCAmelCase_							:  Optional[int]			,					UpperCAmelCase_							:  Any			,					UpperCAmelCase_							:  int			,					UpperCAmelCase_							:  Dict)			->Any:



   '''simple docstring'''



   lowerCamelCase__: Optional[Any]         =self.num_labels
   lowerCamelCase__: List[Any]         =SqueezeBertForTokenClassification(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()

   lowerCamelCase__: Any         =model(UpperCAmelCase_			,					attention_mask=UpperCAmelCase_			,					labels=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.num_labels))

 def 				SCREAMING_SNAKE_CASE_   (self							:  Tuple			,					UpperCAmelCase_							:  str			,					UpperCAmelCase_							:  Dict			,					UpperCAmelCase_							:  str			,					UpperCAmelCase_							:  Union[str, Any]			,					UpperCAmelCase_							:  str			,					UpperCAmelCase_							:  int)			->Tuple:



   '''simple docstring'''



   lowerCamelCase__: List[Any]         =self.num_choices
   lowerCamelCase__: List[str]         =SqueezeBertForMultipleChoice(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__: Optional[int]         =input_ids.unsqueeze(1).expand(-1			,					self.num_choices			,					-1).contiguous()
   lowerCamelCase__: int         =input_mask.unsqueeze(1).expand(-1			,					self.num_choices			,					-1).contiguous()
   lowerCamelCase__: List[str]         =model(
       UpperCAmelCase_			,					attention_mask=UpperCAmelCase_			,					labels=UpperCAmelCase_			,					)
   self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_choices))







 def 				SCREAMING_SNAKE_CASE_   (self							:  int)			->Tuple:



   '''simple docstring'''



   lowerCamelCase__: Dict         =self.prepare_config_and_inputs()
   ((lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)): List[str]         =config_and_inputs
   lowerCamelCase__: str         ={"input_ids": input_ids, "attention_mask": input_mask}
   return config, inputs_dict



@require_torch
class   _SCREAMING_SNAKE_CASE		(      __SCREAMING_SNAKE_CASE       ,				__SCREAMING_SNAKE_CASE       ,				unittest.TestCase       ):







 '''simple docstring'''
 lowercase_						=     (
     (
         SqueezeBertModel,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 lowercase_						=     (
     {
         "feature-extraction": SqueezeBertModel,
         "fill-mask": SqueezeBertForMaskedLM,
         "question-answering": SqueezeBertForQuestionAnswering,
         "text-classification": SqueezeBertForSequenceClassification,
         "token-classification": SqueezeBertForTokenClassification,
         "zero-shot": SqueezeBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase_						=     False
 lowercase_						=     True
 lowercase_						=     False

 def 				SCREAMING_SNAKE_CASE_   (self							:  Optional[Any])			->Dict:



   '''simple docstring'''



   lowerCamelCase__: int         =SqueezeBertModelTester(self)
   lowerCamelCase__: List[str]         =ConfigTester(self			,					config_class=UpperCAmelCase_			,					dim=37)

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



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 				SCREAMING_SNAKE_CASE_   (self							:  Any)			->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__: int         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_model(*UpperCAmelCase_)

 def 				SCREAMING_SNAKE_CASE_   (self							:  Optional[int])			->str:



   '''simple docstring'''



   lowerCamelCase__: Tuple         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCAmelCase_)

 def 				SCREAMING_SNAKE_CASE_   (self							:  Any)			->Any:



   '''simple docstring'''



   lowerCamelCase__: List[Any]         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCAmelCase_)

 def 				SCREAMING_SNAKE_CASE_   (self							:  Tuple)			->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__: List[str]         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCAmelCase_)

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



   '''simple docstring'''



   lowerCamelCase__: List[str]         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCAmelCase_)

 def 				SCREAMING_SNAKE_CASE_   (self							:  Optional[int])			->str:



   '''simple docstring'''



   lowerCamelCase__: str         =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCAmelCase_)







 @slow
 def 				SCREAMING_SNAKE_CASE_   (self							:  Tuple)			->List[Any]:



   '''simple docstring'''



   for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowerCamelCase__: List[Any]         =SqueezeBertModel.from_pretrained(UpperCAmelCase_)
     self.assertIsNotNone(UpperCAmelCase_)



@require_sentencepiece
@require_tokenizers
@require_torch
class   _SCREAMING_SNAKE_CASE		(      unittest.TestCase       ):







 '''simple docstring'''

 @slow
 def 				SCREAMING_SNAKE_CASE_   (self							:  int)			->List[str]:



   '''simple docstring'''



   lowerCamelCase__: Optional[Any]         =SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli")

   lowerCamelCase__: Optional[Any]         =torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]])
   lowerCamelCase__: List[str]         =model(UpperCAmelCase_)[0]
   lowerCamelCase__: List[Any]         =torch.Size((1, 3))
   self.assertEqual(output.shape			,					UpperCAmelCase_)
   lowerCamelCase__: Optional[Any]         =torch.tensor([[0.6401, -0.0349, -0.6041]])
   self.assertTrue(torch.allclose(UpperCAmelCase_			,					UpperCAmelCase_			,					atol=1E-4))





# 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()





