TensorFlow
cuInit
CUDA_ERROR_NO_DEVICE
CUDA
Machine Learning Issues

TensorFlow failed call to cuInit CUDA_ERROR_NO_DEVICE

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

failed call to cuInit: CUDA_ERROR_NO_DEVICE means TensorFlow tried to initialize CUDA but could not find a usable NVIDIA GPU. The message is common on laptops without discrete NVIDIA hardware, in containers that do not expose the GPU, and on servers where drivers or visibility settings are wrong.

What cuInit Is Doing

CUDA starts by calling the driver API function cuInit. TensorFlow reaches that code path when it loads GPU support and asks the CUDA runtime what devices are available. If the driver reports that no CUDA-capable device exists, TensorFlow logs CUDA_ERROR_NO_DEVICE.

That does not always mean TensorFlow itself is broken. It usually means one of these cases is true:

  • there is no NVIDIA GPU in the machine
  • the GPU exists but the driver is missing or not loaded
  • the GPU is hidden from the process
  • you installed a GPU-enabled TensorFlow environment on a CPU-only machine

Start With Simple Checks

The fastest way to diagnose the issue is to verify hardware and visibility separately from TensorFlow.

bash
nvidia-smi

If nvidia-smi fails, TensorFlow cannot use the GPU because the NVIDIA driver stack is not available to user space.

Then check what TensorFlow sees:

python
1import tensorflow as tf
2
3print(tf.__version__)
4print(tf.config.list_physical_devices("GPU"))

If that prints an empty list, TensorFlow is running but has no visible GPU.

Common Causes and Fixes

No NVIDIA GPU Present

If the machine has only integrated graphics or a non-NVIDIA GPU, CUDA will not work. The correct fix is not to chase CUDA settings forever. Run the CPU build and ignore the GPU initialization message if it is harmless in your environment.

Driver Not Installed Correctly

If the system has an NVIDIA GPU but nvidia-smi does not work, install or repair the NVIDIA driver first. TensorFlow depends on the driver stack, not just on Python packages.

GPU Hidden by Environment Variables

In shared environments, the process may not be allowed to see the GPU.

bash
echo "$CUDA_VISIBLE_DEVICES"

If that variable is set to an empty value or an invalid device list, TensorFlow will behave as if no GPU exists. In a container, you also need the runtime to pass GPU devices through to the container.

Version Mismatch

Older TensorFlow setups could fail because CUDA, cuDNN, and the NVIDIA driver were not compatible with the installed TensorFlow version. The safest approach is to start from a documented combination or a known-good container image rather than mixing versions ad hoc.

A Small Diagnostic Script

python
1import os
2import tensorflow as tf
3
4print("CUDA_VISIBLE_DEVICES:", os.environ.get("CUDA_VISIBLE_DEVICES"))
5print("Built with CUDA:", tf.test.is_built_with_cuda())
6print("GPUs:", tf.config.list_physical_devices("GPU"))
7
8for gpu in tf.config.list_physical_devices("GPU"):
9    details = tf.config.experimental.get_device_details(gpu)
10    print(gpu, details)

This script answers three practical questions: was TensorFlow built with CUDA support, does the current process see any GPUs, and what device details are actually exposed.

When It Is Safe to Ignore the Message

On some CPU-only systems, a GPU-enabled environment may log the error once and continue on CPU. If your workload is intentionally CPU-only and performance is acceptable, the cleanest fix is usually to use a CPU-oriented environment rather than trying to silence the message through hacks.

The important distinction is between a warning on a CPU machine and a real outage on a GPU machine. If training is supposed to use a GPU and falls back to CPU, treat it as a configuration bug.

Common Pitfalls

A common mistake is installing CUDA toolkit packages and assuming that is enough. Without a working NVIDIA driver, TensorFlow still cannot see a GPU.

Another mistake is debugging only inside Python. Check nvidia-smi first. If the operating system cannot see the GPU, TensorFlow will not fix that.

Container users often forget runtime flags that expose GPU devices. In that case, the host may be configured correctly while the container still reports CUDA_ERROR_NO_DEVICE.

Finally, avoid random version combinations. If you update TensorFlow, CUDA, or drivers independently, verify the full stack rather than assuming partial upgrades are safe.

Summary

  • 'CUDA_ERROR_NO_DEVICE means TensorFlow could not find a usable NVIDIA GPU.'
  • Check nvidia-smi before changing Python code.
  • Verify GPU visibility with tf.config.list_physical_devices("GPU").
  • Common causes are missing drivers, hidden GPUs, CPU-only machines, and version mismatches.
  • If the machine is intentionally CPU-only, use a CPU environment instead of debugging CUDA unnecessarily.

Course illustration
Course illustration

All Rights Reserved.