Skip to content

feat(deepseek-v4): clamp shared-expert SwiGLU to swiglu_limit #550

Closed

Description

@inureyes

Summary

Every DeepSeek-V4 feed-forward block is MoE: 256 routed experts plus one shared expert, all SwiGLU MLPs. The model clamps the SwiGLU pre-activations in both expert kinds before the nonlinearity: the gate pre-activation is clamped from above at swiglu_limit, the up pre-activation is clamped to [-swiglu_limit, +swiglu_limit], then silu(gate) * up proceeds. Omitting the clamp is silent while every pre-activation stays inside the bounds and a real divergence the moment one crosses them.

This issue was filed on 2026-07-01 against a DeepSeek-V4 module that did not exist yet. It anticipated its own resolution: "If #523 lands with the clamp already in place, this issue reduces to verifying both paths clamp identically and adding the regression tests below." That is what happened. #523 landed as PR #1455 with the clamp in place in both paths and the regression test written. This body records the verification so the issue can be closed on evidence rather than assumption.

Current state: implemented by #523 / PR #1455

The clamp is one shared function, limited_swiglu in src/models/deepseek_v4_moe.rs:65:

let gate = mlxcel_core::minimum(gate, &limit_arr);      // upper clamp only
let up   = mlxcel_core::clip(up, &neg_limit, &limit_arr); // both sides
// then silu(gate) * up

This matches the specified math exactly: upper clamp only on the gate (silu already saturates toward 0 for large negative inputs), both sides on the up branch (it multiplies through linearly), applied to pre-activations before silu, elementwise, with limit <= 0 disabling the clamp.

Both expert kinds call the same function, which is what this issue asked to verify:

  • Shared expert: LimitedMlp, held at deepseek_v4_moe.rs:307 as shared_experts, constructed at line 361 with limit: args.swiglu_limit (line 359).
  • Routed experts: LimitedSwitchGlu, constructed with limit: args.swiglu_limit (line 290).

swiglu_limit is read from the checkpoint config with a serde default of 10.0, and DeepSeek-V4-Flash declares 10.0.

The fused-kernel concern is structurally resolved

This issue flagged that switch_layers::SwitchGLU hardcodes the unclamped activation and that its fused single-token MoE kernel hardcodes silu(gate) * up, so the V4 routed path could not reuse either as-is. The port took the GptOssSwitchGLU route the issue recommended: LimitedSwitchGlu is a dedicated type with its own forward (deepseek_v4_moe.rs:242) that never delegates to SwitchGLU, so the fused kernel is not reachable for this family regardless of MLXCEL_FUSED_MOE. It is unreachable by construction, not by a flag check that could drift.

One consequence, already recorded as a known tradeoff on PR #1455: LimitedSwitchGlu also does not take SwitchGLU's gather_sort / scatter_unsort fast path above 64 dispatches. That is a performance gap, not a correctness one, and it is tracked as a follow-up rather than here.

Test coverage

limited_swiglu_clamps_match_reference (src/models/deepseek_v4_tests.rs:971) pins the clamp against a float64 replay of the reference's _limited_swiglu, covering values above, below and inside both bounds. Because both expert kinds call the one function, that test covers both paths.

What remains

Nothing blocking. Two optional hardening items, neither worth holding the issue open for on its own:

  • A test that runs the shared and routed paths on the same input and asserts identical clamping, pinning the "both paths, same key, same bounds, same placement" contract structurally rather than by shared-callsite inspection. Cheap, and it would catch a future refactor that gives one path its own activation.
  • A note in docs/supported-models.md is already present (the entry states the clamp applies to both routed and shared experts), so no doc work is outstanding.

Recommendation

Close as delivered by PR #1455, optionally after adding the shared-vs-routed identity test above. The clamp math, the config plumbing, the both-paths requirement and the fused-kernel avoidance are all satisfied and verified against the code.

Activity

  1. added
    type:enhancement
    status:blocked
    priority:low
    area:models
    on Jul 1, 2026
  2. added
    status:ready
    status:review
    and removed
    status:blocked
    status:ready
    on Aug 27, 2026
  3. added
    status:done
    and removed
    status:review
    on Aug 31, 2026
  4. self-assigned this
    on Aug 31, 2026
  5. added this to the 0.7 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:lowstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions