{"record":{"id":"211cc1c82e7760e2","repo":"opendatalab/MinerU","slug":"ocr-inference-precision-must-be-one-of-auto-fp32","errorCode":null,"errorMessage":"OCR_INFERENCE_PRECISION must be one of: auto, fp32, fp16","messagePattern":"OCR_INFERENCE_PRECISION must be one of: auto, fp32, fp16","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mineru/model/utils/pytorchocr/base_ocr_v20.py","lineNumber":32,"sourceCode":"class BaseOCRV20:\n    def __init__(self, config, **kwargs):\n        self.config = config\n        self.build_net(**kwargs)\n        self.ocr_inference_dtype = torch.float32\n        self.net.eval()\n\n\n    def build_net(self, **kwargs):\n        self.net = BaseModel(self.config, **kwargs)\n\n    def _resolve_inference_dtype(self, device):\n        \"\"\"根据常量和设备类型解析 OCR 网络推理使用的浮点精度。\"\"\"\n        precision = OCR_INFERENCE_PRECISION.lower()\n        device_name = str(device).lower()\n        is_cpu = device_name.startswith(\"cpu\")\n\n        if precision not in {\"auto\", \"fp32\", \"fp16\"}:\n            raise ValueError(\n                \"OCR_INFERENCE_PRECISION must be one of: auto, fp32, fp16\"\n            )\n        if precision == \"fp32\" or is_cpu:\n            return torch.float32\n        return torch.float16\n\n    def _apply_inference_precision(self, device):\n        \"\"\"将 OCR 网络移动到目标设备，并在非 CPU 半精度场景下切到 fp16。\"\"\"\n        self.net.to(device)\n        self.ocr_inference_dtype = self._resolve_inference_dtype(device)\n        if self.ocr_inference_dtype == torch.float16:\n            self.net.to(dtype=torch.float16)\n\n    def _to_inference_dtype(self, tensor):\n        \"\"\"将浮点输入 tensor 转为 OCR 推理精度，整型/布尔辅助输入保持原 dtype。\"\"\"\n        if torch.is_tensor(tensor) and torch.is_floating_point(tensor):\n            inference_dtype = getattr(self, \"ocr_inference_dtype\", torch.float32)\n            return tensor.to(dtype=inference_dtype)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/opendatalab/MinerU/blob/4fe4bde114a23ee5dd637eae99b767f4669bf58c/mineru/model/utils/pytorchocr/base_ocr_v20.py#L14-L50","documentation":"Raised by BaseOCRV20._resolve_inference_dtype when the OCR_INFERENCE_PRECISION constant (typically sourced from an environment variable) lowercases to something other than auto, fp32, or fp16. The value controls whether the OCR network runs float32 on CPU / forced-fp32, or float16 elsewhere.","triggerScenarios":"Setting OCR_INFERENCE_PRECISION to values like 'bf16', 'fp8', 'float16', or 'FP16 ' (with trailing space handled, but e.g. 'fp_16' is not) before running mineru's pytorchocr pipeline.","commonSituations":"Users trying bfloat16 on newer GPUs; typo in .env or CI variables; deployment manifests that set the variable to an unsupported precision name.","solutions":["Set OCR_INFERENCE_PRECISION to one of: auto (fp16 on GPU, fp32 on CPU), fp32, or fp16.","Unset the variable to fall back to the built-in default (auto).","Check for typos like 'float16' vs 'fp16' in your environment/deployment config."],"exampleFix":"# before\nexport OCR_INFERENCE_PRECISION=bf16\n\n# after\nexport OCR_INFERENCE_PRECISION=fp16   # or fp32 / auto","handlingStrategy":"validation","validationCode":"import os\nprecision = os.getenv(\"OCR_INFERENCE_PRECISION\", \"auto\").lower()\nassert precision in {\"auto\", \"fp32\", \"fp16\"}, f\"bad OCR_INFERENCE_PRECISION: {precision!r}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate env vars at process start with a central config checker","Document the three allowed values wherever the variable is referenced","Add allowed-values comments in deployment manifests"],"tags":["ocr","pytorch","precision","environment-variable","validation"],"backgroundTag":null,"analyzedSha":"4fe4bde114a23ee5dd637eae99b767f4669bf58c","analyzedAt":"2026-08-14T21:29:18.456Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}