Skip to content

fix(tenstorrent): keep fp32 bias precision through block-float forward (#3863) - #3889

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3863
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3863

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3863

Root cause

TTBlockfloatLinear.forward narrowed the stored FP32 bias with
self.bias.to(x.dtype) before either the kernel or the dense fallback ran, and
the fallback dequantised the packed grid with self.dequantize(dtype=x.dtype)
before F.linear. The CUDA and HIP bindings also rounded the bias to the
activation dtype (bias->to(x.scalar_type()) in
tt_blockfloat_hip_torch.cpp, bias dtype must match x in
tt_blockfloat_cuda_torch.cpp) before their float32 epilogues. A finite result
was therefore lost to intermediate rounding/overflow: BF16 returned 0.0
instead of 0.5, FP16 returned -inf instead of 0, and the tied head
returned nan instead of 0.

Fix

  • TTBlockfloatLinear.forward: pass the stored FP32 bias to _kernel_linear
    unchanged. 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 shared
    grid, then one cast (this path has no bias).
  • CUDA binding/kernel and HIP binding/kernels: bias is now a float32 pointer
    added to the existing float32 accumulator before the single output cast. The
    bindings convert the bias tensor to float32 at the boundary.
  • The HIP standalone selftest and verify_against_torch.py follow the same
    bias contract.

No GPTQMODEL_* behaviour switches were added. No dense/dequantised weight is
retained: 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).

path case before after FP32 oracle
CPU dense fallback BF16 1024 - 1023.5 0.0 0.5 0.5
CPU dense fallback FP16 65536 - 65536 -inf 0.0 0.0
CPU tied head FP16 65536 - 65536 nan 0.0 0.0
CUDA RTX 4090 native BF16 cancellation 0.0 0.5 0.5
CUDA RTX 4090 native FP16 range -inf 0.0 0.0
ROCm R9700 native HIP BF16 cancellation 0.0 0.5 0.5
ROCm R9700 native HIP FP16 range -inf 0.0 0.0

Tests

New tests/test_tt_blockfloat_precision.py (fails on daa5dae; the native
cases raise bias dtype must match x before the fix and the CPU cases return
the 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.

# CUDA venv (RTX 4090, GPU-d458708b-...)
PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_tt_blockfloat_precision.py -q
5 passed, 2 skipped

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_tt_blockfloat_cuda.py tests/test_tt_blockfloat_cuda_dispatch.py -q
86 passed, 1 skipped

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_tenstorrent_support.py -q
113 passed, 15 skipped

# ROCm venv (R9700, HIP_VISIBLE_DEVICES=2)
HIP_VISIBLE_DEVICES=2 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_tt_blockfloat_precision.py -q
5 passed, 2 skipped

HIP_VISIBLE_DEVICES=2 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_tt_blockfloat_hip.py -q
64 passed

Standalone HIP harness (recompiled with the changed bias contract):

hipcc --offload-arch=gfx1201 -O3 -std=c++17 tt_blockfloat_hip.hip tt_blockfloat_hip_selftest.cpp -o ttbf_selftest
./ttbf_selftest --dump DIR   # OK (0 failures), 57 PASS
python verify_against_torch.py DIR
bf4 linear m=32 norm_l1 0 PASS
bf4 linear bf16 (WMMA) norm_l1 0 PASS

Hardware / environment

  • NVIDIA RTX 4090, CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, torch 2.16.0.dev+cu130
  • AMD Radeon AI PRO R9700, HIP_VISIBLE_DEVICES=2, torch 2.16.0.dev+rocm10.1

Reviewer 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 (bias is const float*), but the public
Python surface (linear(x, codes, exponents, bits, bias=None)) is unchanged
and still accepts any floating bias dtype.

#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.
@Qubitium
Qubitium merged commit 812178b into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3863 branch November 14, 2026 08:27
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.

TT block-float forwards narrow weights and FP32 bias before the final cast

1 participant