-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
167 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,77 @@ | ||
import sys | ||
sys.path.append("..") | ||
|
||
from model import SimpleNetwork | ||
from data import get_tetris, get_random_graph | ||
import time | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
import torch.functional as F | ||
from tqdm.auto import tqdm | ||
from e3nn import o3 | ||
|
||
torch._dynamo.config.capture_scalar_outputs = True | ||
# Borrowed from https://github.com/pytorch-labs/gpt-fast/blob/db7b273ab86b75358bd3b014f1f022a19aba4797/generate.py#L16-L18 | ||
torch.set_float32_matmul_precision("high") | ||
import torch._dynamo.config | ||
import torch._inductor.config | ||
|
||
torch._inductor.config.coordinate_descent_tuning = True | ||
torch._inductor.config.triton.unique_kernel_names = True | ||
|
||
from model import Model | ||
from data import get_tetris, get_random_graph | ||
|
||
graphs = get_tetris() | ||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
model = SimpleNetwork( | ||
relative_vectors_irreps=o3.Irreps.spherical_harmonics(lmax=2), | ||
node_features_irreps=o3.Irreps("16x0e"), | ||
) | ||
def train(steps=200): | ||
model = Model() | ||
model = model.to(device) | ||
opt = optim.Adam(model.parameters(), lr=0.01) | ||
|
||
optimizer = torch.optim.Adam(model.parameters()) | ||
def loss_fn(model_output, graphs): | ||
logits = model_output | ||
labels = graphs.y # [num_graphs] | ||
loss = F.cross_entropy(logits, labels) | ||
loss = torch.mean(loss) | ||
return loss, logits | ||
|
||
def loss_fn(graphs): | ||
logits = model(graphs.numbers, | ||
graphs.pos, | ||
graphs.edge_index, | ||
graphs.num_nodes) | ||
labels = graphs.y.unsqueeze(-1).float() # [num_graphs] | ||
loss = torch.nn.functional.cross_entropy(logits, labels) | ||
return loss, logits | ||
def update_fn(model, opt, graphs): | ||
model_output = model(graphs) | ||
loss, logits = loss_fn(model_output, graphs) | ||
|
||
opt.zero_grad(set_to_none=True) | ||
loss.backward() | ||
opt.step() | ||
|
||
labels = graphs.y | ||
preds = torch.argmax(logits, dim=1) | ||
accuracy = (preds == labels).float().mean() | ||
|
||
def apply_random_rotation(graphs): | ||
"""Apply a random rotation to the nodes of the graph.""" | ||
alpha, beta, gamma = torch.rand(3) * 2 * torch.pi - torch.pi | ||
return loss.item(), accuracy.item() | ||
|
||
rotated_pos = o3.angles_to_matrix(alpha, beta, gamma) @ graphs.pos.T | ||
rotated_pos = rotated_pos.T | ||
# Compile the update function | ||
update_fn_compiled = torch.compile(update_fn, mode="reduce-overhead") | ||
|
||
rotated_graphs = graphs.clone() | ||
rotated_graphs.pos = rotated_pos | ||
return rotated_graphs | ||
# Dataset | ||
graphs = get_tetris() | ||
graphs = graphs.to(device=device) | ||
|
||
model.train() | ||
for _ in range(10): | ||
|
||
graphs = apply_random_rotation(graphs) | ||
optimizer.zero_grad() | ||
loss, logits = loss_fn(graphs) | ||
loss.backward() | ||
optimizer.step() | ||
# compile jit | ||
wall = time.perf_counter() | ||
print("compiling...", flush=True) | ||
# Warmup runs | ||
for i in range(3): | ||
loss, accuracy = update_fn_compiled(model, opt, graphs) | ||
print(f"initial accuracy = {100 * accuracy:.0f}%", flush=True) | ||
print(f"compilation took {time.perf_counter() - wall:.1f}s") | ||
|
||
preds = torch.argmax(logits, dim=1) | ||
accuracy = (preds == graphs.y.squeeze()).float().mean() | ||
# Train | ||
wall = time.perf_counter() | ||
print("training...", flush=True) | ||
for _ in tqdm(range(steps)): | ||
loss, accuracy = update_fn_compiled(model, opt, graphs) | ||
|
||
# es = torch.export.export(model, | ||
# (graphs.numbers, | ||
# graphs.pos, | ||
# graphs.edge_index, | ||
# graphs.num_nodes)) | ||
# print(es) | ||
if accuracy == 1.0: | ||
break | ||
|
||
print(f"final accuracy = {100 * accuracy:.0f}%") | ||
print(f"training took {time.perf_counter() - wall:.1f}s") | ||
|
||
if __name__ == "__main__": | ||
train() |