Skip to content

fix(ds4): correct layer-split HC handoff and per-device state#507

Merged
davide221 merged 4 commits into
Luce-Org:mainfrom
Graffioh:codex/fix-ds4-hc-shard-handoff
Jul 20, 2026
Merged

fix(ds4): correct layer-split HC handoff and per-device state#507
davide221 merged 4 commits into
Luce-Org:mainfrom
Graffioh:codex/fix-ds4-hc-shard-handoff

Conversation

@Graffioh

@Graffioh Graffioh commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes correctness and device-isolation issues in the DeepSeek4 HC layer-split path:

  • hand off the updated full HC state between local shards
  • scope HC weights and cached decode graphs to each shard
  • keep independent CUDA/HIP HC scratch per GPU device, sized for the requested shapes (shared buffers used to stay at whatever dims the first call allocated)
  • select the correct device at layer-range shard entry and hybrid step dispatch
  • add coverage for per-device HC scratch, scratch shape capacity, HC device selection, and the stale HC boundary-state guard

Note: deepseek4_step_hybrid is unchanged — the hybrid branch only gains
the device pin at dispatch; its HC runtime stays on the thread-local
ds4_hybrid_runtime.

Implementation

Local layer split now follows the full HC flow:

token embeddings -> shard 0 -> full HC state -> shard 1 -> ... -> final shard

Runtime caches are owned by each shard and keyed by weights, context, backend, device, layer range, and output ownership. HC scratch is stored in independently locked slots indexed by CUDA/HIP device.

Follow-up

The opt-in GPU F16 HC fn mirror for quantized ROCmFPX fn weights (DFLASH_DS4_ROCMFPX_HC_GPU) was split out into #546, stacked on this PR, including the deepseek4_cuda_hc_upload_f16 / deepseek4_cuda_hc_free primitives.

Validation

  • git diff --check and C++ syntax checks passed
  • at an earlier revision (before the review fixes were folded in): Release CUDA H200 builds passed for test_deepseek4_unit and dflash_server; single-shard and virtual two-shard [0,22) / [22,43) runs on cuda:0 completed sequential requests without HC, CUDA, crash, or abort errors, validating HC handoff and per-shard cache reuse — re-run on the current head pending
  • real multi-device device 0 -> device 1 -> device 0 validation on lucebox3/lucebox5 is still required
  • a barrier-based concurrent scratch test is still required before marking ready

@Graffioh Graffioh changed the title fix(deepseek4): correct HC shard handoff and scope runtime caches per shard fix(ds4): correct HC shard handoff and scope runtime caches per shard Jul 11, 2026
@davide221

Copy link
Copy Markdown
Contributor

We plan to run two AMD GPUs in the same HIP process. g_scratch is currently shared across devices, so the second shard may reuse scratch memory allocated on the first GPU. Please keep one scratch buffer per HIP device, set the correct device in HC worker threads

@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch 2 times, most recently from bf8dc45 to ecf55b1 Compare July 13, 2026 15:13
@Graffioh Graffioh changed the title fix(ds4): correct HC shard handoff and scope runtime caches per shard fix(ds4): correct layer-split HC handoff, per-device state, and greedy decode in local path Jul 13, 2026
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch 2 times, most recently from 581817d to b6a14e6 Compare July 14, 2026 19:48
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch 2 times, most recently from 0441ac2 to 4ae83dd Compare July 16, 2026 12:50
@Graffioh Graffioh changed the title fix(ds4): correct layer-split HC handoff, per-device state, and greedy decode in local path fix(ds4): correct layer-split HC handoff and per-device state Jul 16, 2026
@davide221
davide221 force-pushed the codex/ds4-rocmfpx-server branch from e943f2a to d5fae56 Compare July 17, 2026 05:33
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch from 4ae83dd to 796299e Compare July 19, 2026 06:11
@Graffioh
Graffioh changed the base branch from codex/ds4-rocmfpx-server to main July 19, 2026 06:11
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch 3 times, most recently from 7d6c85c to 5a3b84c Compare July 19, 2026 09:16
Graffioh and others added 2 commits July 19, 2026 11:27
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch from 5a3b84c to 2c38433 Compare July 19, 2026 09:28
Also size the shared scratch for the requested shapes: the bounded
buffers (mix/post/comb) are allocated at their kMaxHc maxima and the
working buffer tracks n_embd capacity, instead of staying at whatever
dims the first call happened to use.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mx99uuhcriPweAFggPwfce
@Graffioh
Graffioh force-pushed the codex/fix-ds4-hc-shard-handoff branch from 2c38433 to 685de03 Compare July 19, 2026 09:53
std::fprintf(stderr, g_failures ? " done\n" : " ok\n");
}

static void test_hc_set_device_contract() {

@Graffioh Graffioh Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(non-blocking) idk if needed or can be removed

@Graffioh
Graffioh marked this pull request as ready for review July 19, 2026 09:57

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 11 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="server/src/deepseek4/deepseek4_hc_cuda.cu">

<violation number="1" location="server/src/deepseek4/deepseek4_hc_cuda.cu:97">
P3: Concurrent HC calls remain unverified: the per-device test runs device workers sequentially, so it cannot validate simultaneous slot creation and use. Add a barrier-based two-device invocation before treating per-device scratch isolation as covered.</violation>
</file>

Comment thread server/tests/test_deepseek4_unit.cpp
// were created. Keep one independently locked slot per logical device so local
// shards cannot reuse another device's scratch pointers.
std::mutex g_scratch_slots_mutex;
std::vector<std::unique_ptr<HcScratchSlot>> g_scratch_slots;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: Concurrent HC calls remain unverified: the per-device test runs device workers sequentially, so it cannot validate simultaneous slot creation and use. Add a barrier-based two-device invocation before treating per-device scratch isolation as covered.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At server/src/deepseek4/deepseek4_hc_cuda.cu, line 97:

<comment>Concurrent HC calls remain unverified: the per-device test runs device workers sequentially, so it cannot validate simultaneous slot creation and use. Add a barrier-based two-device invocation before treating per-device scratch isolation as covered.</comment>

<file context>
@@ -62,8 +81,38 @@ struct HcCudaScratch {
+// were created. Keep one independently locked slot per logical device so local
+// shards cannot reuse another device's scratch pointers.
+std::mutex g_scratch_slots_mutex;
+std::vector<std::unique_ptr<HcScratchSlot>> g_scratch_slots;
+
+HcScratchSlot * current_scratch_slot() {
</file context>

@Graffioh Graffioh Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

non-blocking for this PR to be merged

how do we handle concurrency tests? in integration tests? @davide221

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don’t currently support concurrent generation requests (support coming in August). So non blocking for now

Comment thread server/tests/test_deepseek4_unit.cpp
Reset the backing DeepSeek4 cache buffers for both local and IPC shards instead of only rewinding their counters, and clear stale prefill logits on a fresh request. Extend the adapter reset regression to verify cache tensors are zeroed. Also size the HC reference Sinkhorn scratch from n_hc so the new n_hc=8 capacity test does not overflow its stack buffer.
@davide221
davide221 merged commit ae98774 into Luce-Org:main Jul 20, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants