


'''simple docstring'''





from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


__a     =			logging.get_logger(__name__)






class   __lowercase				(  __snake_case  ):





      def __init__(		self						:					Union[str, Any]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					float					,       **__lowerCamelCase						:					Optional[int]       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     feature_size
         UpperCAmelCase							=     sampling_rate
         UpperCAmelCase							=     padding_value

         UpperCAmelCase							=     kwargs.pop("""padding_side"""					,       """right"""       )
         UpperCAmelCase							=     kwargs.pop("""return_attention_mask"""					,       __lowerCamelCase       )

         super().__init__(**__lowerCamelCase       )





      def        _lowercase				(		self						:					str					,       __lowerCamelCase						:					Union[
              BatchFeature,
              List[BatchFeature],
              Dict[str, BatchFeature],
              Dict[str, List[BatchFeature]],
              List[Dict[str, BatchFeature]],
          ]					,       __lowerCamelCase						:					Union[bool, str, PaddingStrategy] = True					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					bool = False					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[bool] = None					,       __lowerCamelCase						:					Optional[Union[str, TensorType]] = None					,       )    ->   BatchFeature:






         """simple docstring"""







         if isinstance(__lowerCamelCase					,       (list, tuple)       ) and isinstance(processed_features[0]					,       (dict, BatchFeature)       ):
            UpperCAmelCase							=     {
                key: [example[key] for example in processed_features] for key in processed_features[0].keys()
            }

         # The model's main input name, usually `input_values`, has be passed for padding
         if self.model_input_names[0] not in processed_features:
            raise ValueError(
                """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
                F""" to this method that includes {self.model_input_names[0]}, but you provided"""
                F""" {list(processed_features.keys()       )}"""       )

         UpperCAmelCase							=     processed_features[self.model_input_names[0]]
         UpperCAmelCase							=     (
             return_attention_mask if return_attention_mask is not None else self.return_attention_mask
         )

         if len(__lowerCamelCase       ) == 0:
            if return_attention_mask:
               UpperCAmelCase							=     []
            return processed_features

         # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
         # and rebuild them afterwards if no return_tensors is specified
         # Note that we lose the specific device the tensor may be on for PyTorch

         UpperCAmelCase							=     required_input[0]
         if isinstance(__lowerCamelCase					,       (list, tuple)       ):
            # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
            UpperCAmelCase							=     0
            while len(required_input[index]       ) == 0:
               index += 1
            if index < len(__lowerCamelCase       ):
               UpperCAmelCase							=     required_input[index][0]

         if return_tensors is None:
            if is_tf_tensor(__lowerCamelCase       ):
               UpperCAmelCase							=     """tf"""
            elif is_torch_tensor(__lowerCamelCase       ):
               UpperCAmelCase							=     """pt"""
            elif isinstance(__lowerCamelCase					,       (int, float, list, tuple, np.ndarray)       ):
               UpperCAmelCase							=     """np"""
            else:
               raise ValueError(
                   F"""type of {first_element} unknown: {type(__lowerCamelCase       )}. """
                   """Should be one of a python, numpy, pytorch or tensorflow object."""       )

         for key, value in processed_features.items():
            if isinstance(value[0]					,       (int, float)       ):
               UpperCAmelCase							=     to_numpy(__lowerCamelCase       )
            else:
               UpperCAmelCase							=     [to_numpy(__lowerCamelCase       ) for v in value]

        # Convert padding_strategy in PaddingStrategy
         UpperCAmelCase							=     self._get_padding_strategies(padding=__lowerCamelCase					,       max_length=__lowerCamelCase       )

         UpperCAmelCase							=     processed_features[self.model_input_names[0]]

         UpperCAmelCase							=     len(__lowerCamelCase       )
         if not all(len(__lowerCamelCase       ) == batch_size for v in processed_features.values()       ):
            raise ValueError("""Some items in the output dictionary have a different batch size than others."""       )

         UpperCAmelCase							=     []
         for i in range(__lowerCamelCase       ):
            UpperCAmelCase							=     {k: v[i] for k, v in processed_features.items()}
            # truncation
            UpperCAmelCase							=     self._truncate(
                __lowerCamelCase					,       max_length=__lowerCamelCase					,       pad_to_multiple_of=__lowerCamelCase					,       truncation=__lowerCamelCase					,       )
            truncated_inputs.append(__lowerCamelCase       )

         if padding_strategy == PaddingStrategy.LONGEST:
            # make sure that `max_length` cannot be longer than the longest truncated length
            UpperCAmelCase							=     max(len(input_slice[self.model_input_names[0]]       ) for input_slice in truncated_inputs       )
            UpperCAmelCase							=     PaddingStrategy.MAX_LENGTH

         UpperCAmelCase							=     {}
         for i in range(__lowerCamelCase       ):
            # padding
            UpperCAmelCase							=     self._pad(
                truncated_inputs[i]					,       max_length=__lowerCamelCase					,       padding_strategy=__lowerCamelCase					,       pad_to_multiple_of=__lowerCamelCase					,       return_attention_mask=__lowerCamelCase					,       )

            for key, value in outputs.items():
               if key not in batch_outputs:
                  UpperCAmelCase							=     []
               if value.dtype is np.dtype(np.floataa       ):
                  UpperCAmelCase							=     value.astype(np.floataa       )
               batch_outputs[key].append(__lowerCamelCase       )

         return BatchFeature(__lowerCamelCase					,       tensor_type=__lowerCamelCase       )





      def        _lowercase				(		self						:					Union[str, Any]					,       __lowerCamelCase						:					Union[Dict[str, np.ndarray], BatchFeature]					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					PaddingStrategy = PaddingStrategy.DO_NOT_PAD					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[bool] = None					,       )    ->   dict:






         """simple docstring"""







         UpperCAmelCase							=     processed_features[self.model_input_names[0]]

         if padding_strategy == PaddingStrategy.LONGEST:
            UpperCAmelCase							=     len(__lowerCamelCase       )

         if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
            UpperCAmelCase							=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

         UpperCAmelCase							=     padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__lowerCamelCase       ) < max_length

         if return_attention_mask and "attention_mask" not in processed_features:
            UpperCAmelCase							=     np.ones(len(__lowerCamelCase       )					,       dtype=np.intaa       )

         if needs_to_be_padded:
            UpperCAmelCase							=     max_length - len(__lowerCamelCase       )
            if self.padding_side == "right":
               if return_attention_mask:
                  UpperCAmelCase							=     np.pad(
                      processed_features["""attention_mask"""]					,       (0, difference)       )
               UpperCAmelCase							=     ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
               UpperCAmelCase							=     np.pad(
                   __lowerCamelCase					,       __lowerCamelCase					,       """constant"""					,       constant_values=self.padding_value       )
            elif self.padding_side == "left":
               if return_attention_mask:
                  UpperCAmelCase							=     np.pad(
                      processed_features["""attention_mask"""]					,       (difference, 0)       )
               UpperCAmelCase							=     ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
               UpperCAmelCase							=     np.pad(
                   __lowerCamelCase					,       __lowerCamelCase					,       """constant"""					,       constant_values=self.padding_value       )
            else:
               raise ValueError("""Invalid padding strategy:""" + str(self.padding_side       )       )

         return processed_features





      def        _lowercase				(		self						:					int					,       __lowerCamelCase						:					Union[Dict[str, np.ndarray], BatchFeature]					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[int] = None					,       __lowerCamelCase						:					Optional[bool] = None					,       )    ->   Dict:






         """simple docstring"""







         if not truncation:
            return processed_features
         elif truncation and max_length is None:
            raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""       )

         UpperCAmelCase							=     processed_features[self.model_input_names[0]]

         # find `max_length` that fits `pad_to_multiple_of`
         if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
            UpperCAmelCase							=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

         UpperCAmelCase							=     len(__lowerCamelCase       ) > max_length

         if needs_to_be_truncated:
            UpperCAmelCase							=     processed_features[self.model_input_names[0]][:max_length]
            if "attention_mask" in processed_features:
               UpperCAmelCase							=     processed_features["""attention_mask"""][:max_length]

         return processed_features





      def        _lowercase				(		self						:					Union[str, Any]					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Any=None       )    ->   Dict:






         """simple docstring"""







         if padding is not False:
            if padding is True:
               UpperCAmelCase							=     PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
            elif not isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
               UpperCAmelCase							=     PaddingStrategy(__lowerCamelCase       )
            elif isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
               UpperCAmelCase							=     padding
         else:
            UpperCAmelCase							=     PaddingStrategy.DO_NOT_PAD

         # Set max length if needed
         if max_length is None:
            if padding_strategy == PaddingStrategy.MAX_LENGTH:
               raise ValueError(
                   F"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"""       )

        # Test if we have a padding value
         if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
            raise ValueError(
                """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
                """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""       )

         return padding_strategy







from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a     =			logging.get_logger(__name__)

__a     =			{
    """facebook/nllb-moe-54B""": """https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json""",
}






class   __lowercase				(  __snake_case  ):
      UpperCamelCase								=       '''nllb-moe'''
      UpperCamelCase								=       ['''past_key_values''']
      UpperCamelCase								=       {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}





      def __init__(		self						:					Optional[int]					,       __lowerCamelCase						:					Optional[Any]=1_2_8_1_1_2					,       __lowerCamelCase						:					Dict=1_0_2_4					,       __lowerCamelCase						:					Optional[int]=1_2					,       __lowerCamelCase						:					Union[str, Any]=4_0_9_6					,       __lowerCamelCase						:					List[str]=1_6					,       __lowerCamelCase						:					List[str]=1_2					,       __lowerCamelCase						:					int=4_0_9_6					,       __lowerCamelCase						:					Tuple=1_6					,       __lowerCamelCase						:					str=0.05					,       __lowerCamelCase						:					List[str]=0.05					,       __lowerCamelCase						:					List[Any]=True					,       __lowerCamelCase						:					Tuple=True					,       __lowerCamelCase						:					str="relu"					,       __lowerCamelCase						:					Dict=1_0_2_4					,       __lowerCamelCase						:					List[str]=0.1					,       __lowerCamelCase						:					Optional[int]=0.1					,       __lowerCamelCase						:					List[Any]=0.0					,       __lowerCamelCase						:					Optional[Any]=0.02					,       __lowerCamelCase						:					Dict=2					,       __lowerCamelCase						:					Union[str, Any]=True					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Tuple="float32"					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Optional[int]=1_2_8					,       __lowerCamelCase						:					List[str]=6_4					,       __lowerCamelCase						:					List[Any]=4					,       __lowerCamelCase						:					Tuple=4					,       __lowerCamelCase						:					str=0.001					,       __lowerCamelCase						:					Optional[int]=0.001					,       __lowerCamelCase						:					Tuple="all"					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Optional[int]=False					,       __lowerCamelCase						:					List[str]=1.0					,       __lowerCamelCase						:					Dict=0.2					,       __lowerCamelCase						:					Union[str, Any]=1					,       __lowerCamelCase						:					int=0					,       __lowerCamelCase						:					Dict=2					,       __lowerCamelCase						:					int=False					,       **__lowerCamelCase						:					str					,       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     vocab_size
         UpperCAmelCase							=     max_position_embeddings
         UpperCAmelCase							=     d_model
         UpperCAmelCase							=     encoder_ffn_dim
         UpperCAmelCase							=     encoder_layers
         UpperCAmelCase							=     encoder_attention_heads
         UpperCAmelCase							=     decoder_ffn_dim
         UpperCAmelCase							=     decoder_layers
         UpperCAmelCase							=     decoder_attention_heads
         UpperCAmelCase							=     dropout
         UpperCAmelCase							=     attention_dropout
         UpperCAmelCase							=     activation_dropout
         UpperCAmelCase							=     activation_function
         UpperCAmelCase							=     init_std
         UpperCAmelCase							=     encoder_layerdrop
         UpperCAmelCase							=     decoder_layerdrop
         UpperCAmelCase							=     use_cache
         UpperCAmelCase							=     encoder_layers
         UpperCAmelCase							=     scale_embedding  # scale factor will be sqrt(d_model) if True
         UpperCAmelCase							=     router_z_loss_coef
         UpperCAmelCase							=     router_aux_loss_coef
         UpperCAmelCase							=     decoder_sparse_step
         UpperCAmelCase							=     encoder_sparse_step
         UpperCAmelCase							=     num_experts
         UpperCAmelCase							=     expert_capacity
         UpperCAmelCase							=     router_bias
         if router_dtype not in ["float32", "float16", "bfloat16"]:
            raise ValueError(F"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"""       )
         UpperCAmelCase							=     router_dtype

         UpperCAmelCase							=     router_ignore_padding_tokens
         UpperCAmelCase							=     batch_prioritized_routing
         UpperCAmelCase							=     second_expert_policy
         UpperCAmelCase							=     normalize_router_prob_before_dropping
         UpperCAmelCase							=     moe_eval_capacity_token_fraction
         UpperCAmelCase							=     moe_token_dropout
         UpperCAmelCase							=     output_router_logits
         super().__init__(
             pad_token_id=__lowerCamelCase					,       bos_token_id=__lowerCamelCase					,       eos_token_id=__lowerCamelCase					,       is_encoder_decoder=__lowerCamelCase					,       decoder_start_token_id=__lowerCamelCase					,       **__lowerCamelCase					,       )



