PaddlePaddle/PaddleOCR · error · RuntimeError
Environment Variable CUDA_VISIBLE_DEVICES is not set correct
Error message
Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id.
What it means
Raised during __init__ of the structure_layout hubserving module when use_gpu=True but the CUDA_VISIBLE_DEVICES environment variable fails its check. The bare except catches KeyError (unset/empty variable) and ValueError (int() of a non-digit first character) and re-raises as this RuntimeError.
Source
Thrown at deploy/hubserving/structure_layout/module.py:61
author_email="paddle-dev@baidu.com",
type="cv/structure_layout",
)
class LayoutPredictor(hub.Module):
def _initialize(self, use_gpu=False, enable_mkldnn=False):
"""
initialize with the necessary elements
"""
cfg = self.merge_configs()
cfg.use_gpu = use_gpu
if use_gpu:
try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0])
print("use gpu: ", use_gpu)
print("CUDA_VISIBLE_DEVICES: ", _places)
cfg.gpu_mem = 8000
except:
raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
)
cfg.ir_optim = True
cfg.enable_mkldnn = enable_mkldnn
self.layout_predictor = _LayoutPredictor(cfg)
def merge_configs(self):
# default cfg
backup_argv = copy.deepcopy(sys.argv)
sys.argv = sys.argv[:1]
cfg = parse_args()
update_cfg_map = vars(read_params())
for key in update_cfg_map:
cfg.__setattr__(key, update_cfg_map[key])
View on GitHub (pinned to 2661c7c0ef)
Solutions
- export CUDA_VISIBLE_DEVICES=0 in the launch environment, then start with use_gpu=True.
- Use use_gpu=False when running CPU-only.
- Persist the export in the service definition so it survives restarts.
Example fix
# before mod = LayoutModule(use_gpu=True) # env unset -> RuntimeError # after import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" mod = LayoutModule(use_gpu=True) # or mod = LayoutModule(use_gpu=False)
Defensive patterns
Strategy: validation
Validate before calling
import os
def decide_use_gpu(requested: bool) -> bool:
if not requested:
return False
v = os.environ.get("CUDA_VISIBLE_DEVICES", "")
return bool(v) and v[0].isdigit()
mod = LayoutModule(use_gpu=decide_use_gpu(True)) Try / catch
try:
mod = LayoutModule(use_gpu=True)
except RuntimeError as e:
if "CUDA_VISIBLE_DEVICES" in str(e):
mod = LayoutModule(use_gpu=False)
else:
raise Prevention
- Keep the GPU env export next to the service start command in docs and scripts.
- Validate the env in a preflight step for GPU deployments.
- Automate CPU fallback for environments where GPU presence is not guaranteed.
When it happens
Trigger: Instantiating the layout analysis module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset, empty, or starts with a non-digit.
Common situations: Document-layout serving deployed without the GPU visibility export; switching a working CPU deployment to GPU mode without updating the launch script.
Related errors
- Environment Variable CUDA_VISIBLE_DEVICES is not set correct
- Environment Variable CUDA_VISIBLE_DEVICES is not set correct
- Environment Variable CUDA_VISIBLE_DEVICES is not set correct
- Environment Variable CUDA_VISIBLE_DEVICES is not set correct
- Environment Variable CUDA_VISIBLE_DEVICES is not set correct
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/d73076cee0667ed4.
Report an issue: GitHub.