Repository navigation
Do not script the codec Snake activation: first call after loading differs from later calls - #2
Merged
Conversation
@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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):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.snakeis 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_promptdiffer between call 1 and calls 2 and 3 (4a7d814884vs9b237ae6d5), 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:
and the same hash again in a second process.
scripts/infer_examples.pyon 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 fortts_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 deprecatedFutureWarning on import of this module (seen on 2.14).Notes
docs/INFERENCE.mdthat the first call after loading can differ.