Unverified Commit dec8b49b authored by Minjie Wang's avatar Minjie Wang Committed by GitHub
Browse files

[Examples] Run all the examples (#623)

* all pytorch examples

* scan through mxnet examples

* change reddit data

* tweak numerical range for unittest

* fix ci

* fix ci

* fix

* add seed to workaround
parent 74e13eea
...@@ -23,6 +23,7 @@ endif() ...@@ -23,6 +23,7 @@ endif()
# and add set(OPTION VALUE) to override these build options. # and add set(OPTION VALUE) to override these build options.
# Alernatively, use cmake -DOPTION=VALUE through command-line. # Alernatively, use cmake -DOPTION=VALUE through command-line.
dgl_option(USE_CUDA "Build with CUDA" OFF) dgl_option(USE_CUDA "Build with CUDA" OFF)
dgl_option(USE_OPENMP "Build with OpenMP" ON)
dgl_option(BUILD_CPP_TEST "Build cpp unittest executables" OFF) dgl_option(BUILD_CPP_TEST "Build cpp unittest executables" OFF)
if(USE_CUDA) if(USE_CUDA)
......
...@@ -52,7 +52,7 @@ def main(args): ...@@ -52,7 +52,7 @@ def main(args):
n_test_samples)) n_test_samples))
# create GCN model # create GCN model
g = DGLGraph(data.graph, readonly=True) g = data.graph
g.ndata['features'] = features g.ndata['features'] = features
g.ndata['labels'] = labels g.ndata['labels'] = labels
......
...@@ -23,7 +23,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --graph-file ../../data/5_5_csr.nd \ ...@@ -23,7 +23,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --graph-file ../../data/5_5_csr.nd \
--lr 0.0005 \ --lr 0.0005 \
--batch-size 1024 \ --batch-size 1024 \
--use-spmv \ --use-spmv \
--dgl \
--num-parallel-subgraphs 32 \ --num-parallel-subgraphs 32 \
--gpu 1 \ --gpu 1 \
--num-feats 100 \ --num-feats 100 \
...@@ -38,7 +37,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --dataset "pubmed" \ ...@@ -38,7 +37,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --dataset "pubmed" \
--n-epochs 1000 \ --n-epochs 1000 \
--lr 0.001 \ --lr 0.001 \
--batch-size 30 \ --batch-size 30 \
--dgl \
--use-spmv \ --use-spmv \
--neigh-expand 8 \ --neigh-expand 8 \
--n-hidden 16 --n-hidden 16
......
...@@ -21,7 +21,7 @@ The script will download the [SST dataset] (http://nlp.stanford.edu/sentiment/in ...@@ -21,7 +21,7 @@ The script will download the [SST dataset] (http://nlp.stanford.edu/sentiment/in
## Usage ## Usage
``` ```
python3 train.py --gpu 0 DGLBACKEND=mxnet python3 train.py --gpu 0
``` ```
## Speed Test ## Speed Test
......
...@@ -33,7 +33,7 @@ class GraphPropagation(nn.Module): ...@@ -33,7 +33,7 @@ class GraphPropagation(nn.Module):
self.g.ndata['h'] = h self.g.ndata['h'] = h
if self.edge_drop: if self.edge_drop:
# performing edge dropout # performing edge dropout
ed = self.edge_drop(torch.ones((self.g.number_of_edges(), 1))) ed = self.edge_drop(torch.ones((self.g.number_of_edges(), 1), device=h.device))
self.g.edata['d'] = ed self.g.edata['d'] = ed
self.g.update_all(fn.src_mul_edge(src='h', edge='d', out='m'), self.g.update_all(fn.src_mul_edge(src='h', edge='d', out='m'),
fn.sum(msg='m', out='h')) fn.sum(msg='m', out='h'))
......
...@@ -4,7 +4,6 @@ import scipy.sparse as sp ...@@ -4,7 +4,6 @@ import scipy.sparse as sp
import numpy as np import numpy as np
import dgl import dgl
import os, sys import os, sys
from ..graph_index import create_graph_index
from .utils import download, extract_archive, get_download_dir, _get_dgl_url from .utils import download, extract_archive, get_download_dir, _get_dgl_url
...@@ -20,7 +19,7 @@ class RedditDataset(object): ...@@ -20,7 +19,7 @@ class RedditDataset(object):
extract_archive(zip_file_path, extract_dir) extract_archive(zip_file_path, extract_dir)
# graph # graph
coo_adj = sp.load_npz(os.path.join(extract_dir, "reddit{}_graph.npz".format(self_loop_str))) coo_adj = sp.load_npz(os.path.join(extract_dir, "reddit{}_graph.npz".format(self_loop_str)))
self.graph = create_graph_index(coo_adj, readonly=True) self.graph = dgl.DGLGraph(coo_adj, readonly=True)
# features and labels # features and labels
reddit_data = np.load(os.path.join(extract_dir, "reddit_data.npz")) reddit_data = np.load(os.path.join(extract_dir, "reddit_data.npz"))
self.features = reddit_data["feature"] self.features = reddit_data["feature"]
......
import dgl import dgl
import dgl.function as fn import dgl.function as fn
import networkx as nx import networkx as nx
import numpy as np
import backend as F import backend as F
from itertools import product from itertools import product
np.random.seed(42)
def udf_copy_src(edges): def udf_copy_src(edges):
return {'m': edges.src['u']} return {'m': edges.src['u']}
...@@ -36,21 +38,21 @@ def generate_feature(g, broadcast='none'): ...@@ -36,21 +38,21 @@ def generate_feature(g, broadcast='none'):
nv = g.number_of_nodes() nv = g.number_of_nodes()
ne = g.number_of_edges() ne = g.number_of_edges()
if broadcast == 'e': if broadcast == 'e':
u = F.randn((nv, D1, D2, D3)) u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.randn((ne, D2, 1)) e = F.tensor(np.random.randn(ne, D2, 1) - 1)
v = F.randn((nv, D1, D2, D3)) v = F.tensor(np.random.randn(nv, D1, D2, D3))
elif broadcast == 'u': elif broadcast == 'u':
u = F.randn((nv, D2, 1)) u = F.tensor(np.random.randn(nv, D2, 1) + 1)
e = F.randn((ne, D1, D2, D3)) e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.randn((nv, D1, D2, D3)) v = F.tensor(np.random.randn(nv, D1, D2, D3))
elif broadcast == 'v': elif broadcast == 'v':
u = F.randn((nv, D1, D2, D3)) u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.randn((ne, D1, D2, D3)) e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.randn((nv, D2, 1)) v = F.tensor(np.random.randn(nv, D2, 1))
else: else:
u = F.randn((nv, D1, D2, D3)) u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.randn((ne, D1, D2, D3)) e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.randn((nv, D1, D2, D3)) v = F.tensor(np.random.randn(nv, D1, D2, D3))
return u, v, e return u, v, e
...@@ -175,16 +177,10 @@ def test_all_binary_builtins(): ...@@ -175,16 +177,10 @@ def test_all_binary_builtins():
with F.record_grad(): with F.record_grad():
g.update_all(mfunc, rfunc) g.update_all(mfunc, rfunc)
r2 = g.ndata['r2'] r2 = g.ndata['r2']
F.backward(r2.sum()) F.backward(r2.sum(), F.tensor([1.]))
lhs_grad_2 = F.grad(target_feature_switch(g, lhs)) lhs_grad_2 = F.grad(target_feature_switch(g, lhs))
rhs_grad_2 = F.grad(target_feature_switch(g, rhs)) rhs_grad_2 = F.grad(target_feature_switch(g, rhs))
def _print_error(a, b):
print("Test {}_{}_{}_{} {}".
format(lhs, binary_op, rhs, reducer, broadcast))
print(a)
print(b)
if reducer == 'prod': if reducer == 'prod':
rtol = 1e-2 rtol = 1e-2
atol = 1e-2 atol = 1e-2
...@@ -192,13 +188,23 @@ def test_all_binary_builtins(): ...@@ -192,13 +188,23 @@ def test_all_binary_builtins():
rtol = 1e-4 rtol = 1e-4
atol = 1e-4 atol = 1e-4
def _print_error(a, b):
print("ERROR: Test {}_{}_{}_{} {}".
format(lhs, binary_op, rhs, reducer, broadcast))
print(a, b)
for i, (x, y) in enumerate(zip(F.asnumpy(a).flatten(), F.asnumpy(b).flatten())):
if not np.allclose(x, y, rtol, atol):
print('@{} {} v.s. {}'.format(i, x, y))
if not F.allclose(r1, r2, rtol, atol): if not F.allclose(r1, r2, rtol, atol):
_print_error(r1, r2) _print_error(r1, r2)
assert F.allclose(r1, r2, rtol, atol) assert F.allclose(r1, r2, rtol, atol)
if not F.allclose(rhs_grad_1, rhs_grad_2, rtol, atol):
if not F.allclose(lhs_grad_1, lhs_grad_2, rtol, atol):
print("left grad") print("left grad")
_print_error(lhs_grad_1, lhs_grad_2) _print_error(lhs_grad_1, lhs_grad_2)
assert(F.allclose(lhs_grad_1, lhs_grad_2, rtol, atol)) assert(F.allclose(lhs_grad_1, lhs_grad_2, rtol, atol))
if not F.allclose(rhs_grad_1, rhs_grad_2, rtol, atol): if not F.allclose(rhs_grad_1, rhs_grad_2, rtol, atol):
print("right grad") print("right grad")
_print_error(rhs_grad_1, rhs_grad_2) _print_error(rhs_grad_1, rhs_grad_2)
...@@ -224,7 +230,6 @@ def test_all_binary_builtins(): ...@@ -224,7 +230,6 @@ def test_all_binary_builtins():
for broadcast in ["none", lhs, rhs]: for broadcast in ["none", lhs, rhs]:
_test(g, lhs, rhs, binary_op, reducer) _test(g, lhs, rhs, binary_op, reducer)
if __name__ == '__main__': if __name__ == '__main__':
test_copy_src_reduce() test_copy_src_reduce()
test_copy_edge_reduce() test_copy_edge_reduce()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment