Skip to content

Incompatible LightningModule.to_onnx type-hint #19726

Closed
@dominicgkerr

Description

@dominicgkerr

Bug description

Hello!

The LightningModule.to_onnx [1] method provides a file_path: Union[str, Path] type-hint. However, file_path is later passed (unmodified) into torch.onnx.export [2] which expects an Union[str, io.BytesIO] file handle (though, I think [3] suggests it should in fact only be a str...)

Personally, I'd like to be able to pass Path instances into LightningModule.to_onnx, which can be achieved by changing [4] to:

torch.onnx.export(self, input_sample, str(file_path), **kwargs)

Alternatively, removing the Path type-hint corrects and possible confusion, and wouldn't (as far as I can see) adversely effect any existing Lightning code...

I'd be very happy to open a small PR (let me know if you'd prefer to cast file_path to a str, or simply drop the Path type-hint)! Many thanks

What version are you seeing the problem on?

master

How to reproduce the bug

from pathlib import Path
from lightning.pytorch.demos.boring_classes import BoringModel

model = BoringModel()

file_path = Path("./model.onnx")
model.to_onnx(file_path)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedOpen to be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions