{"record":{"id":"d73076cee0667ed4","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s-d73076","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"deploy/hubserving/structure_layout/module.py","lineNumber":61,"sourceCode":"    author_email=\"paddle-dev@baidu.com\",\n    type=\"cv/structure_layout\",\n)\nclass LayoutPredictor(hub.Module):\n    def _initialize(self, use_gpu=False, enable_mkldnn=False):\n        \"\"\"\n        initialize with the necessary elements\n        \"\"\"\n        cfg = self.merge_configs()\n        cfg.use_gpu = use_gpu\n        if use_gpu:\n            try:\n                _places = os.environ[\"CUDA_VISIBLE_DEVICES\"]\n                int(_places[0])\n                print(\"use gpu: \", use_gpu)\n                print(\"CUDA_VISIBLE_DEVICES: \", _places)\n                cfg.gpu_mem = 8000\n            except:\n                raise RuntimeError(\n                    \"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.\"\n                )\n        cfg.ir_optim = True\n        cfg.enable_mkldnn = enable_mkldnn\n\n        self.layout_predictor = _LayoutPredictor(cfg)\n\n    def merge_configs(self):\n        # default cfg\n        backup_argv = copy.deepcopy(sys.argv)\n        sys.argv = sys.argv[:1]\n        cfg = parse_args()\n\n        update_cfg_map = vars(read_params())\n\n        for key in update_cfg_map:\n            cfg.__setattr__(key, update_cfg_map[key])\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/hubserving/structure_layout/module.py#L43-L79","documentation":"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.","triggerScenarios":"Instantiating the layout analysis module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset, empty, or starts with a non-digit.","commonSituations":"Document-layout serving deployed without the GPU visibility export; switching a working CPU deployment to GPU mode without updating the launch script.","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."],"exampleFix":"# before\nmod = LayoutModule(use_gpu=True)  # env unset -> RuntimeError\n\n# after\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nmod = LayoutModule(use_gpu=True)\n# or\nmod = LayoutModule(use_gpu=False)","handlingStrategy":"validation","validationCode":"import os\n\ndef decide_use_gpu(requested: bool) -> bool:\n    if not requested:\n        return False\n    v = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\n    return bool(v) and v[0].isdigit()\n\nmod = LayoutModule(use_gpu=decide_use_gpu(True))","typeGuard":null,"tryCatchPattern":"try:\n    mod = LayoutModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        mod = LayoutModule(use_gpu=False)\n    else:\n        raise","preventionTips":["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."],"tags":["paddleocr","hubserving","layout","gpu","cuda","environment"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}