Skip to content

Reduce MOSS-TTS v1.5 GPU memory usage - #674

Merged
0xShug0 merged 3 commits into
mainfrom
perf/moss-tts-v15-vram-optimizations
Sep 24, 2026
Merged

0xShug0 merged 3 commits into
mainfrom
perf/moss-tts-v15-vram-optimizations

Conversation

@0xShug0

@0xShug0 0xShug0 commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Use FP16 storage for the codec transformer on CUDA, Vulkan, and Metal because it substantially reduces the codec-stage memory peak without slowing inference. CPU and HIP storage remain F32.

Workload F32 codec peak VRAM FP16 codec peak VRAM F32 codec RTF FP16 codec RTF
Plain TTS 13,188 MiB 11,496 MiB 0.2274 0.2286
Voice cloning (warm RTF) 16,930 MiB 13,444 MiB 0.2095 0.2066

Use FP16 KV cache on CUDA, Vulkan, and Metal because it is the fastest cache type tested. The framework default and the MOSS-TTS-v1.5 CPU and HIP paths remain F32. Unused host-side cache import scratch is allocated lazily.

KV cache Warm clone latency Warm median RTF Peak VRAM Decision
F32 677.6 ms 0.2066 13,444 MiB Existing baseline
FP16 655.4 ms 0.1998 13,448 MiB Selected
BF16 667.0 ms 0.2033 13,438 MiB Slower than FP16

@0xShug0

0xShug0 commented Sep 24, 2026

Copy link
Copy Markdown
Owner Author

@christopherthompson81 I think a simple change to reduce VRAM usage is to use the FP16 codec. An FP16 KV cache gives only a small VRAM reduction, but it was faster in my tests. I’m not sure about the parity impact compared with Python. Could you take a look at this?

@pnnbao97 These two optimizations may also be relevant to Vieneu TTS. You can opt into FP16 in MossAudioTokenizerCodecRuntime and set QwenCausalDecoderConfig::static_cache_type to FP16.

@christopherthompson81

christopherthompson81 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Tested both commits separately on an RTX 3090. They hold up and the PR does more than it claims — one thing I would change, and one number you asked for on #652 that I can now give you.

The change I am suggesting is inline on the diff as review suggestions, so you can apply or dismiss each one.

The one thing I would raise: this ships f16 to four backends on one backend's evidence

Both gates read backend_type() == core::BackendType::Cpu ? F32 : F16, and BackendType is {Cpu, Cuda, Hip, Vulkan, Metal, BestAvailable} — so Hip, Vulkan and Metal take the f16 path too, on tables that look like one CUDA card.

I raise it because the tree already declines to do this, in your own words in two places: module.h:22 ("CUDA and HIP share ggml's ggml-cuda implementation for a small set of explicitly verified operators. This is intentionally not a generic GPU test."), and moss_tts_local's resolve_auto_weight_type(), which keeps HIP on the native default because the CUDA-style BF16 path "needs validation with real models first".

And there is a mechanism to doubt, not only a principle: prefill writes its F32 layer K/V into the now-f16 cache through ggml_cpy into a strided 4-D view. Whether that conversion is supported for a non-contiguous destination is a per-backend property, and a gap surfaces at graph compute rather than at graph build — so it lands on a user, not on CI. The PR gates on Cuda alone, which regresses nobody since no released build ever took the f16 path there; core::uses_ggml_cuda_or_hip_backend is the natural widening point once HIP is measured, the way minimax_music3/depth_decoder.cpp:61 picks prefill_mode.

It turns bf16 cloning from OOM into working

While testing your f16 KV suggestion I had this case failing: bf16 cloning died asking for 3,546,439,680 bytes for moss.audio_tokenizer.encoder, 24,975 MiB against 24,090 available. I concluded the encoder was the blocker and that a mem_saver mode would be needed. Your codec commit makes that unnecessary. Same card, package and reference clip:

build result peak VRAM
fc24c99e (base) OOM, encoder weight buffer 19,582 MiB
36d81872 (codec f16) rc=0 19,876 MiB
3eb4cdff (+ KV f16) rc=0 19,756 MiB

The base reproduces the OOM byte for byte on the current tree, and the codec commit alone is what fixes it. Worth putting in the body: the F32 clone figure is above what some 24 GiB cards have, not merely larger.

The KV commit is bit-identical; the codec commit owns the change

q8_0 plain TTS at a fixed seed, with a determinism control run first so the comparison means something:

comparison waveform peak VRAM
base vs base (control) bit-identical 12,864 / 12,864
base vs codec f16 SNR 46.3 dB, max abs 3.57e-03 12,864 → 11,172
codec f16 vs + KV f16 bit-identical 11,172 → 11,138

So the KV commit costs nothing measurable at all, and the codec commit owns both the numerical change and the whole memory win (1,692 MiB — the same figure you measured, on different hardware). The 46.3 dB is decode-only, and I transcribed all three anyway: identical text and identical word end_sample.

Also, clear_on_backend() is a better fix than the one I had reached for — I had sized the zero fill from ggml_nbytes and kept the loop. Deleting it subsumes the retain_prefix(0) that used to follow.

Your codec change is a smaller precision step than it looks

The published v1.5 packages already store the codec at f16 — 1600/1600 tensors, 3.305 GiB — and load_transformer was asking for TensorStorageType::F32. So on the GGUF path the old code was expanding already-f16 values into F32 VRAM, and this is not a fresh rounding of the weights but f16 computed in f32 versus computed in f16. Split by stage it also explains both of your figures exactly:

stage f16 in the GGUF at F32
audio_tokenizer/encoder 1.651 GiB 3.303 GiB
audio_tokenizer/decoder 1.651 GiB 3.303 GiB

Plain TTS prepares only the decoder → 1.651 GiB ≈ the 1,692 MiB we both measured. Cloning prepares both → 3.3 GiB ≈ your 3,486 MiB. And 3.303 GiB is exactly the allocation that used to fail.

The safetensors path is where a genuine new rounding enters, and there is an in-tree precedent for this same v1 codec in docs/community_models/moss_voicegen.md: f32 source 5.0e-7, codec f16 7.1e-4, codec bf16 1.1e-2 against that port's 1e-3 gate. The tensor split looks right — load narrows the matmul weights while load_f32 keeps both norms and both layer scales wide.

Speed: your RTF table and my wall-time number are both right

Drift-cancelled across four binaries (each rep forward then reversed, since this box drifts 20-30%), q8_0 plain TTS, 6 samples per build: base 4415 ms, codec f16 3166 ms, + KV f16 3147 ms — about 29% off one-shot wall time, which looks like it contradicts 0.2274 → 0.2286. Splitting it:

base    session.wall_ms 1331.79   metrics.wall_ms 4448.34   -> prepare 3116.5
+both   session.wall_ms 1326.05   metrics.wall_ms 3141.70   -> prepare 1815.7

Generation is unchanged; prepare is 41.7% faster. metrics.wall_ms is taken immediately before session->prepare(...) (app/cli/main.cpp:944), so warm RTF amortises away the only thing that moved — prepare no longer converts and uploads twice the bytes. Both numbers are right about different workloads, and CLI users plus the first request after a load get about 1.3 s back on an 8B q8_0. Your warm 5090 loop structurally cannot show it, so it may be worth a line in the body.

The encoder is a VRAM tenant, which prices your mem_saver idea

Three identical clone requests in one session (--request-sequence), which is what separates one-time from per-request cost:

req0  step 953.2 ms (84 calls)  prefill 50.2 ms   session 1547.8 ms
req1  step 963.1 ms (84 calls)  prefill 40.6 ms   session 1286.4 ms
req2  step 955.9 ms (84 calls)  prefill 40.2 ms   session 1220.1 ms

Step compute is flat and the first request carries a 327.7 ms premium — the lazy prepare_encoder(). Steady-state clone residual is about 190 ms against plain TTS's 236 ms, so cloning is not more expensive per request once the encoder is resident; encoding a 3.4 s reference is cheap.

So the encoder holds 1.651 GiB of VRAM to do about a tenth of a second of work per request, which prices the mem_saver mode you raised without building it: 1.651 GiB returned for 334.8 ms per request. Easy trade for the case that was OOMing, bad one for a warm server, so opt-in. release_runtime_graphs() frees graphs but not weights; the encoder is a unique_ptr behind require_encoder(), so the hook is a reset() after encode_reference.

For the profile above: MossTtsDelayBackboneRuntime::log_timing() emits ten per-stage counters and nothing in the tree called it, so the delay family had no stage profile. moss_tts_local calls both halves of the pair — reset_timing() at generator.cpp:265 and log_timing() at :401 — and the PR adds the same two lines, which matters because the counters accumulate: with only the report, a second request in one session includes the first one's time. That is also how I found that prefill is only 81 ms (6%) and that the backbone step at 11.06 ms/call is running at roughly 80% of this card's memory bandwidth — i.e. generation has little headroom left at this weight type.

The PR also stops the cache allocating its host import mirror. This family never calls import_state/export_state (prefill copies K/V in on-device) and the mirror is sized in f32 regardless of cache_type, so after your change it is twice what the f16 cache costs. import_state resizes on demand, so it is safe. Measured honestly: 0 MiB on a short request, 59 MiB on a forced single chunk — tidiness more than a win.

Unrelated, but it will confuse someone

#664 merges cleanly with this. But the reporter on #663 suspected weight precision, and I ruled that out by reproducing the drift at f32. Once this lands, GPU v1.5 codec decode runs at f16, so anyone re-running that comparison measures both effects at once. Might be worth a line in the codec docs.

@christopherthompson81 christopherthompson81 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three suggestions, inline. The first two are the substantive one — gating the 16-bit storage on Cuda rather than on "not CPU" — split across the two sites so they can be applied independently. I wrote them without a shared helper variable so each is a self-contained hunk.

Reasoning is in my comment above; the short version is that != Cpu also hands f16 to Hip, Vulkan and Metal, the tree already declines to generalise a dtype decision that way (module.h:22, moss_tts_local's resolve_auto_weight_type()), and prefill's ggml_cpy into a strided 4-D view of the f16 cache is a per-backend property that fails at graph compute rather than at graph build.

I checked that this does not quietly drop the auto-selected path back to F32: execution_context.cpp:49 resolves BestAvailable through core::backend_type(backend_), which returns a concrete Cuda on a CUDA machine and only falls back to BestAvailable for a backend it cannot identify — where F32 is the right answer anyway.

There is a third change I could not make a suggestion out of, because it touches lines outside this diff: MossTtsDelayBackboneRuntime::log_timing() emits ten per-stage counters and nothing in the tree calls it, so the delay family has no stage profile. Adding the pair moss_tts_local uses — reset_timing() at the top of run(), log_timing() before session.wall_ms — is what produced the profile numbers in my comment. Both halves are needed: the counters accumulate, so reporting without resetting makes a second request in one session include the first one's time. Happy to send that separately or leave it to you.

backbone_weight_context_bytes_,
weight_storage_type_);
weight_storage_type_,
execution_context().backend_type() == core::BackendType::Cpu ? GGML_TYPE_F32 : GGML_TYPE_F16);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gate on the backend this was measured on, so Hip, Vulkan and Metal keep F32 until someone runs the same comparison there.

Suggested change
execution_context().backend_type() == core::BackendType::Cpu ? GGML_TYPE_F32 : GGML_TYPE_F16);
execution_context().backend_type() == core::BackendType::Cuda ? GGML_TYPE_F16 : GGML_TYPE_F32);

core::uses_ggml_cuda_or_hip_backend is the natural widening point once HIP is measured, the way minimax_music3/depth_decoder.cpp:61 selects prefill_mode.

Comment on lines +178 to +180
execution_context().backend_type() == core::BackendType::Cpu
? assets::TensorStorageType::F32
: assets::TensorStorageType::F16,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change for the codec storage.

Suggested change
execution_context().backend_type() == core::BackendType::Cpu
? assets::TensorStorageType::F32
: assets::TensorStorageType::F16,
execution_context().backend_type() == core::BackendType::Cuda
? assets::TensorStorageType::F16
: assets::TensorStorageType::F32,

@0xShug0

0xShug0 commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner Author

@christopherthompson81 Good points! I actually ran the tests on both CUDA and Vulkan but only reported CUDA. Let me test on Mac. It should be safe as YuE2 has the same shape. And I will use cache_options.lazy_import_scratch = true. This API is very new (thanks to @mirek190) but it looks like it should become the default pattern for the relevent models.

@0xShug0

0xShug0 commented Sep 24, 2026

Copy link
Copy Markdown
Owner Author

PR updated. Validated that the PR works on Mac.

@0xShug0
0xShug0 merged commit 7c83594 into main Sep 24, 2026
6 checks passed
@0xShug0
0xShug0 deleted the perf/moss-tts-v15-vram-optimizations branch September 24, 2026 16:19
@0xShug0

0xShug0 commented Sep 24, 2026

Copy link
Copy Markdown
Owner Author

@christopherthompson81 Please consider updating your HF repo README...24 GB of VRAM should no longer be a blocker.

@christopherthompson81

Copy link
Copy Markdown
Contributor

@0xShug0 Done. The card is updated: https://huggingface.co/christopherthompson81/MOSS-TTS-v1.5-GGUF

I re-measured on merged main (RTX 3090) and transcribed every clone rather than only checking that it ran:

package peak VRAM, plain TTS peak VRAM, cloning
q4_k 7.3 GiB 9.0 GiB
q8_0 10.9 GiB 12.6 GiB
bf16 17.6 GiB 19.3 GiB

The "bf16 can't clone on 24 GiB" warning is gone. The card also notes that builds from before #674 still run out of memory there, and explains why.

MOSS-TTSD didn't pick this up, because its session never opted in. That's #687, with one twist: TTSD keeps the codec encoder at f32. The f16 encoder is fine for v1.5, whose clones transcribe exactly at every quantization. But TTSD continues directly from the reference codes, and with an f16 encoder a few seeds collapsed to silence or re-spoke the prompt. The details are in the PR.

0xShug0 pushed a commit that referenced this pull request Sep 24, 2026
…f32 (#687)

moss_tts_v15 loads its codec at f16 and its KV cache at f16 on GPU backends,
which is what lets bf16 clone on a 24 GiB card. moss_ttsd shares the codec
and backbone, but didn't opt in, so bf16 cloning still failed allocating
moss.audio_tokenizer.encoder.

The decoder and KV cache come across unchanged. The KV cache is
bit-identical on output, and the decoder is 44.5 dB with identical
transcripts. The encoder does not come across. This model continues
directly from the reference's codes, and an f16 encoder changes 5-14% of
the finer codebooks. Over 26 seeds that produced silence or a re-spoken
prompt 3 times, against none at f32. Add an optional encoder-only storage
type to the codec options for that. It's appended last, so the other
callers are untouched.

With the encoder at f32, the generated codes match main exactly. Cloning
peaks drop by 1.8 GiB and bf16 clones at 21.0 GiB where it failed before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants