Repository navigation
fix(tenstorrent): keep fp32 bias precision through block-float forward (#3863) - #3889
Merged
Merged
Conversation
#3863) TTBlockfloatLinear.forward narrowed the stored float32 bias to the activation dtype before the kernel or fallback ran, and the dense fallback dequantised the packed grid to that dtype before F.linear. A bf16 cancellation case therefore returned 0.0 instead of 0.5, an fp16 range case returned -inf instead of 0, and the tied-head fallback returned nan for a grid whose fp32 dot is exactly zero. The fallback now accumulates the grid and the stored float32 bias in float32 and casts once; the tied head does the same for its shared weight. Both native bindings now take a float32 bias and add it to their existing float32 accumulator before the single output cast, so a finite bias outside the activation dtype's range no longer becomes inf. The HIP standalone selftest and its torch verifier follow the same bias contract.
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.
Fixes #3863
Root cause
TTBlockfloatLinear.forwardnarrowed the stored FP32 bias withself.bias.to(x.dtype)before either the kernel or the dense fallback ran, andthe fallback dequantised the packed grid with
self.dequantize(dtype=x.dtype)before
F.linear. The CUDA and HIP bindings also rounded the bias to theactivation dtype (
bias->to(x.scalar_type())intt_blockfloat_hip_torch.cpp,bias dtype must match xintt_blockfloat_cuda_torch.cpp) before their float32 epilogues. A finite resultwas therefore lost to intermediate rounding/overflow: BF16 returned
0.0instead of
0.5, FP16 returned-infinstead of0, and the tied headreturned
naninstead of0.Fix
TTBlockfloatLinear.forward: pass the stored FP32 bias to_kernel_linearunchanged. The dense fallback accumulates the grid and bias in float32 for
float16/bfloat16 inputs and casts the output once.
TTBlockfloatTiedHead.forward: same float32 accumulation for the sharedgrid, then one cast (this path has no bias).
biasis now a float32 pointeradded to the existing float32 accumulator before the single output cast. The
bindings convert the bias tensor to float32 at the boundary.
verify_against_torch.pyfollow the samebias contract.
No
GPTQMODEL_*behaviour switches were added. No dense/dequantised weight isretained: the fallback deletes its temporary weight and both native paths still
read the packed codes.
Before / after
Exact issue snippets (
bits=8, FP32 checkpoint, activation dtype as shown)."Before" values were captured from clean
origin/main(daa5dae).1024 - 1023.50.00.50.565536 - 65536-inf0.00.065536 - 65536nan0.00.00.00.50.5-inf0.00.00.00.50.5-inf0.00.0Tests
New
tests/test_tt_blockfloat_precision.py(fails ondaa5dae; the nativecases raise
bias dtype must match xbefore the fix and the CPU cases returnthe wrong value). It asserts against an independent float64 GEMM over the exact
grid plus the stored FP32 bias, covers the dense fallback and both native
kernels, and checks that the fallback releases its temporary dense weight.
Standalone HIP harness (recompiled with the changed bias contract):
Hardware / environment
CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, torch 2.16.0.dev+cu130HIP_VISIBLE_DEVICES=2, torch 2.16.0.dev+rocm10.1Reviewer notes
No logic-flow diagrams: this is a localized numerical fix inside the forward
and binding epilogues, which the logic-flow skill lists as not required.
The native kernel signatures change (
biasisconst float*), but the publicPython surface (
linear(x, codes, exponents, bits, bias=None)) is unchangedand still accepts any floating bias dtype.