Skip to content

Feature/int8 w8a8 - #540

Open
YunseonShin wants to merge 13 commits into
pytorchsim-triton-backendfrom
feature/int8-w8a8
Open

YunseonShin wants to merge 13 commits into
pytorchsim-triton-backendfrom
feature/int8-w8a8

Conversation

@YunseonShin

Copy link
Copy Markdown
Collaborator

PyTorchSim: PR into pytorchsim-triton-backend

Branch: local feature/int8-dequant-epilogue in .worktrees/pts-c. Push it as feature/int8-w8a8 → pytorchsim-triton-backend.

  • It has 13 commits on 2fd9ca0, the current tip of pytorchsim-triton-backend.

Depends on: the PyTorchSim-Triton-Backend PR. The pin must name a PTB commit whose image is published.

Title: [Frontend] W8A8 and W8A16 on the npu: int8 matmuls stay on the device, as the v6e runs them


Summary

This makes torchao's int8 quantization run on the npu, with no CPU fallback:

  • W8A8: Int8DynamicActivationInt8WeightConfig and static per-tensor activations;
  • W8A16: weight-only int8.

The kernels are shaped the way XLA shapes them on a TPU v6e. The int8 matmul is aten._int_mm on the matrix unit, the weight widening is the mm template's prologue, and the dequantized result is stored by the matmul's epilogue. An activation that only matmuls read stays in the scratchpad.

Cycle and time figures are TOGSim simulations unless marked as hardware.

Changes

int8 matmuls on the device

  • 436010a an int8 matmul widened to int32 runs as aten._int_mm on the npu.
    • torchao W8A8 emits mm(int32(int8), int32(int8)), which ran as a CPU fallback.
    • The post-grad rewrite int_mm (postgrad/gemm.py) makes it aten._int_mm.
  • c37bd49 a 1-row int8 matmul stays int_mm when torchao turns on coordinate descent.
    • The npu's mm decomposition is traced with coordinate descent off, and a 1-row int_mm sums in int32.
    • W8A8, M 1: 188208 → 31281 cycles.
  • bab5d74 a weight widened from int8 reaches the mm template as its prologue.
    • The mm template is offered beside the resident mm when the streamed operand is widened.
    • So torchao weight-only (W8A16) compiles and fuses the widening.

Values read again

  • c94fe22 a value the npu reads again through a broadcast is stored once when it does arithmetic. XLA likewise computes a row's scale in its own fusion.
    • The W8A8 matmul epilogue used to recompute it per element.
    • K 1024 matmul: 12545 → 10875 cycles.
  • 69d0b64 a bool that a broadcast reads again is computed where it is read, as before.
    • Storing histc's ge mask made mistral's decode kernel read it through an index.
    • That is an i1 pointer bitcast, which the compiler front end refuses.

Placement

  • 601a79c an intermediate that only matmuls read stays in the scratchpad, as the v6e keeps a W8A8 activation in VMEM.
    • autotune/on_chip.py elects such intermediates per graph, within the resident budget.
    • Each kernel states them as KernelSpec.on_chip.
  • d0bd7e3 an int8 matmul's dequantized bf16 result is stored by its epilogue, not recomputed by its reader.
    • Readers such as a row norm or SiLU(gate) x up no longer read the int32 accumulator back.
    • Qwen3 W8A8 prefill: 814.2 → 664.5 us.

Tests and CI

  • 7c0a92a test_w8a8_linear runs without torchao: its own W8A8Linear takes a Linear with no bias.
  • d34b416 the CI base image carries torchao 0.18.0 and diffusers 0.37.1, and the new tests join the allowlist and the CI matrix.
    • diffusers 0.34 fails to import beside torchao 0.18: its torchao quantizer logs before it defines logger.
    • 0.37.1 is the first release that imports. Diffusion passes with it.
    • The base image tag moves to thirdparty-b6d2a9b115c3.
  • New test files:
    • tests/ops/gemm/test_int_mm.py
    • tests/ops/gemm/test_widened_weight_mm.py
    • tests/ops/gemm/test_w8a8_linear.py
    • tests/system/test_on_chip.py

Compiler pin

Testing

Allowlist (scripts/ci/triton_route_passing.txt, 59 entries) at 2f83e6b with PTB b012d9e2: 56/59.

  • It ran on 4 nodes with enough memory. DeepSeek v3, Diffusion, Gemma 2-2b, Llama3 and Llama3x time out when the run is memory-starved, and pass in 4-7 minutes when it is not.
  • The 3 failures are pre-existing:
test failure also fails on
test_llama4 bf16 MoE: 11 of 128 token rows take other experts (6 allowed), max abs diff 4.596 upstream 2fd9ca0 + PTB 41c4c622 in the same environment: 12 of 128, 4.612
test_mllama scratchpad 561280 bytes per lane, over the budget of 524288 the base before this work
test_conv_decomposition Spike: _depthwise_kernel reads below the DRAM base (padding column) upstream CI since 0325625

Allowlist with the new image's packages (torchao 0.18.0, diffusers 0.37.1, 62 entries): 59/62.

  • The same three tests fail as above.
  • test_int_mm, test_widened_weight_mm and test_w8a8_linear pass. test_w8a8_linear runs all three cases, rownorm included.

New tests without torchao: test_w8a8_linear passes dynamic and static and skips rownorm. test_on_chip (no kernel, matrix only) passes.

CPU fallbacks: none in the W8A8 tests. The generated wrappers have no torch.ops or extern_kernels assignments.

Timing against TPU v6e measurements (tpu-perf-archive)

Simulated time ÷ measured time:

workload geomean within ±20% largest error
W8A8 Linear, 5 shapes 1.001 5/5 +11.9%
W8A16 Linear, 5 shapes 1.22 3/5 +82% (M 1)
bf16 Linear, 5 shapes (for comparison) -13% to -19% (from before this work)
Qwen3-8B decoder layer simulated TPU error
W8A8, prefill 1024 664.5 us 639.3 us +3.9%
bf16, prefill 1024 994.0 us 1085.7 us -8.4%
W8A8, decode (KV 1024) 171.4 us 168.2 us +1.9%
bf16, decode (KV 1024) 280.5 us 297.7 us -5.8%
  • The decode rows were re-run on this branch's head with PTB b012d9e2. The other rows were measured at earlier commits of this branch.
  • On the TPU, W8A8 takes 0.59x of bf16 (prefill) and 0.57x (decode). The simulator gives 0.67x and 0.61x.

Known differences, left for follow-up work:

  • Read-ahead depth (W8A16 M 1, +82%). TOGSim lets a core hold 2 work items, so it reads one block ahead. The XLA fusion reads 3-4 ahead (LLO).
    • With 3 work items in TOGSim, the kernel's error drops to +10% (experiment).
    • That change cannot be global: the vector suite's Pallas references double-buffer, and softmax would move from +6% to -19%.
    • The depth belongs to the kernel, and that is the follow-up.
  • Cross-program prefetch (decode). The TPU loads the next Linear's weight during earlier fusions. The simulator does not model this.
    • The decode layer total is close (+1.9%), but per Linear the error ranges from -23% (k·v) to +29% (down).
  • Matrix-unit kernel compute. In the W8A8 matrix kernel, compute is 1.2-1.7x the TPU's. The causes are instruction count and the core's issue rate.

Before merge

  1. Pin: after the PyTorchSim-Triton-Backend PR merges, set third_party/pytorchsim-triton-backend.json to its merge commit on main, the one with a published image.
  2. Pin commits: the four pin commits' messages say "(local)" and "not pushed". Squash them into the one pin commit of step 1.
  3. CI: done in d34b416. The first CI run rebuilds the base image, because its tag moves.

…e npu

torchao W8A8's mm(int32(i8), int32(i8)) ran as a CPU fallback; the post-grad rewrite int_mm makes it aten._int_mm.
…rdinate descent

npu's mm decomposition is traced with coordinate descent off, and a 1-row int_mm sums in int32 (M 1 W8A8: 188208 -> 31281 cycles).
…prologue

The mm template is offered beside the resident mm when the streamed operand is widened, so torchao weight-only compiles and fuses the widening.
… when it does arithmetic

As XLA computes a row's scale in its own fusion; the W8A8 mm epilogue recomputed it per element (K1024 mm 12545 -> 10875 cycles).
…d, as before

e0ba086d stored histc's ge mask, which mistral's decode kernel then read through an index: an i1 pointer bitcast the compiler front end refuses.
… as v6e keeps a W8A8 activation in VMEM

autotune/on_chip.py elects them per graph within the resident budget; each kernel states them as on_chip in its spec, the JSON one included (PTB dcf3386b takes the field).
…epilogue, not recomputed by its reader

Its reader (a row norm, SiLU(gate) * up) no longer reads the int32 accumulator back (Qwen3 W8A8 prefill 814.2 -> 664.5 us). New case rownorm in test_w8a8_linear.
…es a Linear with no bias

rownorm's bias-free Linear crashed it before the skip; with torchao all three cases pass, without it dynamic and static.
…the W8A8 tests join the allowlist and the matrix

diffusers 0.34 fails to import beside torchao 0.18 (its torchao quantizer logs before defining logger). Allowlist 59/62, the three failures as before (llama4, mllama, conv_decomposition).
… its precompile a finished future

torch 2.14 precompiles template choices through async_compile.triton(kernel_name=, source_code=).future; test_graph_prefetch, test_graph_prefetch_xla and test_on_chip failed on it, upstream 28150cf included.
RoPE's cos/sin were stored transposed, so PTB rejected the 2D RoPE kernel (masked-box-before-tensor) in Llama3x and Gemma 3-1b.
Both pass now; W8A8 Linear and Qwen3 W8A8 layer cycles are unchanged.
…hmarked

Inductor maps each compile future to one pending fusion, so a W8A8 MLP's gate dropped its dequant epilogue when up's fused kernel was the same source.
Qwen3 W8A8 MLP 128: 6 -> 5 kernels, 154.0 -> 141.0 us; prefill 128: 17 -> 14 kernels, 239.0 -> 220.1 us.
…t8 on the matrix unit, kernel format 11) and #408 (the JSON spec states on_chip)
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.

1 participant