
'''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  __A				( _SCREAMING_SNAKE_CASE			):
 lowerCamelCase				       =RobertaConfig
 lowerCamelCase				       ='''roberta'''
 def __init__(   self			:    List[Any]				,   lowerCamelCase			:    Union[str, Any]       ):



   """simple docstring"""





   super().__init__(lowerCamelCase       )

   __A		:     str												=		RobertaEmbeddings(lowerCamelCase       )
   self.init_weights()







@add_start_docstrings(
    '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. '''    ,						_SCREAMING_SNAKE_CASE    ,						)
class  __A				( _SCREAMING_SNAKE_CASE			):
 lowerCamelCase				       =RobertaConfig
 lowerCamelCase				       ='''roberta'''
 def __init__(   self			:    Optional[Any]				,   lowerCamelCase			:    str       ):



   """simple docstring"""





   super().__init__(lowerCamelCase       )
   __A		:     Union[str, Any]												=		config.num_labels
   __A		:     List[Any]												=		config.num_hidden_layers

   __A		:     Optional[Any]												=		DeeRobertaModel(lowerCamelCase       )
   __A		:     str												=		nn.Dropout(config.hidden_dropout_prob       )
   __A		:     Tuple												=		nn.Linear(config.hidden_size				,   self.config.num_labels       )






 @add_start_docstrings_to_model_forward(lowerCamelCase       )
 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    List[Any]=None				,   lowerCamelCase			:    List[Any]=None				,   lowerCamelCase			:    List[Any]=None				,   lowerCamelCase			:    Any=None				,   lowerCamelCase			:    str=None				,   lowerCamelCase			:    List[Any]=None				,   lowerCamelCase			:    Tuple=None				,   lowerCamelCase			:    str=-1				,   lowerCamelCase			:    Dict=False				,   ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		self.num_layers
   try:
     __A		:     int												=		self.roberta(
         lowerCamelCase				,   attention_mask=lowerCamelCase				,   token_type_ids=lowerCamelCase				,   position_ids=lowerCamelCase				,   head_mask=lowerCamelCase				,   inputs_embeds=lowerCamelCase				,   )

     __A		:     Tuple												=		outputs[1]

     __A		:     Union[str, Any]												=		self.dropout(lowerCamelCase       )
     __A		:     Optional[int]												=		self.classifier(lowerCamelCase       )
     __A		:     List[Any]												=		(logits,) + outputs[2:]  # add hidden states and attention if they are here
   except HighwayException as e:
     __A		:     Optional[int]												=		e.message
     __A		:     Optional[Any]												=		e.exit_layer
     __A		:     List[str]												=		outputs[0]

   if not self.training:
     __A		:     Optional[Any]												=		entropy(lowerCamelCase       )
     __A		:     Tuple												=		[]
     __A		:     List[str]												=		[]
   if labels is not None:
     if self.num_labels == 1:
       #  We are doing regression
       __A		:     str												=		MSELoss()
       __A		:     Optional[Any]												=		loss_fct(logits.view(-1       )				,   labels.view(-1       )       )
     else:
       __A		:     Dict												=		CrossEntropyLoss()
       __A		:     Tuple												=		loss_fct(logits.view(-1				,   self.num_labels       )				,   labels.view(-1       )       )

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

     if train_highway:
       __A		:     Optional[Any]												=		(sum(highway_losses[:-1]       ),) + outputs
       # exclude the final highway, of course
     else:
       __A		:     str												=		(loss,) + outputs
   if not self.training:
     __A		:     Tuple												=		outputs + ((original_entropy, highway_entropy), exit_layer)
     if output_layer >= 0:
       __A		:     Any												=		(
           (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'''
from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


A__   :		Dict 		=logging.get_logger(__name__)







class  __A				( _SCREAMING_SNAKE_CASE			):
 lowerCamelCase				       =['''input_values''', '''padding_mask''']
 def __init__(   self			:    Optional[int]				,   lowerCamelCase			:    int = 1				,   lowerCamelCase			:    int = 2_40_00				,   lowerCamelCase			:    float = 0.0				,   lowerCamelCase			:    float = None				,   lowerCamelCase			:    float = None				,   **lowerCamelCase			:    Dict				,   ):



   """simple docstring"""





   super().__init__(feature_size=lowerCamelCase				,   sampling_rate=lowerCamelCase				,   padding_value=lowerCamelCase				,   **lowerCamelCase       )
   __A		:     str												=		chunk_length_s
   __A		:     str												=		overlap
 @property
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   if self.chunk_length_s is None:
     return None
   else:
     return int(self.chunk_length_s * self.sampling_rate       )
 @property
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   if self.chunk_length_s is None or self.overlap is None:
     return None
   else:
     return max(1				,   int((1.0 - self.overlap) * self.chunk_length       )       )






 def __call__(   self			:    List[str]				,   lowerCamelCase			:    Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]				,   lowerCamelCase			:    Optional[Union[bool, str, PaddingStrategy]] = None				,   lowerCamelCase			:    Optional[bool] = False				,   lowerCamelCase			:    Optional[int] = None				,   lowerCamelCase			:    Optional[Union[str, TensorType]] = None				,   lowerCamelCase			:    Optional[int] = None				,   ):



   """simple docstring"""





   if sampling_rate is not None:
     if sampling_rate != self.sampling_rate:
       raise ValueError(
           f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
           f" {self.sampling_rate}. Please make sure that the provided audio input was sampled with"
           f" {self.sampling_rate} and not {sampling_rate}."       )
   else:
     logger.warning(
         """It is strongly recommended to pass the `sampling_rate` argument to this function. """
         """Failing to do so can result in silent errors that might be hard to debug."""       )

   if padding and truncation:
     raise ValueError("""Both padding and truncation were set. Make sure you only set one."""       )
   elif padding is None:
     # by default let's pad the inputs
     __A		:     List[Any]												=		True

   __A		:     List[str]												=		bool(
       isinstance(lowerCamelCase				,   (list, tuple)       ) and (isinstance(raw_audio[0]				,   (np.ndarray, tuple, list)       ))       )

   if is_batched:
     __A		:     Optional[Any]												=		[np.asarray(lowerCamelCase				,   dtype=np.floataa       ).T for audio in raw_audio]
   elif not is_batched and not isinstance(lowerCamelCase				,   np.ndarray       ):
     __A		:     Union[str, Any]												=		np.asarray(lowerCamelCase				,   dtype=np.floataa       )
   elif isinstance(lowerCamelCase				,   np.ndarray       ) and raw_audio.dtype is np.dtype(np.floataa       ):
     __A		:     Optional[Any]												=		raw_audio.astype(np.floataa       )

   # always return batch
   if not is_batched:
     __A		:     Tuple												=		[np.asarray(lowerCamelCase       ).T]

   # verify inputs are valid
   for idx, example in enumerate(lowerCamelCase       ):
     if example.ndim > 2:
       raise ValueError(f"Expected input shape (channels, length) but got shape {example.shape}"       )
     if self.feature_size == 1 and example.ndim != 1:
       raise ValueError(f"Expected mono audio but example has {example.shape[-1]} channels"       )
     if self.feature_size == 2 and example.shape[-1] != 2:
       raise ValueError(f"Expected stereo audio but example has {example.shape[-1]} channels"       )

   __A		:     Optional[Any]												=		None
   __A		:     Dict												=		BatchFeature({"""input_values""": raw_audio}       )
   if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
     if truncation:
       __A		:     Optional[Any]												=		min(array.shape[0] for array in raw_audio       )
       __A		:     Dict												=		int(np.floor(max_length / self.chunk_stride       )       )
       __A		:     Union[str, Any]												=		(nb_step - 1) * self.chunk_stride + self.chunk_length
     elif padding:
       __A		:     int												=		max(array.shape[0] for array in raw_audio       )
       __A		:     Optional[Any]												=		int(np.ceil(max_length / self.chunk_stride       )       )
       __A		:     Any												=		(nb_step - 1) * self.chunk_stride + self.chunk_length
       __A		:     Optional[int]												=		"""max_length"""
     else:
       __A		:     Optional[Any]												=		input_values

        # normal padding on batch
   if padded_inputs is None:
     __A		:     Tuple												=		self.pad(
         lowerCamelCase				,   max_length=lowerCamelCase				,   truncation=lowerCamelCase				,   padding=lowerCamelCase				,   return_attention_mask=lowerCamelCase				,   )
     if padding:
       __A		:     List[Any]												=		padded_inputs.pop("""attention_mask"""       )

   __A		:     List[Any]												=		[]
   for example in padded_inputs.pop("""input_values"""       ):
     if self.feature_size == 1:
       __A		:     Optional[Any]												=		example[..., None]
     input_values.append(example.T       )

   __A		:     str												=		input_values
   if return_tensors is not None:
     __A		:     Any												=		padded_inputs.convert_to_tensors(lowerCamelCase       )

   return padded_inputs



