Skip to content

Do not script the codec Snake activation: first call after loading differs from later calls - #2

Merged
tiamojames merged 1 commit into
ASLP-lab:mainfrom
hiroki-abe-58:fix/deterministic-snake
Oct 18, 2026
Merged

tiamojames merged 1 commit into
ASLP-lab:mainfrom
hiroki-abe-58:fix/deterministic-snake

Conversation

@hiroki-abe-58

Copy link
Copy Markdown
Contributor

Problem

With the released checkpoint, the same request and seed gives different audio on the first generation after loading than on every later one in the same process.

Reproduction on current main (e8bee27), RTX 5090, torch 2.8.0+cu128, bfloat16 (script at the end):

call 1: patches=69 samples=243432 sha256=e6caa13d3ce3
call 2: patches=69 samples=243432 sha256=d63c4d2adad3
call 3: patches=69 samples=243432 sha256=d63c4d2adad3
call 4: patches=69 samples=243432 sha256=d63c4d2adad3

On torch 2.14.1 the same request gave 69 patches on call 1 and 67 on calls 2 to 4 (the stop point moved). Practical effect: a one-off command-line run, the first example of infer_examples.sh, and a long-running service disagree for the same inputs, and results depend on what was generated before.

Cause

savae.snake is decorated with @torch.jit.script. TorchScript re-optimizes a scripted function after its first calls, and the optimized version gives slightly different float32 results. It enters through the reference-audio encoder (SemanticAcousticVAE.encode -> Snake1d -> snake; the prompt latent is sampled and then conditions the autoregressive loop, which amplifies the difference) and, at rounding level, through the causal decoder.

Bisection: with the same seed, the prompt latents from _load_prompt differ between call 1 and calls 2 and 3 (4a7d814884 vs 9b237ae6d5), and so do the first generated patches. With only this function replaced by the same formula in plain ops, every call gives identical latents and audio. (I did not look inside the TorchScript executor; the evidence is this on/off experiment.)

Change

Remove the decorator from snake (same operations), plus a comment saying why. Nothing else.

Result

Same machine, torch 2.8.0+cu128, this branch:

call 1: patches=62 samples=218736 sha256=3aa3d4078fa7
call 2: patches=62 samples=218736 sha256=3aa3d4078fa7
call 3: patches=62 samples=218736 sha256=3aa3d4078fa7
call 4: patches=62 samples=218736 sha256=3aa3d4078fa7

and the same hash again in a second process. scripts/infer_examples.py on this branch generates all 10 prepared examples. The plain-ops result is not bit-identical to either the first-call or the later-call output of the scripted version (different float32 rounding, as between torch versions), so audio regenerated with this change differs from today's at that level. On the four TTS examples a Whisper-small round trip gave the same error rate with and without the change (0.04 for tts_zh_r001, 0 for the other three), so I saw no intelligibility change; I did not do a listening test.

Cost, measured on the codec alone (torch 2.8.0, RTX 5090): decoding 16 s of audio 37.5 ms -> 47.0 ms, encoding 5 s of reference 9.6 ms -> 11.1 ms, peak memory unchanged; generating that audio takes tens of seconds. Recent torch versions also print a torch.jit.script is deprecated FutureWarning on import of this module (seen on 2.14).

Notes

  • The repository has no test setup, so I did not add a test; the script below is the check. I am happy to turn it into one if you want.
  • If you would rather keep TorchScript, say so and I will close this. The only thing I would suggest in that case is a sentence in docs/INFERENCE.md that the first call after loading can differ.
# repro_call_history.py <SemBridge checkout> <model folder or ASLP-lab/SemBridge>
import hashlib, sys, torch
sys.path.insert(0, sys.argv[1])
from sembridge import SemBridge
from sembridge.requests import load_request

model = SemBridge.from_pretrained(sys.argv[2], device="cuda", dtype="bfloat16")
request = load_request(f"{sys.argv[1]}/examples/tts/zh_r001.json")
print("torch", torch.__version__)
for call in range(1, 5):
    wave, patches = model.generate(decoder="auto", **request)
    print(f"call {call}: patches={patches.shape[1]} samples={wave.numel()} sha256={hashlib.sha256(wave.numpy().tobytes()).hexdigest()[:12]}")

@torch.jit.script is re-optimized after the first calls and the optimized version rounds differently, so the first generation after loading differed from later ones for the same request and seed. Plain ops give identical results on every call.
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