Skip to content

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

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

Open
GdoongMathew opened this issue May 11, 2025 · 0 comments · May be fixed by #20811
Open

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

GdoongMathew opened this issue May 11, 2025 · 0 comments · May be fixed by #20811
Labels
feature Is an improvement or enhancement needs triage Waiting to be triaged by maintainers

Comments

@GdoongMathew
Copy link
Contributor

GdoongMathew commented May 11, 2025

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

@GdoongMathew GdoongMathew added feature Is an improvement or enhancement needs triage Waiting to be triaged by maintainers labels May 11, 2025
@GdoongMathew GdoongMathew linked a pull request May 11, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement needs triage Waiting to be triaged by maintainers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant