Skip to content

[ONNX][dynamo] Parameter to export flat graphs #111495

@asfiyab-nvidia

Description

@asfiyab-nvidia

🚀 The feature, motivation and pitch

I'm exporting a model with a single linear layer to ONNX. Each layer of the generated graph is an ONNX function with an underlying function body composed of other functions and operators. The feature I'm requesting is a configurable option to generate a flat graph with no ONNX functions.

The motivation for this request is to enable optimizations like constant folding. With function nodes, important information is not passed down to the function body resulting in fewer optimizations than possible with a flat graph. Additionally, the TorchScript-based ONNX export does provide an argument to export modules as functions. It would be beneficial for users if the TorchDynamo-based ONNX exporter had a similar feature.

Code to reproduce:

import torch

class LinearModel(torch.nn.Module):
  def __init__(self):
      super().__init__()
      self.fc0 = torch.nn.Linear(5, 7)

  def forward(self, tensor_x: torch.Tensor):
      output = self.fc0(tensor_x)
      return output

def linearDataloader():
    yield torch.randn(3, 5).cuda()

# Get model and input
model = LinearModel()
data = next(linearDataloader())

# ONNX Export
export_output = torch.onnx.dynamo_export(
    model.eval().to('cuda'),
    data
)
export_output.save('linear_dynamo.onnx')

Alternatives

No response

Additional context

No response

Metadata

Metadata

Labels

module: onnxRelated to torch.onnxtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions