Skip to content

refactor: remove 836 lines of unreachable asm_templates code, and share the silu/gelu emitter - #73

Merged
qichao-arlo-wang merged 2 commits into
mainfrom
quality/dead-code-elimination
Aug 7, 2026
Merged

refactor: remove 836 lines of unreachable asm_templates code, and share the silu/gelu emitter#73
qichao-arlo-wang merged 2 commits into
mainfrom
quality/dead-code-elimination

Conversation

@qichao-arlo-wang

Copy link
Copy Markdown
Collaborator

Behavior-preserving cleanup of asm_templates/. No numeric, API, or emission
changes — every claim below is backed by a byte-comparison, not by inspection.

Net: -951 / +96 lines, ffn_asm.py 1857 → 1131.

Commit 1 — delete unreachable code (836 lines)

Three symbols have no call site anywhere in PLENA_Compiler, PLENA_Simulator,
PLENA_Tools, or PLENA_RTL:

symbol lines note
ffn_asm._ffn_asm_fused_up_gate 438 plus the use_fused_up_gate parameter and dispatch branch, the only way to reach it
ffn_asm.ffn_intermediate_asm 265 215 of its lines are byte-identical to the live _ffn_asm_with_loops — it was the single largest contributor to this file's 43% clone rate
gemv_asm.py 107 also one of the five copies of the weight_row/act_col projection emitter that have diverged on whether the sub-column offset is mlen-scaled; deleting it removes one diverged copy for free

Commit 2 — share one emitter between silu_asm and gelu_asm

The two were the same emitter. Emitted output differed in exactly four places
(banner text, whether S_LD_FP f2 is emitted, whether a leading V_MUL_VF ..., f2
is emitted, and whether the V_SUB_VF negation reads the scratchpad or the
activation). One pre_scale_fp_address parameter absorbs all four. Both public
functions keep their exact signatures and docstrings.

Verification

Captured on this exact merge base, before and after:

artifact sha256 result
clm60m.asm (78,670 lines) a828a810… identical
clm60m.mem (78,367 lines) 37ec7f62… identical
smolvlm2.asm (4,515,987 lines) ee6fd302… identical
smolvlm2.mem (4,438,689 lines) ab182359… identical

Both models are the ones ci.yml's codegen-smoke job builds, at the same flags.

suite before after
assembler/tests 4 passed 4 passed
asm_templates/tests 31 passed 31 passed
aten/tests 2 failed, 81 passed unchanged
generator/tests 1 collection error unchanged

The aten/tests failures (a missing router comment string, and a
quantization-accuracy threshold at 89.8% vs >95%) and the generator/tests
error (No module named 'memory_mapping' — PLENA_Tools is not checked out in
this environment) are pre-existing on main and untouched by this PR.

For commit 2 specifically, a differential sweep ran the pre-refactor
implementations from main against the new ones over 3,888 parameter
combinations
(register triples × vlen × batch × hidden_dim × both FP constant
addresses × base addresses straddling the IMM2 boundary where
_load_large_int changes shape). Zero mismatches.

An import sweep over every top-level package is unchanged except for the
removal of asm_templates.gemv_asm and the addition of
asm_templates._sigmoid_activation.

Things worth a reviewer's attention

  • ffn_intermediate_asm and gemv_asm were listed in asm_templates.__all__.
    Nothing in any of the four repositories imports either name, so removal is
    safe, but it is technically a reduction of the package's public surface.
  • The end-to-end goldens exercise GELU (12 sites in SmolVLM2) but never reach
    SiLU, so SiLU's guarantee rests on the differential sweep rather than on the
    goldens.
  • The two docs tables listing gemv_asm.py are updated in the same commit so
    the removal does not create fresh documentation drift.

🤖 Generated with Claude Code

qichao-arlo-wang and others added 2 commits August 7, 2026 17:43
Three symbols in asm_templates/ have no call site anywhere in
PLENA_Compiler, PLENA_Simulator, PLENA_Tools, or PLENA_RTL:

  - ffn_asm._ffn_asm_fused_up_gate (438 lines) plus the
    `use_fused_up_gate` parameter and its dispatch branch, which
    were the only way to reach it.
  - ffn_asm.ffn_intermediate_asm (265 lines). 215 of its lines are
    byte-identical to the live _ffn_asm_with_loops, so it was also
    the single largest contributor to this file's 43% clone rate.
  - gemv_asm.py (107 lines), which is additionally one of the five
    copies of the weight_row/act_col projection emitter that have
    silently diverged on whether the sub-column offset is
    mlen-scaled. Deleting it removes one diverged copy for free.

ffn_asm.py drops from 1857 to 1131 lines.

Behavior is unchanged, verified by byte-comparison rather than by
inspection. Before and after this commit, on this same merge base:

  codegen AICrossSim/clm-60m --seq-len 32 --num-layers 1
    -> clm60m.asm  sha256 a828a810...  (78670 lines)  identical
    -> clm60m.mem  sha256 37ec7f62...  (78367 lines)  identical
  codegen HuggingFaceTB/SmolVLM2-256M-Video-Instruct (same flags)
    -> smolvlm2.asm sha256 ee6fd302... (4515987 lines) identical
    -> smolvlm2.mem sha256 ab182359... (4438689 lines) identical

  pytest assembler/tests      4 passed -> 4 passed
  pytest asm_templates/tests 31 passed -> 31 passed
  pytest aten/tests           2 failed, 81 passed -> unchanged
  pytest generator/tests      1 collection error -> unchanged

The aten/tests failures and the generator/tests collection error are
pre-existing on this merge base and unrelated: the former are a
missing router comment string and a quantization-accuracy threshold,
the latter is `No module named 'memory_mapping'` (PLENA_Tools is not
checked out in this environment).

An import sweep over every top-level package is unchanged except for
asm_templates.gemv_asm, which this commit removes.

`ffn_intermediate_asm` and `gemv_asm` were listed in
asm_templates.__all__. Nothing in any of the four repositories
imports either name, so removing them is safe, but it is technically
a reduction of the package's public surface.

The two docs tables that listed gemv_asm.py are updated in the same
commit so the removal does not create fresh documentation drift.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
SiLU(x) = x*sigmoid(x) and GELU(x) ~= x*sigmoid(1.702*x) were two
copies of the same emitter. They used the same three registers in the
same roles, the same num_vectors computation, and the same
negate / exp / +1 / reciprocal / multiply-in-place sequence inside the
same C_LOOP.

The emitted output differed in exactly four places, all of which the
new `pre_scale_fp_address` parameter absorbs:
  - the banner comment,
  - whether `S_LD_FP f2` is emitted,
  - whether a leading `V_MUL_VF ..., f2` is emitted,
  - whether the `V_SUB_VF` negation reads the scratchpad (GELU, which
    negates the scaled value) or the activation (SiLU).

Both public functions keep their exact signatures and docstrings;
they are now thin wrappers over asm_templates/_sigmoid_activation.py.

Behavior is unchanged, verified two ways.

1. Differential sweep against the pre-refactor implementations taken
   from origin/main: 3888 parameter combinations across register
   triples, vlen, batch_size, hidden_dim, both FP constant addresses,
   and activation/scratchpad base addresses chosen to straddle the
   IMM2 boundary where _load_large_int changes shape. Zero mismatches.

2. End-to-end codegen on this merge base is byte-identical:
     clm60m.asm/.mem    sha256 a828a810... / 37ec7f62...
     smolvlm2.asm/.mem  sha256 ee6fd302... / ab182359...
   and pytest asm_templates/tests stays at 31 passed.

Coverage note: the end-to-end goldens exercise GELU 12 times (in
SmolVLM2) but never reach SiLU, so SiLU's guarantee rests on the
differential sweep in (1) rather than on the goldens.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@qichao-arlo-wang
qichao-arlo-wang merged commit 6242ce3 into main Aug 7, 2026
4 checks passed
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