Skip to content

profile: execute the MoE region terminator, and stop shared work inheriting pair labels - #107

Merged
qichao-arlo-wang merged 1 commit into
mainfrom
feat/moe-stage-terminator-exec
Aug 9, 2026
Merged

profile: execute the MoE region terminator, and stop shared work inheriting pair labels#107
qichao-arlo-wang merged 1 commit into
mainfrom
feat/moe-stage-terminator-exec

Conversation

@qichao-arlo-wang

@qichao-arlo-wang qichao-arlo-wang commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Companion to AICrossSim/PLENA_Compiler#72; the submodule pin moves onto it here.

Land #72 first. The vocabulary guard holds StageKind equal to MOE_STAGES in both directions, so neither side can merge alone. With #72 pinned and this branch's StageKind reverted, the guard fails with compiler emits @stage=non_moe but no StageKind matches it — that is the lockstep doing its job, not a problem.

1. The MoE region now has an end

StageKind::NonMoe resolves the compiler's @stage=non_moe. Demonstrated end to end — a shared-expert program followed by an lm_head projection, classified with the emulator's own sticky-marker rule:

=== WITHOUT terminator ===  219 instructions
    shared_expert_projection       116   53.0%   <-- billed to a MoE stage
    shared_expert_activation       103   47.0%   <-- billed to a MoE stage

=== WITH terminator ===  219 instructions
    shared_expert_activation       103   47.0%
    shared_expert_projection        87   39.7%
    non_moe                         29   13.2%

29 instructions of pure non-MoE work stop being billed to the shared branch, and that number feeds shared_branch_fraction.

A distinct variant, not other. Resolving other would have worked mechanically, but a large Other is a coverage problem and a large NonMoe is just a long epilogue. Being unable to tell those apart is how the original bug stayed invisible.

2. Shared work no longer inherits a pair label

The three shared_expert_* stages have no experts — there is no (token, expert) pair for their work to belong to. Without a reset their cost merged into whichever routed pair ran last, and in a shared-only program it invented pairs outright.

The reset list is now PAIRLESS_STAGES, a named constant, so the test asserts membership instead of restating the match arm — the two could previously drift.

3. An unterminated region is now visible in the JSON

classification.trailing_stage and classification.trailing_instructions.

The per-stage totals still add up when a region is left open — that is precisely what made this class of bug invisible. A large trailing count against a MoE stage now says so, without anyone having to read the source that emitted it. A properly terminated program shows non_moe here and the count is simply how long the epilogue is.

On test_branch_split.py

non_moe is classified explicitly, in its own NON_MOE_STAGES tuple rather than folded into PLUMBING_STAGES. Plumbing is MoE cost that both branches share; this is not MoE cost at all. The exhaustiveness assertion is what forced the decision rather than letting a new stage drop out of the split and read as free — working as intended.

Validation

  • cargo test --workspace --release181 passed, 0 failed (3 new: terminator resolution, trailing attribution both ways, and pairless-stage membership including the negative case that a routed stage keeps its label).
  • Cross-repo vocabulary guard passes with the new pin.
  • testbench guards 10 passed; just test-moe-shared-all passes with 3 bit-exact comparisons; all six routed-MoE suites pass.
  • cargo fmt --check, ruff format --check, ruff check clean.

🤖 Generated with Claude Code

@qichao-arlo-wang
qichao-arlo-wang force-pushed the feat/moe-stage-terminator-exec branch from 8634933 to 29c2e8b Compare August 9, 2026 12:13
…k inheriting pair labels

Companion to PLENA_Compiler's terminator change; the pin moves onto it here.

`StageKind::NonMoe` resolves the compiler's `@stage=non_moe`. Markers are sticky
and a program does not stop where its MoE region does, so without a terminator
the last MoE marker ran to the end of the file and the epilogue -- an lm_head,
the next sublayer -- was billed to a MoE stage. Measured end to end on a
shared-expert program followed by an lm_head projection: 29 of 219 instructions
move out of `shared_expert_projection` once the terminator is emitted.

A distinct variant rather than resolving `other`: a large `Other` is a coverage
problem and a large `NonMoe` is just a long epilogue, and being unable to tell
those apart is how the original bug stayed invisible.

The pair-label reset now covers the three `shared_expert_*` stages and `NonMoe`.
They have no experts at all, so there is no `(token, expert)` pair for their work
to belong to, and without the reset their cost merged into whichever routed pair
ran last. The list is a named `PAIRLESS_STAGES` constant so the test asserts
against the classifier rather than restating the match arm.

Three new reported fields make an unterminated region visible without reading
the source that produced it: `moe_region_terminated`, `trailing_stage` and
`trailing_labels`. All three are recorded when the marker is applied, from the
same place, so they cannot contradict one another -- an earlier version took
`trailing_stage` from the last label instead and disagreed with the other two.
`trailing_labels` counts from the last marker rather than from the last change
of stage, so adjacent regions marked with the same name are not merged, and it
is named for labels because it counts emitted ASM like `label_count`, not
executions. When no marker ever resolves it is 0, not `label_count`: a
legacy-classified file has no final marker for anything to ride on.

`test_branch_split.py` classifies `non_moe` explicitly, in its own tuple rather
than folded into PLUMBING_STAGES -- plumbing is MoE cost both branches share and
this is not MoE cost at all. The exhaustiveness assertion forced the decision
rather than letting a new stage drop out of the split and read as free.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@qichao-arlo-wang
qichao-arlo-wang force-pushed the feat/moe-stage-terminator-exec branch from 29c2e8b to 07bed8e Compare August 9, 2026 12:28
@qichao-arlo-wang
qichao-arlo-wang merged commit 980e48a into main Aug 9, 2026
5 checks passed
@qichao-arlo-wang
qichao-arlo-wang deleted the feat/moe-stage-terminator-exec branch August 9, 2026 12:40
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