Problem Setup
- Take a cudagraph with a conditional node (WHILE/IF)
- Setup CUPT tracing before
cudaGraphInstantiate. - Then
cudaGraphLaunchdoesn’t return until every other stream on the device is idle.- The cudagraph’s first kernel also doesn’t start until the above condition.
- With no CUPTI or with CUPTI 13.0.2, the same launch returns in under 0.07 ms and runs alongside the other streams.
This hits profilers (e.g. pytorch profiler) that set up CUPTI early. For example, in a PyTorch job, a cudagraph launch blocked the CPU for 250–300 ms while another stream was busy. The CPU couldn’t launch anything during this time. This shows up in the pytorch profiler as serialization which doesn’t happen when the pytorch profiler is off.
Repro
The repro uses the explicit graph API, with no stream capture:
// repro.cu
// cudaGraphLaunch of a graph with a conditional node waits for the whole GPU
// when CUPTI tracing was set up before cudaGraphInstantiate.
//
// Stream `busy` runs a 300 ms spin kernel. 20 ms later, the graph is launched
// on stream `work`. cudaGraphLaunch should return in microseconds, while the
// spin kernel is still running.
//
// Graph: reset(counter = 10) -> WHILE(counter != 0) { body: counter-- ->
// setCondition }.
//
// ./repro none # no CUPTI
// ./repro after # CUPTI on after cudaGraphInstantiate
// ./repro before # CUPTI on before cudaGraphInstantiate
// ./repro register # callbacks registered before, kind enabled after
// ./repro before-hw # like `before`, plus cuptiActivityEnableHWTrace(1)
#include <cuda_runtime.h>
#include <cupti.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <thread>
#define CHECK(call) \
do { \
auto status = (call); \
if (status != 0) { \
std::fprintf(stderr, "%s failed: %d\n", #call, int(status)); \
return 1; \
} \
} while (0)
constexpr int kIterations = 10;
constexpr int kTrials = 3;
constexpr long long kBusyNs = 300'000'000;
__device__ long long globalTimerNs() {
long long t;
asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));
return t;
}
__global__ void spin(long long ns) {
long long start = globalTimerNs();
while (globalTimerNs() - start < ns) {
}
}
__global__ void reset(int *counter) { *counter = kIterations; }
__global__ void body(int *counter) { *counter -= 1; }
__global__ void setCondition(cudaGraphConditionalHandle handle,
const int *counter) {
cudaGraphSetConditional(handle, *counter != 0);
}
static void CUPTIAPI requestBuffer(uint8_t **buffer, size_t *size,
size_t *maxRecords) {
*size = 1024 * 1024;
*buffer = static_cast<uint8_t *>(std::malloc(*size));
*maxRecords = 0;
}
static void CUPTIAPI completeBuffer(CUcontext, uint32_t, uint8_t *buffer,
size_t, size_t) {
std::free(buffer); // the stall does not depend on reading the records
}
static int addKernelNode(cudaGraphNode_t *node, cudaGraph_t graph,
const cudaGraphNode_t *dependency, void *func,
void **args) {
cudaGraphNodeParams params = {};
params.type = cudaGraphNodeTypeKernel;
params.kernel.func = func;
params.kernel.gridDim = dim3(1);
params.kernel.blockDim = dim3(1);
params.kernel.kernelParams = args;
CHECK(cudaGraphAddNode(node, graph, dependency, nullptr, dependency ? 1 : 0,
¶ms));
return 0;
}
int main(int argc, char **argv) {
std::string mode = argc == 2 ? argv[1] : "";
if (mode != "none" && mode != "after" && mode != "before" &&
mode != "register" && mode != "before-hw") {
std::fprintf(stderr, "usage: %s none|after|before|register|before-hw\n",
argv[0]);
return 2;
}
uint32_t cuptiVersion = 0;
cuptiGetVersion(&cuptiVersion);
std::printf("mode=%s cupti_api=%u\n", mode.c_str(), cuptiVersion);
int *counter = nullptr;
cudaStream_t busy, work;
CHECK(cudaMalloc(&counter, sizeof(int)));
CHECK(cudaStreamCreateWithFlags(&busy, cudaStreamNonBlocking));
CHECK(cudaStreamCreateWithFlags(&work, cudaStreamNonBlocking));
void *counterArgs[] = {&counter};
cudaGraph_t graph;
CHECK(cudaGraphCreate(&graph, 0));
cudaGraphNode_t resetNode;
CHECK(addKernelNode(&resetNode, graph, nullptr, (void *)reset, counterArgs));
// Default launch value 1: the body runs once before the condition is read.
cudaGraphConditionalHandle handle;
CHECK(cudaGraphConditionalHandleCreate(&handle, graph, 1,
cudaGraphCondAssignDefault));
cudaGraphNodeParams whileParams = {};
whileParams.type = cudaGraphNodeTypeConditional;
whileParams.conditional.handle = handle;
whileParams.conditional.type = cudaGraphCondTypeWhile;
whileParams.conditional.size = 1;
cudaGraphNode_t whileNode;
CHECK(cudaGraphAddNode(&whileNode, graph, &resetNode, nullptr, 1,
&whileParams));
cudaGraph_t loopBody = whileParams.conditional.phGraph_out[0];
cudaGraphNode_t bodyNode, conditionNode;
CHECK(addKernelNode(&bodyNode, loopBody, nullptr, (void *)body, counterArgs));
void *conditionArgs[] = {&handle, &counter};
CHECK(addKernelNode(&conditionNode, loopBody, &bodyNode,
(void *)setCondition, conditionArgs));
if (mode == "before" || mode == "before-hw" || mode == "register")
CHECK(cuptiActivityRegisterCallbacks(requestBuffer, completeBuffer));
if (mode == "before-hw")
CHECK(cuptiActivityEnableHWTrace(1));
if (mode == "before" || mode == "before-hw")
CHECK(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
cudaGraphExec_t executable;
CHECK(cudaGraphInstantiate(&executable, graph, 0));
if (mode == "after")
CHECK(cuptiActivityRegisterCallbacks(requestBuffer, completeBuffer));
if (mode == "after" || mode == "register")
CHECK(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
CHECK(cudaGraphLaunch(executable, work)); // first launch uploads the graph
CHECK(cudaDeviceSynchronize());
for (int i = 0; i < kTrials; ++i) {
spin<<<1, 1, 0, busy>>>(kBusyNs);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
auto start = std::chrono::steady_clock::now();
CHECK(cudaGraphLaunch(executable, work));
double ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - start)
.count();
// Expected: cudaErrorNotReady, because the spin kernel has ~280 ms left.
bool busyFinished = cudaStreamQuery(busy) == cudaSuccess;
CHECK(cudaDeviceSynchronize());
std::printf(" cudaGraphLaunch %8.3f ms, busy stream finished first: %s\n",
ms, busyFinished ? "yes (waited)" : "no");
}
if (mode != "none") {
CHECK(cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED));
CHECK(cuptiActivityDisable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
}
CHECK(cudaGraphExecDestroy(executable));
CHECK(cudaGraphDestroy(graph));
CHECK(cudaStreamDestroy(work));
CHECK(cudaStreamDestroy(busy));
CHECK(cudaFree(counter));
return 0;
}
What the modes are: Each mode is a different order of the same three CUPTI calls around where the cudagraph gets created (cudaGraphInstantiate).
- build graph: cudaGraphInstantiate
- A: plug CUPTI in; cuptiActivityRegisterCallbacks
- B: start recording kernels; cuptiActivityEnable
- H: use the hardware clock for those records; cuptiActivityEnableHWTrace
build graph
│
none ───────┼─────────────── never plugged in → fine
after ───────┼── A B ──────── plugged in late → fine
before ── A B ┼─────────────── plugged in early → STALL
register ── A ──┼── B ────────── plugged early, record later → STALL
before-hw ─ A H B┼─────────────── plugged early, hardware clock → fine
Expected: in every mode, cudaGraphLaunch returns in well under 1 ms, while the spin kernel still runs.
Actual Result (CUPTI 13.4.2):
// Unexpected behavior
$ ./repro before
mode=before cupti_api=130401
cudaGraphLaunch 280.001 ms, busy stream finished first: yes (waited)
cudaGraphLaunch 279.984 ms, busy stream finished first: yes (waited)
cudaGraphLaunch 279.974 ms, busy stream finished first: yes (waited)
// Expected behavior
$ ./repro after
mode=after cupti_api=130401
cudaGraphLaunch 0.017 ms, busy stream finished first: no
cudaGraphLaunch 0.063 ms, busy stream finished first: no
cudaGraphLaunch 0.037 ms, busy stream finished first: no
Actual Result (Table form):
The table below shows that the stall depends on two things:
A. CUPTI being hooked in before the graph is built (i.e. modes before & register)
B. the CUPTI version (i.e. 13.2.1 & 13.4.2)
| Mode | CUPTI 13.0.2 | CUPTI 13.2.1 | CUPTI 13.4.2 |
|---|---|---|---|
none |
0.010–0.012 | 0.008–0.024 | 0.008–0.014 |
after |
0.020–0.032 | 0.014–0.054 | 0.017–0.063 |
before |
0.013–0.025 | 279.971–279.996 | 279.974–280.001 |
register |
0.013–0.020 | 279.964–279.992 | 279.971–279.993 |
before-hw |
0.018–0.042 | 0.018–0.020 | 0.018–0.054 |
The exception to (A) is before-hw which uses hardware timestamps instead of CUPTI’s default software timestamps.
**
Environment**
- GPU: GB300 (SM 10.3)
- Driver: 580.126.20 (CUDA 13.0)
- CUDA toolkit: 13.0.2
- CUPTI: 13.2.1 and 13.4.2 stall. 13.0.2 does not, except once (see below). The same binary runs with each CUPTI first on
LD_LIBRARY_PATH. - OS: Linux aarch64