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

[WIP] Adding torch.export #6

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ benchmark_c: clebsch_gordan.o e3nn.o benchmark_c.o tp.o

.PHONY: benchmark
benchmark: benchmark_c
mkdir -p extra/build
rm benchmark.txt
./benchmark_c >> benchmark.txt
python extra/benchmark_python.py >> benchmark.txt
python extra/plot_benchmark.py
rm extra/build/*

# -fPIC makes the code considerably slower, but is needed to call the c code
# from python; for this reason compiling separately
Expand Down
23 changes: 23 additions & 0 deletions extra/benchmark_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ def run_e3nn_torch2(lmax):
tp = torch.compile(tp, mode="reduce-overhead", fullgraph=True)

print(f"{lmax},", benchmark(tp, input1, input2))

def run_e3nn_torch2_export(lmax):
import torch
import e3nn as e3nn_torch

import torch._inductor.config as config
config.cpp_wrapper = True
# torch.set_num_threads(1) # note this actually makes it faster
with torch.no_grad():
irreps1 = e3nn_torch.o3.Irreps.spherical_harmonics(lmax)
irreps2 = (channels * irreps1).sort().irreps.simplify()

input1 = irreps1.randn(-1) * 0
input2 = irreps2.randn(-1) * 0
tp = e3nn_torch.o3.experimental.FullTensorProductv2(irreps1, irreps2)
so_path = torch._export.aot_compile(
tp,
(input1, input2,),
options={"aot_inductor.output_path": os.path.join(os.getcwd(), f"extra/build/model_{lmax}.so")}
)
tp_compiled = torch._export.aot_load(os.path.join(os.getcwd(), f"extra/build/model_{lmax}.so"), device='cpu')
print(f"{lmax},", benchmark(tp_compiled, input1, input2))

def run_e3nn_torch2_ipex(lmax):
import torch
Expand All @@ -98,6 +120,7 @@ def main():
(run_e3nn_jax, "e3nn-jax"),
(run_e3nn_torch, "e3nn-torch"),
(run_e3nn_torch2, "e3nn-torch2"),
(run_e3nn_torch2_export, "e3nn-torch2-export"),
# (run_e3nn_torch2_ipex, "e3nn-torch2-ipex"),
]:
print(name)
Expand Down
2 changes: 1 addition & 1 deletion extra/plot_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
plt.close(fig)

fig = plt.figure(figsize=(6,3))
for name in ["e3nn.c v3", "e3nn-jax", "e3nn-torch", "e3nn-torch2"]: # , "e3nn-torch2-ipex"]:
for name in ["e3nn.c v3", "e3nn-jax", "e3nn-torch", "e3nn-torch2", "e3nn-torch2-export"]: # , "e3nn-torch2-ipex"]:
label = "e3nn.c" if "e3nn.c" in name else name
plt.plot(data[name]["lmax"], data[name]["time"], label=label)
plt.yscale("log")
Expand Down