Skip to content

perf(ds4): overlap q5 verification and stabilize 16K - #575

Merged
davide221 merged 9 commits into
mainfrom
codex/ds4-overlapped-moe-balance
Aug 13, 2026
Merged

perf(ds4): overlap q5 verification and stabilize 16K#575
davide221 merged 9 commits into
mainfrom
codex/ds4-overlapped-moe-balance

Conversation

@davide221

@davide221 davide221 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • batch q=5 hyper-connection and drafter-feature work across verifier lanes
  • balance expert placement against the two-device critical path
  • use bounded heterogeneous-prefill scratch and stable long-context graph shapes
  • add overlap tracing and exact-context qualification controls

This PR is stacked on #569.

Validation

Configuration: R9700 target plus Strix Halo expert device, q=5, four routed experts, two warmups and three measured 128-token replies per context.

Context Client decode median
2K 75.818 tok/s
4K 74.530 tok/s
8K 69.898 tok/s
16K 62.685 tok/s
2K after 16K 76.703 tok/s

All 25 responses matched the expected deterministic hash. Peak target-GPU memory use was 31.089 GiB.

  • test_deepseek4_unit: passed
  • test_server_unit: 322 passed, 0 failed
  • shell and Python checks: passed
  • git diff --check: passed

@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.

All reported issues were addressed across 17 files

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

Re-trigger cubic

Comment thread server/scripts/analyze_rocprof_overlap.py Outdated
Comment thread server/scripts/analyze_rocprof_overlap.py Outdated
Comment thread server/src/common/moe_hybrid_placement.cpp Outdated
Comment thread server/scripts/qualify_ds4_q5_amd.sh Outdated
Comment thread server/src/deepseek4/deepseek4_backend.cpp
Comment thread server/scripts/rocprof_server_wrapper.sh Outdated
@davide221
davide221 force-pushed the codex/ds4-q5-verifier branch from e24ad65 to 4ce3b82 Compare August 12, 2026 15:01
@davide221
davide221 force-pushed the codex/ds4-overlapped-moe-balance branch 4 times, most recently from 3240627 to 805d5a7 Compare August 12, 2026 19:36
@davide221
davide221 force-pushed the codex/ds4-q5-verifier branch from b5d3515 to 3244b9c Compare August 12, 2026 19:39
@davide221
davide221 force-pushed the codex/ds4-overlapped-moe-balance branch 3 times, most recently from bae0822 to c358b9d Compare August 13, 2026 13:02
@davide221

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review the current head after the branch rebase. Please focus on correctness, maintainability, security, and updated harness paths.

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review the current head after the branch rebase. Please focus on correctness, maintainability, security, and updated harness paths.

@davide221 I have started the AI code review. It will take a few minutes to complete.

@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.

All reported issues were addressed across 28 files

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

Re-trigger cubic

Comment thread server/src/common/moe_hybrid_ffn_eval.cpp
Comment thread harness/qualification/deepseek4/analyze_rocprof_overlap.py
Comment thread harness/qualification/deepseek4/qualify_ds4_q5_amd.sh
Comment thread server/src/common/moe_hybrid_routing_stats.cpp
Comment thread server/deps/llama.cpp/ggml/src/ggml.c
Comment thread server/src/common/moe_hybrid_placement.cpp Outdated
Comment thread server/src/common/moe_hybrid_ffn_eval.cpp Outdated
Comment thread server/src/common/moe_hybrid_routing_stats.cpp Outdated
Comment thread server/src/deepseek4/deepseek4_backend.cpp Outdated
Comment thread harness/qualification/deepseek4/analyze_rocprof_overlap.py Outdated
@davide221

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review the current head. All 12 findings from the previous review were addressed with focused regression coverage; please re-check correctness, maintainability, and security.

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review the current head. All 12 findings from the previous review were addressed with focused regression coverage; please re-check correctness, maintainability, and security.

@davide221 I have started the AI code review. It will take a few minutes to complete.

@davide221
davide221 force-pushed the codex/ds4-overlapped-moe-balance branch from 50f9f2b to ceaf788 Compare August 13, 2026 14:04
@davide221
davide221 changed the base branch from codex/ds4-q5-verifier to main August 13, 2026 14:04

@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.

7 issues found and verified against the latest diff

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="harness/qualification/deepseek4/analyze_rocprof_overlap.py">

<violation number="1" location="harness/qualification/deepseek4/analyze_rocprof_overlap.py:135">
P3: `--top` is the only numeric option not range-validated; every other flag (floats, `--timeline-max`, `--timeline-merge-gap-us`) rejects invalid values. A negative `--top` slips through and, because the top-kernel section is gated only by `args.top > 0`, it silently disables `top_kernels_*` output. Validate `--top >= 0` alongside the other numeric options so an invalid setting fails loudly instead of changing behavior without notice.</violation>

<violation number="2" location="harness/qualification/deepseek4/analyze_rocprof_overlap.py:182">
P2: Finite extreme values still crash the analyzer with an uncaught `OverflowError` during nanosecond conversion. Validate the scaled values, or catch the conversion overflow and reject these options through `parser.error()` before processing the trace.</violation>

<violation number="3" location="harness/qualification/deepseek4/analyze_rocprof_overlap.py:235">
P2: A tiny positive `--bin-ms` can turn a normal trace into an effectively unbounded one-nanosecond output loop. Reject bin sizes that would exceed a fixed maximum number of bins before entering the loop.</violation>
</file>

<file name="server/deps/llama.cpp/ggml/src/ggml.c">

<violation number="1" location="server/deps/llama.cpp/ggml/src/ggml.c:8427">
P2: When `global_ids` has dimensions beyond route and token, this constructor creates an output for every plane but the balanced-owner kernel processes only the first plane, leaving the remaining IDs uninitialized. Reject non-2D route tensors here, as `ggml_ds4_moe_align_ids` already does, or add full multidimensional support.</violation>
</file>

<file name="server/src/common/moe_hybrid_ffn_eval.cpp">

<violation number="1" location="server/src/common/moe_hybrid_ffn_eval.cpp:853">
P1: When dynamic route balance is enabled with a CPU cold-expert backend, the secondary balanced-ID node runs on CPU and aborts because `GGML_OP_MOE_FUSED` has no CPU implementation. Guard this mode on backend capability or provide a CPU owner-ID implementation before routing the node to the peer.</violation>
</file>

<file name="harness/qualification/deepseek4/qualify_ds4_q5_amd.sh">

<violation number="1" location="harness/qualification/deepseek4/qualify_ds4_q5_amd.sh:368">
P2: When `PROFILED_SERVER_BIN` is missing or non-executable, this forwarding loop lets qualification reach GPU setup and then fail when the profiler wrapper launches the server. Validate `PROFILED_SERVER_BIN` as an executable during preflight, alongside `SERVER_BIN`, so invalid profiler runs fail before changing performance levels.

(Based on your team's feedback about validating environment-provided binaries.) .</violation>
</file>

<file name="server/src/deepseek4/deepseek4_fused_verify.inc">

<violation number="1" location="server/src/deepseek4/deepseek4_fused_verify.inc:185">
P3: When dynamic route balance is enabled, `preferred_hot` is already `layer.hot_local_by_global` (selected above), so this fallback assigns `hot` the exact value it already holds — a no-op. The guard only has an effect on the static path where `preferred_hot` is the phase-specific `decode_hot_local_by_global`. Gate the fallback on `!dynamic_route_balance` (or drop it in dynamic mode) so the dead assignment isn't silently carried in the balance path.</violation>
</file>

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

Re-trigger cubic

[](int32_t local) { return local < 0; })) {
return false;
}
owner.local_ids = track(ggml_ds4_moe_balanced_owner_ids(

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.

P1: When dynamic route balance is enabled with a CPU cold-expert backend, the secondary balanced-ID node runs on CPU and aborts because GGML_OP_MOE_FUSED has no CPU implementation. Guard this mode on backend capability or provide a CPU owner-ID implementation before routing the node to the peer.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At server/src/common/moe_hybrid_ffn_eval.cpp, line 853:

<comment>When dynamic route balance is enabled with a CPU cold-expert backend, the secondary balanced-ID node runs on CPU and aborts because `GGML_OP_MOE_FUSED` has no CPU implementation. Guard this mode on backend capability or provide a CPU owner-ID implementation before routing the node to the peer.</comment>

<file context>
@@ -784,6 +842,24 @@ static bool build_moe_owner_remap(
+                [](int32_t local) { return local < 0; })) {
+            return false;
+        }
+        owner.local_ids = track(ggml_ds4_moe_balanced_owner_ids(
+            ctx, global_ids, router_weights,
+            *owner.local_lut, *owner.valid_lut,
</file context>

merged = {agent: merge_intervals(intervals_by_agent[agent]) for agent in agents}
overlap = intersect_intervals(merged[agents[0]], merged[agents[1]])

window_start = trace_start + int(args.window_start_s * 1e9)

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.

P2: Finite extreme values still crash the analyzer with an uncaught OverflowError during nanosecond conversion. Validate the scaled values, or catch the conversion overflow and reject these options through parser.error() before processing the trace.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At harness/qualification/deepseek4/analyze_rocprof_overlap.py, line 182:

<comment>Finite extreme values still crash the analyzer with an uncaught `OverflowError` during nanosecond conversion. Validate the scaled values, or catch the conversion overflow and reject these options through `parser.error()` before processing the trace.</comment>

<file context>
@@ -0,0 +1,282 @@
+    merged = {agent: merge_intervals(intervals_by_agent[agent]) for agent in agents}
+    overlap = intersect_intervals(merged[agents[0]], merged[agents[1]])
+
+    window_start = trace_start + int(args.window_start_s * 1e9)
+    requested_end = (
+        trace_start + int(args.window_end_s * 1e9)
</file context>

f"either_busy_s={union_ns/1e9:.3f}"
)

bin_ns = max(1, int(args.bin_ms * 1e6))

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.

P2: A tiny positive --bin-ms can turn a normal trace into an effectively unbounded one-nanosecond output loop. Reject bin sizes that would exceed a fixed maximum number of bins before entering the loop.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At harness/qualification/deepseek4/analyze_rocprof_overlap.py, line 235:

<comment>A tiny positive `--bin-ms` can turn a normal trace into an effectively unbounded one-nanosecond output loop. Reject bin sizes that would exceed a fixed maximum number of bins before entering the loop.</comment>

<file context>
@@ -0,0 +1,282 @@
+        f"either_busy_s={union_ns/1e9:.3f}"
+    )
+
+    bin_ns = max(1, int(args.bin_ms * 1e6))
+    print("bin_start_s,agent1_busy_pct,agent2_busy_pct,both_busy_pct")
+    cursor = window_start
</file context>

GGML_ASSERT(router_weights->type == GGML_TYPE_F32);
GGML_ASSERT(local_id_lut->type == GGML_TYPE_I32);
GGML_ASSERT(main_candidate_lut->type == GGML_TYPE_F32);
GGML_ASSERT(ggml_are_same_shape(global_ids, router_weights));

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.

P2: When global_ids has dimensions beyond route and token, this constructor creates an output for every plane but the balanced-owner kernel processes only the first plane, leaving the remaining IDs uninitialized. Reject non-2D route tensors here, as ggml_ds4_moe_align_ids already does, or add full multidimensional support.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At server/deps/llama.cpp/ggml/src/ggml.c, line 8427:

<comment>When `global_ids` has dimensions beyond route and token, this constructor creates an output for every plane but the balanced-owner kernel processes only the first plane, leaving the remaining IDs uninitialized. Reject non-2D route tensors here, as `ggml_ds4_moe_align_ids` already does, or add full multidimensional support.</comment>

<file context>
@@ -8412,6 +8412,43 @@ struct ggml_tensor * ggml_ds4_moe_align_ids(
+    GGML_ASSERT(router_weights->type == GGML_TYPE_F32);
+    GGML_ASSERT(local_id_lut->type == GGML_TYPE_I32);
+    GGML_ASSERT(main_candidate_lut->type == GGML_TYPE_F32);
+    GGML_ASSERT(ggml_are_same_shape(global_ids, router_weights));
+    GGML_ASSERT(ggml_nelements(local_id_lut) ==
+                ggml_nelements(main_candidate_lut));
</file context>
Suggested change
GGML_ASSERT(ggml_are_same_shape(global_ids, router_weights));
GGML_ASSERT(ggml_n_dims(global_ids) == 2);
GGML_ASSERT(ggml_are_same_shape(global_ids, router_weights));


# Preserve only the explicit profiler-wrapper controls across env -i. Ordinary
# qualification runs leave these unset and retain the exact established env.
for profiler_var in PROFILED_SERVER_BIN ROCPROF_OUTPUT_DIR \

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.

P2: When PROFILED_SERVER_BIN is missing or non-executable, this forwarding loop lets qualification reach GPU setup and then fail when the profiler wrapper launches the server. Validate PROFILED_SERVER_BIN as an executable during preflight, alongside SERVER_BIN, so invalid profiler runs fail before changing performance levels.

(Based on your team's feedback about validating environment-provided binaries.) .

View Feedback

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At harness/qualification/deepseek4/qualify_ds4_q5_amd.sh, line 368:

<comment>When `PROFILED_SERVER_BIN` is missing or non-executable, this forwarding loop lets qualification reach GPU setup and then fail when the profiler wrapper launches the server. Validate `PROFILED_SERVER_BIN` as an executable during preflight, alongside `SERVER_BIN`, so invalid profiler runs fail before changing performance levels.

(Based on your team's feedback about validating environment-provided binaries.) .</comment>

<file context>
@@ -275,6 +336,42 @@ for visibility_var in HIP_VISIBLE_DEVICES ROCR_VISIBLE_DEVICES; do
+
+# Preserve only the explicit profiler-wrapper controls across env -i. Ordinary
+# qualification runs leave these unset and retain the exact established env.
+for profiler_var in PROFILED_SERVER_BIN ROCPROF_OUTPUT_DIR \
+    ROCPROF_START_SECONDS ROCPROF_DURATION_SECONDS; do
+    if [[ -n "${!profiler_var:-}" ]]; then
</file context>

// A phase-specific placement may move a resident expert to the
// peer only when the peer owns a copy. Otherwise retain its
// physical main-owner route so no selected expert is dropped.
if (hot < 0 && cold < 0) {

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: When dynamic route balance is enabled, preferred_hot is already layer.hot_local_by_global (selected above), so this fallback assigns hot the exact value it already holds — a no-op. The guard only has an effect on the static path where preferred_hot is the phase-specific decode_hot_local_by_global. Gate the fallback on !dynamic_route_balance (or drop it in dynamic mode) so the dead assignment isn't silently carried in the balance path.

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

<comment>When dynamic route balance is enabled, `preferred_hot` is already `layer.hot_local_by_global` (selected above), so this fallback assigns `hot` the exact value it already holds — a no-op. The guard only has an effect on the static path where `preferred_hot` is the phase-specific `decode_hot_local_by_global`. Gate the fallback on `!dynamic_route_balance` (or drop it in dynamic mode) so the dead assignment isn't silently carried in the balance path.</comment>

<file context>
@@ -166,20 +166,38 @@ static void ds4_fused_verify_refresh_hybrid_luts(
+            // A phase-specific placement may move a resident expert to the
+            // peer only when the peer owns a copy. Otherwise retain its
+            // physical main-owner route so no selected expert is dropped.
+            if (hot < 0 && cold < 0) {
+                hot = layer.hot_local_by_global[(size_t) ie];
+            }
</file context>

parser.add_argument("--bin-ms", type=float, default=1000.0)
parser.add_argument("--window-start-s", type=float, default=0.0)
parser.add_argument("--window-end-s", type=float)
parser.add_argument("--top", type=int, default=12)

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: --top is the only numeric option not range-validated; every other flag (floats, --timeline-max, --timeline-merge-gap-us) rejects invalid values. A negative --top slips through and, because the top-kernel section is gated only by args.top > 0, it silently disables top_kernels_* output. Validate --top >= 0 alongside the other numeric options so an invalid setting fails loudly instead of changing behavior without notice.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At harness/qualification/deepseek4/analyze_rocprof_overlap.py, line 135:

<comment>`--top` is the only numeric option not range-validated; every other flag (floats, `--timeline-max`, `--timeline-merge-gap-us`) rejects invalid values. A negative `--top` slips through and, because the top-kernel section is gated only by `args.top > 0`, it silently disables `top_kernels_*` output. Validate `--top >= 0` alongside the other numeric options so an invalid setting fails loudly instead of changing behavior without notice.</comment>

<file context>
@@ -0,0 +1,282 @@
+    parser.add_argument("--bin-ms", type=float, default=1000.0)
+    parser.add_argument("--window-start-s", type=float, default=0.0)
+    parser.add_argument("--window-end-s", type=float)
+    parser.add_argument("--top", type=int, default=12)
+    parser.add_argument(
+        "--timeline-max", type=int, default=0,
</file context>

@davide221
davide221 merged commit c81499a into main Aug 13, 2026
1 check 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.

1 participant