pytorch - 💡(How to fix) Fix [ONNX] `optimize=True` produces invalid export when reusing `Conv2d`+`BatchNorm2d` block [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#179559Fetched 2026-04-08 03:00:27
View on GitHub
Comments
2
Participants
2
Timeline
25
Reactions
1
Author
Participants
Timeline (top)
mentioned ×7subscribed ×7labeled ×4assigned ×2

Error Message

This yields an error

Code Example

import io
import torch
import torch.nn as nn
import onnxruntime as ort

class MWE(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(1, 16, kernel_size=3, padding=1)
        self.bn   = nn.BatchNorm2d(16)

    def forward(self, x):
        f_1 = self.bn(self.conv(x[:, 0:1]))
        f_2 = self.bn(self.conv(x[:, 1:2]))
        return f_1 + f_2


model       = MWE().eval()
dummy_input = (torch.randn(1, 2, 2, 2),)
dummy_file  = io.BytesIO()

torch.onnx.export(
    model,
    dummy_input,
    dummy_file,
    dynamo=True,
    optimize=True,  # Issue is only with optimize set to `True`
    verify=True,    # Fails to `Execute the model with ONNX Runtime...`
)

dummy_file.seek(0)
# This yields an error
_ = ort.InferenceSession(dummy_file.getvalue())

---

(snip)
[torch.onnx] Obtain model graph for `MWE([...]` with `torch.export.export(..., strict=False)`... 
(snip)
[torch.onnx] Check the ONNX model...[torch.onnx] Execute the model with ONNX Runtime...
[torch.onnx] Execute the model with ONNX Runtime... 
(snip)
InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid model. Node input 'conv.weight_1' is not a graph input, initializer, or output of a previous node.

---

torch==2.11.0
onnx==1.21.0
onnxscript==0.6.2
onnxruntime==1.24.4
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Hi there !

The title says it all, take this MWE for example :

import io
import torch
import torch.nn as nn
import onnxruntime as ort

class MWE(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(1, 16, kernel_size=3, padding=1)
        self.bn   = nn.BatchNorm2d(16)

    def forward(self, x):
        f_1 = self.bn(self.conv(x[:, 0:1]))
        f_2 = self.bn(self.conv(x[:, 1:2]))
        return f_1 + f_2


model       = MWE().eval()
dummy_input = (torch.randn(1, 2, 2, 2),)
dummy_file  = io.BytesIO()

torch.onnx.export(
    model,
    dummy_input,
    dummy_file,
    dynamo=True,
    optimize=True,  # Issue is only with optimize set to `True`
    verify=True,    # Fails to `Execute the model with ONNX Runtime...`
)

dummy_file.seek(0)
# This yields an error
_ = ort.InferenceSession(dummy_file.getvalue())

The above yields ↓

(snip)
[torch.onnx] Obtain model graph for `MWE([...]` with `torch.export.export(..., strict=False)`... ✅
(snip)
[torch.onnx] Check the ONNX model... ✅
[torch.onnx] Execute the model with ONNX Runtime...
[torch.onnx] Execute the model with ONNX Runtime... ❌
(snip)
InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid model. Node input 'conv.weight_1' is not a graph input, initializer, or output of a previous node.

The exported .onnx looks like this :

<img width="1512" height="470" alt="Image" src="https://github.com/user-attachments/assets/0a6e1f59-c294-4c69-abe2-8a4f07f2d878" />

Notes

  • Switching around the layers like self.conv(self.bn(x[:, 0:1])) exports just fine
  • With optimize=False, there seems to be no problem

Versions

<details><summary><code>requirements.txt</code></summary>
torch==2.11.0
onnx==1.21.0
onnxscript==0.6.2
onnxruntime==1.24.4
</details>

cc @justinchuby @titaiwangms

extent analysis

TL;DR

The issue can be resolved by setting optimize=False in the torch.onnx.export function or by reordering the layers in the model to avoid optimization issues.

Guidance

  • The error message indicates that the issue is related to the optimization of the ONNX model, specifically with the conv.weight_1 node.
  • The fact that switching the layers around or setting optimize=False resolves the issue suggests that the problem is with the optimization process.
  • To verify the fix, export the model with optimize=False and check if the exported model can be loaded by ONNX Runtime.
  • To mitigate the issue, consider reordering the layers in the model to avoid optimization issues, or set optimize=False until the optimization issue is resolved.

Example

torch.onnx.export(
    model,
    dummy_input,
    dummy_file,
    dynamo=True,
    optimize=False,  # Set optimize to False to resolve the issue
    verify=True,
)

Notes

  • The issue seems to be specific to the torch==2.11.0 and onnxruntime==1.24.4 versions.
  • The optimization issue may be related to the specific model architecture or the ONNX export process.

Recommendation

Apply workaround: Set optimize=False in the torch.onnx.export function to resolve the issue. This is a safe and temporary solution until the optimization issue is resolved.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [ONNX] `optimize=True` produces invalid export when reusing `Conv2d`+`BatchNorm2d` block [2 comments, 2 participants]