diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7309f558..84f53e75 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -38,6 +38,7 @@ #include "strata/kernels/s2_expert_grouped.hpp" #include "strata/kernels/sampler.hpp" #include "strata/kernels/verify_kernels.hpp" +#include "strata/kernels/elementwise.hpp" #include "strata/kernels/shared_expert.hpp" #include "strata/kernels/native_moe.hpp" #include "strata/kernels/native_gdn.hpp" @@ -151,6 +152,30 @@ bool pin_blobs_on() { return false; #endif } +// #884 (HIP, opt-in STRATA_HIP_ADAPT_KERNEL_COPY=1): an adaptive swap's host-to-device copy as a kernel on the refill stream +// instead of a copy-engine (SDMA) memcpy. 2x RX 6900 XT hung after a long prompt only with the MMQ prompt path, adaptive swaps +// AND SDMA together (any one off avoids it; the barrier packet that never passes is a ROCm-side problem, #884). This keeps +// the swaps off SDMA without turning SDMA off for the prompt read (HSA_ENABLE_SDMA=0 costs 10-24% of it). Needs the source to +// be mapped, 16-byte aligned memory (the registered arena); anything else takes the normal cudaMemcpyAsync. +cudaError_t adapt_copy_h2d(void* dst, const void* src, size_t bytes, cudaStream_t st) { +#if defined(STRATA_USE_HIP) + static const bool kernel = [] { + const char* e = std::getenv("STRATA_HIP_ADAPT_KERNEL_COPY"); + const bool on = e != nullptr && e[0] == '1'; + if (on) std::fprintf(stderr, "strata: STRATA_HIP_ADAPT_KERNEL_COPY=1: adaptive swaps copy with a kernel, not SDMA (#884)\n"); + return on; + }(); + if (kernel && bytes % 16 == 0 && ((uintptr_t) src & 15) == 0 && ((uintptr_t) dst & 15) == 0) { + void* dsrc = nullptr; + if (cudaHostGetDevicePointer(&dsrc, const_cast(src), 0) == cudaSuccess && dsrc != nullptr) { + strata::kernels::copy_from_mapped((float*) dst, (const float*) dsrc, (int64_t) (bytes / 4), (void*) st); + return cudaGetLastError(); + } + (void) cudaGetLastError(); // not registered / not mapped: the ordinary copy + } +#endif + return cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, st); +} int pin_blobs(std::vector> r, std::vector& live) { int failed = 0; // ranges left pageable (a copy from them still works, through the driver's staging) #if (defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906)) && !defined(_WIN32) @@ -6219,9 +6244,9 @@ int main(int argc, char** argv) { GpuStage* gs = stn > 0 ? stages[(size_t) stn - 1].get() : nullptr; const strata::core::OnDevice on(gs ? gs->dev : -1); if (slot < 0 || b == nullptr || - cudaMemcpyAsync(gs ? gs->cache.device_slot(slot) : xcache.device_slot(slot), b, - (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), - cudaMemcpyHostToDevice, gs ? gs->adapt_stream : adapt_stream) != cudaSuccess) { + adapt_copy_h2d(gs ? gs->cache.device_slot(slot) : xcache.device_slot(slot), b, + (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), + gs ? gs->adapt_stream : adapt_stream) != cudaSuccess) { std::fprintf(stderr, "strata serve: adaptive swap copy failed (layer %d, slot %d, pinned %d): %s\n", (int) s.layer, (int) slot, pin_live.empty() ? 0 : 1, cudaGetErrorString(cudaGetLastError())); return false; @@ -9723,8 +9748,8 @@ int main(int argc, char** argv) { const uint8_t* b = srcp->blob(s.layer, s.in); // asynchronous: the copies run while the MTP drafts; the next window waits for them if (slot < 0 || b == nullptr || - cudaMemcpyAsync(xcache.device_slot(slot), b, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), - cudaMemcpyHostToDevice, adapt_stream) != cudaSuccess) { + adapt_copy_h2d(xcache.device_slot(slot), b, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), + adapt_stream) != cudaSuccess) { std::fprintf(stderr, "strata generate: an adaptive refill failed\n"); return false; }