Skip to content

feat(dflash): P2P opt-in, host-staged GPU copy, sharded target daemon - #148

Merged
davide221 merged 3 commits into
Luce-Org:mainfrom
davidmroth:feature/dual-gpu-dflash-safe-p2p-port
May 11, 2026
Merged

davide221 merged 3 commits into
Luce-Org:mainfrom
davidmroth:feature/dual-gpu-dflash-safe-p2p-port

Conversation

@davidmroth

@davidmroth davidmroth commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

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-access with pinned host-staged fallback when disabled or unavailable. Adds sharded test_dflash daemon for --target-gpus. Updates server.py/bench_he.py with GPU/sharding flags. Excludes assets/banner.png from Git LFS. Links CUDA::cudart in CMake for tests using internal.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 from copy_peer_async if --peer-access unset or peers unavailable.
  • --peer-access: gates cudaMemcpyPeerAsync; logs fallback once.
  • Multi-GPU --target-gpus + --daemon: new run_target_layer_split_daemon (stdin protocol, token streaming on stream_fd, no SNAPSHOT/RESTORE — stubs). Shared run_target_layer_split_request for prefill/decode/dflash.
  • Split-GPU draft: safer target_hidden_cat (mirror vs same-GPU BF16→F32 vs CPU staging via ggml_backend_tensor_get/set).
  • Library: added src/cuda_cross_device_copy.cpp to dflash27b.
  • Build: CUDA::cudart for tests including internal.h.

Scripts

  • bench_he.py: forwards --peer-access to test_dflash.
  • server.py: defaults binary dir to bin/; adds --target-gpu/--draft-gpu/--target-gpus/--target-layer-split/--draft-feature-mirror/--peer-access; builds extra_daemon_args; disables prefix/prefill cache on sharded GPUs (snapshot unsupported).

Example working CMD

python /usr/local/lucebox/scripts/server.py --target /model/qwen3.6/Qwen3.6-27B-Q6_K.gguf --draft /model/qwen3.6/draft/model.safetensors --tokenizer Qwen/Qwen3.5-27B --port "8000" --max-ctx "65536" --fa-window "2048" --daemon --prefill-compression auto --prefill-drafter /model/qwen3.6/draft/model.safetensors --prefill-threshold "8000" --target-gpus=0,1 --draft-gpu=1 --draft-feature-mirror --target-layer-split=0.5,0.5 --cache-type-k q4_0 --cache-type-v q4_0

Repo hygiene

  • .gitattributes: overrides assets/banner.png to skip LFS.

Benchmark Results (Qwen3.6-27B)
Note: Testing does not include splitting.

Config

  • target: /model/qwen3.6/Qwen3.6-27B-Q4_K_M.gguf
  • draft: /model/qwen3.6/draft/model.safetensors
  • AR bin: /usr/local/lucebox/build/test_generate
  • DF bin: /usr/local/lucebox/build/test_dflash
  • tokenizer: Qwen/Qwen3.5-27B
  • budget: 22

SUMMARY

Task AR DFlash AL Speedup Score
HumanEval 34.31 110.73 8.14 3.23x
GSM8K 34.37 79.91 5.82 2.32x
Math500 30.51 97.25 7.53 3.19x 3/10

Copilot AI review requested due to automatic review settings May 10, 2026 17:12

Copilot AI 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.

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.

Comment on lines +56 to +69
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) {
Comment thread dflash/src/cuda_cross_device_copy.cpp Outdated
Comment on lines +38 to +54
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;
}
Comment on lines +1868 to +1870
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;
Comment on lines +119 to +132
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
}
Comment thread dflash/src/internal.h Outdated
Comment on lines +487 to +492
#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);

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

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.

Comment thread dflash/src/cuda_cross_device_copy.cpp Resolved
Comment thread dflash/src/cuda_cross_device_copy.cpp Resolved
const char * out_path,
int stream_fd) {
if (shards.empty() || prompt.empty()) return false;
if ((int)prompt.size() + n_gen + 1 > max_ctx) {

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: 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>

@davide221

Copy link
Copy Markdown
Contributor

Thanks for the great contribution @davidmroth ! Can you please rebase and review P0,P1 from cubic. Happy to merge into main when ready

@davidmroth davidmroth left a comment •

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.

@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]>
@davide221

Copy link
Copy Markdown
Contributor

lgtm now, thanks for your first contribution

@davide221
davide221 merged commit 359a41f into Luce-Org:main May 11, 2026
1 check passed
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
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]>
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
…sh-safe-p2p-port

feat(dflash): P2P opt-in, host-staged GPU copy, sharded target daemon
@davidmroth
davidmroth deleted the feature/dual-gpu-dflash-safe-p2p-port branch July 3, 2026 14:20
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.

3 participants