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
Codec frames per second, used when a model does not report its own. |
|
|
|
MusicGen's context in seconds (1500 frames); the MLX port does not stop there itself. |
|
The checkpoints the MLX port loads. |
|
plain text, follow a melody, or continue audio. |
|
The backends by the name |
Functions
|
The library that runs |
Classes
|
A loaded |
|
What the adapter needs from a library that runs a loaded MusicGen model. |
|
MusicGen on MLX, in the |
|
Sampling settings shared by every MusicGen mode. |
|
A loaded transformers |
- class arioso.platforms.musicgen.backends.AudiocraftBackend(model)[source]#
Bases:
objectA loaded
audiocraft.models.MusicGen.- classmethod can_continue(model_variant)[source]#
Every checkpoint takes an audio prompt to extend.
- Return type:
- continuation(prompt, tail, *, seconds, sampling)[source]#
Continue
tail(mono, atsample_rate) byseconds.
- 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:
- Returns:
The backend wrapping the loaded model.
- Raises:
ImportError – If audiocraft is not installed.
- 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_BACKENDtakes.
- arioso.platforms.musicgen.backends.BACKEND_ENV_VAR = 'ARIOSO_MUSICGEN_BACKEND'#
audiocraft,transformersormlx.- Type:
Forces a backend
- class arioso.platforms.musicgen.backends.Backend(*args, **kwargs)[source]#
Bases:
ProtocolWhat 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:
- continuation(prompt, tail, *, seconds, sampling)[source]#
The re-decoded
tailandsecondsafter it, mono.- 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:
objectMusicGen on MLX, in the
mlx-examplesruntime’s own process (text only).The model lives in a
Workerrunningarioso/_workers/musicgen_mlx_worker.py; audio comes back through a.npyfile.top_phas 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:
- classmethod can_continue(model_variant)[source]#
No: the MLX port generates from text only.
- Return type:
- continuation(prompt, tail, *, seconds, sampling)[source]#
Not on MLX: the selection sends continuations to another backend.
- classmethod load(model_variant, *, make_worker=None)[source]#
Start the worker and load
model_variantin it.- Raises:
ValueError – For a checkpoint the MLX port does not run.
arioso._isolated.RuntimeNotInstalled – With the commands that install it.
- Return type:
- melody(prompt, ref, *, seconds, sampling)[source]#
Not on MLX: the selection sends melody takes to another backend.
- class arioso.platforms.musicgen.backends.Sampling(*, temperature, top_k, top_p, guidance)[source]#
Bases:
objectSampling 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:
objectA loaded transformers
Musicgen(Melody)ForConditionalGenerationand 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:
- continuation(prompt, tail, *, seconds, sampling)[source]#
Continue
tail(mono, atsample_rate) byseconds.- Parameters:
- Return type:
ndarray- Returns:
The re-decoded
tailfollowed 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.
- classmethod load(model_variant)[source]#
Load
model_variantand 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:
- Returns:
The backend wrapping the loaded model and processor.
- arioso.platforms.musicgen.backends.select_backend_class(model_variant, *, task='text')[source]#
The library that runs
model_variantfortask(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.