Repository navigation
feat(ext): add CPU kernels for Tenstorrent bf8_b/bf4_b/bf2_b block floats - #3808
Conversation
c8faf63 to
497a55a
Compare
497a55a to
6cb4058
Compare
6cb4058 to
7e8a314
Compare
|
Review of head
Design suggestion: a shared packed-weight descriptor carrying format, logical shape, layout and validated buffer sizes would prevent the independent shape/stride calculations that caused findings 4–5. Validation: |
|
Addressed all five findings in
Verification: |
|
The remaining FTZ/DAZ case is fixed in
Reproduction now: |
|
Reviewed current head
Validation: 283 tests passed with the GIL disabled on this AVX2+FMA host; the bundled Torch-helper comparison passed 200 seeds × 4 distributions × 3 formats. A focused shared-read-only-weight PyTorch wrapper probe also passed in three fresh GIL-disabled processes, each using 8 workers for 24 tasks × 8 forwards. This review did not execute AVX-512, non-x86, or the optional CUDA extension, and does not establish blanket thread safety. No repository source edits. |
|
Both remaining findings are addressed in
I also resolved the CodeQL import-style comments ( Verification: |
|
Final consistency pass on top of the review fixes (
Verification at
|
The library executes the Tenstorrent block-float formats directly on CPU; it is not an emulator. Rename the reference page to ttbfp-cpu-kernels.md and update the README, extension README, NOTICE, Makefile and source headers.
Adopt the canonical Tenstorrent block-float names introduced with the native TTNN backend (#3760), defined in hw/dtypes.md: - bf8_b (Bfp8_b / BLOCKFP8) - bf4_b (Bfp4_b / BLOCKFP4) - bf2_b (Bfp2_b / BLOCKFP2) The C enum, Python constants, format strings and examples now use these names. TTBFP_TTBF8/BF4/BF2, ttbfp.TTBF8/BF4/BF2, BFP8/BFP4/BFP2 and the "ttbf8"/"ttbf4"/"ttbf2" format strings remain accepted as deprecated aliases, so existing callers keep working. Also document bf2_b alongside bf4_b/bf8_b in hw/dtypes.md (layout, encode/decode, 0.3125 B/weight storage), note that the CPU kernels implement it even though this host's ttnn build does not expose it, and link the CPU-kernel page from docs/tenstorrent.md. Rename the page to docs/tt-blockfloat-cpu.md and fix the README link. Verified: 252 tests pass; tools/verify_against_tt.py matches upstream tt-llk over 200 seeds x 4 distributions x 3 formats.
- dispatch: split 9..15-row remainders into an 8-row tile plus the remaining 1..7 rows in both the full- and partial-column paths, so M = 16 + r (r = 1..15) writes every output row. - scalar decode: bf2_b uses four 2-bit fields per byte; the nibble path read eight bytes from a four-byte block. - scale: store a normal high factor for e < 7 and fold 2^-6 into the decode, so FTZ/DAZ no longer flushes normal weights through a subnormal scale. The table is FTZ-safe at construction. - python: validate complete format-specific buffer shapes and tile payload sizes before ctypes; reject flat cols that would change the stored row stride (cropping a whole block). - tests: cover every row remainder in both column switches, scalar bf2 decode, ambient FTZ with the table initialized under FTZ, malformed buffers, and the flat-stride crop case. 277 tests pass; tools/verify_against_tt.py still matches upstream.
The library's ftz flag could not preserve genuinely subnormal weights when the calling thread had MXCSR FTZ/DAZ enabled: the final scaling multiply flushed them, and DAZ treated them as zero FMA inputs. Clear FTZ/DAZ for the duration of a call with ftz=False, per OpenMP worker, and restore the caller's MXCSR on return. ftz=True is unchanged. Regression: bf8_b e=1/mag=32 and bf4_b e=1/mag=2 decode to 2^-127; with A=2^126 the result is 0.5 for ftz=False and 0 for ftz=True, on AVX-512, AVX2, scalar and the int8 layout. 283 tests pass.
- Restrict the int8 kernel-native layout (convert_s8/prepack_s8/ matmul_prepacked_s8) to bf8_b. bf4_b/bf2_b would expand the persistent payload 2x/4x; those formats stay in the packed sign-magnitude block-major layout, as required by the quantized-storage rule. - Return zeros from every matmul wrapper when K == 0 instead of returning an uninitialized np.empty buffer; document the C-side K > 0 precondition. - Update the bench scripts and docs to report s8 only for bf8_b and block-major for bf4_b/bf2_b. - Resolve the CodeQL import-style warnings and replace the unused collect_ignore_glob with a pytest_ignore_collect hook. 280 tests pass; upstream-helper comparison still OK.
d56ea7b to
81138fc
Compare
Summary
Adds
gptqmodel_ext/ttbfp/: a standalone, bit-exact CPU implementation ofTenstorrent's
bf8_b/bf4_b/bf2_bblock-float formats. The kernelsexecute the packed block-float representation directly; they are not an
emulator. Models and kernels that use these formats can therefore run on
non-Tenstorrent hardware.
The dtype names match the canonical
bf8_b/bf4_b/bf2_bspelling from themerged native TTNN backend (#3760) and
hw/dtypes.md.TTBFP_TTBF8,ttbfp.TTBF8,BFP8, and the"ttbf8"format strings remain accepted asdeprecated aliases, so existing callers keep working.
The change is purely additive.
setup.pydoes not build the directory and thegptqmodelpackage does not import it; no existing dispatch, inference, orweight-storage path changes.
Review fixes
Addresses the five findings in the native-kernel review:
9–15-row tile into an 8-row tile plus the remaining 1–7 rows, so
M = 16 + rwrites every output row. Covered for everyr = 1..15,both full and partial column blocks, and both FP32 and BF16 entry points.
bf2_bscalar decode. The scalar fallback now extracts four 2-bitfields per byte instead of reading nibbles from eight bytes. A scalar-path
test decodes two
bf2_bblocks whose second block is zero, which the oldnibble path mis-read.
e < 7the scale table storesa normal high factor and the decode folds in
2^-6; table construction andthe first multiply are independent of ambient FTZ/DAZ. A fresh-process test
initializes the table under
torch.set_flush_denormal(True)and checks2^126 * 2^-126 == 1with FTZ on and off.ftz=Falseunder ambient FTZ/DAZ. Fixing the subnormal-scalepath protected normal weights, but a genuinely subnormal decoded weight was
still flushed by the final multiply, and DAZ zeroed it as an FMA input.
ftz=Falsenow clears MXCSR FTZ/DAZ per worker for the call and restoresit on return;
ftz=Trueis unchanged. Covered forbf8_b/bf4_bonAVX-512, AVX2, scalar and the int8 layout.
bf8_b-only, where it does not expand the payload. bf4_b/bf2_b remain in the
packed sign-magnitude block-major layout, and the s8 wrappers reject them.
for
K == 0instead of returning an uninitializednp.emptybuffer; the Cheader now declares the
K > 0precondition for direct callers.unpack,matmul, the BF16 andblock-major variants,
convert_s8andtile_unpacknow validate thecomplete format-specific shape/length before ctypes and raise
ValueError.colsthat wouldchange the stored block count (cropping a whole block); trimming the final
partial block remains supported. The review's 3×32 example now rejects
cols=16and returns[1,2,3]forcols=17.What Changed
gptqmodel_ext/ttbfp/— the library:tt-llkhelpers forbf8_b,bf4_bandbf2_b(P300C BLOCKFP8/4/2)C = A @ dequant(B)) for scalar, AVX2+FMA andAVX-512, in row-major, block-major and int8 kernel-native layouts (int8 is
bf8_b-only;bf4_b/bf2_bstay in the packed block-major layout)plus the review regressions above
bench/ab_compare.pysame-process A/B andtools/verify_against_tt.pyupstream validation
.agents/skills/ttbfp-perf/SKILL.md— measurement rules, tile-selectionrationale, and measured dead ends for future work.
docs/tt-blockfloat-cpu.md— dtype naming (TT is not OCP MX), dispatchflow, layouts, and the performance model.
hw/dtypes.md— §8 coversbf2_balongsidebf4_b/bf8_b(layout,encode/decode, 0.3125 B/weight) and records that the CPU kernels implement
bf2_beven though the TTNN and HIP backends do not.docs/tenstorrent.md— short CPU-kernels section linking the page above.README.md— one subsection under "Platform and HW Support".No API or behavior changes to existing code.
Performance
Single thread,
K = N = 4096, process CPU time, bitwise identical acrosspaths:
That is 5.8x / 2.3x / 2.4x / 2.4x over the first row-major implementation for
bf8_b.
int8isbf8_b-only because abf8_bblock already occupies 16bytes, so the expansion is free;
bf4_b/bf2_bkeep their packed payload(8/4 bytes per block) and decode from it, which is why their M=1 times are
higher. The int8 kernel runs at ~70% of the measured pure-FMA floor (18.2 ms
for the M=64 problem); the extension README accounts for the remainder and
lists the alternatives that were measured and rejected. Going below the floor
would require integer dot products, which change the fp32 accumulation order
and break bitwise parity with tt-llk, so it is deliberately not done.
Tests
The parity suite covers scalar / AVX2 / AVX-512, both decoders, every tile
shape and row remainder, several thread counts, partial N blocks, and tile
pack/unpack.
Model Support / Module Plan
Not applicable: no
MODEL_MAP, model definition, or module-plan change.Logic Flow Documentation
and docs page are included below.
The new dispatch flow is documented in
docs/tt-blockfloat-cpu.md(
matmul_driver->prepacked_tile->mm_panel_*). It is a new standaloneentry point, not a change to an existing flow.
file:lineevidence.Not applicable: nothing is removed or replaced.