
'''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


UpperCAmelCase            = logging.get_logger(__name__)

class     a (					__magic_name__     ):
				_snake_case		     =			['''input_values''', '''attention_mask''']




				def __init__(				self						:					List[str],     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=1,     SCREAMING_SNAKE_CASE_						:					Optional[int]=1_60_00,     SCREAMING_SNAKE_CASE_						:					List[Any]=0.0,     SCREAMING_SNAKE_CASE_						:					List[Any]=False,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=True,     **SCREAMING_SNAKE_CASE_						:					Any,     ):
										super().__init__(feature_size=SCREAMING_SNAKE_CASE_,     sampling_rate=SCREAMING_SNAKE_CASE_,     padding_value=SCREAMING_SNAKE_CASE_,     **SCREAMING_SNAKE_CASE_   )
										snake_case							:  Any			=     return_attention_mask
										snake_case							:  List[str]			=     do_normalize




				@staticmethod
				def 						__snake_case				(				SCREAMING_SNAKE_CASE_						:					List[np.ndarray],     SCREAMING_SNAKE_CASE_						:					List[np.ndarray],     SCREAMING_SNAKE_CASE_						:					float = 0.0   ):
										if attention_mask is not None:
																snake_case							:  str			=     np.array(SCREAMING_SNAKE_CASE_,     np.intaa   )
																snake_case							:  int			=     []

																for vector, length in zip(SCREAMING_SNAKE_CASE_,     attention_mask.sum(-1   )   ):
																						snake_case							:  Dict			=     (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7   )
																						if length < normed_slice.shape[0]:
																												snake_case							:  Any			=     padding_value

																						normed_input_values.append(SCREAMING_SNAKE_CASE_   )
										else:
																snake_case							:  Any			=     [(x - x.mean()) / np.sqrt(x.var() + 1e-7   ) for x in input_values]

										return normed_input_values




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

										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 `raw_speech` 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.'''   )

										snake_case							:  Optional[int]			=     isinstance(SCREAMING_SNAKE_CASE_,     np.ndarray   ) and len(raw_speech.shape   ) > 1
										if is_batched_numpy and len(raw_speech.shape   ) > 2:
																raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""   )
										snake_case							:  Any			=     is_batched_numpy or (
										    isinstance(SCREAMING_SNAKE_CASE_,     (list, tuple)   ) and (isinstance(raw_speech[0],     (np.ndarray, tuple, list)   ))
										)

										# always return batch
										if not is_batched:
																snake_case							:  Optional[int]			=     [raw_speech]

										# convert into correct format for padding
										snake_case							:  Tuple			=     BatchFeature({'''input_values''': raw_speech}   )

										snake_case							:  Dict			=     self.pad(
										    SCREAMING_SNAKE_CASE_,     padding=SCREAMING_SNAKE_CASE_,     max_length=SCREAMING_SNAKE_CASE_,     truncation=SCREAMING_SNAKE_CASE_,     pad_to_multiple_of=SCREAMING_SNAKE_CASE_,     return_attention_mask=SCREAMING_SNAKE_CASE_,     )

										# convert input values to correct format
										snake_case							:  Tuple			=     padded_inputs['''input_values''']
										if not isinstance(input_values[0],     np.ndarray   ):
																snake_case							:  List[Any]			=     [np.asarray(SCREAMING_SNAKE_CASE_,     dtype=np.floataa   ) for array in input_values]
										elif (
										    not isinstance(SCREAMING_SNAKE_CASE_,     np.ndarray   )
										    and isinstance(input_values[0],     np.ndarray   )
										    and input_values[0].dtype is np.dtype(np.floataa   )
										):
																snake_case							:  int			=     [array.astype(np.floataa   ) for array in input_values]
										elif isinstance(SCREAMING_SNAKE_CASE_,     np.ndarray   ) and input_values.dtype is np.dtype(np.floataa   ):
																snake_case							:  Union[str, Any]			=     input_values.astype(np.floataa   )

										# convert attention_mask to correct format
										snake_case							:  List[str]			=     padded_inputs.get('''attention_mask'''   )
										if attention_mask is not None:
																snake_case							:  Tuple			=     [np.asarray(SCREAMING_SNAKE_CASE_,     dtype=np.intaa   ) for array in attention_mask]

										# zero-mean and unit-variance normalization
										if self.do_normalize:
																snake_case							:  int			=     (
																    attention_mask
																    if self._get_padding_strategies(SCREAMING_SNAKE_CASE_,     max_length=SCREAMING_SNAKE_CASE_   ) is not PaddingStrategy.DO_NOT_PAD
																    else None
																)
																snake_case							:  Optional[int]			=     self.zero_mean_unit_var_norm(
																    padded_inputs['''input_values'''],     attention_mask=SCREAMING_SNAKE_CASE_,     padding_value=self.padding_value   )

										if return_tensors is not None:
																snake_case							:  List[str]			=     padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_   )

										return padded_inputs



'''simple docstring'''





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


UpperCAmelCase            = logging.get_logger(__name__)

UpperCAmelCase            = {
    "facebook/musicgen-small": "https://huggingface.co/facebook/musicgen-small/resolve/main/config.json",
    # See all Musicgen models at https://huggingface.co/models?filter=musicgen
}

class     a (					__magic_name__     ):
				_snake_case		     =			'''musicgen_decoder'''
				_snake_case		     =			['''past_key_values''']




				def __init__(				self						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Dict=20_48,     SCREAMING_SNAKE_CASE_						:					List[str]=20_48,     SCREAMING_SNAKE_CASE_						:					int=24,     SCREAMING_SNAKE_CASE_						:					Dict=40_96,     SCREAMING_SNAKE_CASE_						:					str=16,     SCREAMING_SNAKE_CASE_						:					int=0.0,     SCREAMING_SNAKE_CASE_						:					Optional[Any]=True,     SCREAMING_SNAKE_CASE_						:					str="gelu",     SCREAMING_SNAKE_CASE_						:					List[Any]=10_24,     SCREAMING_SNAKE_CASE_						:					int=0.1,     SCREAMING_SNAKE_CASE_						:					List[Any]=0.0,     SCREAMING_SNAKE_CASE_						:					int=0.0,     SCREAMING_SNAKE_CASE_						:					str=0.02,     SCREAMING_SNAKE_CASE_						:					Any=False,     SCREAMING_SNAKE_CASE_						:					List[str]=4,     SCREAMING_SNAKE_CASE_						:					str=20_48,     SCREAMING_SNAKE_CASE_						:					int=20_48,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=None,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=False,     **SCREAMING_SNAKE_CASE_						:					Tuple,     ):
										snake_case							:  Optional[int]			=     vocab_size
										snake_case							:  Optional[int]			=     max_position_embeddings
										snake_case							:  Dict			=     hidden_size
										snake_case							:  Any			=     ffn_dim
										snake_case							:  Optional[Any]			=     num_hidden_layers
										snake_case							:  Tuple			=     num_attention_heads
										snake_case							:  int			=     dropout
										snake_case							:  Union[str, Any]			=     attention_dropout
										snake_case							:  Optional[int]			=     activation_dropout
										snake_case							:  Union[str, Any]			=     activation_function
										snake_case							:  Tuple			=     initializer_factor
										snake_case							:  List[Any]			=     layerdrop
										snake_case							:  Optional[int]			=     use_cache
										snake_case							:  Dict			=     scale_embedding  # scale factor will be sqrt(d_model) if True
										snake_case							:  Tuple			=     num_codebooks
										super().__init__(
										    pad_token_id=SCREAMING_SNAKE_CASE_,     bos_token_id=SCREAMING_SNAKE_CASE_,     eos_token_id=SCREAMING_SNAKE_CASE_,     tie_word_embeddings=SCREAMING_SNAKE_CASE_,     **SCREAMING_SNAKE_CASE_,     )




class     a (					__magic_name__     ):
				_snake_case		     =			'''musicgen'''
				_snake_case		     =			True




				def __init__(				self						:					Dict,     **SCREAMING_SNAKE_CASE_						:					Optional[int]   ):
										super().__init__(**SCREAMING_SNAKE_CASE_   )
										if "text_encoder" not in kwargs or "audio_encoder" not in kwargs or "decoder" not in kwargs:
																raise ValueError('''Config has to be initialized with text_encoder, audio_encoder and decoder config'''   )

										snake_case							:  Any			=     kwargs.pop('''text_encoder'''   )
										snake_case							:  List[Any]			=     text_encoder_config.pop('''model_type'''   )

										snake_case							:  Union[str, Any]			=     kwargs.pop('''audio_encoder'''   )
										snake_case							:  Tuple			=     audio_encoder_config.pop('''model_type'''   )

										snake_case							:  List[str]			=     kwargs.pop('''decoder'''   )

										snake_case							:  List[str]			=     AutoConfig.for_model(SCREAMING_SNAKE_CASE_,     **SCREAMING_SNAKE_CASE_   )
										snake_case							:  List[Any]			=     AutoConfig.for_model(SCREAMING_SNAKE_CASE_,     **SCREAMING_SNAKE_CASE_   )
										snake_case							:  Optional[Any]			=     MusicgenDecoderConfig(**SCREAMING_SNAKE_CASE_   )
										snake_case							:  Any			=     True




				@classmethod
				def 						__snake_case				(				cls						:					Tuple,     SCREAMING_SNAKE_CASE_						:					PretrainedConfig,     SCREAMING_SNAKE_CASE_						:					PretrainedConfig,     SCREAMING_SNAKE_CASE_						:					MusicgenDecoderConfig,     **SCREAMING_SNAKE_CASE_						:					Optional[Any],     ):
										return cls(
										    text_encoder=text_encoder_config.to_dict(),     audio_encoder=audio_encoder_config.to_dict(),     decoder=decoder_config.to_dict(),     **SCREAMING_SNAKE_CASE_,     )




				def 						__snake_case				(				self						:					Optional[Any]   ):
										snake_case							:  Dict			=     copy.deepcopy(self.__dict__   )
										snake_case							:  Tuple			=     self.text_encoder.to_dict()
										snake_case							:  str			=     self.audio_encoder.to_dict()
										snake_case							:  Dict			=     self.decoder.to_dict()
										snake_case							:  Optional[int]			=     self.__class__.model_type
										return output

