CUDA error: invalid configuration argument

cudaErrorInvalidConfigurationerror 9

In PyTorch the same failure appears as RuntimeError: CUDA error: invalid configuration argument.

Common causes

More than 1024 threads per block

The limit is on the product of all three block dimensions, so dim3(32, 32, 2) (2048 threads) fails even though each dimension is legal.

A grid with zero blocks

(n + 255) / 256 is 0 when n is 0. An empty input, an empty batch or an unset size variable turns into a launch with no blocks, which is invalid.

gridDim.y or gridDim.z above 65535

Only gridDim.x can go up to 2^31 - 1. Mapping a large dimension (rows, batch, tokens) onto y or z works in tests and fails on bigger inputs.

Shared memory over the limit (reported as "invalid argument")

Asking for more than 48 KB of dynamic shared memory without opting in fails with cudaErrorInvalidValue, not this error. The two are easy to confuse.

Reproduce it

Press Run to execute this on a real NVIDIA GPU in the playground.

Repro: four launches the GPU rejects
main.cu
1234567891011121314151617181920
#include <cstdio>
#include <cuda_runtime.h>

__global__ void k(float *x) { if (x) x[0] = 1; }
__global__ void ks(float *x) { extern __shared__ float s[]; s[threadIdx.x] = 1; if (x) x[0] = s[0]; }

void report(const char *what) {
    cudaError_t e = cudaGetLastError();
    printf("%-30s -> %s (%s)\n", what, cudaGetErrorString(e), cudaGetErrorName(e));
}

int main() {
    k<<<1, 2048>>>(nullptr);                report("<<<1, 2048>>>");
    k<<<0, 256>>>(nullptr);                 report("<<<0, 256>>> (n = 0)");
    k<<<dim3(1, 70000), 256>>>(nullptr);    report("gridDim.y = 70000");
    k<<<1, dim3(32, 32, 2)>>>(nullptr);     report("block 32x32x2");
    ks<<<1, 256, 64 * 1024>>>(nullptr);     report("64 KB dynamic shared mem");
    k<<<1, 1024>>>(nullptr);                report("<<<1, 1024>>> (valid)");
    return 0;
}
Output on an NVIDIA T4
<<<1, 2048>>>                  -> invalid configuration argument (cudaErrorInvalidConfiguration)
<<<0, 256>>> (n = 0)           -> invalid configuration argument (cudaErrorInvalidConfiguration)
gridDim.y = 70000              -> invalid configuration argument (cudaErrorInvalidConfiguration)
block 32x32x2                  -> invalid configuration argument (cudaErrorInvalidConfiguration)
64 KB dynamic shared mem       -> invalid argument (cudaErrorInvalidValue)
<<<1, 1024>>> (valid)          -> no error (cudaSuccess)

Fix

Fixed: size the grid from n, skip empty input, opt in to more shared memory
main.cu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

#define CUDA_CHECK(call)                                                  \
    do {                                                                  \
        cudaError_t err_ = (call);                                        \
        if (err_ != cudaSuccess) {                                        \
            fprintf(stderr, "%s:%d: %s (%s)\n", __FILE__, __LINE__,       \
                    cudaGetErrorString(err_), cudaGetErrorName(err_));    \
            exit(1);                                                      \
        }                                                                 \
    } while (0)

__global__ void add1(float *x, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) x[i] += 1.0f;
}

__global__ void bigShared(float *x) {
    extern __shared__ float s[];
    s[threadIdx.x] = threadIdx.x;
    __syncthreads();
    if (threadIdx.x == 0) x[0] = s[255];
}

void launchAdd1(float *d, int n) {
    if (n <= 0) return;                         // an empty grid is an error
    const int threads = 256;                    // 128-512 is a good default
    const int blocks = (n + threads - 1) / threads;
    add1<<<blocks, threads>>>(d, n);            // big sizes go on x, never y or z
    CUDA_CHECK(cudaGetLastError());
}

int main() {
    const int n = 100000;
    float *d;
    CUDA_CHECK(cudaMalloc(&d, n * sizeof(float)));
    CUDA_CHECK(cudaMemset(d, 0, n * sizeof(float)));
    launchAdd1(d, n);
    launchAdd1(d, 0);

    // Above 48 KB of dynamic shared memory, opt in first (T4 allows up to 64 KB).
    CUDA_CHECK(cudaFuncSetAttribute(bigShared, cudaFuncAttributeMaxDynamicSharedMemorySize, 64 * 1024));
    bigShared<<<1, 256, 64 * 1024>>>(d);
    CUDA_CHECK(cudaGetLastError());

    float first;
    CUDA_CHECK(cudaMemcpy(&first, d, sizeof(float), cudaMemcpyDeviceToHost));
    printf("all launches OK, x[0] = %.0f\n", first);
    CUDA_CHECK(cudaFree(d));
    return 0;
}
Output on an NVIDIA T4
all launches OK, x[0] = 255

Debugging checklist

  1. Call cudaGetLastError() immediately after every launch. Launch errors are only reported there; the kernel itself just never runs.
  2. Print the grid and block sizes right before the failing launch. A zero or a huge number usually stands out.
  3. Check the limits of your GPU with cudaGetDeviceProperties: maxThreadsPerBlock, maxThreadsDim, maxGridSize, sharedMemPerBlock and sharedMemPerBlockOptin.
  4. If the error only appears for some inputs, look for a size that can be 0 or a dimension that can exceed 65535.

FAQ

What are the CUDA launch limits?

On every GPU since compute capability 2.0: at most 1024 threads per block, block dimensions up to 1024 x 1024 x 64, and a grid up to 2^31 - 1 blocks in x and 65535 in y and z. Shared memory is 48 KB per block by default; newer GPUs allow more after an opt-in (64 KB on a T4).

Why does my kernel silently do nothing?

A rejected launch does not crash or print anything. The kernel is skipped and the error waits in cudaGetLastError(). If you never check it, the only symptom is output that was never written.

I get this error from PyTorch. What causes it?

Usually an operation on an empty tensor or on a dimension above 65535 inside a custom or third-party kernel. Print the shapes going into the failing op; with CUDA_LAUNCH_BLOCKING=1 the traceback points at it.