{"library":"onnx2torch","title":"ONNX to PyTorch Converter","description":"onnx2torch is a Python library designed to convert ONNX (Open Neural Network Exchange) models into PyTorch models. It enables users to leverage existing ONNX models within the PyTorch ecosystem, facilitating migration and interoperability between frameworks. The current stable version is 1.6.0, with an active release cadence, typically every few months.","language":"python","status":"active","last_verified":"Sun May 17","install":{"commands":["pip install onnx2torch"],"cli":null},"imports":["from onnx2torch import convert"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import torch\nfrom onnx2torch import convert\nimport os\n\n# Define paths for dummy model\nonnx_model_path = 'dummy_model.onnx'\n\ntry:\n    # 1. Create a dummy PyTorch model and export it to ONNX\n    class SimpleModel(torch.nn.Module):\n        def __init__(self):\n            super().__init__()\n            self.linear = torch.nn.Linear(10, 2)\n\n        def forward(self, x):\n            return self.linear(x)\n\n    dummy_torch_model = SimpleModel()\n    dummy_input = torch.randn(1, 10) # Batch size 1, 10 features\n\n    torch.onnx.export(dummy_torch_model, dummy_input, onnx_model_path, \n                       opset_version=11, input_names=['input'], output_names=['output'],\n                       do_constant_folding=True, # Recommended for stable ONNX\n                       dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}})\n    print(f\"Dummy ONNX model saved to {onnx_model_path}\")\n\n    # 2. Convert the ONNX model to a PyTorch model using onnx2torch\n    torch_model_converted = convert(onnx_model_path)\n    print(\"ONNX model converted to PyTorch successfully.\")\n\n    # 3. Perform inference with the converted model\n    inference_input = torch.randn(2, 10) # Example: batch size 2\n    torch_model_converted.eval() # Set to evaluation mode\n    with torch.no_grad():\n        output = torch_model_converted(inference_input)\n    \n    print(f\"Input shape for inference: {inference_input.shape}\")\n    print(f\"Output shape from converted model: {output.shape}\")\n\n    # (Optional) Verify output matches original model if possible\n    # original_output = dummy_torch_model(inference_input)\n    # print(f\"Original model output shape: {original_output.shape}\")\n    # assert torch.allclose(output, original_output, atol=1e-5), \"Outputs do not match!\"\n    # print(\"Outputs of original and converted model match (within tolerance).\")\n\nfinally:\n    # Clean up the dummy ONNX file\n    if os.path.exists(onnx_model_path):\n        os.remove(onnx_model_path)\n        print(f\"Cleaned up {onnx_model_path}\")\n","lang":"python","description":"This quickstart demonstrates the full cycle of using `onnx2torch`. It starts by creating a dummy PyTorch model, exporting it to an ONNX file, converting that ONNX file back into a PyTorch model using `onnx2torch.convert`, and finally running inference with the newly converted model to show its usage.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-17","installed_version":"1.5.15","pypi_latest":"1.5.15","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":40,"avg_install_s":73.6,"avg_import_s":13.13,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"onnx2torch","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":81.6,"import_time_s":10.06,"mem_mb":128.4,"disk_size":"4.8G"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"onnx2torch","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":75.2,"import_time_s":14.4,"mem_mb":144.1,"disk_size":"4.9G"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"onnx2torch","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":71.1,"import_time_s":15.61,"mem_mb":139.9,"disk_size":"4.9G"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"onnx2torch","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":66.6,"import_time_s":12.44,"mem_mb":140.8,"disk_size":"4.9G"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"onnx2torch","exit_code":1,"wheel_type":null,"failure_reason":"timeout","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]}}