Skip to content

A lesson replays the routing she lived: pinned experts for MoE training graphs - #167

Merged
joelteply merged 3 commits into
fable/context-memory-rowsfrom
fable/pinned-experts
Nov 27, 2026
Merged

joelteply merged 3 commits into
fable/context-memory-rowsfrom
fable/pinned-experts

Conversation

@joelteply

Copy link
Copy Markdown

A near-tie in a row's top-k picks a different expert when the arithmetic differs slightly, so a
tiled walk (64-row graphs) and one 512-row graph trained different functions on the 35B MoE.
llama_set_pinned_experts carries each position's chosen experts per layer; build_moe_ffn replays
them (one-line hook) for any graph whose rows are all covered, including the last layer's
output-only cut.

Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M, window 512, all rows labelled, exact walk,
64-row tiles vs one graph (test-walk-exact WALK_PINNED):
routed by each graph: cosine 0.3663, norm ratio 3.1704
pinned as lived: cosine 0.9932, norm ratio 0.9709
The remainder is in the early full-attention layers (blk 3, 7 ~0.95), below the linear-attention
layers whose chunked arithmetic differs by row count.

Fork addition in its own file (src/llama-pinned-experts.{h,cpp}); upstream touched only by one-line hooks: the cparams pointer, the selection line in build_moe_ffn, the context member/method declaration, the public API declaration, CMake source list.

Not yet wired into continuum's walk: the recorder in the test reads ffn_moe_topk through cb_eval; production records the lived ids at serve time (IN-PROCESS-LEARNING.md, pinned routing). Next: the residual 0.993 in the linear-attention layers.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…ng graphs

A near-tie in a row's top-k picks a different expert when the arithmetic differs slightly, so a
tiled walk (64-row graphs) and one 512-row graph trained different functions on the 35B MoE.
llama_set_pinned_experts carries each position's chosen experts per layer; build_moe_ffn replays
them (one-line hook) for any graph whose rows are all covered, including the last layer's
output-only cut.

Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M, window 512, all rows labelled, exact walk,
64-row tiles vs one graph (test-walk-exact WALK_PINNED):
  routed by each graph:   cosine 0.3663, norm ratio 3.1704
  pinned as lived:        cosine 0.9932, norm ratio 0.9709
The remainder is in the early full-attention layers (blk 3, 7 ~0.95), below the linear-attention
layers whose chunked arithmetic differs by row count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply changed the base branch from master to fable/context-memory-rows November 27, 2026 22:02

@joelteply joelteply left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fork #167 at 883dfdc: the pin is context-wide and covers() checks only ubatch.pos. On a shared serving context, a neighbour decoding the same positions can receive expert IDs recorded for the lesson resident. Conversely, one out-of-range neighbour row causes the entire ubatch to route afresh, including the lesson. Scope replay selection to the owning sequence/walk and test two residents with overlapping positions and mixed covered/uncovered rows. Validate expert IDs against the model expert count on ingress. The single-sequence M5 measurement does not exercise these cases.

…dex on #167)

The pin was context-wide and keyed only by position: a neighbour decoding the same positions got the
lesson's experts, and one uncovered row sent the whole ubatch back to the router. Now the pin names its
sequence; the hook runs after the router and replaces only rows of that sequence inside the range
(routed + mask * (pinned - routed), exact in F32 for expert ids), every other row routes as it serves.
llama_set_pinned_experts refuses, setting nothing, an id that is not one of the model's experts.

Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M (test-walk-exact WALK_PINNED):
  two sequences at positions [0,64) in one ubatch, seq 0 pinned on [32,64) to deliberately wrong ids:
    0 of 128 rows x 40 layers differ from (seq 0 [32,64) replayed, seq 0 [0,32) and seq 1 routed as unpinned)
  tiles vs one graph, pinned as lived: cosine 0.9932, norm ratio 0.9709 (unchanged); the case still
    exits 1 against its 0.997 gate: the remainder in blk 3/7 attention adapters is the next bisect

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

@joelteply joelteply left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 83d4edd: row selection now checks sequence and position, and ingress checks expert IDs. One cache case remains: llama_pin_experts returns routed directly if no current row is covered; that graph has no pin input. llm_graph_params::allow_reuse does not compare pin selection, and set_pinned_experts does not invalidate cached graphs. A same-shape graph built unpinned can therefore bypass a later pin, including when the same sequence advances into a pinned span. Please exercise consecutive eligible decode graphs on the same context (uncovered then covered) with distinctive pinned IDs and capture the actual selected IDs.

…under the same pin (Codex on #167)

A graph built with no covered row had no pin input, and a later covered ubatch of the same shape reused
it and skipped the pin; one built before the pin was set was reused after. Now, while a pin is set,
every MoE layer carries the pin input (its mask is all zeros when no row is covered), the input reuses a
graph only while the same pin is set, and setting or clearing a pin drops the cached graphs.

Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M (test-walk-exact WALK_PINNED), one context,
decodes [0,32) then [32,64) (same shape, reuse eligible), [32,64) pinned to deliberately wrong ids:
  pin set before the first decode: 0 of 64 rows x 40 layers differ from ([0,32) routed, [32,64) replayed)
  pin set between the decodes:     0 of 64 rows x 40 layers differ
  per sequence (unchanged):        0 of 128 rows x 40 layers differ
  tiles vs one graph pinned:       cosine 0.9932 (unchanged; the case exits 1 against its 0.997 gate)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

@joelteply joelteply left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 17e1077. Keeping the per-row mask in all eligible graphs while a pin exists and discarding cached graphs on pin changes addresses my reuse finding. Your reported M5 replay covers both orderings. Isolation and graph reuse findings closed at this head. The pinned learning cosine 0.9932 is still below 0.997; I have not independently rerun the M5 or CUDA measurements.

@joelteply
joelteply merged commit 0e5aa51 into fable/context-memory-rows Nov 27, 2026
5 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant