Repository navigation
feat(dflash): P2P opt-in, host-staged GPU copy, sharded target daemon - #148
Conversation
There was a problem hiding this comment.
Pull request overview
Adds safer multi-GPU DFlash execution by making CUDA P2P copies opt-in (--peer-access) with a host-staged fallback, and introduces a sharded test_dflash daemon path for --target-gpus with corresponding script/build updates.
Changes:
- Implement opt-in cross-GPU peer copies with fallback to host-staged pinned-memory transfers.
- Add multi-GPU target-layer-split daemon/request path with token streaming via
--stream-fd. - Update scripts and CMake to plumb new flags and ensure CUDA runtime linking where needed; adjust LFS attributes for
assets/banner.png.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
dflash/test/test_dflash.cpp |
Adds --peer-access gating, sharded target daemon/request path, token streaming, and safer cross-GPU feature handling. |
dflash/src/internal.h |
Declares the host-staged cross-device CUDA copy helper under CUDA builds. |
dflash/src/cuda_cross_device_copy.cpp |
Implements pinned host-staged device↔device copies (no P2P required). |
dflash/scripts/server.py |
Plumbs new GPU/sharding flags into daemon spawn; disables snapshot-based caches when sharded. |
dflash/scripts/bench_he.py |
Forwards --peer-access to test_dflash. |
dflash/CMakeLists.txt |
Adds new source file; ensures tests link CUDA::cudart when including CUDA runtime headers. |
.gitattributes |
Excludes assets/banner.png from Git LFS tracking. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| cudaError_t err = cudaSetDevice(src_dev); | ||
| if (err != cudaSuccess) { | ||
| return false; | ||
| } | ||
| err = cudaMemcpyAsync(pin, src, nbytes, cudaMemcpyDeviceToHost, stream); | ||
| if (err != cudaSuccess) { | ||
| return false; | ||
| } | ||
| err = cudaSetDevice(dst_dev); | ||
| if (err != cudaSuccess) { | ||
| return false; | ||
| } | ||
| err = cudaMemcpyAsync(dst, pin, nbytes, cudaMemcpyHostToDevice, stream); | ||
| if (err != cudaSuccess) { |
| void * pin = nullptr; | ||
| { | ||
| std::lock_guard<std::mutex> lock(g_pin_mu); | ||
| if (g_pin_cap < nbytes) { | ||
| if (g_pin) { | ||
| cudaFreeHost(g_pin); | ||
| g_pin = nullptr; | ||
| g_pin_cap = 0; | ||
| } | ||
| cudaError_t err = cudaMallocHost(&g_pin, nbytes); | ||
| if (err != cudaSuccess) { | ||
| return false; | ||
| } | ||
| g_pin_cap = nbytes; | ||
| } | ||
| pin = g_pin; | ||
| } |
| for (size_t i = 0; i < target_gpus.size(); i++) { | ||
| for (size_t j = i + 1; j < target_gpus.size(); j++) { | ||
| (void)enable_peer_access_pair(target_gpus[i], target_gpus[j]); |
| int max_verify_tokens) { | ||
| int max_verify_tokens, | ||
| bool peer_access) { | ||
| g_peer_access_opt_in = peer_access; |
| static void stream_emit_fd(int stream_fd, int32_t tok) { | ||
| if (stream_fd < 0) return; | ||
| #if defined(_WIN32) | ||
| DWORD written = 0; | ||
| const int32_t v = tok; | ||
| WriteFile((HANDLE)(intptr_t)stream_fd, &v, sizeof(v), &written, nullptr); | ||
| #else | ||
| const int32_t v = tok; | ||
| const ssize_t n = ::write(stream_fd, &v, sizeof(v)); | ||
| if (n < 0) { | ||
| // Best-effort stream path; daemon loop handles downstream EOF/termination. | ||
| } | ||
| #endif | ||
| } |
| #include <cuda_runtime.h> | ||
| // Host-staged copy between CUDA devices (no peer access required). | ||
| // When stream is nullptr, uses the per-device default stream and synchronizes. | ||
| bool dflash_cuda_copy_between_devices(int src_dev, const void * src, | ||
| int dst_dev, void * dst, size_t nbytes, | ||
| cudaStream_t stream); |
There was a problem hiding this comment.
3 issues found across 7 files
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="dflash/src/cuda_cross_device_copy.cpp">
<violation number="1" location="dflash/src/cuda_cross_device_copy.cpp:53">
P1: The global pinned staging buffer `g_pin` is shared across all callers, but the mutex is only held during the (re)allocation block. After the lock is released on this line, concurrent calls will use the same buffer for their D2H and H2D copies, silently corrupting each other's data. Either hold `g_pin_mu` for the entire staged-copy operation, or use a `thread_local` pinned buffer to avoid contention.</violation>
<violation number="2" location="dflash/src/cuda_cross_device_copy.cpp:60">
P0: Cross-device staged copy lacks required ordering between source D2H and destination H2D operations because CUDA streams are device-specific. Using the same `stream` handle after `cudaSetDevice` places the two memcpy operations in different device streams with no ordering guarantee. The H2D read from `pin` can race ahead of the D2H write, causing data corruption.</violation>
</file>
<file name="dflash/test/test_dflash.cpp">
<violation number="1" location="dflash/test/test_dflash.cpp:1779">
P2: Daemon-mode request failures can return before writing the `-1` stream terminator, so clients may hang waiting for request completion.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| const char * out_path, | ||
| int stream_fd) { | ||
| if (shards.empty() || prompt.empty()) return false; | ||
| if ((int)prompt.size() + n_gen + 1 > max_ctx) { |
There was a problem hiding this comment.
P2: Daemon-mode request failures can return before writing the -1 stream terminator, so clients may hang waiting for request completion.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At dflash/test/test_dflash.cpp, line 1779:
<comment>Daemon-mode request failures can return before writing the `-1` stream terminator, so clients may hang waiting for request completion.</comment>
<file context>
@@ -1693,6 +1763,232 @@ static bool run_target_layer_split_dflash_decode(
+ const char * out_path,
+ int stream_fd) {
+ if (shards.empty() || prompt.empty()) return false;
+ if ((int)prompt.size() + n_gen + 1 > max_ctx) {
+ std::fprintf(stderr, "target-split prompt (%zu) + gen (%d) exceeds max_ctx (%d)\n",
+ prompt.size(), n_gen, max_ctx);
</file context>
|
Thanks for the great contribution @davidmroth ! Can you please rebase and review P0,P1 from cubic. Happy to merge into main when ready |
There was a problem hiding this comment.
@davide221 P0 and P1 have been addressed and tested. Kindly let me know if you need any other issue addressed. Appreciate your review and guidiance.
[bench] target = /model/qwen3.6/Qwen3.6-27B-Q4_K_M.gguf
[bench] draft = /model/qwen3.6/draft/model.safetensors
[bench] ar bin = /usr/local/lucebox/build/test_generate
[bench] df bin = /usr/local/lucebox/build/test_dflash
[bench] tokenizer = Qwen/Qwen3.5-27B
[...]
[bench] === SUMMARY ===
Task AR DFlash AL Speedup Score
HumanEval 34.29 110.70 8.14 3.23x
GSM8K 34.35 79.58 5.82 2.32x
Math500 31.47 96.51 7.53 3.07x 3/10
[bench] wrote /tmp/dflash_bench/bench_llm_results.json
Resolve conflict in dflash/scripts/server.py: union of extra_daemon_args (PR Luce-Org#148) + lazy_draft/verbose_daemon (main). Co-Authored-By: WOZCODE <[email protected]>
|
lgtm now, thanks for your first contribution |
Resolve conflict in dflash/scripts/server.py: union of extra_daemon_args (PR Luce-Org#148) + lazy_draft/verbose_daemon (main). Co-Authored-By: WOZCODE <[email protected]>
…sh-safe-p2p-port feat(dflash): P2P opt-in, host-staged GPU copy, sharded target daemon
feature/dual-gpu-dflash-safe-p2p-port
Summary
Improves cross-GPU DFlash and multi-GPU layer splitting: CUDA peer memcpy is opt-in via
--peer-accesswith pinned host-staged fallback when disabled or unavailable. Adds shardedtest_dflashdaemon for--target-gpus. Updatesserver.py/bench_he.pywith GPU/sharding flags. Excludesassets/banner.pngfrom Git LFS. LinksCUDA::cudartin CMake for tests usinginternal.h.DFlash runtime (
test_dflash.cpp,cuda_cross_device_copy.cpp,internal.h,CMakeLists.txt)dflash_cuda_copy_between_devices: host-staged device↔device copies when P2P off; pulled fromcopy_peer_asyncif--peer-accessunset or peers unavailable.--peer-access: gatescudaMemcpyPeerAsync; logs fallback once.--target-gpus+--daemon: newrun_target_layer_split_daemon(stdin protocol, token streaming onstream_fd, no SNAPSHOT/RESTORE — stubs). Sharedrun_target_layer_split_requestfor prefill/decode/dflash.target_hidden_cat(mirror vs same-GPU BF16→F32 vs CPU staging viaggml_backend_tensor_get/set).src/cuda_cross_device_copy.cppto dflash27b.CUDA::cudartfor tests includinginternal.h.Scripts
bench_he.py: forwards--peer-accesstotest_dflash.server.py: defaults binary dir tobin/; adds--target-gpu/--draft-gpu/--target-gpus/--target-layer-split/--draft-feature-mirror/--peer-access; buildsextra_daemon_args; disables prefix/prefill cache on sharded GPUs (snapshot unsupported).Example working CMD
Repo hygiene
.gitattributes: overridesassets/banner.pngto skip LFS.Benchmark Results (Qwen3.6-27B)
Note: Testing does not include splitting.
Config
SUMMARY