Skip to content

ATen codegen fixes: matmul column-stride, FFN weight row-align, single-pass softmax#63

Merged
booth-algo merged 3 commits into
mainfrom
kev/compiler/llama
Jul 6, 2026
Merged

ATen codegen fixes: matmul column-stride, FFN weight row-align, single-pass softmax#63
booth-algo merged 3 commits into
mainfrom
kev/compiler/llama

Conversation

@booth-algo

Copy link
Copy Markdown
Collaborator

Three ATen PlenaCompiler codegen fixes, verified end-to-end on the PLENA RTL simulator.

  • fix(matrix): row-align non-transposed weight-column stride — corrects mat_col_stride in the sub-projection lowering (RTL linear ATen 27% → ~95%).
  • fix(ffn): row-align weight-read pointer in FFN projection — row-aligns the UP-projection weight pointer (ffn UP 7% → 94%).
  • fix(softmax): single-pass register-direct row softmax — reworks softmax_plena to a per-row single pass, keeping max/sum in FP registers and feeding them directly to the vector consumers (no FP-SRAM round-trip, no S_MAX_FP). Fixes the reduction-result capture hazard the batched/online path hit (softmax ATen 51.95% → 100%). Attention's online-softmax is untouched.

Verified on RTL: softmax ATen 100%, ffn ATen 91.6%, linear ATen ~96% (= template), rms/scratchpad 100%.

vram_sub_projection (non-transposed M_MM) strode the weight-column sub-tile
base by blen (sub-row). The RTL matrix SRAM read is row-granular
(raddr >> log2(MLEN)), so successive BLEN-column output sub-tiles collapsed
onto the same MRAM rows and the output columns replicated ([a,b,c,d]x4) ->
ATen linear = 27% on RTL. Stride by blen*mlen (one full row per sub-tile,
matching the projection_asm template and the transposed M_TMM path).
ATen linear 27% -> 95.17% on RTL. Prefetch is block-granular (mlen*mlen),
so no prefetch change is needed.

Note: the transactional emulator's M_MM weight model is element-granular
(matrix_machine.rs asserts mat_offset < mlen) and rejects this row-aligned
stride; that emulator model is infaithful to the RTL and is a separate fix.
_emit_ffn_projection_chunk advanced the weight-read pointer by blen between
sub-column tiles. The RTL matrix SRAM read is row-granular (raddr >> log2(MLEN)),
so +blen collapsed sub-columns 1..mlen/blen-1 onto MRAM row 0 (output cols
4-15 = 0). Stride the weight pointer by blen*mlen (row-aligned, matching
projection_asm); the output-write pointer stays at +blen. FFN UP projection
7.6% -> 94.3% on RTL. Same class as the vram_sub_projection mat_col_stride fix.
Rework softmax_plena to compute each row in one vector pass, keeping the
reduction results in FP registers and feeding them directly to the next
vector op (V_RED_MAX f2 -> V_SUB_VF ..f2 ; V_RED_SUM f3 -> S_RECI_FP f3 ->
V_MUL_VF ..f3). Drops the online/streaming path that relied on S_MAX_FP and
round-tripped max/sum through FP SRAM, which tripped a reduction-result
capture hazard (softmax ATen 51.95% -> 100%). Attention's online-softmax is
unchanged.
@booth-algo booth-algo merged commit 02677b8 into main Jul 6, 2026
3 checks passed
@booth-algo booth-algo deleted the kev/compiler/llama branch July 6, 2026 10:39
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