Debug cuda kernel correctness

From tensormux/kernel-skills · View interactive version

Debug cuda kernel correctness is an AI agent skill from the GitHub repository tensormux/kernel-skills. Guide the agent through a systematic process of isolating, reproducing, and diagnosing correctness errors in CUDA kernels — covering indexing bugs, layout mismatches, synchronization races, reduction

How to use this skill

Skill file

Skill: Debug CUDA Kernel Correctness

Purpose

Guide the agent through a systematic process of isolating, reproducing, and diagnosing correctness errors in CUDA kernels — covering indexing bugs, layout mismatches, synchronization races, reduction errors, numerical drift, and out-of-bounds memory access.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Reproduce the error on a minimal input. Before anything else, reduce the input size to the smallest case that still fails. A 4x4 matrix multiplication failure is much easier to diagnose than a 4096x4096 one. Powers of two are not representative of all failure modes — test sizes like 31, 63, 65, 127, and 129 to exercise partial-tile boundary conditions.
  1. Classify the error pattern. Print the output tensor (for small failing inputs) and compare element-by-element against the reference:
  1. Check indexing arithmetic explicitly. Index bugs are the most common class of CUDA kernel correctness errors. For every global memory access in the kernel:
  1. Audit shared memory usage and synchronization. For every shared memory read/write pair:
  1. Check reduction correctness. For kernels that perform parallel reductions:
  1. Check dtype and precision. For every intermediate value in the compute path:
  1. Use cuda-memcheck / compute-sanitizer for memory safety. Run the kernel under compute-sanitizer --tool memcheck to detect:

Any of these can produce wrong results silently without sanitizer tooling. This step is mandatory for any kernel that fails with apparently random or intermittently wrong outputs.

  1. Isolate to a single thread block. For non-trivial correctness bugs, add a guard if (blockIdx.x != 0 || blockIdx.y != 0) return; to run only one block, print its outputs, and verify them in isolation. This eliminates global memory aliasing, ordering-dependent writes, and inter-block synchronization issues from the investigation.
  1. Check the host-side launch configuration. Verify that:
  1. Binary search with assertion kernels. For large, complex kernels, insert assertion checks at intermediate stages using assert() inside the kernel (enabled under CUDART_DEVICE_ASSERTION) or by writing intermediate results to debug buffers and validating them on the host. Narrow the failing region to the smallest unit of code that first introduces wrong values.

Kernel design rules

Correctness requirements

Performance requirements

Correctness debugging does not require performance optimization. However, do not introduce debugging infrastructure (debug buffers, assertion checks, per-thread output printing) that disables the performance features being debugged, unless the goal is to simplify the problem space temporarily. Validate correctness at the full target configuration after the bug is fixed.

Output format

The final response must include:

  1. Error classification: what category of bug is this (indexing, synchronization, dtype, boundary, launch config)?
  2. Minimal reproduction: the smallest input size and configuration that reproduces the bug.
  3. Root cause: the specific line(s) of code responsible, with an explanation of why they produce wrong output.
  4. Fix: the corrected code with a clear explanation of what changed and why.
  5. Verification plan: how to confirm the fix is correct (reference comparison, input size sweep, sanitizer run).
  6. Regression tests to add: what test cases should be added to prevent this class of bug from regressing.

Common failure modes

Review checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.