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 ocr_system (full pipeline det+cls+rec) hubserving module when use_gpu=True and the CUDA_VISIBLE_DEVICES check fails. A bare try/except wraps os.environ["CUDA_VISIBLE_DEVICES"] and int(value[0]); KeyError or ValueError both become this RuntimeError.

Source

Thrown at deploy/hubserving/ocr_system/module.py:63

    type="cv/PP-OCR_system",
)
class OCRSystem(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.text_sys = TextSystem(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:

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. export CUDA_VISIBLE_DEVICES=0 before starting the hub serving process.
  2. Fall back to use_gpu=False if the deployment is CPU-only.
  3. In docker/k8s, inject CUDA_VISIBLE_DEVICES via -e / env entries.

Example fix

# before
mod = SystemModule(use_gpu=True)  # env unset -> RuntimeError

# after
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
mod = SystemModule(use_gpu=True)
# or
mod = SystemModule(use_gpu=False)
Defensive patterns

Strategy: validation

Validate before calling

import os

if USE_GPU:
    v = os.environ.get("CUDA_VISIBLE_DEVICES", "")
    assert v and v[0].isdigit(), "export CUDA_VISIBLE_DEVICES=<id> before use_gpu=True"
mod = SystemModule(use_gpu=USE_GPU)

Try / catch

try:
    mod = SystemModule(use_gpu=True)
except RuntimeError as e:
    if "CUDA_VISIBLE_DEVICES" in str(e):
        mod = SystemModule(use_gpu=False)
        logger.warning("GPU env invalid; falling back to CPU")
    else:
        raise

Prevention

When it happens

Trigger: Building the full-system module with use_gpu=True while CUDA_VISIBLE_DEVICES is missing, empty, or its first character is not a digit.

Common situations: Running the end-to-end OCR hub service on a GPU host without the export; CI environments that strip env vars; misordered startup scripts that start the service before setting device visibility.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/6cbaa3ac463bfe97. Report an issue: GitHub.