Skip to content

Commit eb67800

Browse files
committed
csrmm2 not supported anymore, replaced with SpMM()
1 parent 1e7ad9f commit eb67800

28 files changed

+236
-107
lines changed

.gitignore

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
__pycache__/
2+
data/
23
job_scripts/
34
outputs/
45
profs/
5-
data/
6-
.*.swm
7-
.*.swn
8-
.*.swo
6+
sparse-extension/build/
7+
sparse-extension/dist/
8+
sparse-extension/sparse_coo_tensor_cpp.egg-info/
99
.*.swp
1010
.nfs*
11-
test*
12-
distr*.txt
13-
distr*.prof
14-
gcn*.txt
15-
*saved*
16-
gcn.py
17-
gcn_conv.py
18-
gcn_distr_cpu.py
19-
gcn_distr_1_5d_gpu_edit.py
20-
gcn_distr_2d_actandnorm.py
21-
gcn_distr_2d_gen.py
22-
gcn_distr_2d_cpu.py
23-
gcn_distr_2d_gpu.py
24-
gcn_distr_2d_gpu_gen.py
25-
gcn_distr_2d_gpu_floor_edit.py
26-
gcn_distr_2d_gpu_floor_old.py
27-
gcn_distr_2d_gpu_floor_gpu_gen.py
11+
*.txt
2812
*.pt

gcn.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import os.path as osp
2+
import argparse
3+
4+
import torch
5+
from torch.nn import Parameter
6+
import torch.nn.functional as F
7+
from torch_geometric.datasets import Planetoid, PPI, Reddit
8+
import torch_geometric.transforms as T
9+
from torch_geometric.nn import GCNConv # noqa
10+
11+
import time
12+
13+
parser = argparse.ArgumentParser()
14+
parser.add_argument('--use_gdc', action='store_true',
15+
help='Use GDC preprocessing.')
16+
args = parser.parse_args()
17+
18+
dataset = 'Cora'
19+
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset)
20+
dataset = Planetoid(path, dataset, transform=T.NormalizeFeatures())
21+
# dataset = PPI(path, 'train', T.NormalizeFeatures())
22+
# dataset = Reddit(path, T.NormalizeFeatures())
23+
# dataset = Yelp(path, T.NormalizeFeatures())
24+
data = dataset[0]
25+
26+
seed = 0
27+
28+
if args.use_gdc:
29+
gdc = T.GDC(self_loop_weight=1, normalization_in='sym',
30+
normalization_out='col',
31+
diffusion_kwargs=dict(method='ppr', alpha=0.05),
32+
sparsification_kwargs=dict(method='topk', k=128,
33+
dim=0), exact=True)
34+
data = gdc(data)
35+
36+
37+
class Net(torch.nn.Module):
38+
def __init__(self):
39+
super(Net, self).__init__()
40+
self.conv1 = GCNConv(dataset.num_features, 16, cached=True, normalize=False, bias=False)
41+
self.conv2 = GCNConv(16, dataset.num_classes, cached=True, normalize=False, bias=False)
42+
43+
self.conv1.node_dim = 0
44+
self.conv2.node_dim = 0
45+
46+
with torch.no_grad():
47+
self.conv1.weight = Parameter(weight1)
48+
self.conv2.weight = Parameter(weight2)
49+
# self.conv1 = ChebConv(data.num_features, 16, K=2)
50+
# self.conv2 = ChebConv(16, data.num_features, K=2)
51+
52+
def forward(self):
53+
x, edge_index = data.x, data.edge_index
54+
# x = F.relu(self.conv1(x, edge_index))
55+
# x = F.dropout(x, training=self.training)
56+
# x = self.conv2(x, edge_index)
57+
# return F.log_softmax(x, dim=1)
58+
x = self.conv1(x, edge_index)
59+
# x = F.relu(x)
60+
x = self.conv2(x, edge_index)
61+
# return F.log_softmax(x, dim=1)
62+
return x
63+
64+
65+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
66+
67+
torch.manual_seed(seed)
68+
weight1 = torch.rand(dataset.num_features, 16)
69+
weight1 = weight1.to(device)
70+
71+
weight2 = torch.rand(16, dataset.num_classes)
72+
weight2 = weight2.to(device)
73+
74+
data.y = data.y.type(torch.LongTensor)
75+
model, data = Net().to(device), data.to(device)
76+
77+
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
78+
79+
def train():
80+
model.train()
81+
optimizer.zero_grad()
82+
outputs = model()
83+
84+
# Note: bool type removes warnings, unsure of perf penalty
85+
F.nll_loss(outputs[data.train_mask.bool()], data.y[data.train_mask.bool()]).backward()
86+
# F.nll_loss(outputs, torch.max(data.y, 1)[1]).backward()
87+
88+
for W in model.parameters():
89+
if W.grad is not None:
90+
print(W.grad)
91+
92+
optimizer.step()
93+
return outputs
94+
95+
def test(outputs):
96+
model.eval()
97+
logits, accs = outputs, []
98+
for _, mask in data('train_mask', 'val_mask', 'test_mask'):
99+
pred = logits[mask].max(1)[1]
100+
acc = pred.eq(data.y[mask]).sum().item() / mask.sum().item()
101+
accs.append(acc)
102+
return accs
103+
104+
def main():
105+
best_val_acc = test_acc = 0
106+
outputs = None
107+
108+
tstart = time.time()
109+
110+
# for epoch in range(1, 101):
111+
for epoch in range(1):
112+
outputs = train()
113+
train_acc, val_acc, tmp_test_acc = test(outputs)
114+
if val_acc > best_val_acc:
115+
best_val_acc = val_acc
116+
test_acc = tmp_test_acc
117+
log = 'Epoch: {:03d}, Train: {:.4f}, Val: {:.4f}, Test: {:.4f}'
118+
print(log.format(epoch, train_acc, best_val_acc, test_acc))
119+
120+
tstop = time.time()
121+
print("Time: " + str(tstop - tstart))
122+
123+
return outputs
124+
125+
if __name__=='__main__':
126+
print(main())

gcn_distr.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ def broad_func(node_count, am_partitions, inputs, rank, size, group):
235235

236236
tstart_comp = start_time(group, rank)
237237

238-
spmm_gpu(am_partitions[i].indices()[0].int(), am_partitions[i].indices()[1].int(),
239-
am_partitions[i].values(), am_partitions[i].size(0),
240-
am_partitions[i].size(1), inputs_recv, z_loc)
238+
# spmm_gpu(am_partitions[i].indices()[0].int(), am_partitions[i].indices()[1].int(),
239+
# am_partitions[i].values(), am_partitions[i].size(0),
240+
# am_partitions[i].size(1), inputs_recv, z_loc)
241+
z_loc += torch.mm(am_partitions[i], inputs_recv)
241242

242243
dur = stop_time(group, rank, tstart_comp)
243244
comp_time[run][rank] += dur
@@ -472,6 +473,9 @@ def oned_partition(rank, size, inputs, adj_matrix, data, features, classes, devi
472473
am_partitions = None
473474
am_pbyp = None
474475

476+
inputs = inputs.to(torch.device("cpu"))
477+
adj_matrix = adj_matrix.to(torch.device("cpu"))
478+
475479
# Compute the adj_matrix and inputs partitions for this process
476480
# TODO: Maybe I do want grad here. Unsure.
477481
with torch.no_grad():
@@ -530,7 +534,6 @@ def run(rank, size, inputs, adj_matrix, data, features, classes, device):
530534
# adj_matrix_loc = torch.rand(node_count, n_per_proc)
531535
# inputs_loc = torch.rand(n_per_proc, inputs.size(1))
532536

533-
534537
inputs_loc, adj_matrix_loc, am_pbyp = oned_partition(rank, size, inputs, adj_matrix, data,
535538
features, classes, device)
536539

@@ -682,6 +685,7 @@ def main():
682685
outputs = None
683686
if "OMPI_COMM_WORLD_RANK" in os.environ.keys():
684687
os.environ["RANK"] = os.environ["OMPI_COMM_WORLD_RANK"]
688+
685689
# Initialize distributed environment with SLURM
686690
if "SLURM_PROCID" in os.environ.keys():
687691
os.environ["RANK"] = os.environ["SLURM_PROCID"]
@@ -701,14 +705,15 @@ def main():
701705
# device = torch.device('cpu')
702706
devid = rank_to_devid(rank, acc_per_rank)
703707
device = torch.device('cuda:{}'.format(devid))
708+
print(f"device: {device}")
704709
torch.cuda.set_device(device)
705710
curr_devid = torch.cuda.current_device()
706711
# print(f"curr_devid: {curr_devid}", flush=True)
707712
devcount = torch.cuda.device_count()
708713

709714
if graphname == "Cora":
710715
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', graphname)
711-
dataset = Planetoid(path, graphname, T.NormalizeFeatures())
716+
dataset = Planetoid(path, graphname, transform=T.NormalizeFeatures())
712717
data = dataset[0]
713718
data = data.to(device)
714719
data.x.requires_grad = True

gcn_distr_15d.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ def oned_partition(rank, size, inputs, adj_matrix, data, features, classes, devi
502502
am_partitions = None
503503
am_pbyp = None
504504

505+
inputs = inputs.to(torch.device("cpu"))
506+
adj_matrix = adj_matrix.to(torch.device("cpu"))
507+
505508
rank_c = rank // replication
506509
# Compute the adj_matrix and inputs partitions for this process
507510
# TODO: Maybe I do want grad here. Unsure.
@@ -723,6 +726,17 @@ def main():
723726
if "OMPI_COMM_WORLD_RANK" in os.environ.keys():
724727
os.environ["RANK"] = os.environ["OMPI_COMM_WORLD_RANK"]
725728

729+
# Initialize distributed environment with SLURM
730+
if "SLURM_PROCID" in os.environ.keys():
731+
os.environ["RANK"] = os.environ["SLURM_PROCID"]
732+
733+
if "SLURM_NTASKS" in os.environ.keys():
734+
os.environ["WORLD_SIZE"] = os.environ["SLURM_NTASKS"]
735+
736+
if "MASTER_ADDR" not in os.environ.keys():
737+
os.environ["MASTER_ADDR"] = "127.0.0.1"
738+
739+
os.environ["MASTER_PORT"] = "1234"
726740
dist.init_process_group(backend='nccl')
727741
rank = dist.get_rank()
728742
size = dist.get_world_size()
@@ -738,7 +752,7 @@ def main():
738752

739753
if graphname == "Cora":
740754
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', graphname)
741-
dataset = Planetoid(path, graphname, T.NormalizeFeatures())
755+
dataset = Planetoid(path, graphname, transform=T.NormalizeFeatures())
742756
data = dataset[0]
743757
data = data.to(device)
744758
data.x.requires_grad = True

gcn_distr_2d.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ def twod_partition(rank, size, inputs, adj_matrix, data, features, classes, devi
10951095
proc_row = proc_row_size(size)
10961096
proc_col = proc_col_size(size)
10971097

1098+
inputs = inputs.to(torch.device("cpu"))
1099+
adj_matrix = adj_matrix.to(torch.device("cpu"))
1100+
10981101
# n_per_proc = math.ceil(float(node_count) / proc_row)
10991102
n_per_proc = node_count // proc_row
11001103

@@ -1401,7 +1404,7 @@ def main():
14011404

14021405
# mid_layer = 16
14031406
if graphname == 'Cora':
1404-
dataset = Planetoid(path, graphname, T.NormalizeFeatures())
1407+
dataset = Planetoid(path, graphname, transform=T.NormalizeFeatures())
14051408
data = dataset[0]
14061409
num_features = dataset.num_features
14071410
num_classes = dataset.num_classes
@@ -1463,6 +1466,18 @@ def main():
14631466

14641467
if "OMPI_COMM_WORLD_RANK" in os.environ.keys():
14651468
os.environ["RANK"] = os.environ["OMPI_COMM_WORLD_RANK"]
1469+
1470+
# Initialize distributed environment with SLURM
1471+
if "SLURM_PROCID" in os.environ.keys():
1472+
os.environ["RANK"] = os.environ["SLURM_PROCID"]
1473+
1474+
if "SLURM_NTASKS" in os.environ.keys():
1475+
os.environ["WORLD_SIZE"] = os.environ["SLURM_NTASKS"]
1476+
1477+
if "MASTER_ADDR" not in os.environ.keys():
1478+
os.environ["MASTER_ADDR"] = "127.0.0.1"
1479+
1480+
os.environ["MASTER_PORT"] = "1234"
14661481
dist.init_process_group(backend='nccl')
14671482
# dist.init_process_group('gloo', init_method='env://')
14681483
rank = dist.get_rank()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

sparse-extension/build/temp.linux-x86_64-3.8/.ninja_log

-2
This file was deleted.

sparse-extension/build/temp.linux-x86_64-3.8/build.ninja

-20
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)