feat: Phase 43 β Sparse/Grouped MoE Dispatch (v4.0.0-alpha.3) - #50
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves the "documented future optimisation" carried forward unresolved
since v2 Β§35 and v3's out-of-scope list: real sparse dispatch,
where each token's forward pass only computes its assigned top-k experts,
rather than every expert computing on every token and being masked
afterward (the dense-masked-matmul trade-off v2 Phase 22 and v3 Phase 31
deliberately shipped).
The output is numerically equivalent to DenseMasked (same tokens, same
weights, same per-expert reduction order) β just faster, because the
masked-away matmuls never run. This is the milestone tagged
v4.0.0-alpha.3.What's New
Core (
aarambh-studio-core)DispatchKindenum (src/config.rs, alongsideAttentionKind):DenseMaskedβ v2/v3 behaviour, default, CPU fallback, correctness referenceSparseβ new, CUDA-only throughput windense_masked/sparse(snake_case)MoeConfig.dispatchfield defaulting toDenseMaskedfor exactbackward compatibility with every existing MoE checkpoint β old
TOML/JSON without the field deserialises to the dense path,
byte-identical to v2/v3.
DispatchKindfromlib.rs.moe_config_dispatch_defaults_to_dense_masked,moe_config_dispatch_serializes_as_snake_case, + assertion inold_moe_json_defaults_to_phase22_behavior.Neural Network (
aarambh-studio-nn)sparse_grouped_dispatch(src/dispatch.rs): tokens are grouped byrouter assignment into per-expert contiguous batches via
arg_sort(no-grad permutation), then each expert's SwiGLU matmul executes only
on its assigned token group via
index_selectβexpert.forwardβindex_addscatter. Fully differentiable through candle'sgather/index_select/index_addβ router logits, expertparameters, and input activations all receive correct gradients.
effective_dispatch_kind:Sparseonly activates on a CUDA device;the CPU path keeps
DenseMaskedregardless of configuration, documentedplainly as "GPU only pays off" (the honesty discipline v2 Β§29 applied to
speculative decoding's speed claim).
MoeFfn::dispatch_kind(): exposes the configured kind. The forwardpath selects dense vs sparse by the effective kind. QAT calibration
(
forward_with_capture) always uses the dense reference to observefull per-expert activation distributions.
top_k_gatingbefore dispatch, so identical for both kinds. Sparsechanges the compute path only, not the loss the router is trained
against.
sparse_grouped_dispatch,effective_dispatch_kindfromlib.rs.Why no custom
.cukernelcandle's
index_select/index_add/matmulalready use cuBLAS onCUDA, so
sparse_grouped_dispatchis the CUDA grouped path and trulyskips non-routed experts. A hand-written grouped-GEMM kernel can't be
tested in CPU CI and the release audit forbids empty kernel bodies β
documented as a future optimisation.
Tests (18 new)
sparse_dispatch_output_matches_dense_masked_reference_within_tolerancedispatch_kind_dense_masked_is_bit_identical_to_v2_v3_behavioursparse_dispatch_supports_top_k_greater_than_onesparse_dispatch_backward_reaches_router_and_expert_weightssparse_dispatch_empty_expert_group_is_skippedsparse_dispatch_matches_dense_with_shared_expert_summed_separatelyload_balancing_loss_value_is_unaffected_by_dispatch_kindsparse_configured_moe_falls_back_to_dense_masked_on_cpueffective_dispatch_kind_uses_sparse_on_cudasparse_dispatch_cuda_throughput_exceeds_dense_masked_at_kaggle_gpu_scalemoe_config_dispatch_defaults_to_dense_maskedmoe_config_dispatch_serializes_as_snake_caseCUDA-only tests use
Device::cuda_if_available(0)and early-return onCPU β they compile cleanly in CPU CI and exercise the real CUDA path
when GPU hardware is present.
Configs & Scripts
configs/moe_sparse_smoke.tomlβ tiny CPU smoke (dispatch = "sparse",runs through the dense fallback).
configs/large_sparse_moe.tomlβ Kaggle GPU config mirroringlarge_finegrained_moe.tomlwithdispatch = "sparse".scripts/phase43_smoke.shβ unit tests + 2-step CPU train + checkpointverification + scorecard.
Docs
docs/phase43_sparse_moe.mdβ full phase doc (mechanism, differentiability,CPU/CUDA policy, test matrix).
ROADMAP_V4.mdβ Phase 43 tasks marked[x], tests annotated asimplemented.
CHANGELOG.mdβ added[4.0.0-alpha.3]section.README.mdβ bumped alpha.2 β alpha.3, added Phase 43 to v4 arc +capabilities table + phase-docs table.
ARCHITECTURE_V4.md Β§57β added "Status: shipped" + implementationsubsection.
SELF_LEARNING_V4.md Β§44β added "Status: shipped" note.4.0.0-alpha.2β4.0.0-alpha.3across all 20packages (workspace inheritance).
CI Gates (all green β )
cargo fmt --all --checkcargo check --workspace --all-targets --lockedcargo clippy --workspace --all-targets --locked -- -D warnings -D clippy::undocumented_unsafe_blockscargo test --workspace --no-fail-fast --lockedRUSTDOCFLAGS="-D warnings -D missing_docs" cargo doc --workspace --no-deps --lockedscripts/phase28_release_audit.shcargo build --release -p aarambh-studio --locked--helpsmoke (all subcommands)Smoke Test
Phase 43 smoke (
scripts/phase43_smoke.shsubstance) ran a 2-step CPUtraining on
moe_sparse_smoke.toml(dispatch="sparse"β densefallback on CPU): loss 4.87 β 4.61,
moe_auxcomputed correctly(dispatch-independent),
routed_experts=8 active_routed=2 shared_experts=1 dead_experts=0. Checkpoint saved with 45 tensorsincluding all 8 routed expert down-projections + 1 shared expert.
Scorecard:
artifacts/phase43_sparse_moe_smoke.json.Backward Compatibility
dispatch = DenseMaskedβ byte-identical to v2/v3 behaviour(verified by
dispatch_kind_dense_masked_is_bit_identical_to_v2_v3_behaviour,diff == 0.0).
dispatchfield deserialize unchanged.are unaffected.