Skip to content

Commit

Permalink
[Examples] Run all the examples (dmlc#623)
Browse files Browse the repository at this point in the history
* all pytorch examples

* scan through mxnet examples

* change reddit data

* tweak numerical range for unittest

* fix ci

* fix ci

* fix

* add seed to workaround
  • Loading branch information
jermainewang authored Jun 9, 2019
1 parent 74e13ee commit dec8b49
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endif()
# and add set(OPTION VALUE) to override these build options.
# Alernatively, use cmake -DOPTION=VALUE through command-line.
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)

if(USE_CUDA)
Expand Down
2 changes: 1 addition & 1 deletion examples/mxnet/sampling/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def main(args):
n_test_samples))

# create GCN model
g = DGLGraph(data.graph, readonly=True)
g = data.graph
g.ndata['features'] = features
g.ndata['labels'] = labels

Expand Down
2 changes: 0 additions & 2 deletions examples/mxnet/sse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --graph-file ../../data/5_5_csr.nd \
--lr 0.0005 \
--batch-size 1024 \
--use-spmv \
--dgl \
--num-parallel-subgraphs 32 \
--gpu 1 \
--num-feats 100 \
Expand All @@ -38,7 +37,6 @@ DGLBACKEND=mxnet python3 sse_batch.py --dataset "pubmed" \
--n-epochs 1000 \
--lr 0.001 \
--batch-size 30 \
--dgl \
--use-spmv \
--neigh-expand 8 \
--n-hidden 16
Expand Down
2 changes: 1 addition & 1 deletion examples/mxnet/tree_lstm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The script will download the [SST dataset] (http://nlp.stanford.edu/sentiment/in

## Usage
```
python3 train.py --gpu 0
DGLBACKEND=mxnet python3 train.py --gpu 0
```

## Speed Test
Expand Down
2 changes: 1 addition & 1 deletion examples/pytorch/appnp/appnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def forward(self, h):
self.g.ndata['h'] = h
if self.edge_drop:
# 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.update_all(fn.src_mul_edge(src='h', edge='d', out='m'),
fn.sum(msg='m', out='h'))
Expand Down
3 changes: 1 addition & 2 deletions python/dgl/data/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import dgl
import os, sys
from ..graph_index import create_graph_index
from .utils import download, extract_archive, get_download_dir, _get_dgl_url


Expand All @@ -20,7 +19,7 @@ def __init__(self, self_loop=False):
extract_archive(zip_file_path, extract_dir)
# graph
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
reddit_data = np.load(os.path.join(extract_dir, "reddit_data.npz"))
self.features = reddit_data["feature"]
Expand Down
47 changes: 26 additions & 21 deletions tests/compute/test_kernel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import dgl
import dgl.function as fn
import networkx as nx
import numpy as np
import backend as F
from itertools import product

np.random.seed(42)

def udf_copy_src(edges):
return {'m': edges.src['u']}
Expand Down Expand Up @@ -36,21 +38,21 @@ def generate_feature(g, broadcast='none'):
nv = g.number_of_nodes()
ne = g.number_of_edges()
if broadcast == 'e':
u = F.randn((nv, D1, D2, D3))
e = F.randn((ne, D2, 1))
v = F.randn((nv, D1, D2, D3))
u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.tensor(np.random.randn(ne, D2, 1) - 1)
v = F.tensor(np.random.randn(nv, D1, D2, D3))
elif broadcast == 'u':
u = F.randn((nv, D2, 1))
e = F.randn((ne, D1, D2, D3))
v = F.randn((nv, D1, D2, D3))
u = F.tensor(np.random.randn(nv, D2, 1) + 1)
e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.tensor(np.random.randn(nv, D1, D2, D3))
elif broadcast == 'v':
u = F.randn((nv, D1, D2, D3))
e = F.randn((ne, D1, D2, D3))
v = F.randn((nv, D2, 1))
u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.tensor(np.random.randn(nv, D2, 1))
else:
u = F.randn((nv, D1, D2, D3))
e = F.randn((ne, D1, D2, D3))
v = F.randn((nv, D1, D2, D3))
u = F.tensor(np.random.randn(nv, D1, D2, D3) + 1)
e = F.tensor(np.random.randn(ne, D1, D2, D3) - 1)
v = F.tensor(np.random.randn(nv, D1, D2, D3))
return u, v, e


Expand Down Expand Up @@ -175,30 +177,34 @@ def rfunc(nodes):
with F.record_grad():
g.update_all(mfunc, rfunc)
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))
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':
rtol = 1e-2
atol = 1e-2
else:
rtol = 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):
_print_error(r1, r2)
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_error(lhs_grad_1, lhs_grad_2)
assert(F.allclose(lhs_grad_1, lhs_grad_2, rtol, atol))

if not F.allclose(rhs_grad_1, rhs_grad_2, rtol, atol):
print("right grad")
_print_error(rhs_grad_1, rhs_grad_2)
Expand All @@ -224,7 +230,6 @@ def _print_error(a, b):
for broadcast in ["none", lhs, rhs]:
_test(g, lhs, rhs, binary_op, reducer)


if __name__ == '__main__':
test_copy_src_reduce()
test_copy_edge_reduce()
Expand Down

0 comments on commit dec8b49

Please sign in to comment.