from_pretrained defaults to device='cuda' and blocks CPU fallback under tight VRAM
Summary
LFM2AudioModel.from_pretrained defaults to device="cuda" and uses init_on_device(device, include_buffers=True) as a context manager during construction. This means a post-hoc model = LFM2AudioModel.from_pretrained(repo).to("cpu") does not work as a CPU fallback — the model is already (partially) constructed on CUDA before .to("cpu") runs, and OOMs when CUDA memory is tight.
This is a problem for any environment where the GPU is shared with another process (e.g. an inference server camped on a GPU). The model can't be loaded at all without explicitly passing device="cpu" to from_pretrained, even when CPU is the user's intent.
Environment
liquid-audio1.2.0 (pip install liquid-audio)torch2.11.0+cu130transformers5.8.0- Python 3.12.3, Linux
- GPU: NVIDIA RTX 5090 (32 GiB), but only ~14 MiB free at test time (vLLM resident in the other 27 GiB)
Reproducer
In a coresident-GPU scenario (or just any scenario where you want CPU):
from liquid_audio import LFM2AudioModel
# Intent: load on CPU because GPU is busy
model = LFM2AudioModel.from_pretrained("LiquidAI/LFM2.5-Audio-1.5B").to("cpu")
Result:
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 64.00 MiB.
GPU 0 has a total capacity of 31.35 GiB of which 14.06 MiB is free.
Process 719326 has 27.23 GiB memory in use.
...
File ".../liquid_audio/model/lfm2_audio.py", line 160, in from_pretrained
model = cls(conf).to(device=device, dtype=dtype)
File ".../transformers/models/lfm2/modeling_lfm2.py", line 148, in __init__
self.w3 = nn.Linear(config.hidden_size, intermediate_size, bias=False)
The OOM happens inside cls(conf), before the user's .to("cpu") is reached, because init_on_device("cuda") is the active context.
Source location
liquid_audio/model/lfm2_audio.py lines 136–164:
def from_pretrained(
cls,
repo_id: str | Path,
*,
revision: str | None = None,
dtype: torch.dtype = torch.bfloat16,
device: torch.device | str = "cuda", # <-- default
) -> Self:
...
if isinstance(device, str):
device = torch.device(device)
with init_on_device(device, include_buffers=True):
model = cls(conf).to(device=device, dtype=dtype)
...
Workaround
Always pass device="cpu" explicitly:
model = LFM2AudioModel.from_pretrained(
"LiquidAI/LFM2.5-Audio-1.5B",
device="cpu",
)
But this isn't discoverable from the README, which uses the default-device form throughout.
Suggested fixes (any one is fine)
- Auto-fallback default. Change to
device: torch.device | str | None = None, then:
This already covers most non-GPU users.if device is None: device = "cuda" if torch.cuda.is_available() else "cpu" - VRAM-aware fallback. If
device == "cuda"andtorch.cuda.mem_get_info()[0]is below an estimated weight footprint, warn and fall back to CPU. - Documentation. Add a "Loading on CPU / under tight VRAM" section to the README that calls out the
device=kwarg explicitly. The.to(...)post-hoc pattern is so well-established in the PyTorch ecosystem that the current trap is genuinely surprising.
Happy to send a PR for option 1 if useful.
Related: #26 covers the same default-CUDA trap in LFM2AudioProcessor.audio_detokenizer. Together the two issues are why CPU runs currently need two distinct workarounds.
Hi, CPU inference is not supported in this repo, this model implementation is simply not optimized for it. This is why we default to and only support CUDA here.
For the CPU-optimized version, please see the GGUF version: https://huggingface.co/LiquidAI/LFM2.5-Audio-1.5B-GGUF