AudioCraft

repository·main·Indexed 12 days ago

https://github.com/facebookresearch/audiocraft

A PyTorch-based library for deep learning research in audio generation. It provides inference and training code for state-of-the-art generative models including MusicGen, AudioGen, JASCO, MAGNeT, EnCodec, and AudioSeal for producing high-quality music, sound effects, and neural audio codecs.

Tokens
29.6K
Snippets
86
Records
117
Agent score
96%

What's inside AudioCraft

  1. Overview of AudioCraft models

    main

    AudioCraft provides inference and training code for several generative audio models:

    • MusicGen: Controllable text-to-music model.
    • AudioGen: Text-to-sound model.
    • EnCodec: High-fidelity neural audio codec.
    • Multi Band Diffusion: An EnCodec compatible decoder using diffusion.
    • MAGNeT: Non-autoregressive model for text-to-music and text-to-sound.
    • AudioSeal: Audio watermarking.
    • MusicGen Style: Text-and-style-to-music model.
    • JASCO: High-quality text-to-music model conditioned on chords, melodies, and drum tracks.
  2. Overview of AudioSeal Watermarking

    main

    AudioSeal is a method for speech localized watermarking designed for proactive detection of voice cloning. It features a joint training approach:

    1. A Generator that embeds a watermark into audio.
    2. A Detector that identifies watermarked fragments within longer audio sequences, maintaining robustness even when the audio has been edited.
  3. How AudioCraft conditioning modules work together

    main

    AudioCraft uses a modular system to control audio generation via conditioning signals. The process follows a specific pipeline involving three main components:

    1. Conditioners: Torch modules that process raw signals (text, waveforms, or joint embeddings) into dense embeddings. They implement a two-step process (tokenize for preprocessing/synchronization and forward for core computation) to avoid GPU synchronization bottlenecks.
    2. ConditionProvider: A coordinator that maps metadata (via ConditioningAttributes) to the appropriate conditioners. It manages the batch-level tokenize and forward steps for all active conditioners.
    3. ConditionFuser: Takes the processed embeddings from the ConditionProvider and merges them with the language model's inputs using a specific strategy (e.g., prepend, sum, cross, or input_interpolate).

    This architecture allows developers to extend the model by implementing new conditioners or fusion strategies.

  4. AudioGen Model Details and Architecture

    main

    AudioGen is a textually guided audio generation model developed by Meta AI's FAIR team. It consists of two main components:

    1. EnCodec model: Used for audio tokenization.
    2. Auto-regressive language model: A transformer-based architecture used for audio modeling.

    Key Specifications (v2):

    • Parameters: 1.5B
    • Generation Length: Trained on 10-second clips (compared to 5 seconds in v1).
    • Tokenization: Uses a discrete representation extracted via a retrained EnCodec model on environmental sound data.
    • License: The code is released under MIT, but the model weights are released under CC-BY-NC 4.0 (Non-Commercial).
  5. Choose between FSDP and autocast for training

    main

    When configuring training, note that FSDP (Fully Sharded Data Parallel) and autocast are mutually exclusive because FSDP handles autocast internally.

    • Use autocast: If you are working with models up to 1.5B (medium) and have sufficient GPU RAM.
    • Use FSDP: If you need to free up memory for activations by sharding the optimizer state, though it increases configuration complexity.
  6. Compute metrics on reconstructed audio instead of generated samples

    main

    For all audio generation metrics (FAD, KLD, Text Consistency, etc.), you can choose to compute the metric on the audio reconstructed by EnCodec rather than the raw generated sample. This is controlled by the flag <metric>.use_gt=true.

    # Example for FAD
    dora run <...> evaluate.metrics.fad=true metrics.fad.use_gt=true
  7. Implement a custom conditioner

    main

    To create a new conditioning module, you should implement a class that follows the BaseConditioner pattern. All conditioners must implement two specific methods to ensure efficient execution and avoid synchronization points:

    • tokenize(...): Performs preprocessing (like BPE tokenization or moving data to GPU). The output of this method is passed to the forward method.
    • forward(...): Performs the core computation to produce the conditioning embedding and a mask indicating valid indices (e.g., for padding).
  8. Limitations and usage constraints of MusicGen-Style

    main

    When using MusicGen-Style, be aware of the following technical and ethical limitations:

    Technical Limitations:

    • Vocals: The model cannot generate realistic vocals (vocals were removed from training data using HT-Demucs).
    • Language: Optimized for English descriptions; performance may degrade in other languages.
    • Audio Artifacts: The model may occasionally collapse to silence at the end of songs.
    • Prompting: Results are sensitive to text descriptions; prompt engineering is often required.
    • Cultural Representation: The model may not perform equally well across all music styles and cultures due to dataset biases.

    Usage Constraints:

    • Research Focus: Primarily intended for research on AI-based music generation.
    • Downstream Use: Should not be used in downstream applications without thorough risk evaluation and mitigation.
    • Prohibited Use: Do not use to create music that is disturbing, distressing, offensive, or propagates stereotypes.
  9. Train EnCodec models using CompressionSolver

    main

    EnCodec training is implemented via the CompressionSolver class. It trains an encoder-decoder with a quantization bottleneck (specifically a SEANet encoder-decoder with Residual Vector Quantization) using a combination of objective and perceptual losses (discriminators).

    Configuration files for compression are located in config/solver/compression, and example training grids can be found in audiocraft/grids/compression.

    # Example: Run training for base causal encodec on monophonic 24khz audio
    dora grid compression.encodec_base_24khz
    
    # Example: Run training for encodec model used for MusicGen on monophonic 32khz audio
    dora grid compression.encodec_musicgen_32khz
  10. Metadata requirements for MusicDataset and SoundDataset

    main

    Both MusicDataset and SoundDataset extend the base AudioDataset by requiring additional metadata.

    MusicDataset

    Expects additional metadata to be stored in a .json file located at the same path as the corresponding audio file (e.g., audio_file.wav requires audio_file.json).

    SoundDataset

    Expects metadata in a .json file at the same path as the audio file. Additionally, SoundDataset supports an external_metadata_source parameter, which allows you to point to a separate folder containing all the JSON metadata files (provided they share the same filename as the audio files).

  11. Understand MusicGen model variants and architectures

    main

    MusicGen is an auto-regressive transformer-based model for music generation that uses an EnCodec model for audio tokenization. It is available in several parameter sizes and functional variants:

    Parameter Sizes

    • 300M (Small)
    • 1.5B (Medium)
    • 3.3B (Large)

    Functional Variants

    • Text-to-Music: Generates music based on text descriptions.
    • Melody-guided: Generates music guided by an input melody.
    • Stereo: Models fine-tuned to support stereophonic audio by interleaving two streams of EnCodec tokens.

    Available Model Identifiers

    • facebook/musicgen-small (300M)
    • facebook/musicgen-medium (1.5B)
    • facebook/musicgen-large (3.3B)
    • facebook/musicgen-melody (Melody-guided)
    • facebook/musicgen-stereo-small (Stereo small)
    • facebook/musicgen-stereo-medium (Stereo medium)
    • facebook/musicgen-stereo-large (Stereo large)
    • facebook/musicgen-stereo-melody (Stereo melody-guided)
    • facebook/musicgen-melody-large (Large melody-guided)
    • facebook/musicgen-stereo-melody-large (Stereo large melody-guided)
  12. Define chord progressions for JASCO

    main

    When using JASCO models that support chord control, you can pass a list of tuples to the chords parameter in model.generate_music(). Each tuple should contain a chord name (as a string) and a start time (as a float).

    Example format: [('C', 0.0), ('D', 2.0)]

    # Example chord progression: C at 0s, D at 2s, F at 4s, etc.
    chords = [('C', 0.0), ('D', 2.0), ('F', 4.0), ('Ab', 6.0), ('Bb', 7.0), ('C', 8.0)]