codingutf8 2023 meta ai and the huggingface inc team 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 orglicenseslicense2 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 musicgen model configuration from configurationutils import pretrainedconfig from utils import logging from auto configurationauto import autoconfig logger logging getloggername musicgenpretrainedconfigarchivemap facebookmusicgensmall https huggingface cofacebookmusicgensmallresolvemainconfig json see all musicgen models at https huggingface comodels filtermusicgen class musicgendecoderconfigpretrainedconfig r this is the configuration class to store the configuration of an musicgendecoder it is used to instantiate a musicgen decoder according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the musicgen facebookmusicgensmallhttps huggingface cofacebookmusicgensmall architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocabsize int optional defaults to 2048 vocabulary size of the musicgendecoder model defines the number of different tokens that can be represented by the inputsids passed when calling musicgendecoder hiddensize int optional defaults to 1024 dimensionality of the layers and the pooler layer numhiddenlayers int optional defaults to 24 number of decoder layers numattentionheads int optional defaults to 16 number of attention heads for each attention layer in the transformer block ffndim int optional defaults to 4096 dimensionality of the intermediate often named feedforward layer in the transformer block activationfunction str or function optional defaults to gelu the nonlinear activation function function or string in the decoder and pooler if string gelu relu silu and gelunew are supported dropout float optional defaults to 0 1 the dropout probability for all fully connected layers in the embeddings textencoder and pooler attentiondropout float optional defaults to 0 0 the dropout ratio for the attention probabilities activationdropout float optional defaults to 0 0 the dropout ratio for activations inside the fully connected layer maxpositionembeddings int optional defaults to 2048 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 initializerfactor float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices layerdrop float optional defaults to 0 0 the layerdrop probability for the decoder see the layerdrop papersee https arxiv orgabs1909 11556 for more details scaleembedding bool optional defaults to false scale embeddings by diving by sqrthiddensize usecache bool optional defaults to true whether the model should return the last keyvalues attentions not used by all models numcodebooks int optional defaults to 4 the number of parallel codebooks forwarded to the model tiewordembeddingsbool optional defaults to false whether input and output word embeddings should be tied audiochannels int optional defaults to 1 number of channels in the audio data either 1 for mono or 2 for stereo stereo models generate a separate audio stream for the leftright output channels mono models generate a single audio stream output return cls textencodertextencoderconfig todict audioencoderaudioencoderconfig todict decoderdecoderconfig todict kwargs property this is a property because you might want to change the codec model on the fly def samplingrateself return self audioencoder samplingrate coding utf 8 2023 meta ai and the huggingface inc team 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 musicgen model configuration see all musicgen models at https huggingface co models filter musicgen this is the configuration class to store the configuration of an musicgendecoder it is used to instantiate a musicgen decoder according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the musicgen facebook musicgen small https huggingface co facebook musicgen small architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocab_size int optional defaults to 2048 vocabulary size of the musicgendecoder model defines the number of different tokens that can be represented by the inputs_ids passed when calling musicgendecoder hidden_size int optional defaults to 1024 dimensionality of the layers and the pooler layer num_hidden_layers int optional defaults to 24 number of decoder layers num_attention_heads int optional defaults to 16 number of attention heads for each attention layer in the transformer block ffn_dim int optional defaults to 4096 dimensionality of the intermediate often named feed forward layer in the transformer block activation_function str or function optional defaults to gelu the non linear activation function function or string in the decoder and pooler if string gelu relu silu and gelu_new are supported dropout float optional defaults to 0 1 the dropout probability for all fully connected layers in the embeddings text_encoder and pooler attention_dropout float optional defaults to 0 0 the dropout ratio for the attention probabilities activation_dropout float optional defaults to 0 0 the dropout ratio for activations inside the fully connected layer max_position_embeddings int optional defaults to 2048 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 initializer_factor float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices layerdrop float optional defaults to 0 0 the layerdrop probability for the decoder see the layerdrop paper see https arxiv org abs 1909 11556 for more details scale_embedding bool optional defaults to false scale embeddings by diving by sqrt hidden_size use_cache bool optional defaults to true whether the model should return the last key values attentions not used by all models num_codebooks int optional defaults to 4 the number of parallel codebooks forwarded to the model tie_word_embeddings bool optional defaults to false whether input and output word embeddings should be tied audio_channels int optional defaults to 1 number of channels in the audio data either 1 for mono or 2 for stereo stereo models generate a separate audio stream for the left right output channels mono models generate a single audio stream output scale factor will be sqrt d_model if true this is the configuration class to store the configuration of a musicgenmodel it is used to instantiate a musicgen model according to the specified arguments defining the text encoder audio encoder and musicgen decoder configs configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args kwargs optional dictionary of keyword arguments notably text_encoder pretrainedconfig optional an instance of a configuration object that defines the text encoder config audio_encoder pretrainedconfig optional an instance of a configuration object that defines the audio encoder config decoder pretrainedconfig optional an instance of a configuration object that defines the decoder config example python from transformers import musicgenconfig musicgendecoderconfig t5config encodecconfig musicgenforconditionalgeneration initializing text encoder audio encoder and decoder model configurations text_encoder_config t5config audio_encoder_config encodecconfig decoder_config musicgendecoderconfig configuration musicgenconfig from_sub_models_config text_encoder_config audio_encoder_config decoder_config initializing a musicgenforconditionalgeneration with random weights from the facebook musicgen small style configuration model musicgenforconditionalgeneration configuration accessing the model configuration configuration model config config_text_encoder model config text_encoder config_audio_encoder model config audio_encoder config_decoder model config decoder saving the model including its configuration model save_pretrained musicgen model loading model and config from pretrained folder musicgen_config musicgenconfig from_pretrained musicgen model model musicgenforconditionalgeneration from_pretrained musicgen model config musicgen_config instantiate a musicgenconfig or a derived class from text encoder audio encoder and decoder configurations returns musicgenconfig an instance of a configuration object this is a property because you might want to change the codec model on the fly
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


logger = logging.get_logger(__name__)

MUSICGEN_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "facebook/musicgen-small": "https://huggingface.co/facebook/musicgen-small/resolve/main/config.json",
    
}


class MusicgenDecoderConfig(PretrainedConfig):
    r

    model_type = "musicgen_decoder"
    keys_to_ignore_at_inference = ["past_key_values"]

    def __init__(
        self,
        vocab_size=2048,
        max_position_embeddings=2048,
        num_hidden_layers=24,
        ffn_dim=4096,
        num_attention_heads=16,
        layerdrop=0.0,
        use_cache=True,
        activation_function="gelu",
        hidden_size=1024,
        dropout=0.1,
        attention_dropout=0.0,
        activation_dropout=0.0,
        initializer_factor=0.02,
        scale_embedding=False,
        num_codebooks=4,
        audio_channels=1,
        pad_token_id=2048,
        bos_token_id=2048,
        eos_token_id=None,
        tie_word_embeddings=False,
        **kwargs,
    ):
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.hidden_size = hidden_size
        self.ffn_dim = ffn_dim
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.dropout = dropout
        self.attention_dropout = attention_dropout
        self.activation_dropout = activation_dropout
        self.activation_function = activation_function
        self.initializer_factor = initializer_factor
        self.layerdrop = layerdrop
        self.use_cache = use_cache
        self.scale_embedding = scale_embedding  
        self.num_codebooks = num_codebooks

        if audio_channels not in [1, 2]:
            raise ValueError(f"Expected 1 (mono) or 2 (stereo) audio channels, got {audio_channels} channels.")
        self.audio_channels = audio_channels

        super().__init__(
            pad_token_id=pad_token_id,
            bos_token_id=bos_token_id,
            eos_token_id=eos_token_id,
            tie_word_embeddings=tie_word_embeddings,
            **kwargs,
        )


class MusicgenConfig(PretrainedConfig):
    r

    model_type = "musicgen"
    is_composition = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        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")

        text_encoder_config = kwargs.pop("text_encoder")
        text_encoder_model_type = text_encoder_config.pop("model_type")

        audio_encoder_config = kwargs.pop("audio_encoder")
        audio_encoder_model_type = audio_encoder_config.pop("model_type")

        decoder_config = kwargs.pop("decoder")

        self.text_encoder = AutoConfig.for_model(text_encoder_model_type, **text_encoder_config)
        self.audio_encoder = AutoConfig.for_model(audio_encoder_model_type, **audio_encoder_config)
        self.decoder = MusicgenDecoderConfig(**decoder_config)
        self.is_encoder_decoder = True

    @classmethod
    def from_sub_models_config(
        cls,
        text_encoder_config: PretrainedConfig,
        audio_encoder_config: PretrainedConfig,
        decoder_config: MusicgenDecoderConfig,
        **kwargs,
    ):
        r

        return cls(
            text_encoder=text_encoder_config.to_dict(),
            audio_encoder=audio_encoder_config.to_dict(),
            decoder=decoder_config.to_dict(),
            **kwargs,
        )

    @property
    
    def sampling_rate(self):
        return self.audio_encoder.sampling_rate