From a6346cdceaefc50a4893bbf7b300e84dd8ec808e Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 9 Oct 2026 14:29:45 +0200 Subject: [PATCH] conversation snapshot/checkpoint copies: private non-blocking stream instead of the legacy stream (HIP capture conflict) --- src/core/conversation_copy.hpp | 32 ++++++++++++++++++++++++++++++ src/core/conversation_snapshot.cpp | 3 ++- src/core/conversation_state.cpp | 4 +++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/core/conversation_copy.hpp diff --git a/src/core/conversation_copy.hpp b/src/core/conversation_copy.hpp new file mode 100644 index 00000000..e15b1197 --- /dev/null +++ b/src/core/conversation_copy.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include +#include + +namespace strata::core::conversation_detail { + +/// Conversation snapshot / checkpoint copies go through a private non-blocking stream of the current device +/// instead of the legacy (null) stream. A plain cudaMemcpy is ordered against every blocking stream of the +/// device, and while another thread captures a graph on one (a drafter or a stage worker), HIP refuses it with +/// "operation would make the legacy stream depend on a capturing blocking stream". The caller's contract is +/// unchanged: the device was synchronized before the copy (the copy itself is complete on return). +inline cudaError_t copy_nonblocking(void* dst, const void* src, size_t bytes) { + int dev = 0; + cudaError_t e = cudaGetDevice(&dev); + if (e != cudaSuccess) return e; + thread_local std::vector streams; + if (dev < 0) dev = 0; + if ((size_t) dev >= streams.size()) streams.resize((size_t) dev + 1, nullptr); // never destroyed: process lifetime + if (!streams[(size_t) dev]) { + e = cudaStreamCreateWithFlags(&streams[(size_t) dev], cudaStreamNonBlocking); + if (e != cudaSuccess) { streams[(size_t) dev] = nullptr; return e; } + } + cudaStream_t s = streams[(size_t) dev]; + e = cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDefault, s); + if (e != cudaSuccess) return e; + return cudaStreamSynchronize(s); +} + +} // namespace strata::core::conversation_detail diff --git a/src/core/conversation_snapshot.cpp b/src/core/conversation_snapshot.cpp index 0ae73629..1468dc65 100644 --- a/src/core/conversation_snapshot.cpp +++ b/src/core/conversation_snapshot.cpp @@ -1,6 +1,7 @@ #include "strata/core/conversation_snapshot.hpp" #include "strata/kernels/kv_q4.hpp" #include "conversation_checked.hpp" +#include "conversation_copy.hpp" #include #include @@ -113,7 +114,7 @@ bool transfer(void* dst, const void* src, size_t n, std::string& error) { if (!n) return true; if (!src || !dst) { error = "conversation snapshot: missing state buffer"; return false; } // Default handles both device allocations and device-mapped host pool aliases. - const cudaError_t e = cudaMemcpy(dst, src, n, cudaMemcpyDefault); + const cudaError_t e = conversation_detail::copy_nonblocking(dst, src, n); if (e == cudaSuccess) return true; error = std::string("conversation snapshot copy: ") + cudaGetErrorString(e); return false; diff --git a/src/core/conversation_state.cpp b/src/core/conversation_state.cpp index ae40f3ac..9c2342f8 100644 --- a/src/core/conversation_state.cpp +++ b/src/core/conversation_state.cpp @@ -1,5 +1,6 @@ #include "strata/core/conversation_snapshot.hpp" #include "conversation_checked.hpp" +#include "conversation_copy.hpp" #include #include @@ -32,7 +33,7 @@ bool sync(std::string& error) { bool copy(void* dst, const void* src, size_t bytes, std::string& error) { if (!bytes) return true; if (!dst || !src) return fail(error, "missing running-state buffer"); - const auto status = cudaMemcpy(dst, src, bytes, cudaMemcpyDefault); + const auto status = conversation_detail::copy_nonblocking(dst, src, bytes); if (status == cudaSuccess) return true; error = std::string("conversation snapshot running-state copy: ") + cudaGetErrorString(status); return false; @@ -174,6 +175,7 @@ bool conversation_checkpoint_restore(const ConversationCheckpoint& c, SessionSta if (!conversation_checkpoint_validate(c, ss, g, error)) return false; ConversationStateSizes z; if (!conversation_session_sizes(g, ss, z, error)) return false; + if (!sync(error)) return false; // the copies below no longer ride the legacy stream's implicit ordering if (!copy(ss.gdn_state, c.gdn.data(), c.gdn.size(), error) || !copy(ss.ple_hist, c.ple.data(), c.ple.size(), error)) return false; for (size_t j = 0; j < owned_qsa(ss); ++j) {