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_table (table recognition) hubserving module when use_gpu=True and CUDA_VISIBLE_DEVICES fails validation. Because the env-var read and int(first-char) are wrapped in a bare except, an unset variable, empty string, or non-digit-leading value all become this RuntimeError.

Source

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

    author_email="paddle-dev@baidu.com",
    type="cv/structure_table",
)
class TableSystem(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.table_sys = _TableSystem(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

  1. export CUDA_VISIBLE_DEVICES=0, then construct with use_gpu=True.
  2. Construct with use_gpu=False on CPU-only hosts.
  3. Set the variable inside the process (os.environ) before module creation if you cannot change the shell.

Example fix

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

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

Strategy: validation

Validate before calling

import os

def safe_use_gpu(flag: bool) -> bool:
    v = os.environ.get("CUDA_VISIBLE_DEVICES", "")
    return flag and bool(v) and v[0].isdigit()

mod = TableModule(use_gpu=safe_use_gpu(True))

Try / catch

try:
    mod = TableModule(use_gpu=True)
except RuntimeError as e:
    if "CUDA_VISIBLE_DEVICES" in str(e):
        mod = TableModule(use_gpu=False)
    else:
        raise

Prevention

When it happens

Trigger: Building the table-recognition module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset or malformed.

Common situations: Table-OCR serving on a GPU node without the export in the startup script; moving a working CPU service to GPU without updating environment configuration.

Related errors


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