Skip to content

Fix(es): align expert_shared golden with kernel int32 matmul order - #847

Open
lwDavid wants to merge 1 commit into
hw-native-sys:mainfrom
lwDavid:fix/dsv4-expert-shared-int32-golden
Open

Fix(es): align expert_shared golden with kernel int32 matmul order#847
lwDavid wants to merge 1 commit into
hw-native-sys:mainfrom
lwDavid:fix/dsv4-expert-shared-int32-golden

Conversation

@lwDavid

@lwDavid lwDavid commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

What & why

expert_shared was a flaky precision FAIL (~3/5 runs on a5). Root cause: golden_expert_shared computed all three matmuls (gate/up/W2) as fp32 matmuls of pre-dequantized operands, while the kernel does exact INT8×INT8→INT32 accumulate then dequant. The golden's fp32 accumulation rounding flipped bf16 last-bit ties on the final fp32→bf16 cast.

Fix

Mirror the kernel's "accumulate-int32, then one fp32 dequant mul" order in golden_expert_shared. Integer accumulation is exact and order-independent, so the golden's int32 matmul is bit-identical to the kernel's tiled int32 accumulate regardless of K-tiling.

This is a golden-side fix: the kernel was already numerically correct (int32 matmul is the exact dot product), and the golden was an imperfect fp32 emulator of the device's int8 path. Per docs/precision-tuning.md §2, for an algebraically-equivalent reorder the golden mirrors the kernel's order. Aligning the golden also made the test stricter (a real kernel bug would still fail against the exact-int32 reference).

Verification

5/5 PASS on a5 (was flaky ~3/5). expert_shared was the only v4-pro single-card case hitting this; the int32 alignment makes it deterministic.

golden_expert_shared computed gate/up/W2 as fp32 matmuls of pre-dequantized
operands, but the kernel does exact INT8xINT8->INT32 accumulate then dequant.
The golden's fp32 accumulation rounding flipped bf16 last-bit ties on the
final cast, causing flaky precision FAILs (~3/5 runs on a5).

Mirror the kernel's 'accumulate-int32, then one fp32 dequant mul' order in
the golden. Integer accumulation is exact and order-independent, so the
golden's int32 matmul is bit-identical to the kernel's tiled int32 accumulate
regardless of K-tiling.

Verified: 5/5 PASS on a5 (was flaky).
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

golden_expert_shared now mirrors kernel numeric ordering by using INT8×INT8 matmuls with INT32 accumulation, then applying dequantization and scaling for the gate/up and W2 stages.

Changes

Expert reference numerics

Layer / File(s) Summary
INT32 accumulated expert computation
models/deepseek/v4-pro/expert_shared.py
golden_expert_shared performs gate/up and W2 matmuls with INT32 accumulation before dequantization, while preserving SWIGLU clamping and post-activation INT8 requantization.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a bunny with bytes in a row,
Watching INT32 sums safely grow.
Scales wait their turn,
While kernels return,
And rounded results now match to the toe.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: aligning the golden expert_shared math order with the kernel's int32 accumulation.
Description check ✅ Passed The description directly matches the changeset and explains the precision fix, root cause, and verification outcome.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@models/deepseek/v4-pro/expert_shared.py`:
- Around line 322-324: Split the semicolon-separated assignments in the shared
weight initialization block into separate statements, keeping each tensor and
scale assignment unchanged and preserving the existing symbols w1_i8, w1_scale,
w3_i8, w3_scale, w2_i8, and w2_scale.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2b626c61-1a7a-4a84-8f1e-325c2cdafa9d

📥 Commits

Reviewing files that changed from the base of the PR and between 7e7d4cc and ece84e7.

📒 Files selected for processing (1)
  • models/deepseek/v4-pro/expert_shared.py

Comment on lines +322 to +324
w1_i8 = tensors["shared_w1"]; w1_scale = tensors["shared_w1_scale"].float() # [MOE_INTER, D], [MOE_INTER]
w3_i8 = tensors["shared_w3"]; w3_scale = tensors["shared_w3_scale"].float()
w2_i8 = tensors["shared_w2"]; w2_scale = tensors["shared_w2_scale"].float() # [D, MOE_INTER], [D]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Split the semicolon-separated assignments.

Lines 322-324 violate Ruff E702 and can fail linting.

Proposed fix
-    w1_i8 = tensors["shared_w1"]; w1_scale = tensors["shared_w1_scale"].float()   # [MOE_INTER, D], [MOE_INTER]
-    w3_i8 = tensors["shared_w3"]; w3_scale = tensors["shared_w3_scale"].float()
-    w2_i8 = tensors["shared_w2"]; w2_scale = tensors["shared_w2_scale"].float()   # [D, MOE_INTER], [D]
+    w1_i8 = tensors["shared_w1"]
+    w1_scale = tensors["shared_w1_scale"].float()
+    w3_i8 = tensors["shared_w3"]
+    w3_scale = tensors["shared_w3_scale"].float()
+    w2_i8 = tensors["shared_w2"]
+    w2_scale = tensors["shared_w2_scale"].float()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
w1_i8 = tensors["shared_w1"]; w1_scale = tensors["shared_w1_scale"].float() # [MOE_INTER, D], [MOE_INTER]
w3_i8 = tensors["shared_w3"]; w3_scale = tensors["shared_w3_scale"].float()
w2_i8 = tensors["shared_w2"]; w2_scale = tensors["shared_w2_scale"].float() # [D, MOE_INTER], [D]
w1_i8 = tensors["shared_w1"]
w1_scale = tensors["shared_w1_scale"].float()
w3_i8 = tensors["shared_w3"]
w3_scale = tensors["shared_w3_scale"].float()
w2_i8 = tensors["shared_w2"]
w2_scale = tensors["shared_w2_scale"].float()
🧰 Tools
🪛 Ruff (0.15.21)

[error] 322-322: Multiple statements on one line (semicolon)

(E702)


[error] 323-323: Multiple statements on one line (semicolon)

(E702)


[error] 324-324: Multiple statements on one line (semicolon)

(E702)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@models/deepseek/v4-pro/expert_shared.py` around lines 322 - 324, Split the
semicolon-separated assignments in the shared weight initialization block into
separate statements, keeping each tensor and scale assignment unchanged and
preserving the existing symbols w1_i8, w1_scale, w3_i8, w3_scale, w2_i8, and
w2_scale.

Source: Linters/SAST tools

@lwDavid lwDavid self-assigned this Jul 28, 2026
@lwDavid lwDavid added the enhancement New feature or request label Jul 28, 2026
@lwDavid lwDavid moved this to Done in pto project Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant