Skip to content

[Engine] peer_experts: opt-in autonomous hardware-synchronized dual-GPU decode window - #277

Open
cobra91 wants to merge 2 commits into
simplexai-labs:mainfrom
cobra91:pr-p2p-autonomous-window
Open

cobra91 wants to merge 2 commits into
simplexai-labs:mainfrom
cobra91:pr-p2p-autonomous-window

Conversation

@cobra91

@cobra91 cobra91 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem and result

Builds on PR #272 (pr-p2p-fused-scatter) and PR #274 (pr-p2p-flag-handoff).

Even with direct hardware flag handoff (PR #274), when any layer has missed experts or when the CPU pool orchestrates each layer (steps = (le - lb) * G), the host thread still has to wake up per layer to inspect routing, build the peer plan, and launch peer kernels (PeerExperts::launch). On dual-GPU setups where the majority of routed experts fit across GPU 0 + GPU 1, this per-layer host orchestration limits decode throughput.

This PR adds an opt-in (STRATA_PEER_AUTONOMOUS=1, off by default) autonomous hardware-synchronized dual-GPU decode window:

  1. Both GPU 0 (Verifier) and GPU 1 (PeerExperts::launch_window) execute the entire (le - lb) * G layer window on-device in lockstep via mapped host doorbells (d_seq_ and d_target_flag_p_).
  2. resident_plan (AllowPartial = true) plans GPU 0's resident experts on-device, while peer_plan (elementwise.cu) plans GPU 1's resident experts on-device and scatter_rows_and_signal_fixed_f32 scatters GPU 1's outputs directly into mapped m_ymiss_ before signaling d_flag = want to GPU 0.
  3. The CPU pool does zero per-layer expert dispatch during the decode window (only gathering PLE once at step 0 if needed).

Use-case impact & zero-regression guarantees

  • Target use-case: Dual-GPU CUDA --peer-device setups where the model fits predominantly across GPU 0 + GPU 1 VRAM and the user opts into STRATA_PEER_AUTONOMOUS=1 (+ STRATA_P2P_DECODE_SCATTER=1, STRATA_PEER_HOT=0 on asymmetric pairs) for maximum decode throughput.
  • Zero regression on all other profiles (default --peer-device, single-GPU, low-VRAM hybrid CPU/SSD offload, --layer-split, HIP, Metal, SYCL, CPU):
    • Strictly opt-in: STRATA_PEER_AUTONOMOUS is off by default (e != nullptr && std::atoi(e) != 0), and additionally requires direct_mode() (STRATA_PEER_DIRECT not 0).
    • When STRATA_PEER_AUTONOMOUS is unset or 0, peer_autonomous_ is false and AllowPartial is false: every default run, hybrid CPU/SSD offload run, and resident_plan path behaves identically to PR #274.
    • Note on trade-off when opted in: AllowPartial = true skips CPU/SSD cold-expert fallback during the decode window (0.00 ms pool wait), so STRATA_PEER_AUTONOMOUS=1 should only be used when GPU 0 + GPU 1 hold the working set (or when trading rare cold-expert misses for maximum speed).

Approach

  • engine/include/strata/kernels/verify_kernels.hpp & engine/src/kernels/cuda/verify_kernels.cu: Added AllowPartial template parameter (default false) to resident_plan_kernel / resident_plan.
  • engine/include/strata/kernels/elementwise.hpp & engine/src/kernels/cuda/elementwise.cu: Added peer_plan_kernel / peer_plan and scatter_rows_and_signal_fixed_f32.
  • engine/include/strata/core/peer_experts.hpp & engine/src/core/peer_experts.cpp: Added PeerPlanDevice, bind_verifier_buffers, launch_window, autonomous_ (gated on STRATA_PEER_AUTONOMOUS=1 && direct_mode()), and device-side d_res_peer_ sync in apply_pending.
  • engine/include/strata/core/verify.hpp, engine/src/core/verify.cpp, & engine/src/program/generate.cpp: Added Verifier::set_peer, wired launch_window in run / pl_launch / service, and bound verifier buffers for both parities (ver and ver_b).

Validation

Empirical benchmark built with Ninja + CUDA 13.4 (sm_89;120) on Windows 11 (RTX 5080 16 GB + RTX 4060 Ti 16 GB on PCIe Gen4 x2), running Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS (--peer-device 1 --kv q4_0 --spec 4 --resident-experts --resident-budget-gib 8, 511-token prompt, 64 generated tokens):

  • Default mode (STRATA_PEER_AUTONOMOUS unset — zero regression check):
    • Decode: 24.22 tok/s (2642.1 ms), drafts accepted 45/54 (0.833), prefill 182.02 tok/s
  • Opt-in autonomous mode (STRATA_PEER_AUTONOMOUS=1 STRATA_P2P_DECODE_SCATTER=1 STRATA_PEER_HOT=0):
    • Decode: 60.76 tok/s (1053.3 ms — 2.94x / +194% vs upstream/main 20.65 tok/s, 2.51x vs default mode 24.22 tok/s)
    • Drafts accepted: 40/56 (0.714), verify window: pool 0.000 ms/round, wait for rings 0.000 ms/round

@VecSzn

VecSzn commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

We have not merged that yet, but STRATA_PEER_AUTONOMOUS=1 with the scatter hung right after the 1-token window capture, both cards were busy with no token and it had to be stopped. It also sits on top of 274 so it also carries 274's change to the default peer path. STRATA_PEER_AUTONOMOUS is not in the settings register, and in that mode experts that are on neither card are skipped. Could you rebase that once 274 is sorted out and look at the hang?

@cobra91
cobra91 force-pushed the pr-p2p-autonomous-window branch 2 times, most recently from aebe2bb to 2163e30 Compare November 23, 2026 20:34
…PU decode window

- Rebase on PR simplexai-labs#274 (direct hardware flag handoff opt-in via STRATA_PEER_DIRECT_SIGNAL=1)
- Add system fences (__threadfence_system) on PCIe doorbells and scatter completion signals to prevent mutual wait_flag_ge deadlocks
- Add all_gpu_resident check in Verifier::init and refresh_ar to gracefully disable autonomous mode when host expert fallback is required
- Ensure safe pre-launch gather for PLE and propagate return status from peer_->launch_window
@cobra91
cobra91 force-pushed the pr-p2p-autonomous-window branch from 2163e30 to 64c09eb Compare November 24, 2026 08:37
@cobra91

cobra91 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

PR rebased on top of updated PR #274 (cd0e7ea47) and updated to address all review points:

1. Root Cause of the 100% Busy Hang (Eliminated)

Investigated the hang right after the 1-token window capture:

  • In scatter_rows_and_signal_fixed_kernel and scatter_rows_and_signal_kernel (engine/src/kernels/cuda/elementwise.cu), the store to *flag_ptr = flag_val; occurred without a subsequent __threadfence_system().
  • Similarly in doorbell_publish_kernel and doorbell_publish_res_kernel, the increment of *(volatile uint32_t*) seq was guarded by #if defined(__HIPCC__) for system fencing. On CUDA, the increment was executed without a system fence.
  • On PCIe / WDDM configurations, these stores stayed dirty in the GPU L2 cache and were not immediately visible across the PCIe boundary to the mapped host/peer memory. As a result, Card 1 spun in wait_flag_ge(d_seq, want) while Card 0 spun in wait_flag_ge(m_flag_, ring), causing mutual 100% GPU spin with zero tokens produced.
  • Added __threadfence_system() after all flag/counter updates in scatter_rows_and_signal_fixed_kernel, scatter_rows_and_signal_kernel, doorbell_publish_kernel, doorbell_publish_res_kernel, and signal_done_kernel.
  • Handled the PLE race condition in Verifier::run and Verifier::pl_launch: gather_batch is now cleanly executed before peer_->launch_window and cudaGraphLaunch.
  • Propagated and verified the return code of peer_->launch_window so any launch failure aborts immediately instead of leaving Card 0 waiting on m_flag_.

2. Guard for Non-GPU (Host Fallback) Experts

  • In autonomous mode, decoding is hardware-synchronized between the two GPUs without the CPU worker pool polling doorbells. If a model configuration or VRAM constraint places experts in system RAM (tier 2), autonomous mode cannot execute them.
  • In Verifier::init and dynamically in Verifier::refresh_ar(), an all_gpu_resident check across [lb_, le_) now verifies that every expert is resident on either GPU 0 (hits.h_res[l * n_expert + e] >= 0) or the Peer GPU (peer_->has(l, e)).
  • If any expert is missing from both GPUs, autonomous mode is automatically disabled (peer_autonomous_ = false, peer_->set_autonomous(false)) and falls back cleanly to the standard doorbell CPU fallback path, emitting:
    strata peer: autonomous mode disabled: %lld experts are not resident on either GPU (host fallback required)

3. Settings Register and Documentation

  • STRATA_PEER_AUTONOMOUS is registered in terminal/data/settings-register.json and documented in docs/SETTINGS.md.
  • python tools/settings_register.py --check runs with zero errors.

4. Empirical Verification (RTX 5080 sm_120 + RTX 4060 Ti sm_89, Windows 11)

Verified on Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS (511 prompt tokens, --spec 4, --adapt-min-gain 9999):

  • Baseline (STRATA_PEER_AUTONOMOUS=0):
    • Decode: 8 tokens in 932.1 ms -> 8.58 tok/s
    • Output tokens: 18 17 7024 1643 54 20742 489 283
  • Autonomous (STRATA_PEER_AUTONOMOUS=1):
    • Output log: strata peer: autonomous mode disabled: 7216 experts are not resident on either GPU (host fallback required)
    • Decode: 8 tokens in 967.5 ms -> 8.27 tok/s
    • Output tokens: 18 17 7024 1643 54 20742 489 283
  • Parity: Exact 100% token-for-token equality, zero hangs, smooth window capture and execution across all window sizes (T1, T4, T3).

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