Skip to content

Allow to return ONNXProgram when calling to_onnx(dynamo=True) #20810

Open
@GdoongMathew

Description

@GdoongMathew
Contributor

Description & Motivation

Since torch 2.7.0, they started to unify the onnx export logic, thus when calling

torch.onnx.export(dynamo=True)

torch would start to return the ONNXProgram object. (pytorch/pytorch#137296)

The LightningModule should also have the same behavior.

Solution

class LightningModule:
    @torch.no_grad()
    def to_onnx(
        self,
        file_path: Union[str, Path, BytesIO, None] = None,
        input_sample: Optional[Any] = None,
        **kwargs: Any,
    ) -> Union["ONNXProgram", None]:
        if kwargs.get("dynamo", False) and not _ONNXSCRIPT_AVAILABLE:
            raise ModuleNotFoundError(...)
        ...
        ret = torch.onnx.export(self, input_sample, file_path, **kwargs)
        self.train(mode)
        return ret

Pitch

No response

Alternatives

No response

Additional context

No response

cc @lantiga @Borda

Activity

added
featureIs an improvement or enhancement
needs triageWaiting to be triaged by maintainers
on May 11, 2025
linked a pull request that will close this issue on May 11, 2025
GdoongMathew

GdoongMathew commented on Jun 25, 2025

@GdoongMathew
ContributorAuthor

Related torch issue

pytorch/pytorch#151693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIs an improvement or enhancementneeds triageWaiting to be triaged by maintainers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @GdoongMathew

      Issue actions

        Allow to return `ONNXProgram` when calling `to_onnx(dynamo=True)` · Issue #20810 · Lightning-AI/pytorch-lightning