Skip to content

contract(trace-moe-gpu-sub-stages-v1): v1.0.0 PROPOSED scaffold for M-GPU-MOE-1.4 bisection#1498

Merged
noahgift merged 1 commit into
mainfrom
contract/trace-moe-gpu-sub-stages-v1-scaffold
May 5, 2026
Merged

contract(trace-moe-gpu-sub-stages-v1): v1.0.0 PROPOSED scaffold for M-GPU-MOE-1.4 bisection#1498
noahgift merged 1 commit into
mainfrom
contract/trace-moe-gpu-sub-stages-v1-scaffold

Conversation

@noahgift

@noahgift noahgift commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Sibling contract to trace-attn-sub-stages-v1; pins the SaveTensorStage extensions needed for M-GPU-MOE-1.4 NaN/Inf bisection per qwen3-moe-forward-gpu-v1 v1.4.0 (PR #1492).

Why now

After M-GPU-MOE-1.3 partial fix (PR #1491 f0cbe37f9 MERGED), heavy test produces 100% NaN logits on lambda-vector RTX 4090. Steps 1-9 are CPU-shared with finite output; step 10 (GPU MoE FFN) is the only candidate. The bisection plan needs trace-stage instrumentation, which requires the new SaveTensorStage variants pinned here.

What this contract pins

  • 2 mandatory new SaveTensorStage variants: MoeRouter (top-k weights), MoeFfnOut (aggregated)
  • 4 optional per-expert variants (with expert_id qualifier): MoeExpertGate/Up/Swigl/Out (promoted to mandatory only if 2-stage bisection isn't precise enough)
  • Bisection chain CPU-vs-GPU
  • 4 falsification tests (parse, byte-identity, bisection, fix-PR cites bisected stage)
  • 4 implementation stages M-MOE-SUB-0..4

Validation

pv validate contracts/trace-moe-gpu-sub-stages-v1.yaml → 0 errors, 0 warnings

Stacks on

PR #1492 (fe8877424 v1.4.0 amendment) which is already on main.

Test plan

  • pv validate clean
  • CI ci/gate green

🤖 Generated with Claude Code

Sibling contract to trace-attn-sub-stages-v1; pins the SaveTensorStage
extensions needed for M-GPU-MOE-1.4 NaN/Inf bisection per
qwen3-moe-forward-gpu-v1 v1.4.0 amendment.

WHY THIS CONTRACT:

  After M-GPU-MOE-1.3 partial fix (PR #1491 squash f0cbe37
  MERGED), heavy `qwen3_moe_gpu_parity` test on lambda-vector
  RTX 4090 produces 100% NaN logits. Steps 1-9 are CPU-shared
  with finite output; step 10 (GPU MoE FFN — moe_ffn_forward_layer_cuda
  → expert_swiglu_cuda → q4k_matvec/q6k_gemv) is the only candidate.
  Per qwen3-moe-forward-gpu-v1 v1.4.0 bisection plan step (a),
  apr trace needs new SaveTensorStage variants for MoE GPU stages.

WHAT IT DEFINES:

  * 2 mandatory new SaveTensorStage variants:
    - MoeRouter — top-k weights post-softmax/renormalize
    - MoeFfnOut — aggregated MoE FFN output (Σ w_e * expert_out_e)
  * 4 optional per-expert variants (with expert_id qualifier):
    - MoeExpertGate, MoeExpertUp, MoeExpertSwigl, MoeExpertOut
    - Promoted to mandatory only if 2-stage bisection isn't
      precise enough.

  * Bisection chain CPU-vs-GPU:
    cos_sequence = [
      cos(CPU.ffn_norm,    GPU.ffn_norm),     # parent enum
      cos(CPU.moe_router,  GPU.moe_router),   # NEW
      cos(CPU.moe_ffn_out, GPU.moe_ffn_out),  # NEW
    ]

FALSIFICATION TESTS (4):

  FALSIFY-MOE-SUB-001: New variants exist + parse correctly
  FALSIFY-MOE-SUB-002: Existing 20-stage byte-identity preserved
  FALSIFY-MOE-SUB-003: Bisection identifies first NaN-producing stage
  FALSIFY-MOE-SUB-004: Fix PR cites bisected stage by name

IMPLEMENTATION_STAGES (4):

  M-MOE-SUB-0: This contract scaffold (SHIPPED)
  M-MOE-SUB-1: Add MoeRouter + MoeFfnOut variants (PENDING)
  M-MOE-SUB-2: Wire MoeRouter into both CPU + GPU forward (PENDING)
  M-MOE-SUB-3: Wire MoeFfnOut + run heavy bisection (PENDING)
  M-MOE-SUB-4: OPTIONAL per-expert promotion (PENDING)

VALIDATION: pv validate exits 0 errors, 0 warnings.

Per CLAUDE.md "NEVER write code before writing a provable contract"
— this PR pins the trace-stage architecture before code lands.
M-MOE-SUB-1 follows in a separate PR.

Refs: M-GPU-MOE-1.4, R10, qwen3-moe-forward-gpu-v1 v1.4.0,
      trace-attn-sub-stages-v1 (sibling pattern).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@noahgift noahgift force-pushed the contract/trace-moe-gpu-sub-stages-v1-scaffold branch from 0a18b9d to c2afb5a Compare May 5, 2026 03:49
@noahgift noahgift merged commit 529eb56 into main May 5, 2026
10 checks passed
@noahgift noahgift deleted the contract/trace-moe-gpu-sub-stages-v1-scaffold branch May 5, 2026 04:09
noahgift added a commit that referenced this pull request May 5, 2026
…-MOE-SUB-1

Per `contracts/trace-moe-gpu-sub-stages-v1.yaml` v1.0.0 (PR #1498),
M-MOE-SUB-1 implementation: extend the parent SaveTensorStage enum
with the 2 new mandatory MoE-GPU bisection variants.

WHAT THIS PR ADDS:

  * `MoeRouter` — top-k expert weights post-softmax + renormalize,
    per-layer, captured AFTER FfnNorm and BEFORE per-expert SwiGLU.
  * `MoeFfnOut` — aggregated MoE FFN output Σ w_e * expert_out_e,
    per-layer, captured AFTER all per-expert computations.

CHANGES:

  * `SaveTensorStage` enum: 2 new variants between `PostFfnResidual`
    and `FinalNorm`.
  * `ALL` array: 20 → 22 entries; new variants in canonical position.
  * `canonical_name()`: 2 new arms returning "moe_router" + "moe_ffn_out".
  * `FromStr::from_str()` lowercase match: 2 new accepted strings.
  * `is_per_layer_count_matches_contract` test: per_layer 18 → 20,
    total 20 → 22.
  * `canonical_names_match_contract_enumeration` test: expected[]
    grows to 22 with new entries in canonical order.
  * 5 new `falsify_moe_sub_001_*` tests:
      - `moe_router_round_trip`
      - `moe_ffn_out_round_trip`
      - `2_new_stages_in_canonical_order` (ALL position assertions)
      - `parse_list_accepts_2_new_stages_together`
      - `parse_list_accepts_full_moe_block_chain` (3-stage bisection
        chain `ffn_norm,moe_router,moe_ffn_out`)

DISCHARGES: FALSIFY-MOE-SUB-001 (per contract).

NOT DISCHARGED YET (separate follow-up scope per contract):

  * FALSIFY-MOE-SUB-002 (byte-identity preservation) — needs the
    instrumentation PR (M-MOE-SUB-2 + 3) that wires MoeRouter +
    MoeFfnOut capture into both forward bodies.
  * FALSIFY-MOE-SUB-003 (live bisection on lambda-vector RTX 4090).
  * FALSIFY-MOE-SUB-004 (fix PR cites bisected stage by name).

VERIFICATION:

  cargo test -p aprender-serve --lib save_tensor_stage
  → 29 passed; 0 failed; 0 ignored

  rustfmt --check  → exit 0

DEPENDS ON: PR #1498 (`trace-moe-gpu-sub-stages-v1` v1.0.0 contract
scaffold) being on aprender main first. The contract pinned the
new variants' names + semantics; this PR implements them.

Refs: M-GPU-MOE-1.4 step (a) instrumentation, R10,
      qwen3-moe-forward-gpu-v1 v1.4.0,
      trace-moe-gpu-sub-stages-v1 v1.0.0,
      FALSIFY-MOE-SUB-001 (DISCHARGED).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
noahgift added a commit that referenced this pull request May 5, 2026
…-MOE-SUB-1

Per `contracts/trace-moe-gpu-sub-stages-v1.yaml` v1.0.0 (PR #1498),
M-MOE-SUB-1 implementation: extend the parent SaveTensorStage enum
with the 2 new mandatory MoE-GPU bisection variants.

WHAT THIS PR ADDS:

  * `MoeRouter` — top-k expert weights post-softmax + renormalize,
    per-layer, captured AFTER FfnNorm and BEFORE per-expert SwiGLU.
  * `MoeFfnOut` — aggregated MoE FFN output Σ w_e * expert_out_e,
    per-layer, captured AFTER all per-expert computations.

CHANGES:

  * `SaveTensorStage` enum: 2 new variants between `PostFfnResidual`
    and `FinalNorm`.
  * `ALL` array: 20 → 22 entries; new variants in canonical position.
  * `canonical_name()`: 2 new arms returning "moe_router" + "moe_ffn_out".
  * `FromStr::from_str()` lowercase match: 2 new accepted strings.
  * `is_per_layer_count_matches_contract` test: per_layer 18 → 20,
    total 20 → 22.
  * `canonical_names_match_contract_enumeration` test: expected[]
    grows to 22 with new entries in canonical order.
  * 5 new `falsify_moe_sub_001_*` tests:
      - `moe_router_round_trip`
      - `moe_ffn_out_round_trip`
      - `2_new_stages_in_canonical_order` (ALL position assertions)
      - `parse_list_accepts_2_new_stages_together`
      - `parse_list_accepts_full_moe_block_chain` (3-stage bisection
        chain `ffn_norm,moe_router,moe_ffn_out`)

DISCHARGES: FALSIFY-MOE-SUB-001 (per contract).

NOT DISCHARGED YET (separate follow-up scope per contract):

  * FALSIFY-MOE-SUB-002 (byte-identity preservation) — needs the
    instrumentation PR (M-MOE-SUB-2 + 3) that wires MoeRouter +
    MoeFfnOut capture into both forward bodies.
  * FALSIFY-MOE-SUB-003 (live bisection on lambda-vector RTX 4090).
  * FALSIFY-MOE-SUB-004 (fix PR cites bisected stage by name).

VERIFICATION:

  cargo test -p aprender-serve --lib save_tensor_stage
  → 29 passed; 0 failed; 0 ignored

  rustfmt --check  → exit 0

DEPENDS ON: PR #1498 (`trace-moe-gpu-sub-stages-v1` v1.0.0 contract
scaffold) being on aprender main first. The contract pinned the
new variants' names + semantics; this PR implements them.

Refs: M-GPU-MOE-1.4 step (a) instrumentation, R10,
      qwen3-moe-forward-gpu-v1 v1.4.0,
      trace-moe-gpu-sub-stages-v1 v1.0.0,
      FALSIFY-MOE-SUB-001 (DISCHARGED).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
noahgift added a commit that referenced this pull request May 5, 2026
…-MOE-SUB-1 (#1499)

Per `contracts/trace-moe-gpu-sub-stages-v1.yaml` v1.0.0 (PR #1498),
M-MOE-SUB-1 implementation: extend the parent SaveTensorStage enum
with the 2 new mandatory MoE-GPU bisection variants.

WHAT THIS PR ADDS:

  * `MoeRouter` — top-k expert weights post-softmax + renormalize,
    per-layer, captured AFTER FfnNorm and BEFORE per-expert SwiGLU.
  * `MoeFfnOut` — aggregated MoE FFN output Σ w_e * expert_out_e,
    per-layer, captured AFTER all per-expert computations.

CHANGES:

  * `SaveTensorStage` enum: 2 new variants between `PostFfnResidual`
    and `FinalNorm`.
  * `ALL` array: 20 → 22 entries; new variants in canonical position.
  * `canonical_name()`: 2 new arms returning "moe_router" + "moe_ffn_out".
  * `FromStr::from_str()` lowercase match: 2 new accepted strings.
  * `is_per_layer_count_matches_contract` test: per_layer 18 → 20,
    total 20 → 22.
  * `canonical_names_match_contract_enumeration` test: expected[]
    grows to 22 with new entries in canonical order.
  * 5 new `falsify_moe_sub_001_*` tests:
      - `moe_router_round_trip`
      - `moe_ffn_out_round_trip`
      - `2_new_stages_in_canonical_order` (ALL position assertions)
      - `parse_list_accepts_2_new_stages_together`
      - `parse_list_accepts_full_moe_block_chain` (3-stage bisection
        chain `ffn_norm,moe_router,moe_ffn_out`)

DISCHARGES: FALSIFY-MOE-SUB-001 (per contract).

NOT DISCHARGED YET (separate follow-up scope per contract):

  * FALSIFY-MOE-SUB-002 (byte-identity preservation) — needs the
    instrumentation PR (M-MOE-SUB-2 + 3) that wires MoeRouter +
    MoeFfnOut capture into both forward bodies.
  * FALSIFY-MOE-SUB-003 (live bisection on lambda-vector RTX 4090).
  * FALSIFY-MOE-SUB-004 (fix PR cites bisected stage by name).

VERIFICATION:

  cargo test -p aprender-serve --lib save_tensor_stage
  → 29 passed; 0 failed; 0 ignored

  rustfmt --check  → exit 0

DEPENDS ON: PR #1498 (`trace-moe-gpu-sub-stages-v1` v1.0.0 contract
scaffold) being on aprender main first. The contract pinned the
new variants' names + semantics; this PR implements them.

Refs: M-GPU-MOE-1.4 step (a) instrumentation, R10,
      qwen3-moe-forward-gpu-v1 v1.4.0,
      trace-moe-gpu-sub-stages-v1 v1.0.0,
      FALSIFY-MOE-SUB-001 (DISCHARGED).

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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