




"""simple docstring"""





import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


_lowercase						   =     logging.get_logger(__name__)

@dataclass
class    __lowercase							:


				'''simple docstring'''


				_lowerCAmelCase						      =42
				_lowerCAmelCase						      =42
				_lowerCAmelCase						      =None
				_lowerCAmelCase						      =None

				def    _lowerCamelCase       ( self :  str   ):
										return json.dumps(dataclasses.asdict(self   )   ,indent=2   ) + "\n"







@dataclass(frozen=snake_case_						)
class    __lowercase							:


				'''simple docstring'''


				_lowerCAmelCase						      =42
				_lowerCAmelCase						      =None
				_lowerCAmelCase						      =None
				_lowerCAmelCase						      =None

				def    _lowerCamelCase       ( self :  Any   ):
										return json.dumps(dataclasses.asdict(self   )   ) + "\n"







class    __lowercase							:


				'''simple docstring'''


				def    _lowerCamelCase       ( self :  List[Any]   ,SCREAMING_SNAKE_CASE_ :  str   ):
										raise NotImplementedError()

				def    _lowerCamelCase       ( self :  Optional[int]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ):
										raise NotImplementedError()

				def    _lowerCamelCase       ( self :  Tuple   ,SCREAMING_SNAKE_CASE_ :  str   ):
										raise NotImplementedError()

				def    _lowerCamelCase       ( self :  int   ,SCREAMING_SNAKE_CASE_ :  Optional[int]   ):
										raise NotImplementedError()

				def    _lowerCamelCase       ( self :  Optional[int]   ):
										raise NotImplementedError()

				def    _lowerCamelCase       ( self :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Any   ):
										if len(self.get_labels()   ) > 1:
																snake_case__							:       Dict				      =       self.get_labels()[int(example.label   )]
										return example





				@classmethod
				def    _lowerCamelCase       ( cls :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Any   ,SCREAMING_SNAKE_CASE_ :  List[Any]=None   ):
										with open(SCREAMING_SNAKE_CASE_   ,'''r'''   ,encoding='''utf-8-sig'''   ) as f:
																return list(csv.reader(SCREAMING_SNAKE_CASE_   ,delimiter='''\t'''   ,quotechar=SCREAMING_SNAKE_CASE_   )   )







class    __lowercase							(      snake_case_						):


				'''simple docstring'''


				def __init__( self :  Union[str, Any]   ,SCREAMING_SNAKE_CASE_ :  int=None   ,SCREAMING_SNAKE_CASE_ :  Dict=None   ,SCREAMING_SNAKE_CASE_ :  Dict="classification"   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ):
										snake_case__							:       Any				      =       [] if labels is None else labels
										snake_case__							:       Tuple				      =       [] if examples is None else examples
										snake_case__							:       str				      =       mode
										snake_case__							:       Any				      =       verbose

				def __len__( self :  int   ):
										return len(self.examples   )

				def __getitem__( self :  List[Any]   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ):
										if isinstance(SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ):
																return SingleSentenceClassificationProcessor(labels=self.labels   ,examples=self.examples[idx]   )
										return self.examples[idx]

				@classmethod
				def    _lowerCamelCase       ( cls :  Any   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ,SCREAMING_SNAKE_CASE_ :  Any=""   ,SCREAMING_SNAKE_CASE_ :  Any=0   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=1   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=None   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ,**SCREAMING_SNAKE_CASE_ :  Optional[Any]   ):
										snake_case__							:       List[str]				      =       cls(**SCREAMING_SNAKE_CASE_   )
										processor.add_examples_from_csv(
										    SCREAMING_SNAKE_CASE_   ,split_name=SCREAMING_SNAKE_CASE_   ,column_label=SCREAMING_SNAKE_CASE_   ,column_text=SCREAMING_SNAKE_CASE_   ,column_id=SCREAMING_SNAKE_CASE_   ,skip_first_row=SCREAMING_SNAKE_CASE_   ,overwrite_labels=SCREAMING_SNAKE_CASE_   ,overwrite_examples=SCREAMING_SNAKE_CASE_   ,)
										return processor

				@classmethod
				def    _lowerCamelCase       ( cls :  Dict   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Tuple=None   ,**SCREAMING_SNAKE_CASE_ :  Dict   ):
										snake_case__							:       List[str]				      =       cls(**SCREAMING_SNAKE_CASE_   )
										processor.add_examples(SCREAMING_SNAKE_CASE_   ,labels=SCREAMING_SNAKE_CASE_   )
										return processor

				def    _lowerCamelCase       ( self :  str   ,SCREAMING_SNAKE_CASE_ :  str   ,SCREAMING_SNAKE_CASE_ :  Tuple=""   ,SCREAMING_SNAKE_CASE_ :  List[str]=0   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=1   ,SCREAMING_SNAKE_CASE_ :  List[str]=None   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=False   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=False   ,SCREAMING_SNAKE_CASE_ :  int=False   ,):
										snake_case__							:       Dict				      =       self._read_tsv(SCREAMING_SNAKE_CASE_   )
										if skip_first_row:
																snake_case__							:       Any				      =       lines[1:]
										snake_case__							:       List[str]				      =       []
										snake_case__							:       Tuple				      =       []
										snake_case__							:       List[Any]				      =       []
										for i, line in enumerate(SCREAMING_SNAKE_CASE_   ):
																texts.append(line[column_text]   )
																labels.append(line[column_label]   )
																if column_id is not None:
																						ids.append(line[column_id]   )
																else:
																						snake_case__							:       Any				      =       F'''{split_name}-{i}''' if split_name else str(SCREAMING_SNAKE_CASE_   )
																						ids.append(SCREAMING_SNAKE_CASE_   )

										return self.add_examples(
										    SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,overwrite_labels=SCREAMING_SNAKE_CASE_   ,overwrite_examples=SCREAMING_SNAKE_CASE_   )

				def    _lowerCamelCase       ( self :  Union[str, Any]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=None   ,SCREAMING_SNAKE_CASE_ :  int=None   ,SCREAMING_SNAKE_CASE_ :  Any=False   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=False   ):
										if labels is not None and len(SCREAMING_SNAKE_CASE_   ) != len(SCREAMING_SNAKE_CASE_   ):
																raise ValueError(
																    F'''Text and labels have mismatched lengths {len(SCREAMING_SNAKE_CASE_   )} and {len(SCREAMING_SNAKE_CASE_   )}'''   )
										if ids is not None and len(SCREAMING_SNAKE_CASE_   ) != len(SCREAMING_SNAKE_CASE_   ):
																raise ValueError(F'''Text and ids have mismatched lengths {len(SCREAMING_SNAKE_CASE_   )} and {len(SCREAMING_SNAKE_CASE_   )}'''   )
										if ids is None:
																snake_case__							:       str				      =       [None] * len(SCREAMING_SNAKE_CASE_   )
										if labels is None:
																snake_case__							:       Any				      =       [None] * len(SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Any				      =       []
										snake_case__							:       Tuple				      =       set()
										for text_or_text_and_label, label, guid in zip(SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ):
																if isinstance(SCREAMING_SNAKE_CASE_   ,(tuple, list)   ) and label is None:
																						snake_case__      ,						snake_case__							:       Tuple				      =       text_or_text_and_label
																else:
																						snake_case__							:       int				      =       text_or_text_and_label
																added_labels.add(SCREAMING_SNAKE_CASE_   )
																examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_   ,text_a=SCREAMING_SNAKE_CASE_   ,text_b=SCREAMING_SNAKE_CASE_   ,label=SCREAMING_SNAKE_CASE_   )   )

										# Update examples
										if overwrite_examples:
																snake_case__							:       Union[str, Any]				      =       examples
										else:
																self.examples.extend(SCREAMING_SNAKE_CASE_   )

										# Update labels
										if overwrite_labels:
																snake_case__							:       Tuple				      =       list(SCREAMING_SNAKE_CASE_   )
										else:
																snake_case__							:       int				      =       list(set(self.labels   ).union(SCREAMING_SNAKE_CASE_   )   )

										return self.examples





				def    _lowerCamelCase       ( self :  int   ,SCREAMING_SNAKE_CASE_ :  Any   ,SCREAMING_SNAKE_CASE_ :  Tuple=None   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ,SCREAMING_SNAKE_CASE_ :  List[str]=0   ,SCREAMING_SNAKE_CASE_ :  int=True   ,SCREAMING_SNAKE_CASE_ :  List[str]=None   ,):
										if max_length is None:
																snake_case__							:       Optional[Any]				      =       tokenizer.max_len

										snake_case__							:       int				      =       {label: i for i, label in enumerate(self.labels   )}

										snake_case__							:       Optional[int]				      =       []
										for ex_index, example in enumerate(self.examples   ):
																if ex_index % 1_0_0_0_0 == 0:
																						logger.info(F'''Tokenizing example {ex_index}'''   )

																snake_case__							:       Dict				      =       tokenizer.encode(
																    example.text_a   ,add_special_tokens=SCREAMING_SNAKE_CASE_   ,max_length=min(SCREAMING_SNAKE_CASE_   ,tokenizer.max_len   )   ,)
																all_input_ids.append(SCREAMING_SNAKE_CASE_   )

										snake_case__							:       List[Any]				      =       max(len(SCREAMING_SNAKE_CASE_   ) for input_ids in all_input_ids   )

										snake_case__							:       Optional[Any]				      =       []
										for ex_index, (input_ids, example) in enumerate(zip(SCREAMING_SNAKE_CASE_   ,self.examples   )   ):
																if ex_index % 1_0_0_0_0 == 0:
																						logger.info(F'''Writing example {ex_index}/{len(self.examples   )}'''   )
																# The mask has 1 for real tokens and 0 for padding tokens. Only real
																# tokens are attended to.
																snake_case__							:       str				      =       [1 if mask_padding_with_zero else 0] * len(SCREAMING_SNAKE_CASE_   )

																# Zero-pad up to the sequence length.
																snake_case__							:       str				      =       batch_length - len(SCREAMING_SNAKE_CASE_   )
																if pad_on_left:
																						snake_case__							:       Tuple				      =       ([pad_token] * padding_length) + input_ids
																						snake_case__							:       List[Any]				      =       ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
																else:
																						snake_case__							:       Optional[Any]				      =       input_ids + ([pad_token] * padding_length)
																						snake_case__							:       Optional[int]				      =       attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

																if len(SCREAMING_SNAKE_CASE_   ) != batch_length:
																						raise ValueError(F'''Error with input length {len(SCREAMING_SNAKE_CASE_   )} vs {batch_length}'''   )
																if len(SCREAMING_SNAKE_CASE_   ) != batch_length:
																						raise ValueError(F'''Error with input length {len(SCREAMING_SNAKE_CASE_   )} vs {batch_length}'''   )

																if self.mode == "classification":
																						snake_case__							:       List[Any]				      =       label_map[example.label]
																elif self.mode == "regression":
																						snake_case__							:       List[str]				      =       float(example.label   )
																else:
																						raise ValueError(self.mode   )

																if ex_index < 5 and self.verbose:
																						logger.info('''*** Example ***'''   )
																						logger.info(F'''guid: {example.guid}'''   )
																						logger.info(F'''input_ids: {' '.join([str(SCREAMING_SNAKE_CASE_   ) for x in input_ids]   )}'''   )
																						logger.info(F'''attention_mask: {' '.join([str(SCREAMING_SNAKE_CASE_   ) for x in attention_mask]   )}'''   )
																						logger.info(F'''label: {example.label} (id = {label})'''   )

																features.append(InputFeatures(input_ids=SCREAMING_SNAKE_CASE_   ,attention_mask=SCREAMING_SNAKE_CASE_   ,label=SCREAMING_SNAKE_CASE_   )   )

										if return_tensors is None:
																return features
										elif return_tensors == "tf":
																if not is_tf_available():
																						raise RuntimeError('''return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'''   )
																import tensorflow as tf

																def gen():
																						for ex in features:
																												yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

																snake_case__							:       str				      =       tf.data.Dataset.from_generator(
																    SCREAMING_SNAKE_CASE_   ,({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa}, tf.intaa)   ,({'''input_ids''': tf.TensorShape([None]   ), '''attention_mask''': tf.TensorShape([None]   )}, tf.TensorShape([]   ))   ,)
																return dataset
										elif return_tensors == "pt":
																if not is_torch_available():
																						raise RuntimeError('''return_tensors set to \'pt\' but PyTorch can\'t be imported'''   )
																import torch
																from torch.utils.data import TensorDataset

																snake_case__							:       Any				      =       torch.tensor([f.input_ids for f in features]   ,dtype=torch.long   )
																snake_case__							:       Union[str, Any]				      =       torch.tensor([f.attention_mask for f in features]   ,dtype=torch.long   )
																if self.mode == "classification":
																						snake_case__							:       Optional[int]				      =       torch.tensor([f.label for f in features]   ,dtype=torch.long   )
																elif self.mode == "regression":
																						snake_case__							:       Tuple				      =       torch.tensor([f.label for f in features]   ,dtype=torch.float   )

																snake_case__							:       Tuple				      =       TensorDataset(SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   ,SCREAMING_SNAKE_CASE_   )
																return dataset
										else:
																raise ValueError('''return_tensors should be one of \'tf\' or \'pt\''''   )








"""simple docstring"""





import copy

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


_lowercase						   =     logging.get_logger(__name__)

_lowercase						   =     {
    """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    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen_decoder"
				_lowerCAmelCase						      =["past_key_values"]

				def __init__( self :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Dict=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  List[str]=2_4   ,SCREAMING_SNAKE_CASE_ :  int=4_0_9_6   ,SCREAMING_SNAKE_CASE_ :  List[Any]=1_6   ,SCREAMING_SNAKE_CASE_ :  Dict=0.0   ,SCREAMING_SNAKE_CASE_ :  Any=True   ,SCREAMING_SNAKE_CASE_ :  Any="gelu"   ,SCREAMING_SNAKE_CASE_ :  Tuple=1_0_2_4   ,SCREAMING_SNAKE_CASE_ :  Any=0.1   ,SCREAMING_SNAKE_CASE_ :  List[str]=0.0   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=0.0   ,SCREAMING_SNAKE_CASE_ :  Tuple=0.02   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=False   ,SCREAMING_SNAKE_CASE_ :  List[str]=4   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=None   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ,**SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ,):
										snake_case__							:       List[str]				      =       vocab_size
										snake_case__							:       List[str]				      =       max_position_embeddings
										snake_case__							:       List[Any]				      =       hidden_size
										snake_case__							:       Dict				      =       ffn_dim
										snake_case__							:       Union[str, Any]				      =       num_hidden_layers
										snake_case__							:       List[str]				      =       num_attention_heads
										snake_case__							:       Optional[Any]				      =       dropout
										snake_case__							:       Dict				      =       attention_dropout
										snake_case__							:       Union[str, Any]				      =       activation_dropout
										snake_case__							:       Dict				      =       activation_function
										snake_case__							:       Union[str, Any]				      =       initializer_factor
										snake_case__							:       List[str]				      =       layerdrop
										snake_case__							:       Any				      =       use_cache
										snake_case__							:       Tuple				      =       scale_embedding  # scale factor will be sqrt(d_model) if True
										snake_case__							:       Any				      =       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    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen"
				_lowerCAmelCase						      =True

				def __init__( self :  Optional[Any]   ,**SCREAMING_SNAKE_CASE_ :  Dict   ):
										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__							:       Optional[int]				      =       kwargs.pop('''text_encoder'''   )
										snake_case__							:       Optional[Any]				      =       text_encoder_config.pop('''model_type'''   )

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

										snake_case__							:       Optional[Any]				      =       kwargs.pop('''decoder'''   )

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

				@classmethod
				def    _lowerCamelCase       ( cls :  Dict   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  MusicgenDecoderConfig   ,**SCREAMING_SNAKE_CASE_ :  str   ,):
										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    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       Optional[Any]				      =       copy.deepcopy(self.__dict__   )
										snake_case__							:       Any				      =       self.text_encoder.to_dict()
										snake_case__							:       Dict				      =       self.audio_encoder.to_dict()
										snake_case__							:       Dict				      =       self.decoder.to_dict()
										snake_case__							:       Optional[int]				      =       self.__class__.model_type
										return output


