






'''simple docstring'''


from __future__ import absolute_import, division, print_function, unicode_literals

from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers import RobertaConfig
from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.roberta.modeling_roberta import (
    ROBERTA_INPUTS_DOCSTRING,
    ROBERTA_START_DOCSTRING,
    RobertaEmbeddings,
)

from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy
@add_start_docstrings(
    "The RoBERTa Model transformer with early exiting (DeeRoBERTa). "       ,SCREAMING_SNAKE_CASE       ,)
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							List[Any]					     =					RobertaConfig
			__a							:							Union[str, Any]					     =					"roberta"






			def __init__(   self							:						Union[str, Any] ,      lowercase							:						List[str]			)							->    Optional[Any]:

									'''simple docstring'''







									super().__init__(lowercase			)

									UpperCamelCase__         = RobertaEmbeddings(lowercase			)
									self.init_weights()
@add_start_docstrings(
    "RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n    also takes care of multi-layer training. "       ,SCREAMING_SNAKE_CASE       ,)
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							Dict					     =					RobertaConfig
			__a							:							Tuple					     =					"roberta"






			def __init__(   self							:						Union[str, Any] ,      lowercase							:						Optional[Any]			)							->    List[str]:

									'''simple docstring'''







									super().__init__(lowercase			)
									UpperCamelCase__         = config.num_labels
									UpperCamelCase__         = config.num_hidden_layers

									UpperCamelCase__         = DeeRobertaModel(lowercase			)
									UpperCamelCase__         = nn.Dropout(config.hidden_dropout_prob			)
									UpperCamelCase__         = nn.Linear(config.hidden_size ,      self.config.num_labels			)







			@add_start_docstrings_to_model_forward(lowercase			)
			def 				A      (   self							:						Optional[Any] ,      lowercase							:						str=None ,      lowercase							:						Tuple=None ,      lowercase							:						List[str]=None ,      lowercase							:						Union[str, Any]=None ,      lowercase							:						int=None ,      lowercase							:						Dict=None ,      lowercase							:						Optional[int]=None ,      lowercase							:						Optional[Any]=-1 ,      lowercase							:						Optional[Any]=False ,      )							->    Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__         = self.num_layers
									try:
															UpperCamelCase__         = self.roberta(
															    lowercase ,      attention_mask=lowercase ,      token_type_ids=lowercase ,      position_ids=lowercase ,      head_mask=lowercase ,      inputs_embeds=lowercase ,      )

															UpperCamelCase__         = outputs[1]

															UpperCamelCase__         = self.dropout(lowercase			)
															UpperCamelCase__         = self.classifier(lowercase			)
															UpperCamelCase__         = (logits,) + outputs[2:]  # add hidden states and attention if they are here
									except HighwayException as e:
															UpperCamelCase__         = e.message
															UpperCamelCase__         = e.exit_layer
															UpperCamelCase__         = outputs[0]

									if not self.training:
															UpperCamelCase__         = entropy(lowercase			)
															UpperCamelCase__         = []
															UpperCamelCase__         = []
									if labels is not None:
															if self.num_labels == 1:
																					#  We are doing regression
																					UpperCamelCase__         = MSELoss()
																					UpperCamelCase__         = loss_fct(logits.view(-1			) ,      labels.view(-1			)			)
															else:
																					UpperCamelCase__         = CrossEntropyLoss()
																					UpperCamelCase__         = loss_fct(logits.view(-1 ,      self.num_labels			) ,      labels.view(-1			)			)

															# work with highway exits
															UpperCamelCase__         = []
															for highway_exit in outputs[-1]:
																					UpperCamelCase__         = highway_exit[0]
																					if not self.training:
																											highway_logits_all.append(lowercase			)
																											highway_entropy.append(highway_exit[2]			)
																					if self.num_labels == 1:
																											#  We are doing regression
																											UpperCamelCase__         = MSELoss()
																											UpperCamelCase__         = loss_fct(highway_logits.view(-1			) ,      labels.view(-1			)			)
																					else:
																											UpperCamelCase__         = CrossEntropyLoss()
																											UpperCamelCase__         = loss_fct(highway_logits.view(-1 ,      self.num_labels			) ,      labels.view(-1			)			)
																					highway_losses.append(lowercase			)

															if train_highway:
																					UpperCamelCase__         = (sum(highway_losses[:-1]			),) + outputs
																					# exclude the final highway, of course
															else:
																					UpperCamelCase__         = (loss,) + outputs
									if not self.training:
															UpperCamelCase__         = outputs + ((original_entropy, highway_entropy), exit_layer)
															if output_layer >= 0:
																					UpperCamelCase__         = (
																					    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
																					)  # use the highway of the last layer

									return outputs  # (loss), logits, (hidden_states), (attentions), entropy








'''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.
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							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							int					     =					"microsoft/speecht5_tts"
			__a							:							int					     =					(
			    "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."
			)
			__a							:							Optional[Any]					     =					"text_reader"
			__a							:							Tuple					     =					SpeechTaProcessor
			__a							:							int					     =					SpeechTaForTextToSpeech
			__a							:							int					     =					SpeechTaHifiGan

			__a							:							Optional[int]					     =					["text"]
			__a							:							Union[str, Any]					     =					["audio"]






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

									'''simple docstring'''







									if self.post_processor is None:
															UpperCamelCase__         = """microsoft/speecht5_hifigan"""
									super().setup()






			def 				A      (   self							:						List[Any] ,      lowercase							:						Any ,      lowercase							:						Union[str, Any]=None			)							->    Tuple:

									'''simple docstring'''







									UpperCamelCase__         = self.pre_processor(text=lowercase ,      return_tensors="""pt""" ,      truncation=lowercase			)

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

															UpperCamelCase__         = load_dataset("""Matthijs/cmu-arctic-xvectors""" ,      split="""validation"""			)
															UpperCamelCase__         = torch.tensor(embeddings_dataset[7_3_0_5]["""xvector"""]			).unsqueeze(0			)

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






			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						Optional[int]			)							->    int:

									'''simple docstring'''







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







			def 				A      (   self							:						str ,      lowercase							:						Union[str, Any]			)							->    Tuple:

									'''simple docstring'''







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