Cupti classifies memset differently using instrumentation and hes

Example code:

    CUDA_MEMSET_NODE_PARAMS memset_params;
    memset(&memset_params, 0, sizeof(memset_params));
    memset_params.dst = output;
    memset_params.value = 0;
    memset_params.elementSize = sizeof(uint32_t);
    memset_params.width = (size_t)KERNELS_PER_GRAPH * count;
    memset_params.height = 1;
    CUgraphNode memset_node;
    CUDA(cuGraphAddMemsetNode(&memset_node, bundle->graph,
                              dependency ? &dependency : NULL,
                              dependency ? 1 : 0, &memset_params, context));

Using instrumentation mode, CUPTI will classify the node as a GPU kernel and return memset32 as the kernel name. However, using the HES mode, CUPTI will classify the node as a memset node with CUPTI_ACTIVITY_KIND_MEMSET.

Do you see this issue for memset nodes that are conditional nodes, or does it also occur with non-conditional memset nodes?

We have a known issue where the memset is misclasssified in case of a conditional node.

Hi Sanjiv, are you asking whether cuGraphAddConditionalNode has been used? No I confirm there’s no use of cuGraphAddConditionalNode APIs

Here’s a complete reproducer if you’re interested

#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)

extern "C" __global__ void kernel(float *output, int iterations) {
  float value = threadIdx.x;
  for (int i = 0; i < iterations; ++i)
    value = value * 1.0000001192092896f + 0.0000001192092896f;
  output[threadIdx.x] = value;
}

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);
}

int main(int argc, char **argv) {
  if (argc != 2 || (std::strcmp(argv[1], "--hes") != 0 &&
                    std::strcmp(argv[1], "--no-hes") != 0)) {
    std::fprintf(stderr, "usage: %s --hes|--no-hes\n", argv[0]);
    return 2;
  }
  bool enableHes = std::strcmp(argv[1], "--hes") == 0;
  std::printf("HES=%s\n", enableHes ? "on" : "off");

  CHECK(cuInit(0));
  if (enableHes) {
    CHECK(cuptiActivityEnableHWTrace(1));
    CHECK(cuptiActivityRegisterCallbacks(requestBuffer, completeBuffer));
    CHECK(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
  }

  constexpr int threads = 256;
  constexpr int elements = 16384;
  float *output;
  cudaStream_t stream;
  cudaGraph_t graph;
  cudaGraphExec_t executable;
  CHECK(cudaMalloc(&output, elements * sizeof(*output)));
  CHECK(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
  CHECK(cudaGraphCreate(&graph, 0));

  CUcontext context;
  CHECK(cuCtxGetCurrent(&context));
  CUDA_MEMSET_NODE_PARAMS memsetParams{};
  memsetParams.dst = reinterpret_cast<CUdeviceptr>(output);
  memsetParams.value = 0;
  memsetParams.elementSize = sizeof(float);
  memsetParams.width = elements;
  memsetParams.height = 1;
  CUgraphNode driverMemsetNode;
  CHECK(cuGraphAddMemsetNode(&driverMemsetNode, reinterpret_cast<CUgraph>(graph),
                             nullptr, 0, &memsetParams, context));
  auto memsetNode = reinterpret_cast<cudaGraphNode_t>(driverMemsetNode);

  int iterations = 100000;
  void *arguments[] = {&output, &iterations};
  cudaKernelNodeParams kernelParams{};
  kernelParams.func = reinterpret_cast<void *>(kernel);
  kernelParams.gridDim = dim3(1);
  kernelParams.blockDim = dim3(threads);
  kernelParams.kernelParams = arguments;
  cudaGraphNode_t kernelNode;
  CHECK(cudaGraphAddKernelNode(&kernelNode, graph, &memsetNode, 1,
                               &kernelParams));

  CHECK(cudaGraphInstantiate(&executable, graph));
  CHECK(cudaGraphUpload(executable, stream));
  CHECK(cudaGraphLaunch(executable, stream));
  CHECK(cudaStreamSynchronize(stream));
  if (enableHes) {
    CHECK(cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED));
    for (const auto &buffer : completedBuffers)
      std::free(buffer.first);
    completedBuffers.clear();
  } else {
    CHECK(cuptiActivityRegisterCallbacks(requestBuffer, completeBuffer));
    CHECK(cuptiActivityEnable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
  }

  CHECK(cudaGraphLaunch(executable, stream));
  CHECK(cudaStreamSynchronize(stream));
  CHECK(cuptiActivityFlushAll(CUPTI_ACTIVITY_FLAG_FLUSH_FORCED));

  int count = 0;
  for (auto [buffer, validSize] : completedBuffers) {
    CUpti_Activity *activity = nullptr;
    while (cuptiActivityGetNextRecord(buffer, validSize, &activity) ==
           CUPTI_SUCCESS) {
      ++count;
      if (activity->kind == CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL) {
        auto *record = reinterpret_cast<CUpti_ActivityKernel12 *>(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));
      } else {
        std::printf("activity kind=%u\n", unsigned(activity->kind));
      }
    }
    std::free(buffer);
  }
  std::printf("activity count=%d\n", count);

  CHECK(cuptiActivityDisable(CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL));
  CHECK(cudaGraphExecDestroy(executable));
  CHECK(cudaGraphDestroy(graph));
  CHECK(cudaStreamDestroy(stream));
  CHECK(cudaFree(output));
  if (enableHes)
    CHECK(cuptiActivityEnableHWTrace(0));
  return 0;
}

Hi Keren,
Thank you for the reproducer.

After investigation, we’ve identified this as a late-attach limitation in CUPTI’s CUDA Graph support.

CUPTI is initialized after `cudaGraphInstantiate()` is called in your reproducer in the case with --no-hes.

CUPTI records all node metadata at instantiation time. Without this information, CUPTI incorrectly generates a kernel activity record for the memset node.

We recommend the following workaround:

Workaround: Call `cuptiActivityRegisterCallbacks()` before `cudaGraphInstantiate()`
You do not need to enable any activities at that point. This ensures CUPTI is registered before graph instantiation and caches the necessary node information correctly. Activities can still be enabled and disabled independently as per your requirements.

Please let us know if the workaround resolves the issue on your end.
Thanks.

The proposed solution is fine but these limitations should be documented somewhere I think.

Sure, we will work on the documentation.
Thanks