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

[Relax][PyTorch] Add support for torch.export.ExportedProgram in Relax PyTorch Frontend #17396

Merged
merged 2 commits into from
Sep 27, 2024

Conversation

mshr-h
Copy link
Contributor

@mshr-h mshr-h commented Sep 20, 2024

Part of #17346
To keep the PR small, this implementation only supports the Core ATen operators used in AlexNet. I'll add more support after the PR is merged.

I ran the AlexNet model from Torchvision and confirmed that the inference outputs from PyTorch and Relax match.
Here's a repro.

test_alexnet.py
import torch
from torch.export import export
from tvm.contrib.download import download_testdata
from torchvision.models import get_model, get_model_weights
from torchvision.io import read_image
import tvm
from tvm import relax
import tvm.testing
from tvm.relax.frontend.torch import from_exported_program

# prepare sample image
img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"
img_name = "cat.png"
img_path = download_testdata(img_url, img_name, module="data")
image_tensor = read_image(img_path)

# load model from torchvision
model_name = "alexnet"
torch_model = get_model(model_name, weights="DEFAULT").eval()
weights = get_model_weights(model_name).DEFAULT
transforms = weights.transforms()

batch = transforms(image_tensor).unsqueeze(0)
example_args = (batch,)

# PyTorch
exported_program = export(torch_model, args=example_args)
expected: torch.Tensor = exported_program.module()(*example_args)

# Relax
target = "llvm"
dev = tvm.cpu()
mod = from_exportedprogram(exported_program)
mod = tvm.relax.transform.DecomposeOpsForInference()(mod)
exe = relax.build(mod, target=target)
vm = relax.VirtualMachine(exe, dev)
tvm_args = [tvm.nd.from_dlpack(x.contiguous()) for x in example_args]
tvm_output = vm["main"](*tvm_args)[0]
actual = torch.from_numpy(tvm_output.numpy())

# see if the outputs match
print(f"allclose: {torch.allclose(actual, expected, rtol=1e-5, atol=1e-5, equal_nan=True)}")

@mshr-h mshr-h changed the title [Relax][PyTorch] Add support for `torch.export.ExportedProgram in Relax PyTorch Frontend [Relax][PyTorch] Add support for torch.export.ExportedProgram in Relax PyTorch Frontend Sep 20, 2024
@mshr-h mshr-h force-pushed the relax-exportedprogram-6 branch 6 times, most recently from 5d1261d to 3d14562 Compare September 20, 2024 12:23
@mshr-h
Copy link
Contributor Author

mshr-h commented Sep 20, 2024

It seems like the CI is using the old docker image tlcpack/ci-cpu:20240105-165030-51bdaec6, which contains PyTorch 2.0. But the torch.export is introduced in PyTorch 2.1 so we need to upgrade it.
The dockerfile is already updated in #17338 so I guess all we have to do is to upload the new docker image to tlcpack/ci-cpu and update the image name in the jenkins file at ci/jenkins/unity_jenkinsfile.groovy.
cc @yongwww @tqchen

@tqchen
Copy link
Member

tqchen commented Sep 20, 2024

we need to update https://github.com/apache/tvm/blob/main/ci/jenkins/docker-images.ini

@mshr-h mshr-h force-pushed the relax-exportedprogram-6 branch from 3d14562 to 9338734 Compare September 23, 2024 12:50
@mshr-h
Copy link
Contributor Author

mshr-h commented Sep 23, 2024

Looks like it's still using old docker image.
https://ci.tlcpack.ai/blue/organizations/jenkins/tvm-unity/detail/PR-17396/8/pipeline
cc @tqchen

@tqchen
Copy link
Member

tqchen commented Sep 23, 2024

Ah, https://github.com/apache/tvm/blob/main/ci/jenkins/unity_jenkinsfile.groovy#L35 needs to be manually updated for this file

@mshr-h mshr-h force-pushed the relax-exportedprogram-6 branch from 9338734 to 6cdfbab Compare September 25, 2024 14:32
@mshr-h
Copy link
Contributor Author

mshr-h commented Sep 26, 2024

@tvm-bot rerun

@mshr-h mshr-h marked this pull request as ready for review September 26, 2024 11:42
@mshr-h
Copy link
Contributor Author

mshr-h commented Sep 26, 2024

Can you take a look at it? Thanks!
@yongwww @tqchen

@tqchen
Copy link
Member

tqchen commented Sep 26, 2024

overall LGTM, just a minor namimg nit. Thanks @mshr-h !

@mshr-h mshr-h merged commit 3f2c91a into apache:main Sep 27, 2024
20 checks passed
@mshr-h mshr-h deleted the relax-exportedprogram-6 branch September 27, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants