Skip to content

campplus: write the graph's constants before every run, not only the first - #1284

Merged
0xShug0 merged 2 commits into
0xShug0:mainfrom
magnus-dev-melin:campplus-rewrite-constants-every-run
Nov 14, 2026
Merged

0xShug0 merged 2 commits into
0xShug0:mainfrom
magnus-dev-melin:campplus-rewrite-constants-every-run

Conversation

@magnus-dev-melin

Copy link
Copy Markdown
Contributor

The CAMPPlus graph creates its constant tensors (the segment-pooling correction factors) with DeferredTensorWriter, which writes them once, right after the graph is allocated. They are not graph inputs, so ggml_gallocr may hand their memory to intermediate results during the compute. The runner is cached per frame count and reused, and run() only rewrote the input tensor, so every embedding after the first one on a runner was computed with whatever the previous compute left in those tensors. run() now flushes the writer before every compute. Shared framework module; affects every model that uses it (Chatterbox cloning and VC, IndexTTS2, Seed-VC). No API or option changes.

Affects outputs: the first embedding on a runner is unchanged; every later one now equals what a fresh runner gives.

Validation (macOS, M1 Max, Metal, chatterbox_q8_0, legacy server, OMP_NUM_THREADS=4; main at a91e36b against this branch):

  • CAMPPlus embedding, the same 14 s reference prepared repeatedly with another voice in between (conditionals_cache_slots 0, so every request prepares again; embedding dumped from ChatterboxConditionalsComponent::prepare): on main the first embedding had a norm of 16.49 and later ones 21.8–22.4, with cosine similarity to each other as low as 0.89 (against 0.49–0.59 between two different speakers). With this branch every preparation of that reference gave the bit-identical embedding of the first one, and the other voice its own, also stable. The other prepared conditionals (prompt mel, prompt tokens, voice-encoder embedding, T3 prompt tokens) were already identical on main.
  • Chatterbox VC (/v1/tasks/run, vc_reference_cache_slots 0, seed 7 in the request, targets A, A, B, A): main gave a different WAV for each A after the first, and its B, prepared after A, differed from this branch's; this branch gave the first A's WAV every time (SHA-256 equal to main's first A) and a stable B.
  • Chatterbox cloning (/v1/audio/speech, seed 7, the same voice in de three times and da three times): main gave a different WAV on each request; this branch gave the same WAV every time, equal to the one main gives with its default cache (one slot), where the voice is prepared once.
  • Request times unchanged (the flush writes a few short vectors).
  • model_spec_system_test, cli_request_options_test and tools/check_loader_catalog_sync.py pass.

Not measured: CUDA/Vulkan (the change is backend-independent); IndexTTS2 and Seed-VC were not built or run here (this build has Chatterbox only). tests/glm_tts/campplus_shared_default_probe.cpp computes a single embedding, so it could not catch this; running the encoder twice there, with another input in between, would. I did not change it, as I have no standalone CAMPPlus weights here to run it with.

@0xShug0

0xShug0 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

@magnus-dev-melin Thanks for the PR. The issue is real. However, the original PR calls writer_.flush() before every CAMPPlus run. That uploads 53 small constant tensors separately. On CUDA, each upload performs a host-to-device copy followed by a stream synchronization. Those repeated calls add roughly 0.19–0.26 ms per complete embedding, causing the measured 6–7% slowdown (very minor overhead).

I updated the PR directly to preserve the constants’ storage instead of uploading them before every run. Both approaches fix the correctness issue. The updated approach avoids that overhead, with correctness verified on CPU, CUDA, and Vulkan.

50 measured runs per setting after warmup:

Backend Frames Main PR c703e59 PR 44697c7
CUDA 250 2.729 ms 2.921 ms 2.697 ms
CUDA 1,400 4.294 ms 4.556 ms 4.285 ms

PR merged!

@0xShug0
0xShug0 merged commit ed0e87a into 0xShug0:main Nov 14, 2026
7 checks passed
@magnus-dev-melin

Copy link
Copy Markdown
Contributor Author

Great catch, preserving the storage is the better fix. Thanks for verifying it on CUDA and Vulkan, and for merging both!

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