Skip to content

fix(kernels): store per-row scale for every row in layernorm_fp8 - #17

Open
pauliano22 wants to merge 1 commit into
mainfrom
fix/layernorm-fp8-missing-row-scales
Open

fix(kernels): store per-row scale for every row in layernorm_fp8#17
pauliano22 wants to merge 1 commit into
mainfrom
fix/layernorm-fp8-missing-row-scales

Conversation

@pauliano22

Copy link
Copy Markdown
Owner

What

kernels/layer_norm_fp8.py's layernorm_quant_kernel gates the per-row FP8 scale store behind if tl.program_id(0) == 0:, so only row 0's Triton program ever writes its scale.

Why it's a bug

scales is allocated with torch.empty((M,), ...) — uninitialized memory. Since one program handles each row (row_idx = tl.program_id(0)), the guard means scales[1:] is never written and stays as garbage. Any caller dequantizing this kernel's per-row FP8 output (y_fp8 * scale) gets correct results only for row 0 and garbage for every other row.

Fix

Each program already computes its own correct per-row scale from that row's data — just store it unconditionally, matching how Y_ptr's row is stored a line above.

Testing

This repo has no CI or test suite (run_all_benchmarks.py is the only "check," and it requires a CUDA GPU). This sandbox has no GPU and no torch/triton installed, so I could not execute this. The change is a minimal, one-line removal of an incorrectly-scoped conditional — worth a quick python3 -c smoke test (create a small x/w/b, call layernorm_fp8, and confirm scales has no zero/garbage rows) on a GPU box before merging.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FLJ8M3VeafVxxNyQesNtfi


Generated by Claude Code

The scale store was gated behind `if tl.program_id(0) == 0`, so only
row 0's Triton program ever wrote its scale. `scales` is allocated via
torch.empty((M,)), so scales[1:] was left as uninitialized garbage —
any caller dequantizing per-row FP8 output with these scales got wrong
results for every row but the first. Each program already computes its
own correct per-row `scale`; just store it unconditionally.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLJ8M3VeafVxxNyQesNtfi
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.

2 participants