Skip to content
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

Saving models with ONNX. #323

Open
Joao-L-S-Almeida opened this issue Dec 11, 2024 · 2 comments
Open

Saving models with ONNX. #323

Joao-L-S-Almeida opened this issue Dec 11, 2024 · 2 comments
Assignees
Labels

Comments

@Joao-L-S-Almeida
Copy link
Member

Is something interesting for TerraTorch to have a way to export models to ONNX format ?

@Joao-L-S-Almeida Joao-L-S-Almeida self-assigned this Dec 11, 2024
@rbavery
Copy link

rbavery commented Dec 16, 2024

Yes! ONNX or Pytorch ExportedPrograms are more portable than distributing the model with weights files and code, no need to install a complex project to use the model, just onnx runtime or torch. And in the export/compile process you can get speedups and reduced gpu memory during inference.

Between ONNX and ExportedProgram, I think ExportedProgram might be better to start with. I think the speedups might be more significant. and ExportedPrograms have the simplest dependencies, just Pytorch to load and run the model.

https://pytorch.org/docs/stable/export.html#an-example

Here is a code example for exporting the sam2 encoder as an ExportedProgram. Based on this func from Clay which has an example for a different model signature.

from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor

from torch.export import Dim
import torch

sam2_checkpoint = "./checkpoints/sam2.1_hiera_large.pt"
model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
device="cuda"

sam2_model = build_sam2(model_cfg, sam2_checkpoint, device=device)

predictor = SAM2ImagePredictor(sam2_model)


def export_to_torchep(model, name, img_size=1024):
    "Save the model to pytorch ExportedProgram format."

    dummy_batch =  torch.randn(5, 3, img_size, img_size).to("cuda").type(torch.bfloat16)

    # dynamic shapes for model export
    batch_size = Dim("batch", min=2, max=20)
    #height = Dim("height", min=2, max=2048)
    #width = Dim("width", min=2, max=2048)
    dynamic_shapes = {
        "sample": {0: batch_size},
    }

    # Export the model to pytorch ExportedProgram format
    with torch.no_grad():
        ep = torch.export.export(
            model.eval(),
            (dummy_batch,),
            dynamic_shapes=dynamic_shapes,
            strict=False,
        )

    # Save the exported model
    torch.export.save(ep, f"checkpoints/compiled/{name}")
    print(
        f"Model exported to pytorch ExportedProgram format: checkpoints/compiled/{name}"  # noqa: E501
    )

    return ep

export_to_torchep(predictor.model.image_encoder.bfloat16(), "sam2.1_hiera_large_exported")

@Joao-L-S-Almeida
Copy link
Member Author

Hi, @rbavery Thank you for the feedback and for the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants