{"record":{"id":"f12092af1205a199","repo":"ultralytics/yolov5","slug":"failed-to-load-onnx-file-onnx","errorCode":null,"errorMessage":"failed to load ONNX file: {onnx}","messagePattern":"failed to load ONNX file: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"export.py","lineNumber":666,"sourceCode":"    if verbose:\n        logger.min_severity = trt.Logger.Severity.VERBOSE\n\n    builder = trt.Builder(logger)\n    config = builder.create_builder_config()\n    if is_trt10:\n        config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, workspace << 30)\n    else:  # TensorRT versions 7, 8\n        config.max_workspace_size = workspace * 1 << 30\n    if cache:  # enable timing cache\n        Path(cache).parent.mkdir(parents=True, exist_ok=True)\n        buf = Path(cache).read_bytes() if Path(cache).exists() else b\"\"\n        timing_cache = config.create_timing_cache(buf)\n        config.set_timing_cache(timing_cache, ignore_mismatch=True)\n    flag = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)\n    network = builder.create_network(flag)\n    parser = trt.OnnxParser(network, logger)\n    if not parser.parse_from_file(str(onnx)):\n        raise RuntimeError(f\"failed to load ONNX file: {onnx}\")\n\n    inputs = [network.get_input(i) for i in range(network.num_inputs)]\n    outputs = [network.get_output(i) for i in range(network.num_outputs)]\n    for inp in inputs:\n        LOGGER.info(f'{prefix} input \"{inp.name}\" with shape{inp.shape} {inp.dtype}')\n    for out in outputs:\n        LOGGER.info(f'{prefix} output \"{out.name}\" with shape{out.shape} {out.dtype}')\n\n    if dynamic:\n        if im.shape[0] <= 1:\n            LOGGER.warning(f\"{prefix} --dynamic model requires maximum --batch-size argument\")\n        profile = builder.create_optimization_profile()\n        for inp in inputs:\n            profile.set_shape(inp.name, (1, *im.shape[1:]), (max(1, im.shape[0] // 2), *im.shape[1:]), im.shape)\n        config.add_optimization_profile(profile)\n\n    LOGGER.info(f\"{prefix} building FP{16 if builder.platform_has_fast_fp16 and half else 32} engine as {f}\")\n    if builder.platform_has_fast_fp16 and half:","sourceCodeStart":648,"sourceCodeEnd":684,"githubUrl":"https://github.com/ultralytics/yolov5/blob/20d1d78a08277e365d57bfa3a2cce752772d9e59/export.py#L648-L684","documentation":"export.py's TensorRT builder raises RuntimeError when trt.OnnxParser.parse_from_file() returns False, meaning the TensorRT parser could not digest the ONNX file. This happens inside export_engine after the network is created, so TensorRT itself is installed and working; the ONNX graph is the problem (corrupt file, unsupported op, or opset/IR version too new for the installed TensorRT).","triggerScenarios":"Running export.py --include engine against an ONNX file produced by a newer onnx/onnxruntime than the installed TensorRT supports; a truncated/corrupt .onnx from an interrupted export; custom layers or ops not supported by trt.OnnxParser; pointing --weights at a hand-edited ONNX file.","commonSituations":"Mixed toolchain versions (torch 2.x + onnx opset 17 with TensorRT 7.x); reusing an old .onnx after upgrading TensorRT; CI runners where the ONNX export step partially failed but left a file; concatenating exports across machines.","solutions":["Regenerate the ONNX from scratch in the same run so versions match: python export.py --weights yolov5s.pt --include onnx engine.","Validate the file first: python -c \"import onnx; m=onnx.load('yolov5s.onnx'); onnx.checker.check_model(m)\".","Downgrade the ONNX opset to one your TensorRT supports (export.py --opset 12) or upgrade TensorRT.","If the file is corrupt, delete the .onnx and re-export; never hand-copy partial files."],"exampleFix":"# before\npython export.py --weights yolov5s.pt --include engine  # reuses stale/corrupt yolov5s.onnx\n\n# after\nrm yolov5s.onnx && python export.py --weights yolov5s.pt --include onnx engine --opset 12","handlingStrategy":"validation","validationCode":"import onnx\n\ndef onnx_is_parsable(path: str) -> bool:\n    model = onnx.load(path)  # raises if corrupt\n    onnx.checker.check_model(model)\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    export_engine(...)  # or run export.py --include engine\nexcept RuntimeError as e:\n    if 'failed to load ONNX' in str(e):\n        onnx.checker.check_model(onnx.load(onnx_path))  # diagnose\n        re_export_onnx()  # regenerate and retry once","preventionTips":["Always export ONNX and engine in the same export.py invocation.","Pin onnx/opset versions to what your TensorRT supports.","Checksum exported artifacts when copying them between machines."],"tags":["export","tensorrt","onnx","version-mismatch"],"backgroundTag":null,"analyzedSha":"20d1d78a08277e365d57bfa3a2cce752772d9e59","analyzedAt":"2026-08-15T02:56:15.443Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}