Skip to content

Commit

Permalink
Update and add log argument in timeit
Browse files Browse the repository at this point in the history
  • Loading branch information
yanbing-j committed Aug 24, 2022
1 parent 4fef86a commit 696a89a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions benchmark/inference/inference_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

supported_sets = {
'ogbn-mag': ['rgat', 'rgcn'],
'ogbn-products': ['edge_cnn', 'gat', 'gcn', 'pna', 'graph_sage'],
'Reddit': ['edge_cnn', 'gat', 'gcn', 'pna', 'graph_sage'],
'ogbn-products': ['edge_cnn', 'gat', 'gcn', 'pna', 'sage'],
'Reddit': ['edge_cnn', 'gat', 'gcn', 'pna', 'sage'],
}


Expand Down
2 changes: 1 addition & 1 deletion benchmark/inference/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'gat': GAT,
'gcn': GCN,
'pna': PNA,
'graph_sage': GraphSAGE,
'sage': GraphSAGE,
'rgat': HeteroGAT,
'rgcn': HeteroGraphSAGE,
}
Expand Down
12 changes: 6 additions & 6 deletions benchmark/kernel/main_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def run_train():
dataset_name)

for num_layers, hidden in product(layers, hiddens):
print("--\n{} - {} - {} - {}".format(dataset_name, Net.__name__,
num_layers, hidden))
print(f'--\n{dataset_name} - {Net.__name__}'
'- {num_layers} - {hidden}')

model = Net(dataset, num_layers, hidden).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
Expand All @@ -80,10 +80,10 @@ def run_train():
acc_list = []
for epoch in range(1, args.epochs + 1):
loss, stats = train(model, optimizer, train_loader)
with timeit() as t:
with timeit(log=False) as t:
val_acc = eval_acc(model, val_loader)
val_time = t.duration
with timeit() as t:
with timeit(log=False) as t:
test_acc = eval_acc(model, test_loader)
test_time = t.duration

Expand All @@ -101,8 +101,8 @@ def run_inference():
dataset, _, _, test_loader = prepare_dataloader(dataset_name)

for num_layers, hidden in product(layers, hiddens):
print("--\n{} - {} - {} - {}".format(dataset_name, Net.__name__,
num_layers, hidden))
print(f'--\n{dataset_name} - {Net.__name__}'
'- {num_layers} - {hidden}')

model = Net(dataset, num_layers, hidden).to(device)

Expand Down
12 changes: 8 additions & 4 deletions torch_geometric/profile/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def wrapper(*args, **kwargs) -> Tuple[Any, Stats]:


class timeit(ContextDecorator):
r"""A ContextDecorator to facilitate timing a function, *e.g.*, obtaining
r"""A context decorator to facilitate timing a function, *e.g.*, obtaining
the runtime of a specific model on a specific dataset.
.. code-block:: python
Expand All @@ -105,10 +105,13 @@ class timeit(ContextDecorator):
def test(model, x, edge_index):
return model(x, edge_index)
with timeit() as timeit:
with timeit() as t:
z = test(model, x, edge_index)
time = timeit.duration
time = t.duration
"""
def __init__(self, log: bool = True):
self.log = log

def __enter__(self):
if torch.cuda.is_available():
torch.cuda.synchronize()
Expand All @@ -120,7 +123,8 @@ def __exit__(self, *args):
torch.cuda.synchronize()
self.t_end = time.time()
self.duration = self.t_end - self.t_start
print(f'Time: {self.duration:.8f}s', flush=True)
if self.log:
print(f'Time: {self.duration:.8f}s', flush=True)


def get_stats_summary(stats_list: List[Stats]):
Expand Down

0 comments on commit 696a89a

Please sign in to comment.