CUDA error: an illegal memory access was encountered

cudaErrorIllegalAddresserror 700sticky

In PyTorch the same failure appears as RuntimeError: CUDA error: an illegal memory access was encountered.

This error is sticky

After it happens, every later CUDA call in the same process fails with the same error, even ones that are correct. Find the first failing call, fix it, and restart the program (or the notebook kernel). Resetting the error is not possible.

Common causes

A host pointer was passed to the kernel

Memory from malloc, new or std::vector::data() lives in CPU RAM. The kernel needs a pointer from cudaMalloc (or cudaMallocManaged), with the data copied over by cudaMemcpy. This is the most common cause in beginner code.

An index runs past the end of the buffer

The grid is usually rounded up to a whole number of blocks, so the last block has threads with i >= n. Without an if (i < n) guard they write past the end. 2D code that swaps width and height has the same effect.

The pointer is null, uninitialised or already freed

If cudaMalloc failed and the result was not checked, the pointer is garbage. Using a pointer after cudaFree, or one allocated on a different device, also faults.

Shared memory or a struct is indexed wrongly

Writing past a __shared__ array, or computing byte offsets with the wrong sizeof, produces the same error from inside the kernel.

Reproduce it

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

Repro: a host pointer passed to a kernel
main.cu
123456789101112131415161718192021222324252627
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

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

int main() {
    const int n = 1 << 20;
    float *h = (float *)malloc(n * sizeof(float));
    for (int i = 0; i < n; i++) h[i] = 1.0f;

    // Bug: h is a host pointer. The GPU cannot dereference it.
    scale<<<(n + 255) / 256, 256>>>(h, n);

    cudaError_t err = cudaDeviceSynchronize();
    printf("cudaDeviceSynchronize: %s (%s)\n", cudaGetErrorString(err), cudaGetErrorName(err));

    // The error is sticky: unrelated calls fail too.
    float *d;
    err = cudaMalloc(&d, 4);
    printf("cudaMalloc afterwards: %s\n", cudaGetErrorString(err));
    free(h);
    return 0;
}
Output on an NVIDIA T4
cudaDeviceSynchronize: an illegal memory access was encountered (cudaErrorIllegalAddress)
cudaMalloc afterwards: an illegal memory access was encountered

Fix

Fixed: allocate on the device and copy
main.cu
1234567891011121314151617181920212223242526272829303132333435363738
#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 scale(float *data, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) data[i] *= 2.0f;
}

int main() {
    const int n = 1 << 20;
    const size_t bytes = n * sizeof(float);
    float *h = (float *)malloc(bytes);
    for (int i = 0; i < n; i++) h[i] = 1.0f;

    float *d;
    CUDA_CHECK(cudaMalloc(&d, bytes));
    CUDA_CHECK(cudaMemcpy(d, h, bytes, cudaMemcpyHostToDevice));

    scale<<<(n + 255) / 256, 256>>>(d, n);
    CUDA_CHECK(cudaGetLastError());
    CUDA_CHECK(cudaMemcpy(h, d, bytes, cudaMemcpyDeviceToHost));

    printf("h[0] = %.1f, h[n-1] = %.1f\n", h[0], h[n - 1]);
    CUDA_CHECK(cudaFree(d));
    free(h);
    return 0;
}
Output on an NVIDIA T4
h[0] = 2.0, h[n-1] = 2.0

Debugging checklist

  1. Check every CUDA call and every launch. A launch returns nothing, so follow it with cudaGetLastError().
  2. While debugging, call cudaDeviceSynchronize() right after each launch (or set CUDA_LAUNCH_BLOCKING=1). Kernel errors are reported at the next call that waits for the GPU, which is often a later cudaMemcpy or cudaFree, not the kernel that caused them.
  3. Run the program under compute-sanitizer ./app. It names the kernel, the thread, the bad address and how far outside an allocation it was. Compile with -lineinfo to also get the source line.
  4. Restart the process after fixing. Nothing can be done with CUDA in a process that hit this error.

FAQ

Why does the error point at cudaMemcpy or cudaFree instead of my kernel?

Kernel launches are asynchronous. The host keeps running and only learns about the fault at the next call that waits for the GPU. Synchronize after each launch while debugging to see which kernel failed.

Why does my out-of-bounds code sometimes not crash?

Allocations are rounded up and mapped in large pages, so a small overrun often lands in memory the process owns. On our T4, writing 24 floats past a 1000-float buffer reported no error at all, while a large overrun failed with error 700. A missing bounds check can silently corrupt data, which is why compute-sanitizer is worth running even when nothing crashes.

Can I recover without restarting the program?

No. NVIDIA documents this error as leaving the process in an inconsistent state: any further CUDA work returns the same error until the process is terminated and relaunched. In Jupyter, restart the kernel.

I get this error in PyTorch. What should I check?

In pure PyTorch code, out-of-range indices usually raise "device-side assert triggered" instead. Error 700 more often comes from a custom CUDA extension, a Triton kernel, or a library bug. Rerun with CUDA_LAUNCH_BLOCKING=1 to get the real failing line.