Skip to content

refactor: extract shared routed-expert FFN helper (byte-identical) - #1035

Merged
JustVugg merged 1 commit into
JustVugg:devfrom
lineape:pr551-prefactor-shared-ffn
Aug 16, 2026
Merged

refactor: extract shared routed-expert FFN helper (byte-identical)#1035
JustVugg merged 1 commit into
JustVugg:devfrom
lineape:pr551-prefactor-shared-ffn

Conversation

@lineape

@lineape lineape commented Aug 15, 2026

Copy link
Copy Markdown

Summary

moe() inlines the four-step routed-expert FFN (expert_gate_up β†’ silu(gate)*up β†’ [fmt=6 down-input rotation] β†’ down matmul) six separate times, and those six copies were written inconsistently: four omit the fmt=6 down-input rotation, two apply it. This extracts the sequence into one expert_ffn helper and routes all six call sites through it.

The helper takes the expert's own tensors plus buffers and a row count β€” no moe() locals β€” so a downstream distributed worker can call the same helper instead of carrying a parallel copy. Input arrives already rotated by the caller via E8_XE (the per-call cache is preserved, #452); the fmt=6 down-input rotation lives inside the helper and is applied whenever d->fmt==6.

Validation

  • make β†’ 0 warnings.
  • make check β†’ green (C suite ALL PASS (0 failures), Python Ran 473 tests OK).
  • gcc -fsyntax-only -DCOLI_CUDA -DCOLI_VULKAN -DCOLI_METAL -Wall -Wextra c/colibri.c β†’ 0 warnings (the #ifdef-gated call sites still compile).

Compatibility

No new knobs, no behaviour change on any single-format model (the oracle SNAP=./glm_tiny TF=1 ./colibri 64 16 16 is byte-identical before and after). The helper applies the fmt=6 down-input rotation unconditionally β€” it is a pure function of the down tensor's format. Of the four copies that previously skipped it, the two Vulkan device-lost fallbacks provably cannot see fmt=6 (the Vulkan tier loads only fmt 2/4/5), and the other two (Vulkan CPU-share, CUDA early-issued-take fallback) can reach fmt=6 only in a mixed-format container, where this now matches the canonical path. Single-machine path unchanged.

Extract the four-step routed-expert FFN sequence (expert_gate_up ->
silu(gate)*up -> [fmt=6 down-input rotation] -> down matmul) into one
shared expert_ffn helper and replace the six inlined copies in moe()
with calls to it. Single concern, no new knobs.

The helper takes the expert's own tensors plus buffers/row-count (no
moe() locals), so a distributed worker can call it instead of carrying a
parallel copy. Input arrives already rotated by the caller via E8_XE
(per-call cache, JustVugg#452); the fmt=6 down-input rotation lives inside the
helper and is applied whenever d->fmt==6.

Byte-identical on the oracle and on any single-format model (what a
normal conversion produces). Four of the six copies previously omitted the
down-rotation. Two of those -- the Vulkan device-lost fallbacks -- are
provably safe: they reload only registry-resident experts, and
vk_registry_fill admits only fmt 2/4/5, so d->fmt can never be 6 there.
The other two (the Vulkan CPU-share path and the CUDA early-issued-take
fallback) can in principle reach a fmt=6 expert in a mixed-format
container (fmt is derived per-tensor by qt_resolve_fmt, with no uniformity
enforced across experts at load time); routing them through the helper
makes their down-rotation consistent with the canonical path, closing a
latent omission rather than changing any single-format model's output.
@lineape

lineape commented Aug 15, 2026

Copy link
Copy Markdown
Author

Quick context for reviewers: this is a straight extraction β€” the four-step FFN body is moved verbatim into expert_ffn, six call sites now call it, and the fmt=6 down-input rotation is applied unconditionally inside the helper (a pure function of the down tensor's format). The only behavioral nuance is that two of the six copies previously skipped that rotation; both of those (Vulkan device-lost fallback, CUDA early-take fallback) either provably can't see fmt=6 or only could in a mixed-format container, so output is unchanged on any single-format model and the oracle is byte-identical. No new knobs, single concern.

I'm also shipping a follow-up PR (#1036) that consumes this helper from a distributed worker β€” mentioned only so the "so a downstream worker can call it" framing doesn't read as speculative generality.

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