cd /news/developer-tools/testing-cuda-kernel-execution-with-g… · home topics developer-tools article
[ARTICLE · art-105586] src=github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Testing CUDA kernel execution with GPU correctness harness

A new CUDA kernel correctness harness compiles kernels with nvcc, loads them into Python via ctypes, and checks outputs against NumPy baselines, with timing via CUDA events. All 23 tests pass on an NVIDIA T4 GPU, covering vector add, matrix multiplication with tile sizes including 257, reduction, and softmax overflow cases.

read3 min views1 publishedAug 21, 2026
Testing CUDA kernel execution with GPU correctness harness
Image: Michielbdejong (auto-discovered)

CUDA kernels don't throw exceptions when they're wrong. A misaligned tile boundary, a warp reading half the reduction, exp() overflowing to inf - the output is just silently incorrect. This harness surfaces those failure modes.

Kernels compile via nvcc, load into Python through ctypes, and get checked against NumPy baselines. Timing measurements use CUDA events inside the compiled library, not wall-clock.

+-----------------------------------------------------------------------+
|                          PyTest Framework                             |
|      (Boundary Analysis, Softmax Overflow, Edge-Case Vectors)         |
+-----------------------------------------------------------------------+
                                  |
                        (ctypes C-ABI Wrapper)
                                  v
+-----------------------------------------------------------------------+
|                  C++/CUDA Shared Library (.so)                        |
|  +---------------------------------------------------------------+    |
|  | CUDA Event Timer (Start → Launch Kernel → Stop)               |    |
|  +---------------------------------------------------------------+    |
|  | Kernels: vector_add | matrix_mul | reduction | softmax        |    |
|  +---------------------------------------------------------------+    |
+-----------------------------------------------------------------------+
                                  |
                          (Device Execution)
                                  v
+-----------------------------------------------------------------------+
|                      NVIDIA GPU (T4)                                  |
|         Warp-Shuffle Intrinsic (__shfl_down_sync) Execution           |
+-----------------------------------------------------------------------+

N=256 and N=257 are not the same test. One fills a block exactly. The other has a partial block where off-by-one in ceiling division silently drops the last element.

A tiled GEMM parameterized over tile sizes [16, 32, 64, 128, 257]. Everything passes at powers of two. 257 is where index math breaks.

Shared memory tree down to 32 lanes, then __shfl_down_sync for the final warp. No bank conflicts, no unnecessary syncs.

Two-pass stable algorithm. exp(1000) is inf in FP32. The naive kernel fails this test.

  • CUDA Toolkit 11.x or higher
  • Python 3.10 or higher
  • Make build system
  • NVIDIA GPU (tested on T4)

Clean build:

make clean && make

Run the full test suite:

pytest tests/ -v

Run performance benchmarks:

python benchmarks/bench.py

All 23 tests passing on NVIDIA T4:

tests/test_correctness.py::TestVectorAdd::test_equivalence_random            PASSED
tests/test_correctness.py::TestVectorAdd::test_single_element                PASSED
tests/test_correctness.py::TestVectorAdd::test_non_power_of_two_length       PASSED
tests/test_correctness.py::TestVectorAdd::test_exact_block_boundary          PASSED
tests/test_correctness.py::TestVectorAdd::test_mismatched_shapes_raises      PASSED
tests/test_correctness.py::TestVectorAdd::test_wrong_dtype_raises            PASSED
tests/test_correctness.py::TestMatrixMul::test_matrix_mul_equivalence[16]    PASSED
tests/test_correctness.py::TestMatrixMul::test_matrix_mul_equivalence[32]    PASSED
tests/test_correctness.py::TestMatrixMul::test_matrix_mul_equivalence[64]    PASSED
tests/test_correctness.py::TestMatrixMul::test_matrix_mul_equivalence[128]   PASSED
tests/test_correctness.py::TestMatrixMul::test_matrix_mul_equivalence[257]   PASSED
tests/test_correctness.py::TestReduction::test_sum_random                    PASSED
tests/test_correctness.py::TestReduction::test_sum_single_element            PASSED
tests/test_correctness.py::TestReduction::test_sum_exact_block_boundary      PASSED
tests/test_correctness.py::TestReduction::test_sum_non_power_of_two          PASSED
tests/test_correctness.py::TestReduction::test_sum_multi_block               PASSED
tests/test_correctness.py::TestReduction::test_all_zeros                     PASSED
tests/test_correctness.py::TestSoftmax::test_equivalence_random              PASSED
tests/test_correctness.py::TestSoftmax::test_output_sums_to_one             PASSED
tests/test_correctness.py::TestSoftmax::test_numerical_stability_large_values PASSED
tests/test_correctness.py::TestSoftmax::test_uniform_input_gives_uniform_output PASSED
tests/test_correctness.py::TestSoftmax::test_single_element                  PASSED
tests/test_correctness.py::TestSoftmax::test_oversized_input_raises          PASSED

23 passed in 0.45s

CUDA event timing with warmup=5, 20 runs per kernel. NVIDIA T4 peak bandwidth is approximately 320 GB/s.

Kernel Input Size Avg Time Bandwidth
vector_add 16M elements 0.765 ms 263.2 GB/s
reduce_sum 16M elements 0.629 ms 106.7 GB/s
matrix_mul 512x512 0.342 ms 9.2 GB/s
softmax 1024 0.018 ms 0.9 GB/s

Vector add achieves 263 GB/s, which is 82% of T4 peak. This is expected for a memory-bound kernel.

Reduce sum achieves 107 GB/s. The two-phase design adds overhead. Specialized libraries like CUB achieve closer to 250 GB/s with fused algorithms.

Matrix multiply bandwidth is intentionally low. This is a naive GEMM with poor arithmetic intensity. The relevant metric here is correctness at edge cases like N=257, not peak performance.

  • FP16 and BF16 kernel variants
  • Online softmax algorithm for sequences beyond N=1024
  • NVTX markers for Nsight timeline profiling
  • CI integration with GPU runner

This harness validates CUDA kernel correctness across:

  • Non-power-of-two dimensions
  • Boundary conditions and partial blocks
  • Numerical stability under extreme values
  • Warp-level synchronization and memory safety
  • Real-world edge case dimensions (257 is not random)

The goal is to catch silent failures before they reach production, validating both the kernel logic and the infrastructure that wraps it.

── more in #developer-tools 4 stories · sorted by recency
── more on @nvidia t4 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/testing-cuda-kernel-…] indexed:0 read:3min 2026-08-21 ·