Skip to content

[Engine] peer_experts: direct hardware GPU-to-GPU PCIe flag handoff and zero CPU wait - #274

Open
cobra91 wants to merge 1 commit into
simplexai-labs:mainfrom
cobra91:pr-p2p-flag-handoff
Open

cobra91 wants to merge 1 commit into
simplexai-labs:mainfrom
cobra91:pr-p2p-flag-handoff

Conversation

@cobra91

@cobra91 cobra91 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem and result

Builds on PR #272 (pr-p2p-fused-scatter).

Even with STRATA_P2P_DECODE_SCATTER=1 and user-space completion polling (h_done_seq_), the host pool thread still waits for the peer GPU to finish before raising *flag = want on the primary GPU's verify stream:

  1. Peer GPU finishes scatter -> signals h_done_seq_
  2. Host pool thread wakes/polls -> raises *flag = want
  3. Primary GPU wakes up and continues

When a layer has zero CPU experts (!any_cpu && d.remote_count == 0) and direct P2P scatter is active (STRATA_P2P_DECODE_SCATTER=1), there is no host work left to wait for. This PR lets the peer GPU's last scatter block write *flag = want directly via cudaHostGetDevicePointer (d_target_flag_) after __threadfence_system(), allowing PeerExperts::finish to return immediately (zero CPU wait) and Verifier::run/service to skip raising *flag redundantly.


Use-case impact & zero-regression guarantees

  • Target use-case: Dual-GPU CUDA --peer-device with P2P direct scatter (STRATA_P2P_DECODE_SCATTER=1), eliminating the CPU round-trip between peer scatter completion and primary GPU wakeup on GPU-resident layers.
  • Zero regression on all other profiles (single-GPU, no-P2P PHB, staged D2H, --layer-split, HIP, Metal, SYCL, CPU):
    • Only triggers when all of the following hold: direct == true (STRATA_P2P_DECODE_SCATTER=1 + p2p() + direct_mode()), !any_cpu, d.remote_count == 0, d_target_flag_ != nullptr, and STRATA_PEER_DIRECT_SIGNAL is not 0.
    • When !direct (staged D2H or non-P2P PHB), flag_target stays nullptr so PeerExperts::finish always executes its host memcpy from h_out_ into out normally.
    • Can be disabled at runtime with STRATA_PEER_DIRECT_SIGNAL=0.

Approach

  • engine/include/strata/core/verify.hpp & engine/src/program/generate.cpp: Exposed Verifier::h_flag() and bound it via peer.set_target_flag(ver.h_flag()).
  • engine/include/strata/core/expert_source.hpp & engine/src/core/expert_source.cpp: Added want_flag / peer_signals_flag on GpuPlanSink, checked !any_cpu before peer.launch, and skipped host *flag = want in verify.cpp when sink_.peer_signals_flag is set.
  • engine/include/strata/kernels/elementwise.hpp & engine/src/kernels/cuda/elementwise.cu: Extended scatter_rows_and_signal_f32 to optionally write *flag_ptr = flag_val after __threadfence_system() on the final block.
  • engine/include/strata/core/peer_experts.hpp & engine/src/core/peer_experts.cpp: Added set_target_flag and signaled_target_flag(), guarded strictly on direct && direct_signal_env && want_flag > 0.

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, STRATA_P2P_DECODE_SCATTER=1):

@VecSzn

VecSzn commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

It hasn't been merged yet, but STRATA_PEER_DIRECT is on by default, so the flag handoff runs on the default peer path, not just with STRATA_P2P_DECODE_SCATTER=1. With it, the greedy answers on two cards will differ from main's and can change from run to run, while with STRATA_PEER_DIRECT_SIGNAL=0 they will match main so it looks like a race. STRATA_PEER_DIRECT_SIGNAL is also missing from the settings register so the register check will fail. Could you make it opt-in, find the race and register the variable?

@cobra91
cobra91 force-pushed the pr-p2p-flag-handoff branch from b56d415 to aed5648 Compare November 23, 2026 20:31
@cobra91

cobra91 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching all three — fixed and re-verified on pr-p2p-flag-handoff (rebased onto main):

  1. Opt-in (STRATA_PEER_DIRECT_SIGNAL=1, default 0):
    peer_experts.cpp now requires STRATA_PEER_DIRECT_SIGNAL to be explicitly set non-zero (e != nullptr && std::atoi(e) != 0), so default --peer-device runs (with or without STRATA_P2P_DECODE_SCATTER=1) never take the direct flag handoff path.

  2. Root cause of the greedy race (fixed):
    Two races existed when peer_signals_flag was true:

    • On step k == 0 with do_ple / fl_ple_, verify.cpp gathers h_ple_ after pool(...) returns and before raising *flag = 1. Setting sink_.want_flag = want on step 0 let the peer GPU raise *flag = 1 before h_ple_ was written. Fixed by setting sink_.want_flag = (k == 0 && do_ple) ? 0u : want (and (fl_k_ == 0 && fl_ple_) ? 0u : want in service()).
    • In expert_pool_dispatch_multi (expert_source.cpp), d.peer->launch ran before host row zeroing for !gpu_zeroes_hits or kind[i] == -2 (tail-skip) rows. Fixed by checking gpu_zeroes_hits && !any_tail before enabling can_peer_signal.
  3. Settings register (terminal/data/settings-register.json):
    Registered STRATA_PEER_DIRECT_SIGNAL ("status": "opt-in"). python -m unittest tools/test_settings_register.py passes (Ran 8 tests in 0.602s - OK).

  4. Greedy parity check (Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS, 64 tokens):
    Verified token-for-token identical greedy output across STRATA_PEER_DIRECT_SIGNAL=0 and two consecutive STRATA_PEER_DIRECT_SIGNAL=1 runs (18 17 7024 ... 288 390 1741).

@cobra91
cobra91 force-pushed the pr-p2p-flag-handoff branch from aed5648 to cd0e7ea Compare November 23, 2026 20:34
cobra91 added a commit to cobra91/PolyStrata that referenced this pull request Nov 24, 2026
…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
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