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

😶‍🌫️ Create CSRGraph Abstraction #112

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
150 changes: 87 additions & 63 deletions benchmarking/gat/seastar/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import torch
import torch.nn.functional as F
import pynvml
from stgraph.graph.static.StaticGraph import StaticGraph
from stgraph.graph.static.StaticGraph_old import StaticGraph
from stgraph.dataset.CoraDataLoader import CoraDataLoader
from utils import EarlyStopping, accuracy
import snoop
Expand All @@ -25,8 +25,8 @@ def train(args):
cora = CoraDataLoader(verbose=True)

# To account for the initial CUDA Context object for pynvml
tmp = StaticGraph([(0,0)], [1], 1)
tmp = StaticGraph([(0, 0)], [1], 1)

features = torch.FloatTensor(cora.get_all_features())
labels = torch.LongTensor(cora.get_all_targets())
train_mask = cora.get_train_mask()
Expand All @@ -49,15 +49,15 @@ def train(args):

assert train_mask.shape[0] == num_nodes

print('dataset {}'.format("Cora"))
print('# of edges : {}'.format(num_edges))
print('# of nodes : {}'.format(num_nodes))
print('# of features : {}'.format(num_feats))
print("dataset {}".format("Cora"))
print("# of edges : {}".format(num_edges))
print("# of nodes : {}".format(num_nodes))
print("# of features : {}".format(num_feats))

features = torch.FloatTensor(features)
labels = torch.LongTensor(labels)

if hasattr(torch, 'BoolTensor'):
if hasattr(torch, "BoolTensor"):
train_mask = torch.BoolTensor(train_mask)

else:
Expand All @@ -74,17 +74,19 @@ def train(args):

# create model
heads = ([args.num_heads] * args.num_layers) + [args.num_out_heads]
model = GAT(g,
args.num_layers,
num_feats,
args.num_hidden,
n_classes,
heads,
F.elu,
args.in_drop,
args.attn_drop,
args.negative_slope,
args.residual)
model = GAT(
g,
args.num_layers,
num_feats,
args.num_hidden,
n_classes,
heads,
F.elu,
args.in_drop,
args.attn_drop,
args.negative_slope,
args.residual,
)
print(model)
if args.early_stop:
stopper = EarlyStopping(patience=100)
Expand All @@ -94,7 +96,8 @@ def train(args):

# use optimizer
optimizer = torch.optim.Adam(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)

# initialize graph
dur = []
Expand All @@ -103,8 +106,8 @@ def train(args):
Used_memory = 0

for epoch in range(args.num_epochs):
#print('epoch = ', epoch)
#print('mem0 = {}'.format(mem0))
# print('epoch = ', epoch)
# print('mem0 = {}'.format(mem0))
torch.cuda.synchronize()
tf = time.time()
model.train()
Expand All @@ -120,7 +123,7 @@ def train(args):
torch.cuda.synchronize()
loss.backward()
optimizer.step()
t2 =time.time()
t2 = time.time()
run_time_this_epoch = t2 - tf

if epoch >= 3:
Expand All @@ -131,56 +134,77 @@ def train(args):

train_acc = accuracy(logits[train_mask], labels[train_mask])

#log for each step
print('Epoch {:05d} | Time(s) {:.4f} | train_acc {:.6f} | Used_Memory {:.6f} mb'.format(
epoch, run_time_this_epoch, train_acc, (now_mem * 1.0 / (1024**2))
))
# log for each step
print(
"Epoch {:05d} | Time(s) {:.4f} | train_acc {:.6f} | Used_Memory {:.6f} mb".format(
epoch, run_time_this_epoch, train_acc, (now_mem * 1.0 / (1024**2))
)
)

if args.early_stop:
model.load_state_dict(torch.load('es_checkpoint.pt'))
model.load_state_dict(torch.load("es_checkpoint.pt"))

#OUTPUT we need
avg_run_time = avg_run_time *1. / record_time
Used_memory /= (1024**3)
print('^^^{:6f}^^^{:6f}'.format(Used_memory, avg_run_time))
# OUTPUT we need
avg_run_time = avg_run_time * 1.0 / record_time
Used_memory /= 1024**3
print("^^^{:6f}^^^{:6f}".format(Used_memory, avg_run_time))

if __name__ == '__main__':

parser = argparse.ArgumentParser(description='GAT')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="GAT")

# COMMENT IF SNOOP IS TO BE ENABLED
snoop.install(enabled=False)

parser.add_argument("--gpu", type=int, default=0,
help="which GPU to use. Set -1 to use CPU.")
parser.add_argument("--num_epochs", type=int, default=200,
help="number of training epochs")
parser.add_argument("--num_heads", type=int, default=8,
help="number of hidden attention heads")
parser.add_argument("--num_out_heads", type=int, default=1,
help="number of output attention heads")
parser.add_argument("--num_layers", type=int, default=1,
help="number of hidden layers")
parser.add_argument("--num_hidden", type=int, default=32,
help="number of hidden units")
parser.add_argument("--residual", action="store_true", default=False,
help="use residual connection")
parser.add_argument("--in_drop", type=float, default=.6,
help="input feature dropout")
parser.add_argument("--attn_drop", type=float, default=.6,
help="attention dropout")
parser.add_argument("--lr", type=float, default=0.005,
help="learning rate")
parser.add_argument('--weight_decay', type=float, default=5e-4,
help="weight decay")
parser.add_argument('--negative_slope', type=float, default=0.2,
help="the negative slope of leaky relu")
parser.add_argument('--early_stop', action='store_true', default=False,
help="indicates whether to use early stop or not")
parser.add_argument('--fastmode', action="store_true", default=False,
help="skip re-evaluate the validation set")
parser.add_argument(
"--gpu", type=int, default=0, help="which GPU to use. Set -1 to use CPU."
)
parser.add_argument(
"--num_epochs", type=int, default=200, help="number of training epochs"
)
parser.add_argument(
"--num_heads", type=int, default=8, help="number of hidden attention heads"
)
parser.add_argument(
"--num_out_heads", type=int, default=1, help="number of output attention heads"
)
parser.add_argument(
"--num_layers", type=int, default=1, help="number of hidden layers"
)
parser.add_argument(
"--num_hidden", type=int, default=32, help="number of hidden units"
)
parser.add_argument(
"--residual", action="store_true", default=False, help="use residual connection"
)
parser.add_argument(
"--in_drop", type=float, default=0.6, help="input feature dropout"
)
parser.add_argument(
"--attn_drop", type=float, default=0.6, help="attention dropout"
)
parser.add_argument("--lr", type=float, default=0.005, help="learning rate")
parser.add_argument("--weight_decay", type=float, default=5e-4, help="weight decay")
parser.add_argument(
"--negative_slope",
type=float,
default=0.2,
help="the negative slope of leaky relu",
)
parser.add_argument(
"--early_stop",
action="store_true",
default=False,
help="indicates whether to use early stop or not",
)
parser.add_argument(
"--fastmode",
action="store_true",
default=False,
help="skip re-evaluate the validation set",
)
args = parser.parse_args()

print(args)

train(args)
90 changes: 46 additions & 44 deletions benchmarking/gcn/seastar/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import pynvml
import torch.nn as nn
import torch.nn.functional as F
from stgraph.graph.static.StaticGraph import StaticGraph
from stgraph.graph.static.StaticGraph_old import StaticGraph
from stgraph.dataset.CoraDataLoader import CoraDataLoader
from utils import to_default_device, accuracy
from model import GCN

def main(args):

def main(args):
cora = CoraDataLoader(verbose=True)

# To account for the initial CUDA Context object for pynvml
tmp = StaticGraph([(0,0)], [1], 1)
tmp = StaticGraph([(0, 0)], [1], 1)

features = torch.FloatTensor(cora.get_all_features())
labels = torch.LongTensor(cora.get_all_targets())

train_mask = cora.get_train_mask()
test_mask = cora.get_test_mask()

Expand Down Expand Up @@ -47,7 +47,9 @@ def main(args):

# A simple sanity check
print("Measuerd Graph Size (pynvml): ", graph_mem, " B", flush=True)
print("Measuerd Graph Size (pynvml): ", (graph_mem)/(1024**2), " MB", flush=True)
print(
"Measuerd Graph Size (pynvml): ", (graph_mem) / (1024**2), " MB", flush=True
)

# normalization
degs = torch.from_numpy(g.weighted_in_degrees()).type(torch.int32)
Expand All @@ -58,23 +60,18 @@ def main(args):

num_feats = features.shape[1]
n_classes = int(max(labels) - min(labels) + 1)
print("Num Classes: ",n_classes)

model = GCN(g,
num_feats,
args.num_hidden,
n_classes,
args.num_layers,
F.relu)

print("Num Classes: ", n_classes)

model = GCN(g, num_feats, args.num_hidden, n_classes, args.num_layers, F.relu)

if cuda:
model.cuda()
loss_fcn = torch.nn.CrossEntropyLoss()

# use optimizer
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay)
optimizer = torch.optim.Adam(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)

# initialize graph
dur = []
Expand Down Expand Up @@ -106,40 +103,45 @@ def main(args):
dur.append(run_time_this_epoch)

train_acc = accuracy(logits[train_mask], labels[train_mask])
print('Epoch {:05d} | Time(s) {:.4f} | train_acc {:.6f} | Used_Memory {:.6f} mb '.format(
epoch, run_time_this_epoch, train_acc, (now_mem * 1.0 / (1024**2))
))
print(
"Epoch {:05d} | Time(s) {:.4f} | train_acc {:.6f} | Used_Memory {:.6f} mb ".format(
epoch, run_time_this_epoch, train_acc, (now_mem * 1.0 / (1024**2))
)
)

Used_memory /= (1024**3)
print('^^^{:6f}^^^{:6f}'.format(Used_memory, np.mean(dur)))
Used_memory /= 1024**3
print("^^^{:6f}^^^{:6f}".format(Used_memory, np.mean(dur)))


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCN')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="GCN")

# COMMENT IF SNOOP IS TO BE ENABLED
snoop.install(enabled=False)

parser.add_argument("--dropout", type=float, default=0.5,
help="dropout probability")
parser.add_argument("--dataset", type=str,
help="Datset to train your model")
parser.add_argument("--gpu", type=int, default=0,
help="gpu")
parser.add_argument("--lr", type=float, default=1e-2,
help="learning rate")
parser.add_argument("--num_epochs", type=int, default=200,
help="number of training epochs")
parser.add_argument("--num_hidden", type=int, default=16,
help="number of hidden gcn units")
parser.add_argument("--num_layers", type=int, default=1,
help="number of hidden gcn layers")
parser.add_argument("--weight-decay", type=float, default=5e-4,
help="Weight for L2 loss")
parser.add_argument("--self-loop", action='store_true',
help="graph self-loop (default=False)")
parser.add_argument(
"--dropout", type=float, default=0.5, help="dropout probability"
)
parser.add_argument("--dataset", type=str, help="Datset to train your model")
parser.add_argument("--gpu", type=int, default=0, help="gpu")
parser.add_argument("--lr", type=float, default=1e-2, help="learning rate")
parser.add_argument(
"--num_epochs", type=int, default=200, help="number of training epochs"
)
parser.add_argument(
"--num_hidden", type=int, default=16, help="number of hidden gcn units"
)
parser.add_argument(
"--num_layers", type=int, default=1, help="number of hidden gcn layers"
)
parser.add_argument(
"--weight-decay", type=float, default=5e-4, help="Weight for L2 loss"
)
parser.add_argument(
"--self-loop", action="store_true", help="graph self-loop (default=False)"
)
parser.set_defaults(self_loop=False)
args = parser.parse_args()
print(args)

main(args)
main(args)
Loading
Loading