arioso.platforms.musicgen.backends#

The ways to run MusicGen, behind one interface (thorwhalen/arioso#31, #54).

The adapter holds the modes (text, melody, continuation: window checks, chaining, splicing); a backend holds what one library needs to load a model and run it in each of them. audiocraft is Meta’s library, transformers the HuggingFace port, and mlx Apple’s MLX port (ml-explore/mlx-examples), which runs in its own environment through a worker and only does text. Each backend wraps a model that is already loaded, so building one is free; load is what loads one.

Which one runs (select_backend_class()): $ARIOSO_MUSICGEN_BACKEND when set; else MLX for a text take on Apple Silicon when its runtime is installed; else audiocraft when it imports; else transformers. On Apple Silicon, transformers runs on the CPU unless $ARIOSO_TORCH_DEVICE says otherwise: on MPS it was 15-30x slower and grew Metal memory on every decoding step (42 GB after three 10 s clips on an M1 Max).

audiocraft and transformers return the model’s own layout for text and melody, (channels, frames) from audiocraft and (frames,) from transformers’ first channel, and mono for a continuation; MLX returns (frames,).

Module Attributes

DEFAULT_FRAME_RATE

Codec frames per second, used when a model does not report its own.

BACKEND_ENV_VAR

audiocraft, transformers or mlx.

MLX_MAX_SECONDS

MusicGen's context in seconds (1500 frames); the MLX port does not stop there itself.

MLX_CHECKPOINTS

The checkpoints the MLX port loads.

TASKS

plain text, follow a melody, or continue audio.

BACKENDS

The backends by the name $ARIOSO_MUSICGEN_BACKEND takes.

Functions

select_backend_class(model_variant, *[, task])

The library that runs model_variant for task (see the module docstring).

Classes

AudiocraftBackend(model)

A loaded audiocraft.models.MusicGen.

Backend(*args, **kwargs)

What the adapter needs from a library that runs a loaded MusicGen model.

MlxBackend(worker, model_variant, info)

MusicGen on MLX, in the mlx-examples runtime's own process (text only).

Sampling(*, temperature, top_k, top_p, guidance)

Sampling settings shared by every MusicGen mode.

TransformersBackend(model, processor)

A loaded transformers Musicgen(Melody)ForConditionalGeneration and its processor.

class arioso.platforms.musicgen.backends.AudiocraftBackend(model)[source]#

Bases: object

A loaded audiocraft.models.MusicGen.

classmethod can_continue(model_variant)[source]#

Every checkpoint takes an audio prompt to extend.

Return type:

bool

continuation(prompt, tail, *, seconds, sampling)[source]#

Continue tail (mono, at sample_rate) by seconds.

Parameters:
  • prompt (str) – Text description of the music.

  • tail (ndarray) – The audio prompt to continue.

  • seconds (float) – Length of new audio to generate after tail.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The re-decoded tail followed by the new audio, mono.

property frame_rate: int#

Codec frames per second (50 for every MusicGen checkpoint).

classmethod load(model_variant)[source]#

Load model_variant (e.g. 'facebook/musicgen-small') with audiocraft.

Parameters:

model_variant (str) – A MusicGen checkpoint name.

Return type:

AudiocraftBackend

Returns:

The backend wrapping the loaded model.

Raises:

ImportError – If audiocraft is not installed.

melody(prompt, ref, *, seconds, sampling)[source]#

Generate from a text prompt, following the chroma of ref.

Parameters:
  • prompt (str) – Text description of the music.

  • ref (AudioRef) – The audio whose melody to follow.

  • seconds (float) – Length to generate.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The audio, (channels, frames).

property sample_rate: int#

Output samples per second.

text(prompt, *, seconds, sampling)[source]#

Generate from a text prompt.

Parameters:
  • prompt (str) – Text description of the music.

  • seconds (float) – Length to generate.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The audio, (channels, frames).

arioso.platforms.musicgen.backends.BACKENDS = {'audiocraft': <class 'arioso.platforms.musicgen.backends.AudiocraftBackend'>, 'mlx': <class 'arioso.platforms.musicgen.backends.MlxBackend'>, 'transformers': <class 'arioso.platforms.musicgen.backends.TransformersBackend'>}#

The backends by the name $ARIOSO_MUSICGEN_BACKEND takes.

arioso.platforms.musicgen.backends.BACKEND_ENV_VAR = 'ARIOSO_MUSICGEN_BACKEND'#

audiocraft, transformers or mlx.

Type:

Forces a backend

class arioso.platforms.musicgen.backends.Backend(*args, **kwargs)[source]#

Bases: Protocol

What the adapter needs from a library that runs a loaded MusicGen model.

classmethod can_continue(model_variant)[source]#

Whether this library can continue audio with model_variant (cheap: loads nothing).

Return type:

bool

continuation(prompt, tail, *, seconds, sampling)[source]#

The re-decoded tail and seconds after it, mono.

Return type:

ndarray

property frame_rate: int#

Codec frames per second.

melody(prompt, ref, *, seconds, sampling)[source]#

seconds of audio for prompt that follows the chroma of ref.

Return type:

ndarray

property sample_rate: int#

Output samples per second.

text(prompt, *, seconds, sampling)[source]#

seconds of audio for the text prompt.

Return type:

ndarray

arioso.platforms.musicgen.backends.DEFAULT_FRAME_RATE = 50#

Codec frames per second, used when a model does not report its own.

arioso.platforms.musicgen.backends.MLX_CHECKPOINTS = frozenset({'facebook/musicgen-large', 'facebook/musicgen-medium', 'facebook/musicgen-small'})#

The checkpoints the MLX port loads.

arioso.platforms.musicgen.backends.MLX_MAX_SECONDS = 30.0#

MusicGen’s context in seconds (1500 frames); the MLX port does not stop there itself.

class arioso.platforms.musicgen.backends.MlxBackend(worker, model_variant, info)[source]#

Bases: object

MusicGen on MLX, in the mlx-examples runtime’s own process (text only).

The model lives in a Worker running arioso/_workers/musicgen_mlx_worker.py; audio comes back through a .npy file. top_p has no MLX counterpart (a non-zero one is warned about and ignored).

classmethod available(model_variant)[source]#

Apple Silicon, a supported checkpoint, and the runtime installed at its pinned commit.

A runtime at another commit (or with edits) is not available: a warning says how to move it, and selection falls back to the next backend rather than failing a take that another backend can make.

Return type:

bool

classmethod can_continue(model_variant)[source]#

No: the MLX port generates from text only.

Return type:

bool

close()[source]#

Stop the worker (the model is released).

continuation(prompt, tail, *, seconds, sampling)[source]#

Not on MLX: the selection sends continuations to another backend.

property frame_rate: int#

Codec frames per second.

classmethod load(model_variant, *, make_worker=None)[source]#

Start the worker and load model_variant in it.

Raises:
  • ValueError – For a checkpoint the MLX port does not run.

  • arioso._isolated.RuntimeNotInstalled – With the commands that install it.

Return type:

MlxBackend

melody(prompt, ref, *, seconds, sampling)[source]#

Not on MLX: the selection sends melody takes to another backend.

static runtime()[source]#

The mlx-examples runtime (see the musicgen config’s mlx_runtime).

property sample_rate: int#

Output samples per second.

classmethod supports(model_variant)[source]#

Whether the MLX port runs model_variant: Meta’s mono text checkpoints only (the port reads their audiocraft state_dict.bin; fine-tunes in transformers format have none).

Return type:

bool

text(prompt, *, seconds, sampling)[source]#

seconds of mono audio for prompt, (frames,).

Return type:

ndarray

class arioso.platforms.musicgen.backends.Sampling(*, temperature, top_k, top_p, guidance)[source]#

Bases: object

Sampling settings shared by every MusicGen mode.

arioso.platforms.musicgen.backends.TASKS = ('text', 'melody', 'continuation')#

plain text, follow a melody, or continue audio.

Type:

What a backend is asked to do

class arioso.platforms.musicgen.backends.TransformersBackend(model, processor)[source]#

Bases: object

A loaded transformers Musicgen(Melody)ForConditionalGeneration and its processor.

property audio_channels: int#

Channels the decoder generates (2 for the stereo checkpoints); 1 when its config does not say, or it has none.

classmethod can_continue(model_variant)[source]#

Not a melody checkpoint: its audio input is melody chroma, not a prompt to extend.

Return type:

bool

continuation(prompt, tail, *, seconds, sampling)[source]#

Continue tail (mono, at sample_rate) by seconds.

Parameters:
  • prompt (str) – Text description of the music.

  • tail (ndarray) – The audio prompt to continue (duplicated for a stereo model).

  • seconds (float) – Length of new audio to generate after tail.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The re-decoded tail followed by the new audio, mono.

property delay_frames: int#

Frames the codebook delay pattern consumes from max_new_tokens (3 for the 4-codebook checkpoints), so a call yields the seconds asked for.

property frame_rate: int#

Codec frames per second (50 for every MusicGen checkpoint).

classmethod load(model_variant)[source]#

Load model_variant and its processor with transformers, on _transformers_device().

Parameters:

model_variant (str) – A MusicGen checkpoint name; a melody variant gets the melody-specific model class.

Return type:

TransformersBackend

Returns:

The backend wrapping the loaded model and processor.

melody(prompt, ref, *, seconds, sampling)[source]#

Generate from a text prompt, following the chroma of ref.

Parameters:
  • prompt (str) – Text description of the music.

  • ref (AudioRef) – The audio whose melody to follow, resampled to the processor’s rate.

  • seconds (float) – Length to generate.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The first channel of the audio, (frames,).

property sample_rate: int#

Output samples per second.

text(prompt, *, seconds, sampling)[source]#

Generate from a text prompt.

Parameters:
  • prompt (str) – Text description of the music.

  • seconds (float) – Length to generate.

  • sampling (Sampling) – Sampling settings.

Return type:

ndarray

Returns:

The first channel of the audio, (frames,).

arioso.platforms.musicgen.backends.select_backend_class(model_variant, *, task='text')[source]#

The library that runs model_variant for task (see the module docstring).

Loads no checkpoint, so it can be asked before a multi-GB one is read; a present but broken audiocraft install falls back like a missing one.

Return type:

type[Backend]