Skip to content

Commit 8cce352

Browse files
committed
exporter
1 parent c6af6c4 commit 8cce352

8 files changed

Lines changed: 1133 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from __future__ import annotations
2+
3+
import importlib.util
4+
from pathlib import Path
5+
6+
import torch
7+
8+
from transformer_nuggets.export_autograd_triton import Specialization, export_autograd_triton
9+
10+
11+
def affine_relu(x, w):
12+
return torch.relu(x @ w)
13+
14+
15+
def import_generated(path: Path):
16+
spec = importlib.util.spec_from_file_location(path.stem, path)
17+
module = importlib.util.module_from_spec(spec)
18+
assert spec.loader is not None
19+
spec.loader.exec_module(module)
20+
return module
21+
22+
23+
def main():
24+
if not torch.cuda.is_available():
25+
raise RuntimeError("CUDA is required for this example")
26+
27+
x = torch.randn(4, 8, device="cuda", requires_grad=True)
28+
w = torch.randn(8, 3, device="cuda", requires_grad=True)
29+
output_path = Path("generated_affine_relu.py")
30+
31+
export_autograd_triton(
32+
affine_relu,
33+
[Specialization(args=(x, w), name="static_4x8_8x3")],
34+
output_path,
35+
)
36+
generated = import_generated(output_path)
37+
y = generated.affine_relu_compiled(x, w)
38+
y.sum().backward()
39+
print(f"wrote {output_path}")
40+
print(y)
41+
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)