{{ message }}
Repository navigation
fix(tts): PositionalEmbedding.inv_freq silently loses precision under bf16-true - #16397
Open
tritsystem wants to merge 1 commit into
Open
tritsystem wants to merge 1 commit into
tritsystem wants to merge 1 commit into
Conversation
…of default dtype PositionalEmbedding (nemo/collections/tts/modules/transformer.py), used by FFTransformerEncoder/Decoder, built its inv_freq buffer with torch.arange(0.0, demb, 2.0) and no explicit dtype. PyTorch Lightning's "bf16-true" precision setting calls torch.set_default_dtype(torch.bfloat16) for the duration of model construction (the same hazard already documented in nemo/collections/speechlm2/parts/precision.py's fp32_precision()), so under that precision mode the division-then-power-of-10000 computation ran and was stored in bfloat16, silently losing precision in the positional frequencies (verified up to ~0.67% relative error vs. the float32 reference). Fix: always build inv_freq in float32. forward() now upcasts pos_seq to float32 for the sinusoid computation and casts the result back to the caller's original dtype, so a bf16/fp16 pos_seq (as produced by callers via pos_seq.to(inp.dtype)) still matches inv_freq's dtype for the matmul and the module's output dtype is unchanged for callers. Verified by reproducing the bug directly against the unmodified source file (buffer came out bfloat16 with measurable error vs. a float32 reference), then confirming the fix is dtype-correct and precision-correct for both the buffer and a full forward() pass under bf16 and fp32 inputs. Added a regression test covering both. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: tritsystem <gbranaa4@gmail.com>
4 tasks done
This branch has not been deployed
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.

Replaces #16334, which GitHub closed automatically after my DCO sign-off push accidentally rewrote the commit without its parent. Same change, now with the sign-off; no code changes.
Summary
PositionalEmbeddinginnemo/collections/tts/modules/transformer.py(used byFFTransformerEncoder/FFTransformerDecoder, e.g. vianemo/collections/tts/modules/adapters.pyandnemo/collections/tts/models/fastpitch_ssl.py) builds itsinv_freqbuffer with:with no explicit
dtype. PyTorch Lightning'sbf16-trueprecision setting callstorch.set_default_dtype(torch.bfloat16)for the duration of model construction — this is the exact hazard already documented in this repo's ownnemo/collections/speechlm2/parts/precision.py(fp32_precision()docstring: "In bf16-true, PTL changes PyTorch's default dtype, which may break implicit assumptions for some models.").Under that precision mode,
torch.arange(0.0, demb, 2.0)(and the division/power that follows) runs and gets stored inbfloat16instead offloat32, silently losing precision in the sinusoidal/RoPE-style positional frequencies — no crash, no warning, just a quieter/less precise positional signal.I reproduced this directly against the unmodified module: constructing
PositionalEmbeddingundertorch.set_default_dtype(torch.bfloat16)(matching what PTL'sbf16-truedoes) produces aninv_freqbuffer inbfloat16with up to ~0.67% relative error vs. the float32 reference.Fix
inv_freqis now always built withdtype=torch.float32, independent of the ambient default dtype.forward()upcastspos_seqtofloat32for the sinusoid computation and casts the result back to the caller's original dtype before returning — this keeps the output dtype unchanged for both call sites in the same file (both of which already dopos_seq.to(inp.dtype)before calling in), and avoids a dtype-mismatch in thematmulthat would otherwise occur between a half-precisionpos_seqand a now-always-float32inv_freq.Test plan
PositionalEmbeddingclass (buffer dtype + numeric drift vs. float32 reference).inv_freqisfloat32with zero drift under the same bf16-default-dtype construction.forward()end-to-end with bothbfloat16andfloat32pos_seqinputs, confirming matching output dtype and no crash.tests/collections/tts/modules/test_transformer_posemb_dtype.pycovering both the buffer dtype and the numeric match against a float32 reference under a bf16 default dtype.🤖 Generated with Claude Code