Skip to content

Add: compose DeepSeek V4 CSA decode TP output - #940

Merged
zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
wangqin1723-max:feat/compose-deepseek-v4-csa-decode-tp4-output
Aug 18, 2026
Merged

zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
wangqin1723-max:feat/compose-deepseek-v4-csa-decode-tp4-output

Conversation

@wangqin1723-max

@wangqin1723-max wangqin1723-max commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator
  • Chain CSA heads through configurable TP token/head exchange, sharded
    output projection, and dependency-ordered FP32 reduce-scatter.
  • Consolidate TP1/2/4 max and subcapacity fixtures into the CSA
    entrypoint.
  • Reuse the shared TP1 output projection and bound sparse planning by the
    runtime token count.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 251f6356-f4df-43ef-9555-d594b5a9ecf6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a complete DeepSeek-V4 CSA decode output path for CP. It includes JIT execution, distributed launching, deterministic tensor fixtures, a PyTorch golden implementation, sentinel-aware comparison, and CLI cases for full and subcapacity execution.

Changes

CSA CP decode output

Layer / File(s) Summary
Fixture contracts and tensor construction
models/deepseek_v4_flash_dspark/decode_csa_output_cp.py
Defines capacity and alignment checks. Builds deterministic inputs, metadata, projection weights, scales, output sentinels, and tensor specifications.
Distributed decode execution
models/deepseek_v4_flash_dspark/decode_csa_output_cp.py
Adds the rank-local JIT pipeline and host launcher. The pipeline performs sparse attention, all-to-all communication, grouped O projection, and reduce-scatter.
Golden validation and test execution
models/deepseek_v4_flash_dspark/decode_csa_output_cp.py
Adds the PyTorch reference, valid-row and sentinel-tail comparison, and CLI support for capacity cases, compilation, pass dumping, and distributed execution.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant l3_decode_csa_output_cp
  participant decode_csa_output_cp
  participant DistributedBuffers
  participant golden_decode_csa_output_cp
  CLI->>l3_decode_csa_output_cp: run selected capacity case
  l3_decode_csa_output_cp->>DistributedBuffers: allocate communication buffers
  l3_decode_csa_output_cp->>decode_csa_output_cp: launch rank-local decode
  decode_csa_output_cp->>DistributedBuffers: exchange attention tokens and heads
  decode_csa_output_cp->>DistributedBuffers: reduce-scatter output
  CLI->>golden_decode_csa_output_cp: compute reference output
  CLI->>golden_decode_csa_output_cp: compare valid rows and sentinel tails
Loading

Possibly related PRs

Suggested labels: enhancement

Poem

A rabbit hops through sparse arrays bright,
Rank shards gather in the night.
RoPE turns and tensors flow,
Golden rows confirm the show.
Sentinel tails stay tucked away.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description check ✅ Passed The description directly covers CSA head chaining, TP output processing, fixtures, projections, and reduce-scatter changes.
Title check ✅ Passed The title clearly identifies the composition of DeepSeek V4 CSA decode with TP output processing.

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.

@wangqin1723-max
wangqin1723-max force-pushed the feat/compose-deepseek-v4-csa-decode-tp4-output branch from 46986b6 to d1b4d63 Compare August 12, 2026 02:39

@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

🧹 Nitpick comments (2)
models/deepseek_v4_flash_dspark/decode_csa_output_cp.py (2)

440-440: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Name the shared key dim-0 constant.

scores depends on the literal 0.25 that init_ori_kv (Line 249) and init_cmp_kv (Line 276) write into key dimension 0. The value appears in three places with no link between them. If a fixture edit changes one site, the golden diverges silently and the failure looks like a kernel bug.

Define one module constant, for example FIXTURE_KEY_DIM0 = 0.25, and use it at all three sites.

🤖 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_flash_dspark/decode_csa_output_cp.py` at line 440, Define
a module-level constant for the fixture key dimension-0 value and replace the
duplicated 0.25 literals in init_ori_kv, init_cmp_kv, and the scores calculation
with that shared constant, preserving the existing computation.

321-337: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Factor the shared RoPE phase computation into one helper.

init_freqs_cos and init_freqs_sin compute the same phase tensor. Only the lookup table differs. A single helper keeps the two fixtures in sync if the phase rule changes.

♻️ Proposed refactor
-    def init_freqs_cos():
-        rank = torch.arange(SP_SIZE, dtype=torch.int32).reshape(SP_SIZE, 1, 1)
-        token = torch.arange(local_t, dtype=torch.int32).reshape(1, local_t, 1)
-        column = torch.arange(ROPE_DIM, dtype=torch.int32).reshape(1, 1, ROPE_DIM)
-        phase = (rank + token + column).remainder(4)
-        phase[:, :, HALF_ROPE:] = (phase[:, :, HALF_ROPE:] + 1).remainder(4)
-        values = torch.tensor((1.0, 0.0, -1.0, 0.0), dtype=torch.bfloat16)
-        return values[phase]
-
-    def init_freqs_sin():
-        rank = torch.arange(SP_SIZE, dtype=torch.int32).reshape(SP_SIZE, 1, 1)
-        token = torch.arange(local_t, dtype=torch.int32).reshape(1, local_t, 1)
-        column = torch.arange(ROPE_DIM, dtype=torch.int32).reshape(1, 1, ROPE_DIM)
-        phase = (rank + token + column).remainder(4)
-        phase[:, :, HALF_ROPE:] = (phase[:, :, HALF_ROPE:] + 1).remainder(4)
-        values = torch.tensor((0.0, 1.0, 0.0, -1.0), dtype=torch.bfloat16)
-        return values[phase]
+    def rope_phase():
+        rank = torch.arange(SP_SIZE, dtype=torch.int32).reshape(SP_SIZE, 1, 1)
+        token = torch.arange(local_t, dtype=torch.int32).reshape(1, local_t, 1)
+        column = torch.arange(ROPE_DIM, dtype=torch.int32).reshape(1, 1, ROPE_DIM)
+        phase = (rank + token + column).remainder(4)
+        phase[:, :, HALF_ROPE:] = (phase[:, :, HALF_ROPE:] + 1).remainder(4)
+        return phase
+
+    def init_freqs_cos():
+        values = torch.tensor((1.0, 0.0, -1.0, 0.0), dtype=torch.bfloat16)
+        return values[rope_phase()]
+
+    def init_freqs_sin():
+        values = torch.tensor((0.0, 1.0, 0.0, -1.0), dtype=torch.bfloat16)
+        return values[rope_phase()]
🤖 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_flash_dspark/decode_csa_output_cp.py` around lines 321 -
337, Factor the duplicated rank/token/column RoPE phase construction from
init_freqs_cos and init_freqs_sin into one shared helper, then have both
functions reuse that helper while retaining their distinct lookup tables and
outputs.
🤖 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_flash_dspark/decode_csa_output_cp.py`:
- Around line 84-85: Extend the existing import-time guard beside the
divisibility check to require LOCAL_T - ROPE_CS_T_TILE to be strictly positive,
ensuring the derived subcapacity remains valid for build_tensor_specs. Preserve
the current whole-decode-request divisibility validation and raise the fixture
guard error during import rather than deferring it to CLI execution.

---

Nitpick comments:
In `@models/deepseek_v4_flash_dspark/decode_csa_output_cp.py`:
- Line 440: Define a module-level constant for the fixture key dimension-0 value
and replace the duplicated 0.25 literals in init_ori_kv, init_cmp_kv, and the
scores calculation with that shared constant, preserving the existing
computation.
- Around line 321-337: Factor the duplicated rank/token/column RoPE phase
construction from init_freqs_cos and init_freqs_sin into one shared helper, then
have both functions reuse that helper while retaining their distinct lookup
tables and outputs.
🪄 Autofix

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: c2a7a424-1c24-4a3a-9e0c-63daa94933f4

📥 Commits

Reviewing files that changed from the base of the PR and between 185d624 and d1b4d63.

📒 Files selected for processing (1)
  • models/deepseek_v4_flash_dspark/decode_csa_output_cp.py

Comment thread models/deepseek_v4_flash_dspark/decode_csa_output.py Outdated
@wangqin1723-max
wangqin1723-max force-pushed the feat/compose-deepseek-v4-csa-decode-tp4-output branch 3 times, most recently from c603e96 to ab8a116 Compare August 14, 2026 07:30
@wangqin1723-max wangqin1723-max changed the title Add: compose DeepSeek V4 CSA decode TP4 output Add: compose DeepSeek V4 CSA decode TP output Aug 14, 2026
- Chain CSA heads through configurable TP token/head exchange, sharded
  output projection, and dependency-ordered FP32 reduce-scatter.
- Consolidate TP1/2/4 max and subcapacity fixtures into the CSA
  entrypoint.
- Reuse the shared TP1 output projection and bound sparse planning by the
  runtime token count.
@wangqin1723-max
wangqin1723-max force-pushed the feat/compose-deepseek-v4-csa-decode-tp4-output branch from ab8a116 to a563884 Compare August 17, 2026 03:00
@zhangqi-chen
zhangqi-chen merged commit 8bf8860 into hw-native-sys:main Aug 18, 2026
9 of 11 checks passed
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