Files
leleka/scripts/h2d_copy_engine_probe.cu
Pavlo RatushnyiandClaude Sonnet 5.5 23e7b20dc4 perf(prefill): expert stream issuer thread keeping a bounded copy queue (the Original's D-5)
The host-to-device copy engine is a FIFO: with hundreds of expert copies queued ahead every small
upload or status readback of the compute path waited for the whole queue. A helper thread now keeps
LELEKA_PREFILL_DEPTH (16) entries outstanding on the copy stream (event-query paced) while the
compute thread only waits for an entry's enqueue before ordering its stream after it. The inline
mode stays (LELEKA_PREFILL_ISSUER=inline). Adds the copy-engine contention probe.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-17 17:45:59 +02:00

96 lines
5.7 KiB
Plaintext

// Model-free probe: latency of small synchronous host/device copies and kernels while an "expert stream" keeps the
// host-to-device copy engine busy (claude/prefill-overlap). Build: nvcc -O2 -arch=sm_120 h2d_probe.cu -o h2d_probe
#include <cuda_runtime.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <thread>
#include <atomic>
#include <algorithm>
#define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { printf("CUDA error %s at %d: %s\n", #x, __LINE__, cudaGetErrorString(e_)); exit(1);} } while (0)
using Clock = std::chrono::steady_clock;
static double ms(Clock::time_point a) { return std::chrono::duration<double, std::milli>(Clock::now() - a).count(); }
__global__ void tiny(float* p, int n) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) p[i] += 1.0f; }
int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IOLBF, 0);
int only_depth = argc > 1 ? atoi(argv[1]) : -1;
const size_t ENTRY = 2046000; // one expert (gate+up+down), bytes
const int PIECES = 3, RING = 384, NENT = 4096;
const size_t SMALL = 327680; // the routed slot table
uint8_t* host;
uint8_t* ring; CK(cudaMalloc((void**)&ring, ENTRY * RING));
uint8_t* dev_small; CK(cudaMalloc((void**)&dev_small, 64 << 20));
float* dev_f; CK(cudaMalloc((void**)&dev_f, 1 << 20));
std::vector<uint8_t> pageable(SMALL, 7);
uint8_t* pinned_small; CK(cudaHostAlloc((void**)&pinned_small, SMALL, cudaHostAllocDefault));
cudaStream_t copy, hi, other;
CK(cudaStreamCreateWithFlags(&copy, cudaStreamNonBlocking));
int lo, hip; CK(cudaDeviceGetStreamPriorityRange(&lo, &hip));
CK(cudaStreamCreateWithPriority(&hi, cudaStreamNonBlocking, hip));
CK(cudaStreamCreateWithFlags(&other, cudaStreamNonBlocking));
CK(cudaStreamSynchronize(0));
size_t hostN = 1ull << 30;
CK(cudaHostAlloc((void**)&host, hostN * 4, cudaHostAllocDefault)); // 4 GiB
memset(host, 1, hostN * 4);
const size_t entries_in_host = (hostN * 4) / ENTRY;
auto bg = [&](int depth, int pieces, std::atomic<bool>* stop, std::atomic<long>* bytes) {
std::vector<cudaEvent_t> ev(depth + 1);
for (auto& e : ev) CK(cudaEventCreateWithFlags(&e, cudaEventDisableTiming));
long k = 0;
while (!stop->load()) {
int slot = (int)(k % RING);
size_t src = (size_t)(k % entries_in_host) * ENTRY;
size_t piece = ENTRY / pieces;
for (int p = 0; p < pieces; ++p)
CK(cudaMemcpyAsync(ring + (size_t)slot * ENTRY + p * piece, host + src + p * piece, piece, cudaMemcpyHostToDevice, copy));
CK(cudaEventRecord(ev[k % (depth + 1)], copy));
++k; bytes->fetch_add(ENTRY);
if (k > depth) CK(cudaEventSynchronize(ev[(k - depth - 1) % (depth + 1)])); // keep `depth` entries outstanding
}
CK(cudaStreamSynchronize(copy));
for (auto& e : ev) cudaEventDestroy(e);
};
struct Probe { const char* name; };
auto measure = [&](const char* name, int reps, auto&& f) {
std::vector<double> t;
for (int i = 0; i < reps; ++i) { auto a = Clock::now(); f(); t.push_back(ms(a)); std::this_thread::sleep_for(std::chrono::microseconds(300)); }
std::sort(t.begin(), t.end());
double mean = 0; for (double x : t) mean += x; mean /= t.size();
printf(" %-34s mean %7.3f ms p50 %7.3f p90 %7.3f max %7.3f\n", name, mean, t[t.size() / 2], t[t.size() * 9 / 10], t.back());
};
int depths[] = {0, 2, 8, 32, 128, 384};
int pieces_list[] = {3, 1};
for (int pieces : pieces_list) {
for (int depth : depths) {
if (only_depth >= 0 && depth != only_depth) continue;
printf("expert stream: depth=%d entries outstanding, %d copies per entry (%zu B each)\n", depth, pieces, ENTRY / pieces);
std::atomic<bool> stop{false}; std::atomic<long> bytes{0};
std::thread th;
auto t0 = Clock::now();
if (depth > 0) th = std::thread([&] { bg(depth, pieces, &stop, &bytes); });
std::this_thread::sleep_for(std::chrono::milliseconds(150));
long b0 = bytes.load(); auto tm = Clock::now();
measure("cudaMemcpy H2D 327KB pageable", 60, [&] { CK(cudaMemcpy(dev_small, pageable.data(), SMALL, cudaMemcpyHostToDevice)); });
measure("cudaMemcpy H2D 6MB pageable", 20, [&] { std::vector<uint8_t> big(6 << 20, 1); CK(cudaMemcpy(dev_small, big.data(), big.size(), cudaMemcpyHostToDevice)); });
measure("cudaMemcpyAsync pinned 327KB hi-prio", 60, [&] { CK(cudaMemcpyAsync(dev_small, pinned_small, SMALL, cudaMemcpyHostToDevice, hi)); CK(cudaStreamSynchronize(hi)); });
measure("cudaMemcpyAsync pinned 327KB other", 60, [&] { CK(cudaMemcpyAsync(dev_small, pinned_small, SMALL, cudaMemcpyHostToDevice, other)); CK(cudaStreamSynchronize(other)); });
measure("cudaMemcpy D2H 4KB", 60, [&] { uint8_t h[4096]; CK(cudaMemcpy(h, dev_small, 4096, cudaMemcpyDeviceToHost)); });
measure("cudaMemset 4KB (legacy)", 60, [&] { CK(cudaMemset(dev_small, 0, 4096)); });
measure("cudaMemcpy D2D 100MB (legacy)", 20, [&] { CK(cudaMemcpy(dev_small + (32 << 20), dev_small, 16 << 20, cudaMemcpyDeviceToDevice)); });
measure("tiny kernel + sync (other)", 60, [&] { tiny<<<64, 256, 0, other>>>(dev_f, 16384); CK(cudaStreamSynchronize(other)); });
measure("tiny kernel + sync (legacy)", 60, [&] { tiny<<<64, 256, 0, 0>>>(dev_f, 16384); CK(cudaStreamSynchronize(0)); });
long b1 = bytes.load(); double dt = ms(tm);
stop = true; if (th.joinable()) th.join();
if (depth > 0) printf(" expert DMA while probing: %.1f GB/s\n", (b1 - b0) / (dt * 1e6));
(void)t0;
}
}
return 0;
}