'''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 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







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

    snake_case__							:   Tuple							=						["text"]
    snake_case__							:   Tuple							=						["audio"]






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         if self.post_processor is None:
              SCREAMING_SNAKE_CASE					:			str    	=       'microsoft/speecht5_hifigan'
         super().setup()






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__=None )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       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.' )

              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       load_dataset('Matthijs/cmu-arctic-xvectors'    ,							split='validation' )
              SCREAMING_SNAKE_CASE					:			str    	=       torch.tensor(embeddings_dataset[7_305]['xvector'] ).unsqueeze(0 )

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






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Union[str, Any]:
         with torch.no_grad():
              return self.model.generate_speech(**lowercase__ )






    def       _UpperCamelCase    (       self    ,							lowercase__ )							-> int:
         with torch.no_grad():
              return self.post_processor(lowercase__ ).cpu().detach()




'''simple docstring'''





from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_lowerCAmelCase  :Union[str, Any]  				=    logging.get_logger(__name__)

_lowerCAmelCase  :int  				=    {
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   List[str]							=						"marian"
    snake_case__							:   Union[str, Any]							=						["past_key_values"]
    snake_case__							:   str							=						{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






    def __init__(       self    ,							lowercase__=58_101    ,							lowercase__=None    ,							lowercase__=1_024    ,							lowercase__=12    ,							lowercase__=4_096    ,							lowercase__=16    ,							lowercase__=12    ,							lowercase__=4_096    ,							lowercase__=16    ,							lowercase__=0.0    ,							lowercase__=0.0    ,							lowercase__=True    ,							lowercase__=True    ,							lowercase__="gelu"    ,							lowercase__=1_024    ,							lowercase__=0.1    ,							lowercase__=0.0    ,							lowercase__=0.0    ,							lowercase__=0.0_2    ,							lowercase__=58_100    ,							lowercase__=False    ,							lowercase__=58_100    ,							lowercase__=0    ,							lowercase__=0    ,							lowercase__=True    ,							**lowercase__    ,							)							-> int:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       vocab_size
         SCREAMING_SNAKE_CASE					:			Dict    	=       decoder_vocab_size or vocab_size
         SCREAMING_SNAKE_CASE					:			int    	=       max_position_embeddings
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       d_model
         SCREAMING_SNAKE_CASE					:			Dict    	=       encoder_ffn_dim
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       encoder_layers
         SCREAMING_SNAKE_CASE					:			Dict    	=       encoder_attention_heads
         SCREAMING_SNAKE_CASE					:			int    	=       decoder_ffn_dim
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       decoder_layers
         SCREAMING_SNAKE_CASE					:			str    	=       decoder_attention_heads
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       dropout
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       attention_dropout
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       activation_dropout
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       activation_function
         SCREAMING_SNAKE_CASE					:			Tuple    	=       init_std
         SCREAMING_SNAKE_CASE					:			int    	=       encoder_layerdrop
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       decoder_layerdrop
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       use_cache
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       encoder_layers
         SCREAMING_SNAKE_CASE					:			int    	=       scale_embedding  # scale factor will be sqrt(d_model) if True
         SCREAMING_SNAKE_CASE					:			str    	=       share_encoder_decoder_embeddings
         super().__init__(
             pad_token_id=lowercase__    ,							eos_token_id=lowercase__    ,							is_encoder_decoder=lowercase__    ,							decoder_start_token_id=lowercase__    ,							forced_eos_token_id=lowercase__    ,							**lowercase__    ,							)






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    @property
    # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
    def       _UpperCamelCase    (       self )							-> Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
              SCREAMING_SNAKE_CASE					:			Dict    	=       OrderedDict(
                  [
                      ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                      ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                  ] )

              if self.use_past:
                   SCREAMING_SNAKE_CASE					:			List[Any]    	=       {0: 'batch'}
                   SCREAMING_SNAKE_CASE					:			Any    	=       {0: 'batch', 1: 'past_decoder_sequence + sequence'}
              else:
                   SCREAMING_SNAKE_CASE					:			str    	=       {0: 'batch', 1: 'decoder_sequence'}
                   SCREAMING_SNAKE_CASE					:			int    	=       {0: 'batch', 1: 'decoder_sequence'}

              if self.use_past:
                   self.fill_with_past_key_values_(lowercase__    ,							direction='inputs' )
         elif self.task == "causal-lm":
              # TODO: figure this case out.
              SCREAMING_SNAKE_CASE					:			List[Any]    	=       OrderedDict(
                  [
                      ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                      ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                  ] )
              if self.use_past:
                   SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			Dict    	=       self.num_layers
                   for i in range(lowercase__ ):
                        SCREAMING_SNAKE_CASE					:			Dict    	=       {0: 'batch', 2: 'past_sequence + sequence'}
                        SCREAMING_SNAKE_CASE					:			Dict    	=       {0: 'batch', 2: 'past_sequence + sequence'}
         else:
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       OrderedDict(
                  [
                      ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                      ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                      ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
                      ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
                  ] )

         return common_inputs






    @property
    # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
    def       _UpperCamelCase    (       self )							-> Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
              SCREAMING_SNAKE_CASE					:			List[str]    	=       super().outputs
         else:
              SCREAMING_SNAKE_CASE					:			Tuple    	=       super(lowercase__    ,							self ).outputs
              if self.use_past:
                   SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.num_layers
                   for i in range(lowercase__ ):
                        SCREAMING_SNAKE_CASE					:			int    	=       {0: 'batch', 2: 'past_sequence + sequence'}
                        SCREAMING_SNAKE_CASE					:			List[Any]    	=       {0: 'batch', 2: 'past_sequence + sequence'}
         return common_outputs






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = -1    ,							lowercase__ = -1    ,							lowercase__ = False    ,							lowercase__ = None    ,							)							-> Mapping[str, Any]:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self._generate_dummy_inputs_for_encoder_and_decoder(
             lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )

         # Generate decoder inputs
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       seq_length if not self.use_past else 1
         SCREAMING_SNAKE_CASE					:			str    	=       self._generate_dummy_inputs_for_encoder_and_decoder(
             lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       dict(**lowercase__    ,							**lowercase__ )

         if self.use_past:
              if not is_torch_available():
                   raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
              else:
                   import torch
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			int    	=       common_inputs['input_ids'].shape
              SCREAMING_SNAKE_CASE					:			Tuple    	=       common_inputs['decoder_input_ids'].shape[1]
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			str    	=       self.num_attention_heads
              SCREAMING_SNAKE_CASE					:			int    	=       (
                  batch,
                  num_encoder_attention_heads,
                  encoder_seq_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )
              SCREAMING_SNAKE_CASE					:			str    	=       decoder_seq_length + 3
              SCREAMING_SNAKE_CASE					:			Tuple    	=       (
                  batch,
                  num_decoder_attention_heads,
                  decoder_past_length,
                  self._config.hidden_size // num_decoder_attention_heads,
              )

              SCREAMING_SNAKE_CASE					:			List[str]    	=       torch.cat(
                  [common_inputs['decoder_attention_mask'], torch.ones(lowercase__    ,							lowercase__ )]    ,							dim=1 )

              SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       []
              # If the number of encoder and decoder layers are present in the model configuration, both are considered
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			Dict    	=       self.num_layers
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       min(lowercase__    ,							lowercase__ )
              SCREAMING_SNAKE_CASE					:			Tuple    	=       max(lowercase__    ,							lowercase__ ) - min_num_layers
              SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

              for _ in range(lowercase__ ):
                   common_inputs["past_key_values"].append(
                       (
                           torch.zeros(lowercase__ ),
                           torch.zeros(lowercase__ ),
                           torch.zeros(lowercase__ ),
                           torch.zeros(lowercase__ ),
                       ) )
              # TODO: test this.
              SCREAMING_SNAKE_CASE					:			str    	=       encoder_shape if remaining_side_name == 'encoder' else decoder_shape
              for _ in range(lowercase__    ,							lowercase__ ):
                   common_inputs["past_key_values"].append((torch.zeros(lowercase__ ), torch.zeros(lowercase__ )) )
         return common_inputs






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = -1    ,							lowercase__ = -1    ,							lowercase__ = False    ,							lowercase__ = None    ,							)							-> Mapping[str, Any]:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self._generate_dummy_inputs_for_encoder_and_decoder(
             lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )

         if self.use_past:
              if not is_torch_available():
                   raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
              else:
                   import torch
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			Dict    	=       common_inputs['input_ids'].shape
              # Not using the same length for past_key_values
              SCREAMING_SNAKE_CASE					:			Dict    	=       seqlen + 2
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			str    	=       self.num_layers
              SCREAMING_SNAKE_CASE	,   SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.num_attention_heads
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       (
                  batch,
                  num_encoder_attention_heads,
                  past_key_values_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )

              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       common_inputs['attention_mask'].dtype
              SCREAMING_SNAKE_CASE					:			List[str]    	=       torch.cat(
                  [common_inputs['attention_mask'], torch.ones(lowercase__    ,							lowercase__    ,							dtype=lowercase__ )]    ,							dim=1 )
              SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       [
                  (torch.zeros(lowercase__ ), torch.zeros(lowercase__ )) for _ in range(lowercase__ )
              ]
         return common_inputs






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = -1    ,							lowercase__ = -1    ,							lowercase__ = False    ,							lowercase__ = None    ,							)							-> Mapping[str, Any]:
         # Copied from OnnxConfig.generate_dummy_inputs
         # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
         # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
         SCREAMING_SNAKE_CASE					:			List[str]    	=       compute_effective_axis_dimension(
             lowercase__    ,							fixed_dimension=OnnxConfig.default_fixed_batch    ,							num_token_to_add=0 )

         # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
         SCREAMING_SNAKE_CASE					:			List[str]    	=       tokenizer.num_special_tokens_to_add(lowercase__ )
         SCREAMING_SNAKE_CASE					:			Dict    	=       compute_effective_axis_dimension(
             lowercase__    ,							fixed_dimension=OnnxConfig.default_fixed_sequence    ,							num_token_to_add=lowercase__ )

         # Generate dummy inputs according to compute batch and sequence
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       [' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
         SCREAMING_SNAKE_CASE					:			Tuple    	=       dict(tokenizer(lowercase__    ,							return_tensors=lowercase__ ) )
         return common_inputs






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__ = -1    ,							lowercase__ = -1    ,							lowercase__ = False    ,							lowercase__ = None    ,							)							-> Mapping[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
              SCREAMING_SNAKE_CASE					:			List[Any]    	=       self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                  lowercase__    ,							batch_size=lowercase__    ,							seq_length=lowercase__    ,							is_pair=lowercase__    ,							framework=lowercase__ )

         else:
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self._generate_dummy_inputs_for_causal_lm(
                  lowercase__    ,							batch_size=lowercase__    ,							seq_length=lowercase__    ,							is_pair=lowercase__    ,							framework=lowercase__ )

         return common_inputs






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> int:
         if self.task in ["default", "seq2seq-lm"]:
              SCREAMING_SNAKE_CASE					:			List[Any]    	=       super()._flatten_past_key_values_(lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )
         else:
              SCREAMING_SNAKE_CASE					:			Any    	=       super(lowercase__    ,							self )._flatten_past_key_values_(
                  lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )






    @property
    def       _UpperCamelCase    (       self )							-> float:
         return 1E-4



