Untraced launch-latency microbench for a verify-sized CUDA graph (plan 6b step 1) and its holder stub
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
97d548e2b8
commit
adc8af982c
+16
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
# HOLDER JOB (needs only the GPU lock, not the model). Plan 6b step 1: is "cuGraphLaunch of the verify graph -> first kernel = 1.3 ms with the GPU idle"
|
||||
# (CUPTI timeline, ledger "Between graphs") the tracer's? tools/launch-latency/launch_latency.cu launches a verify-shaped graph (250/1000/1850/2050 kernel
|
||||
# nodes, side streams) on an idle stream WITHOUT a tracer and reports the launch call, launch call -> first kernel and the graph span. Run twice (A/A).
|
||||
# Result: /data/leleka-e2e/spd-gap/runs/decode-launch-latency-microbench-verify-sized-graph-untraced/{a,b}.txt
|
||||
set -uo pipefail
|
||||
OUT=/data/leleka-e2e/spd-gap/runs/decode-launch-latency-microbench-verify-sized-graph-untraced
|
||||
LOCK=/data/leleka-e2e/gpu.lock
|
||||
mkdir -p "$OUT"
|
||||
got=0; for _ in $(seq 1 4320); do if mkdir "$LOCK" 2>/dev/null; then echo "launch-latency $(date -u +%FT%TZ) $OUT" >"$LOCK/owner"; got=1; break; fi; sleep 5; done
|
||||
[ $got = 1 ] || { echo "GPU lock held by: $(cat $LOCK/owner 2>/dev/null)"; exit 3; }
|
||||
trap 'rm -f "$LOCK/owner"; rmdir "$LOCK" 2>/dev/null' EXIT
|
||||
[ -z "$(nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)" ] || { echo "something uses the GPU"; exit 3; }
|
||||
nvidia-smi --query-gpu=name,driver_version,clocks.sm,clocks.mem,power.draw --format=csv >"$OUT/gpu.txt"
|
||||
for run in a b; do /data/leleka-e2e/spd-gap/tools/launch-latency/launch_latency 250 1000 1850 2050 >"$OUT/$run.txt" 2>&1; echo "run $run rc=$?"; done
|
||||
cat "$OUT/a.txt"
|
||||
@@ -0,0 +1,150 @@
|
||||
// Untraced latency from the host's graph launch call to the first kernel of a verify-sized CUDA graph (plan 6b, step 1).
|
||||
//
|
||||
// CUPTI shows `cuGraphLaunch` of the verify graph taking 1.3 ms with the GPU idle until the first kernel. CUPTI instruments every
|
||||
// driver call and every node of a graph, so the number may be the tracer's. This program measures the same thing without a tracer:
|
||||
// a graph of N small kernels in the shape of the verify graph (layers that fork into two side streams and join), captured on a
|
||||
// stream and launched on an idle stream. The first kernel and the last kernel of the graph write the GPU's `globaltimer` to
|
||||
// pinned memory; the host timestamps the launch call. Host and GPU clocks are tied by the smallest offset seen over many
|
||||
// single-kernel launches (the offset carries one kernel launch latency, a few microseconds).
|
||||
//
|
||||
// usage: launch_latency [nodes ...] (default 250 1000 1850 2050)
|
||||
// Build: nvcc -O2 -arch=sm_120 -o launch_latency launch_latency.cu
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#define CHECK(call) \
|
||||
do { \
|
||||
cudaError_t status = (call); \
|
||||
if (status != cudaSuccess) { \
|
||||
std::fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, cudaGetErrorString(status)); \
|
||||
std::exit(1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
__device__ __forceinline__ unsigned long long gpu_now() {
|
||||
unsigned long long t;
|
||||
asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));
|
||||
return t;
|
||||
}
|
||||
|
||||
__global__ void write_stamp(volatile unsigned long long* slot) { *slot = gpu_now(); }
|
||||
|
||||
__global__ void spin_for(unsigned long long nanoseconds) {
|
||||
unsigned long long start = gpu_now();
|
||||
while (gpu_now() - start < nanoseconds) {}
|
||||
}
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
static long long host_ns() { return std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now().time_since_epoch()).count(); }
|
||||
|
||||
struct Stamps {
|
||||
volatile unsigned long long* first;
|
||||
volatile unsigned long long* last;
|
||||
};
|
||||
|
||||
// The graph: stamp, then `nodes - 2` spin kernels in layers of 3 kernels on the main stream and 2 on each of two side streams
|
||||
// (forked and joined with events), then a closing stamp.
|
||||
static cudaGraph_t capture_graph(cudaStream_t main, cudaStream_t side[2], cudaEvent_t fork, cudaEvent_t join[2], int nodes, Stamps stamps) {
|
||||
cudaGraph_t graph;
|
||||
CHECK(cudaStreamBeginCapture(main, cudaStreamCaptureModeGlobal));
|
||||
write_stamp<<<1, 1, 0, main>>>(stamps.first);
|
||||
int remaining = nodes - 2;
|
||||
while (remaining > 0) {
|
||||
CHECK(cudaEventRecord(fork, main));
|
||||
for (int i = 0; i < 2; ++i) CHECK(cudaStreamWaitEvent(side[i], fork, 0));
|
||||
for (int i = 0; i < 2 && remaining > 0; ++i, --remaining) spin_for<<<1, 32, 0, side[i]>>>(3000);
|
||||
for (int i = 0; i < 3 && remaining > 0; ++i, --remaining) spin_for<<<1, 32, 0, main>>>(3000);
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
CHECK(cudaEventRecord(join[i], side[i]));
|
||||
CHECK(cudaStreamWaitEvent(main, join[i], 0));
|
||||
}
|
||||
}
|
||||
write_stamp<<<1, 1, 0, main>>>(stamps.last);
|
||||
CHECK(cudaStreamEndCapture(main, &graph));
|
||||
return graph;
|
||||
}
|
||||
|
||||
// offset = GPU clock - host clock, smallest over many single-kernel launches on an idle stream.
|
||||
static long long clock_offset(cudaStream_t stream, volatile unsigned long long* slot) {
|
||||
long long best = 1LL << 62;
|
||||
for (int i = 0; i < 400; ++i) {
|
||||
long long before = host_ns();
|
||||
write_stamp<<<1, 1, 0, stream>>>(slot);
|
||||
CHECK(cudaStreamSynchronize(stream));
|
||||
best = std::min(best, static_cast<long long>(*slot) - before);
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
static double percentile(std::vector<double> values, double part) {
|
||||
std::sort(values.begin(), values.end());
|
||||
return values[std::min(values.size() - 1, static_cast<size_t>(part * values.size()))];
|
||||
}
|
||||
|
||||
static void report(const char* label, const std::vector<double>& values) {
|
||||
std::printf(" %-34s p10 %8.1f p50 %8.1f p90 %8.1f max %8.1f us\n", label, percentile(values, 0.1), percentile(values, 0.5),
|
||||
percentile(values, 0.9), percentile(values, 1.0));
|
||||
}
|
||||
|
||||
static void measure(cudaGraphExec_t exec, cudaStream_t stream, Stamps stamps, long long offset, bool upload, int idle_us) {
|
||||
std::vector<double> call, first, span;
|
||||
for (int i = 0; i < 300; ++i) {
|
||||
if (upload) CHECK(cudaGraphUpload(exec, stream));
|
||||
CHECK(cudaStreamSynchronize(stream));
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(idle_us));
|
||||
*stamps.first = 0;
|
||||
long long before = host_ns();
|
||||
CHECK(cudaGraphLaunch(exec, stream));
|
||||
long long after = host_ns();
|
||||
CHECK(cudaStreamSynchronize(stream));
|
||||
call.push_back((after - before) / 1e3);
|
||||
first.push_back((static_cast<long long>(*stamps.first) - offset - before) / 1e3);
|
||||
span.push_back((static_cast<long long>(*stamps.last) - static_cast<long long>(*stamps.first)) / 1e3);
|
||||
}
|
||||
report("cudaGraphLaunch call (host)", call);
|
||||
report("launch call -> first kernel", first);
|
||||
report("first kernel -> last kernel (graph)", span);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::vector<int> sizes;
|
||||
for (int i = 1; i < argc; ++i) sizes.push_back(std::atoi(argv[i]));
|
||||
if (sizes.empty()) sizes = {250, 1000, 1850, 2050};
|
||||
cudaStream_t main_stream, side[2];
|
||||
CHECK(cudaStreamCreateWithFlags(&main_stream, cudaStreamNonBlocking));
|
||||
for (auto& s : side) CHECK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
|
||||
cudaEvent_t fork, join[2];
|
||||
CHECK(cudaEventCreateWithFlags(&fork, cudaEventDisableTiming));
|
||||
for (auto& e : join) CHECK(cudaEventCreateWithFlags(&e, cudaEventDisableTiming));
|
||||
unsigned long long* host_slots = nullptr;
|
||||
CHECK(cudaHostAlloc(reinterpret_cast<void**>(&host_slots), 4096, cudaHostAllocMapped));
|
||||
unsigned long long* device_slots = nullptr;
|
||||
CHECK(cudaHostGetDevicePointer(reinterpret_cast<void**>(&device_slots), host_slots, 0));
|
||||
Stamps stamps{device_slots, device_slots + 8};
|
||||
Stamps host_view{host_slots, host_slots + 8};
|
||||
std::printf("GPU globaltimer to host steady_clock offset is taken as the minimum of 400 single-kernel launches\n");
|
||||
long long offset = clock_offset(main_stream, device_slots + 16);
|
||||
offset = std::min(offset, clock_offset(main_stream, device_slots + 16));
|
||||
// the offset kernel wrote through the device pointer; read it through the host view
|
||||
for (int nodes : sizes) {
|
||||
cudaGraph_t graph = capture_graph(main_stream, side, fork, join, nodes, stamps);
|
||||
cudaGraphExec_t exec;
|
||||
CHECK(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0));
|
||||
for (int warm = 0; warm < 5; ++warm) CHECK(cudaGraphLaunch(exec, main_stream));
|
||||
CHECK(cudaStreamSynchronize(main_stream));
|
||||
std::printf("graph of %d nodes, stream idle for 2 ms before the launch, no upload\n", nodes);
|
||||
measure(exec, main_stream, host_view, offset, false, 2000);
|
||||
std::printf("graph of %d nodes, stream idle for 2 ms, cudaGraphUpload before\n", nodes);
|
||||
measure(exec, main_stream, host_view, offset, true, 2000);
|
||||
CHECK(cudaGraphExecDestroy(exec));
|
||||
CHECK(cudaGraphDestroy(graph));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user