-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamo-based ONNX Export: Failed to produce a graph during tracing as no tensor operations were found. #123973
Comments
@asfiyab-nvidia try this '''
Install:
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu121
pip3 install onnxscript==0.1.0.dev20240405
pip3 install monai
'''
import torch
from monai.networks.nets import FullyConnectedNet
model = FullyConnectedNet(
10,
3,
[8, 16],
0.15
).to('cuda')
data = torch.randn(4, 10).cuda()
model = torch.export.export(model, args=(data,))
export_output = torch.onnx.dynamo_export(model, data)
export_output.save('fullyconnectednet.onnx') It should work For some reason, when we wrap this model to flatten its output before export, the model becomes not exportable This is the piece that does the wrapping, leading to this issue # torch/onnx/_internal/fx/dynamo_graph_extractor.py
def generate_fx(
self,
options: exporter.ResolvedExportOptions,
model: Union[torch.nn.Module, Callable],
model_args: Sequence[Any],
model_kwargs: Mapping[str, Any],
) -> torch.fx.GraphModule:
# `dynamo.export` does not recognize custom user defined classes as output type.
# Apply wrapper to adapt the outputs back to `dynamo.export` compatible types,
# i.e. :class:`torch.Tensor`.
dynamo_flatten_output_step = DynamoFlattenOutputStep()
wrapped_model = _wrap_model_with_output_adapter(
model, dynamo_flatten_output_step
)
(...) |
Thanks for the comment @thiagocrepaldi . Your suggestion to call Since this is a WAR to enable export, shall I keep the ticket open until the export doesn't require |
Use the workaround, but let's keep this ticket open for triage on dynamo side. I have updated the ticket accordingly |
Also just ran into the same problem by following an introduction guide https://pytorch.org/tutorials/beginner/onnx/export_simple_model_to_onnx_tutorial.html What's Interesting is that it fails if you use build-in modules instead of user defined class import torch, torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
def forward(self, x): return self.conv1(x)
model1 = nn.Sequential(nn.Conv2d(1, 6, 5))
model2 = MyModel()
model2.conv1 = model1[0]
torch_input = torch.randn(1, 1, 32, 32)
assert torch.isclose(model1(torch_input), model2(torch_input)).all()
# works
onnx_program = torch.onnx.dynamo_export(model2, torch_input)
# fails
onnx_program = torch.onnx.dynamo_export(model1, torch_input) |
cc @avikchaudhuri @angelayi on the export side From what I understand, this is expected behavior though undesirable. torch.export via Dynamo does not inline through inbuilt nn modules (like Sequential, Conv2D) etc.
I will let export folks guide on the non-strict mode if necessary. |
🐛 Describe the bug
I'm using Dynamo export on the FullyConnectedNet model by MONAI.
cc @ezyang @chauhang @penguinwu @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4 @msaroufim @bdhirsh @anijain2305 @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @moraxu
I'm using the following script for export:
The export error is below
Versions
Versions of relevant libraries:
[pip3] numpy==1.24.4
[pip3] optree==0.10.0
[pip3] pytorch-quantization==2.1.2
[pip3] pytorch-triton==3.0.0+989adb9a29
[pip3] torch==2.4.0.dev20240412+cu121
[pip3] torch-tensorrt==2.3.0a0
[pip3] torchaudio==2.2.0.dev20240412+cu121
[pip3] torchdata==0.7.1a0
[pip3] torchtext==0.17.0a0
[pip3] torchvision==0.19.0.dev20240412+cu121
[pip3] triton==2.2.0+e28a256
cc @ezyang @msaroufim @bdhirsh @anijain2305 @chauhang
The text was updated successfully, but these errors were encountered: