# RuntimeError: Unexpected error from cudaGetDeviceCount(); Error 802: system not yet initialized

**URL:** <https://discuss.pytorch.org/t/runtimeerror-unexpected-error-from-cudagetdevicecount-error-802-system-not-yet-initialized/219320>\
**Category:** torch.package / torch::deploy\
**Created:** [April 22, 2025, 4:19am UTC](https://discuss.pytorch.org/t/runtimeerror-unexpected-error-from-cudagetdevicecount-error-802-system-not-yet-initialized/219320 "2025-04-22T04:19:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfarisha](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nfarisha/32/76735_2.png) [@nfarisha](https://discuss.pytorch.org/u/nfarisha)\
**Post date:** [April 22, 2025, 4:19am UTC](https://discuss.pytorch.org/t/runtimeerror-unexpected-error-from-cudagetdevicecount-error-802-system-not-yet-initialized/219320/1 "2025-04-22T04:19:16Z")

</div>

Hi all,

I’m trying to set up the environment for CUDA before importing `torch`, but I’m running into the following error:

`RuntimeError: Unexpected error from cudaGetDeviceCount(). Did you run some CUDA functions before calling NumCudaDevices() that might have already set an error? Error 802: system not yet initialized.`

Here’s the code I’m using:

```auto
import os

os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3,4,5,6,7"
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["PYTORCH_NVML_BASED_CUDA_CHECK"] = "1"

import torch
print("CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
    print("Device count:", torch.cuda.device_count())
    print("Torch version:", torch. __version__ )
    print("Current device:", torch.cuda.current_device())
    print("Device name:", torch.cuda.get_device_name(torch.cuda.current_device()))

```

Am I setting these environment variables correctly? Is there something I’m missing that could cause `cudaGetDeviceCount()` to fail with Error 802?

Any insights would be appreciated!

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 22, 2025, 12:23pm UTC](https://discuss.pytorch.org/t/runtimeerror-unexpected-error-from-cudagetdevicecount-error-802-system-not-yet-initialized/219320/2 "2025-04-22T12:23:58Z")

</div>

What does `deviceQuery` return? Assuming the same error, check if your system uses NVSwitch and if FabricManager was properly installed.
