Problem
- I’m tracing a
cudaGraphLaunchwith a conditional WHILE node. - If CUPTI kernel tracing starts after `cudaGraphInstantiate`, each kernel in a WHILE node’s body gets one `CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL` record per graph launch. The record has the timestamps of a late iteration.
- If tracing starts before `cudaGraphInstantiate`, each iteration gets its own record.
This affects profilers that attach after warm-up. For example, the PyTorch profiler would only show 1 of N iterations.
Repro
The repro uses the explicit graph API, with no stream capture:
// repro.cu
// CUPTI drops CUDA graph WHILE-node body executions when tracing starts after
// cudaGraphInstantiate.
//
// Graph: reset(counter = 10) -> WHILE(counter != 0) { body: counter-- ->
// setCondition }. Each launch runs `body` 10 times, so 3 launches should give
// 30 `body` kernel records.
//
// ./repro --before # CUPTI on before cudaGraphInstantiate -> 30 records
// ./repro --after # CUPTI on after cudaGraphInstantiate -> 3 records
#include <cuda.h>
#include <cuda_runtime.h>
#include <cupti.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <utility>
#include <vector>
#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 kLaunches = 3;
__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 std::mutex completedMutex;
static std::vector<std::pair<uint8_t *, size_t>> completedBuffers;
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 validSize) {
std::lock_guard<std::mutex> lock(completedMutex);
completedBuffers.emplace_back(buffer, validSize);
}
static int enableTracing() {
CHECK(cuptiActivityRegisterCallbacks(requestBuffer, completeBuffer));
CHECK(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
return 0;
}
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) {
if (argc != 2 || (std::strcmp(argv[1], "--before") != 0 &&
std::strcmp(argv[1], "--after") != 0)) {
std::fprintf(stderr, "usage: %s --before|--after\n", argv[0]);
return 2;
}
bool enableBeforeInstantiate = std::strcmp(argv[1], "--before") == 0;
std::printf("CUPTI enabled %s cudaGraphInstantiate\n",
enableBeforeInstantiate ? "before" : "after");
int *counter = nullptr;
cudaStream_t stream;
CHECK(cudaMalloc(&counter, sizeof(int)));
CHECK(cudaStreamCreate(&stream));
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));
// --before: CUPTI sees the instantiate, so expect 10 body records per launch.
if (enableBeforeInstantiate)
CHECK(enableTracing());
cudaGraphExec_t executable;
CHECK(cudaGraphInstantiate(&executable, graph, 0));
// --after (bug): CUPTI attaches late; observed 1 body record per launch.
if (!enableBeforeInstantiate)
CHECK(enableTracing());
for (int i = 0; i < kLaunches; ++i)
CHECK(cudaGraphLaunch(executable, stream));
CHECK(cudaStreamSynchronize(stream));
CHECK(cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED));
int bodyCount = 0;
for (auto [buffer, validSize] : completedBuffers) {
CUpti_Activity *activity = nullptr;
while (cuptiActivityGetNextRecord(buffer, validSize, &activity) ==
CUPTI_SUCCESS) {
if (activity->kind != CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL)
continue;
auto *record = reinterpret_cast<CUpti_ActivityKernel10 *>(activity);
std::printf("activity kind=%u name=%s start=%llu end=%llu\n",
unsigned(record->kind),
record->name ? record->name : "<null>",
static_cast<unsigned long long>(record->start),
static_cast<unsigned long long>(record->end));
if (record->name && std::strstr(record->name, "body"))
++bodyCount;
}
std::free(buffer);
}
// Expect 30 in both modes; --after gives 3, one per launch (last iteration).
std::printf("body count=%d expected=%d\n", bodyCount,
kLaunches * kIterations);
CHECK(cuptiActivityDisable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
CHECK(cudaGraphExecDestroy(executable));
CHECK(cudaGraphDestroy(graph));
CHECK(cudaStreamDestroy(stream));
CHECK(cudaFree(counter));
return 0;
}
Expected: body count=30 expected=30 in --before and --after.
Actual:
$ ./repro --before
CUPTI enabled before cudaGraphInstantiate
...
body count=30 expected=30
$ ./repro --after
CUPTI enabled after cudaGraphInstantiate
activity kind=10 name=_Z5resetPi start=1791930486229549886 end=1791930486229551038
activity kind=10 name=_Z4bodyPi start=1791930486229633856 end=1791930486229635072
activity kind=10 name=_Z12setConditionyPKi start=1791930486229634976 end=1791930486229636480
activity kind=10 name=_Z5resetPi start=1791930486229644416 end=1791930486229645248
activity kind=10 name=_Z4bodyPi start=1791930486229718592 end=1791930486229719808
activity kind=10 name=_Z12setConditionyPKi start=1791930486229719712 end=1791930486229721216
activity kind=10 name=_Z5resetPi start=1791930486229729632 end=1791930486229730528
activity kind=10 name=_Z4bodyPi start=1791930486229804577 end=1791930486229805825
activity kind=10 name=_Z12setConditionyPKi start=1791930486229805729 end=1791930486229807233
body count=3 expected=30
The loop exits only after 10 iterations.
- In
--aftermode, eachbodyrecord starts 70–90 µs after itsreset. - In
--beforemode, the first iteration starts 10-20 µs afterreset. So the only record is a last iteration.
What we checked
- Instantiate is the deciding point. Tracing enabled between
cudaGraphCreateandcudaGraphInstantiategives all 30 records. - The workaround from topic 380013 works for SW tracing: call
cuptiActivityRegisterCallbacks()beforecudaGraphInstantiate, then enable the activity kind later. - HW trace (
cuptiActivityEnableHWTrace(1)) enabled after instantiate gives no kernel records for the graph, even outside the loop. Early callback registration does not help. Enabled before instantiate, it gives 30 records with correct correlation IDs. - Tracing does not need to stay on. A graph instantiated with tracing on still gives 30 records after disable and re-enable.
Questions
- Is this the late-attach limitation from topic 380013 (“CUPTI records all node metadata at instantiation time”)? If so, do you plan late-attach support for conditional-node bodies?
- Is it expected that HW trace records nothing after late attach?
Environment
- GPU: GB300 (SM 10.3)
- Driver: 580.126.20 (CUDA 13.0)
- CUDA toolkit: 13.0.2
- CUPTI: 13.0.2. CUPTI 13.2.1 and 13.4.2 give the same result on this driver.
- OS: Linux aarch64