CUDA Versioning, Explained

Background

Vector addition is the hello world of CUDA. So I learned the basics, wrote my first kernel, compiled it, and boom. The simplest example didn’t work out of the box.

nvcc -o main_err main.cu
./main_err
main.cu:33: CUDA err: the provided PTX was compiled with an unsupported toolchain.

After quick searching, the fix is not that complicated, just blindly (for now) add a compiler flag and the code runs.

nvcc -arch=native -o main_ok main.cu
./main_ok
0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 ...
Full main.cu code for context
#include <stdio.h>
#include <stdlib.h>
#define CUDA_CHECK(call) \
do { \
cudaError_t err = call; \
if (err != cudaSuccess) { \
fprintf(stderr, "%s:%d: CUDA err: %s\n", __FILE__, __LINE__, cudaGetErrorString(err)); \
exit(EXIT_FAILURE); \
} \
} while (false)
__global__ void vec_add_kernel(float* a, float* b, float* out, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
out[i] = a[i] + b[i];
}
}
void vec_add(float* a, float* b, float* out, int n) {
float* ad;
float* bd;
float* outd;
int nbytes = n * sizeof(float);
cudaMalloc((void**)&ad, nbytes);
cudaMalloc((void**)&bd, nbytes);
cudaMalloc((void**)&outd, nbytes);
cudaMemcpy(ad, a, nbytes, cudaMemcpyHostToDevice);
cudaMemcpy(bd, b, nbytes, cudaMemcpyHostToDevice);
int threads_per_block = 64;
int blocks_per_grid = (n + threads_per_block - 1) / threads_per_block;
vec_add_kernel<<<blocks_per_grid, threads_per_block>>>(ad, bd, outd, n);
CUDA_CHECK(cudaGetLastError());
cudaMemcpy(out, outd, nbytes, cudaMemcpyDeviceToHost);
cudaFree(ad);
cudaFree(bd);
cudaFree(outd);
}
int main() {
int n = 100;
float* a = (float*)malloc(sizeof(float) * n);
float* b = (float*)malloc(sizeof(float) * n);
float* out = (float*)malloc(sizeof(float) * n);
for (int i = 0; i < n; ++i) {
a[i] = i;
b[i] = i;
}
vec_add(a, b, out, n);
for (int i = 0; i < n; ++i) {
printf("%0.1f ", out[i]);
}
printf("\n");
return 0;
}

FYI, this is my setup as I’m writing this blog:

  • RTX 3060 GPU
  • Max supported CUDA version by the driver is 13.0 (from nvidia-smi)
  • nvcc version (including all the toolkit) is 13.2 (from nvcc --version)

Did I just happen to make the rookie mistake of not installing the same version of CUDA driver and CUDA toolkit? Probably. But then the nvcc -arch=native ... is doing just fine. What gives?

In this post, we will try to understand CUDA versioning so that when the same or similar problems happen, at least we know what the cause is. We will also explore compute capabilities, PTX, and other relevant CUDA jargon.

Hardware and compute capability

There are a lot of NVIDIA GPUs. Right now, what we care about is the compute capability. Compute capability (CC) defines the hardware features and supported instructions for each NVIDIA GPU architecture.1 That means your GPU sets the ceiling of what’s possible, and it is literally burned in the silicon!

But what about the architecture name? We’ve heard a lot of names like: Blackwell, Ampere, Turing, etc. The architecture name is not always mapped one-to-one with specific compute capability. For example both RTX 3060 and A100 are Ampere, but their CC are 8.6 and 8.0, respectively.

The compute capability directly corresponds to the version number of the SM. For example, the SMs within a GPU of CC 12.0 have SM version sm_120. This version is used to label binaries.2 CC format is major.minor, so 8.6 means CC major 8 and CC minor 6.

CUDA compilation pipeline

main_err does not work, but main_ok works. That must mean they are compiled differently. To inspect the executables, we use cuobjdump. First, on the broken one:

nvcc -o main_err main.cu
cuobjdump main_err
Fatbin elf code:
================
arch = sm_75
code version = [1,8]
host = linux
compile_size = 64bit
Fatbin elf code:
================
arch = sm_75
code version = [1,8]
host = linux
compile_size = 64bit
Fatbin ptx code:
================
arch = sm_75
code version = [9,2]
host = linux
compile_size = 64bit
compressed
ptxasOptions =

And then, on the working one:

nvcc -arch=native -o main_ok main.cu
cuobjdump main_ok
Fatbin elf code:
================
arch = sm_86
code version = [1,8]
host = linux
compile_size = 64bit
Fatbin elf code:
================
arch = sm_86
code version = [1,8]
host = linux
compile_size = 64bit
An executable or library contains CPU binary code and a fatbin. The fatbin holds one compute_120 PTX and three cubins: sm_80, sm_86, and sm_100.
Executable anatomy.

Some CUDA jargon in a very simplified manner:

  • ELF (Executable and Linkable Format): Linux/Unix executable or shared object format.
  • Fatbin: container that can hold many cubins and PTXs, hence called fat. cuobjdump is actually listing all the fatbin contents itself. An executable can have more than 1 fatbin (resulting in seemingly duplicate entries).
  • cubin (CUDA binary): binary that will run on real GPU. This is container of SASS. Fatbin entries with the Fatbin elf code: heading are cubins.
  • PTX (Parallel Thread Execution): NVIDIA virtual ISA (Instruction Set Architecture). This is a bytecode for virtual GPU. Similar in spirit to Java bytecode. Fatbin entries with the Fatbin ptx code: heading are PTX.
  • SASS (Streaming Assembler): NVIDIA real ISA, the actual machine code that will be run by the GPU. Think assembly language.
  • ptxas (PTX Assembler): PTX to SASS compiler.

What you really need to pay attention to on the fatbin entries:

  • In SASS, one thing to look for: arch. Arch is the real target GPU to run.
    Fatbin elf code:
    ================
    arch = sm_75 <---- SASS targeting sm_75 GPU
    code version = [1,8]
    host = linux
    compile_size = 64bit
  • In PTX, two things to look for: arch and code version. PTX’ arch however, means virtual instruction the PTX targets.
    Fatbin ptx code:
    ================
    arch = sm_75 <---- PTX contains compute_75 instructions
    code version = [9,2] <---- PTX ISA version
    host = linux
    compile_size = 64bit
    compressed
    ptxasOptions =

The main idea of executable creation is: Source code -> PTX -> SASS. We can see this in more detail below.

Flowchart: nvcc splits example.cu in two. Host code (main and the kernel launcher) goes through the host compiler into CPU binary code. Kernel code goes through the GPU compiler into PTX, which ptxas assembles into a cubin. The executable holds the CPU binary code plus a fatbinary with both the PTX and the cubin.
Basic nvcc CUDA compilation pass.
Flowchart: the same nvcc pipeline, but the GPU compiler emits two PTX targets, compute_80 and compute_90. ptxas turns compute_80 into sm_80 and sm_86 cubins, and compute_90 into an sm_90 cubin. The executable's fatbinary holds both PTXs and all three cubins next to the CPU binary code.
Stuffing more PTXs and cubins into the same executable.

The above image where we stuff many PTXs and cubins into the same executable can be achieved with the command below. arch= means generate what PTX, code= means what to put into the final binary.

nvcc \
-gencode arch=compute_80,code=[compute_80,sm_80,sm_86] \
-gencode arch=compute_90,code=[compute_90,sm_90] \
-o example example.cu

Try cuobjdump-ing on the example binary, it will show 8 entries instead of 5. This is because there are 2 fatbins and the fatbin we care about is the one holding 5 entries.

Perspective from the driver

The NVIDIA Driver can be thought of as the operating system of the GPU.3 The main job of the driver is to talk with the actual GPU hardware.

When someone launches an executable (like ./main_ok above), this is what the driver does:

yesnonoyesnoyeskernel launchcompatible SASS?same major, minor ≤ GPUcompatible PTX?compute_XY ≤ GPUdriver reads ISA?PTX ISA ≤ driver’srun the SASS“no kernel image isavailable for executionon the device”“the provided PTX wascompiled with anunsupported toolchain”JIT compile PTX to SASS,then run it
main_err on driver 13.0: there is no sm_86 SASS, so the driver falls back to the compute_75 PTX. 7.5 ≤ 8.6, so it could be JIT compiled, but its PTX ISA (9.2) is newer than the driver can read.

Rules for SASS:

  • SASS arch major == GPU major
  • SASS arch minor <= GPU minor
  • Example:
    • SASS sm_86 and sm_80 are runnable on sm_86 GPU
    • SASS sm_86 and sm_75 are not runnable on sm_80 GPU

Rules for PTX:

  • PTX ISA version <= driver PTX ISA version
  • PTX can be JIT compiled to any higher or equal SASS
  • Example4:
    • CUDA driver 13.2 with PTX ISA 9.2 can JIT compile PTX ISA 9.2, 9.0, 8.8 or anything lower than or equal to 9.2
    • CUDA driver 12.0 with PTX ISA 8.0 cannot JIT compile PTX ISA 9.2, 9.0

So, why did main_err fail? Why did nvcc produce sm_75 by default? This is because when no flags are supplied to nvcc, it compiles to the lowest supported version. Lowest is chosen so that the final executable can support the widest range of GPUs (thanks to PTX JIT compilation).

We can query the supported PTX and SM using nvcc --list-gpu-arch and nvcc --list-gpu-code, respectively. Below is the output of my nvcc (CUDA 13.2):

nvcc --list-gpu-code
sm_75
sm_80
sm_86
sm_87
sm_88
sm_89
sm_90
sm_100
sm_110
sm_103
sm_120
sm_121

Let’s look back on the main_err case above. Here are the important bits:

  • SASS is targeting sm_75, which RTX 3060 sm_86 can’t run.
  • PTX ISA version 9.2, which is not parsable by driver with max supported CUDA 13.0 having PTX ISA ver 9.0.
  • The above point is important, even though the PTX is forward compatible, i.e. compute_XY can be compiled into sm_AB SASS as long as XY <= AB, the unparsable PTX ISA is the blocker here.

main_ok is fine because -arch=native is the same as -gencode arch=compute_86,code=sm_86. There is no PTX tho, and the driver loads the SASS successfully.

Takeaways

  • Update your driver to the highest CUDA version supported on your GPU.
  • Read the different error logs and refer to the driver flow diagram above to diagnose where the issues are.
  • For smoother operations, make sure the max supported CUDA version by the driver is higher than or equal to the CUDA toolkit.
  • One useful trick: use pixi as the CUDA toolkit version manager. pixi is a better conda IMO, it uses conda-forge as well. Relevant Jeremy Howard’s tweet.

Further reading

  1. https://developer.nvidia.com/cuda/gpus ↩

  2. https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/cuda-platform.html#compute-capability-and-streaming-multiprocessor-versions ↩

  3. https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/cuda-platform.html#cuda-toolkit-and-nvidia-driver ↩

  4. https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#release-notes ↩