Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dgl
Commits
5b409bf7
Unverified
Commit
5b409bf7
authored
Mar 24, 2023
by
Hongzhi (Steve), Chen
Committed by
GitHub
Mar 24, 2023
Browse files
Rename_test (#5487)
Co-authored-by:
Ubuntu
<
ubuntu@ip-172-31-28-63.ap-northeast-1.compute.internal
>
parent
0dd4f767
Changes
36
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
467 additions
and
469 deletions
+467
-469
tests/python/common/test_heterograph-shared-memory.py
tests/python/common/test_heterograph-shared-memory.py
+1
-1
tests/python/common/test_heterograph-specialization.py
tests/python/common/test_heterograph-specialization.py
+1
-1
tests/python/common/test_heterograph.py
tests/python/common/test_heterograph.py
+177
-177
tests/python/common/test_readout.py
tests/python/common/test_readout.py
+12
-12
tests/python/common/test_subgraph.py
tests/python/common/test_subgraph.py
+14
-14
tests/python/common/test_traversal.py
tests/python/common/test_traversal.py
+1
-1
tests/python/common/transforms/test_functional-sort.py
tests/python/common/transforms/test_functional-sort.py
+3
-3
tests/python/common/transforms/test_to_block.py
tests/python/common/transforms/test_to_block.py
+5
-5
tests/python/common/transforms/test_transform.py
tests/python/common/transforms/test_transform.py
+153
-153
tests/python/mxnet/test_nn.py
tests/python/mxnet/test_nn.py
+19
-19
tests/python/pytorch/distributed/optim/test_dist_optim.py
tests/python/pytorch/distributed/optim/test_dist_optim.py
+1
-1
tests/python/pytorch/nn/test_nn.py
tests/python/pytorch/nn/test_nn.py
+46
-46
tests/python/tensorflow/test_nn.py
tests/python/tensorflow/test_nn.py
+10
-10
tests/tools/test_parmetis.py
tests/tools/test_parmetis.py
+9
-9
tests/utils/checks.py
tests/utils/checks.py
+6
-6
tests/utils/graph_cases.py
tests/utils/graph_cases.py
+9
-11
No files found.
tests/python/common/test_heterograph-shared-memory.py
View file @
5b409bf7
...
...
@@ -40,7 +40,7 @@ def _assert_is_identical_hetero(g, g2):
# check if node ID spaces and feature spaces are equal
for
ntype
in
g
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
g2
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
g2
.
num_nodes
(
ntype
)
# check if edge ID spaces and feature spaces are equal
for
etype
in
g
.
canonical_etypes
:
...
...
tests/python/common/test_heterograph-specialization.py
View file @
5b409bf7
...
...
@@ -319,7 +319,7 @@ def test_spmv_3d_feat(idtype):
a
=
sp
.
random
(
n
,
n
,
p
,
data_rvs
=
lambda
n
:
np
.
ones
(
n
))
g
=
dgl
.
DGLGraph
(
a
)
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
m
=
g
.
num
ber_of
_edges
()
m
=
g
.
num_edges
()
# test#1: v2v with adj data
h
=
F
.
randn
((
n
,
5
,
5
))
...
...
tests/python/common/test_heterograph.py
View file @
5b409bf7
...
...
@@ -408,7 +408,7 @@ def test_query(idtype):
# has_nodes
for
ntype
in
ntypes
:
n
=
g
.
num
ber_of
_nodes
(
ntype
)
n
=
g
.
num_nodes
(
ntype
)
for
i
in
range
(
n
):
assert
g
.
has_nodes
(
i
,
ntype
)
assert
not
g
.
has_nodes
(
n
,
ntype
)
...
...
@@ -486,9 +486,9 @@ def test_query(idtype):
src_count
=
Counter
(
srcs
)
dst_count
=
Counter
(
dsts
)
utype
,
_
,
vtype
=
g
.
to_canonical_etype
(
etype
)
for
i
in
range
(
g
.
num
ber_of
_nodes
(
utype
)):
for
i
in
range
(
g
.
num_nodes
(
utype
)):
assert
out_degrees
[
i
]
==
src_count
[
i
]
for
i
in
range
(
g
.
num
ber_of
_nodes
(
vtype
)):
for
i
in
range
(
g
.
num_nodes
(
vtype
)):
assert
in_degrees
[
i
]
==
dst_count
[
i
]
edges
=
{
...
...
@@ -612,10 +612,10 @@ def _test_hypersparse():
{
"user"
:
N1
,
"game"
:
N1
},
device
=
F
.
ctx
(),
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
N1
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
N1
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
1
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
1
assert
g
.
num_nodes
(
"user"
)
==
N1
assert
g
.
num_nodes
(
"game"
)
==
N1
assert
g
.
num_edges
(
"follows"
)
==
1
assert
g
.
num_edges
(
"plays"
)
==
1
assert
g
.
has_edges_between
(
0
,
1
,
"follows"
)
assert
not
g
.
has_edges_between
(
0
,
0
,
"follows"
)
...
...
@@ -935,9 +935,9 @@ def test_view1(idtype):
out_degrees
=
F
.
asnumpy
(
g
.
out_degrees
())
src_count
=
Counter
(
srcs
)
dst_count
=
Counter
(
dsts
)
for
i
in
range
(
g
.
num
ber_of
_nodes
(
utype
)):
for
i
in
range
(
g
.
num_nodes
(
utype
)):
assert
out_degrees
[
i
]
==
src_count
[
i
]
for
i
in
range
(
g
.
num
ber_of
_nodes
(
vtype
)):
for
i
in
range
(
g
.
num_nodes
(
vtype
)):
assert
in_degrees
[
i
]
==
dst_count
[
i
]
edges
=
{
...
...
@@ -971,12 +971,12 @@ def test_view1(idtype):
_test_query
()
# test features
HG
.
nodes
[
"user"
].
data
[
"h"
]
=
F
.
ones
((
HG
.
num
ber_of
_nodes
(
"user"
),
5
))
HG
.
nodes
[
"game"
].
data
[
"m"
]
=
F
.
ones
((
HG
.
num
ber_of
_nodes
(
"game"
),
3
))
*
2
HG
.
nodes
[
"user"
].
data
[
"h"
]
=
F
.
ones
((
HG
.
num_nodes
(
"user"
),
5
))
HG
.
nodes
[
"game"
].
data
[
"m"
]
=
F
.
ones
((
HG
.
num_nodes
(
"game"
),
3
))
*
2
# test only one node type
g
=
HG
[
"follows"
]
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num_nodes
()
==
3
# test ndata and edata
f1
=
F
.
randn
((
3
,
6
))
...
...
@@ -1284,13 +1284,13 @@ def test_convert(idtype):
hg
=
create_test_heterograph
(
idtype
)
hs
=
[]
for
ntype
in
hg
.
ntypes
:
h
=
F
.
randn
((
hg
.
num
ber_of
_nodes
(
ntype
),
5
))
h
=
F
.
randn
((
hg
.
num_nodes
(
ntype
),
5
))
hg
.
nodes
[
ntype
].
data
[
"h"
]
=
h
hs
.
append
(
h
)
hg
.
nodes
[
"user"
].
data
[
"x"
]
=
F
.
randn
((
3
,
3
))
ws
=
[]
for
etype
in
hg
.
canonical_etypes
:
w
=
F
.
randn
((
hg
.
num
ber_of
_edges
(
etype
),
5
))
w
=
F
.
randn
((
hg
.
num_edges
(
etype
),
5
))
hg
.
edges
[
etype
].
data
[
"w"
]
=
w
ws
.
append
(
w
)
hg
.
edges
[
"plays"
].
data
[
"x"
]
=
F
.
randn
((
4
,
3
))
...
...
@@ -1308,7 +1308,7 @@ def test_convert(idtype):
dst
=
F
.
asnumpy
(
dst
)
etype_id
,
eid
=
F
.
asnumpy
(
g
.
edata
[
dgl
.
ETYPE
]),
F
.
asnumpy
(
g
.
edata
[
dgl
.
EID
])
ntype_id
,
nid
=
F
.
asnumpy
(
g
.
ndata
[
dgl
.
NTYPE
]),
F
.
asnumpy
(
g
.
ndata
[
dgl
.
NID
])
for
i
in
range
(
g
.
num
ber_of
_edges
()):
for
i
in
range
(
g
.
num_edges
()):
srctype
=
hg
.
ntypes
[
ntype_id
[
src
[
i
]]]
dsttype
=
hg
.
ntypes
[
ntype_id
[
dst
[
i
]]]
etype
=
hg
.
etypes
[
etype_id
[
i
]]
...
...
@@ -1339,7 +1339,7 @@ def test_convert(idtype):
assert
set
(
hg
.
ntypes
)
==
set
(
hg2
.
ntypes
)
assert
set
(
hg
.
canonical_etypes
)
==
set
(
hg2
.
canonical_etypes
)
for
ntype
in
hg
.
ntypes
:
assert
hg
.
num
ber_of
_nodes
(
ntype
)
==
hg2
.
num
ber_of
_nodes
(
ntype
)
assert
hg
.
num_nodes
(
ntype
)
==
hg2
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
hg
.
nodes
[
ntype
].
data
[
"h"
],
hg2
.
nodes
[
ntype
].
data
[
"h"
]
)
...
...
@@ -1363,12 +1363,12 @@ def test_convert(idtype):
assert
set
(
hg
.
canonical_etypes
)
==
set
(
[(
"l0"
,
"e0"
,
"l1"
),
(
"l1"
,
"e1"
,
"l2"
),
(
"l0"
,
"e2"
,
"l2"
)]
)
assert
hg
.
num
ber_of
_nodes
(
"l0"
)
==
2
assert
hg
.
num
ber_of
_nodes
(
"l1"
)
==
1
assert
hg
.
num
ber_of
_nodes
(
"l2"
)
==
1
assert
hg
.
num
ber_of
_edges
(
"e0"
)
==
2
assert
hg
.
num
ber_of
_edges
(
"e1"
)
==
1
assert
hg
.
num
ber_of
_edges
(
"e2"
)
==
1
assert
hg
.
num_nodes
(
"l0"
)
==
2
assert
hg
.
num_nodes
(
"l1"
)
==
1
assert
hg
.
num_nodes
(
"l2"
)
==
1
assert
hg
.
num_edges
(
"e0"
)
==
2
assert
hg
.
num_edges
(
"e1"
)
==
1
assert
hg
.
num_edges
(
"e2"
)
==
1
assert
F
.
array_equal
(
hg
.
ndata
[
dgl
.
NID
][
"l0"
],
F
.
tensor
([
0
,
1
],
F
.
int64
))
assert
F
.
array_equal
(
hg
.
ndata
[
dgl
.
NID
][
"l1"
],
F
.
tensor
([
2
],
F
.
int64
))
assert
F
.
array_equal
(
hg
.
ndata
[
dgl
.
NID
][
"l2"
],
F
.
tensor
([
3
],
F
.
int64
))
...
...
@@ -1398,11 +1398,11 @@ def test_convert(idtype):
assert
set
(
hg
.
canonical_etypes
)
==
set
(
[(
"user"
,
"watches"
,
"movie"
),
(
"user"
,
"watches"
,
"TV"
)]
)
assert
hg
.
num
ber_of
_nodes
(
"user"
)
==
1
assert
hg
.
num
ber_of
_nodes
(
"TV"
)
==
1
assert
hg
.
num
ber_of
_nodes
(
"movie"
)
==
1
assert
hg
.
num
ber_of
_edges
((
"user"
,
"watches"
,
"TV"
))
==
1
assert
hg
.
num
ber_of
_edges
((
"user"
,
"watches"
,
"movie"
))
==
1
assert
hg
.
num_nodes
(
"user"
)
==
1
assert
hg
.
num_nodes
(
"TV"
)
==
1
assert
hg
.
num_nodes
(
"movie"
)
==
1
assert
hg
.
num_edges
((
"user"
,
"watches"
,
"TV"
))
==
1
assert
hg
.
num_edges
((
"user"
,
"watches"
,
"movie"
))
==
1
assert
len
(
hg
.
etypes
)
==
2
# hetero_to_homo test case 2
...
...
@@ -1415,7 +1415,7 @@ def test_convert(idtype):
g
=
dgl
.
to_homogeneous
(
hg
)
assert
hg
.
idtype
==
g
.
idtype
assert
hg
.
device
==
g
.
device
assert
g
.
num
ber_of
_nodes
()
==
5
assert
g
.
num_nodes
()
==
5
# hetero_to_subgraph_to_homo
hg
=
dgl
.
heterograph
(
...
...
@@ -1514,13 +1514,13 @@ def test_metagraph_reachable(idtype):
new_g
=
dgl
.
metapath_reachable_graph
(
g
,
[
"follows"
,
"plays"
])
assert
new_g
.
idtype
==
idtype
assert
new_g
.
ntypes
==
[
"game"
,
"user"
]
assert
new_g
.
num
ber_of
_edges
()
==
3
assert
new_g
.
num_edges
()
==
3
assert
F
.
asnumpy
(
new_g
.
has_edges_between
([
0
,
0
,
1
],
[
0
,
1
,
1
])).
all
()
new_g
=
dgl
.
metapath_reachable_graph
(
g
,
[
"follows"
])
assert
new_g
.
idtype
==
idtype
assert
new_g
.
ntypes
==
[
"user"
]
assert
new_g
.
num
ber_of
_edges
()
==
2
assert
new_g
.
num_edges
()
==
2
assert
F
.
asnumpy
(
new_g
.
has_edges_between
([
0
,
1
],
[
1
,
2
])).
all
()
...
...
@@ -1560,8 +1560,8 @@ def test_subgraph_mask(idtype):
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"wishes"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
idtype
)
)
assert
sg
.
num
ber_of
_nodes
(
"developer"
)
==
0
assert
sg
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
sg
.
num_nodes
(
"developer"
)
==
0
assert
sg
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
sg
.
nodes
[
"user"
].
data
[
"h"
],
g
.
nodes
[
"user"
].
data
[
"h"
][
1
:
3
]
)
...
...
@@ -1620,8 +1620,8 @@ def test_subgraph(idtype):
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"wishes"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
g
.
idtype
)
)
assert
sg
.
num
ber_of
_nodes
(
"developer"
)
==
0
assert
sg
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
sg
.
num_nodes
(
"developer"
)
==
0
assert
sg
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
sg
.
nodes
[
"user"
].
data
[
"h"
],
g
.
nodes
[
"user"
].
data
[
"h"
][
1
:
3
]
)
...
...
@@ -1683,7 +1683,7 @@ def test_subgraph(idtype):
)
else
:
for
ntype
in
sg
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
sg
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
sg
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"follows"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
g
.
idtype
)
...
...
@@ -1713,7 +1713,7 @@ def test_subgraph(idtype):
)
else
:
for
ntype
in
sg
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
sg
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
sg
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"plays"
].
data
[
dgl
.
EID
]),
...
...
@@ -1739,7 +1739,7 @@ def test_subgraph(idtype):
assert
set
(
sg
.
ntypes
)
==
{
"user"
,
"game"
}
assert
set
(
sg
.
etypes
)
==
{
"follows"
,
"plays"
,
"wishes"
}
for
ntype
in
sg
.
ntypes
:
assert
sg
.
num
ber_of
_nodes
(
ntype
)
==
g
.
num
ber_of
_nodes
(
ntype
)
assert
sg
.
num_nodes
(
ntype
)
==
g
.
num_nodes
(
ntype
)
for
etype
in
sg
.
etypes
:
src_sg
,
dst_sg
=
sg
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
src_g
,
dst_g
=
g
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
...
...
@@ -1768,7 +1768,7 @@ def test_subgraph(idtype):
assert
set
(
sg
.
ntypes
)
==
{
"developer"
,
"game"
}
assert
set
(
sg
.
etypes
)
==
{
"develops"
}
for
ntype
in
sg
.
ntypes
:
assert
sg
.
num
ber_of
_nodes
(
ntype
)
==
g
.
num
ber_of
_nodes
(
ntype
)
assert
sg
.
num_nodes
(
ntype
)
==
g
.
num_nodes
(
ntype
)
for
etype
in
sg
.
etypes
:
src_sg
,
dst_sg
=
sg
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
src_g
,
dst_g
=
g
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
...
...
@@ -2059,9 +2059,9 @@ def test_backward(idtype):
@
parametrize_idtype
def
test_empty_heterograph
(
idtype
):
def
assert_empty
(
g
):
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
0
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
0
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
0
assert
g
.
num_nodes
(
"user"
)
==
0
assert
g
.
num_edges
(
"plays"
)
==
0
assert
g
.
num_nodes
(
"game"
)
==
0
# empty src-dst pair
assert_empty
(
dgl
.
heterograph
({(
"user"
,
"plays"
,
"game"
):
([],
[])}))
...
...
@@ -2071,8 +2071,8 @@ def test_empty_heterograph(idtype):
)
assert
g
.
idtype
==
idtype
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
0
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
0
assert
g
.
num_nodes
(
"user"
)
==
0
assert
g
.
num_edges
(
"follows"
)
==
0
# empty relation graph with others
g
=
dgl
.
heterograph
(
...
...
@@ -2085,11 +2085,11 @@ def test_empty_heterograph(idtype):
)
assert
g
.
idtype
==
idtype
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
0
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
0
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
0
assert
g
.
num_edges
(
"plays"
)
==
0
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
(
"develops"
)
==
2
assert
g
.
num_nodes
(
"developer"
)
==
2
@
parametrize_idtype
...
...
@@ -2179,14 +2179,14 @@ def test_stack_reduce(idtype):
{
"plays"
:
(
mfunc
,
rfunc
),
"wishes"
:
(
mfunc
,
rfunc2
)},
"stack"
)
assert
g
.
nodes
[
"game"
].
data
[
"y"
].
shape
==
(
g
.
num
ber_of
_nodes
(
"game"
),
g
.
num_nodes
(
"game"
),
2
,
200
,
)
# only one type-wise update_all, stack still adds one dimension
g
.
multi_update_all
({
"plays"
:
(
mfunc
,
rfunc
)},
"stack"
)
assert
g
.
nodes
[
"game"
].
data
[
"y"
].
shape
==
(
g
.
num
ber_of
_nodes
(
"game"
),
g
.
num_nodes
(
"game"
),
1
,
200
,
)
...
...
@@ -2200,9 +2200,9 @@ def test_isolated_ntype(idtype):
idtype
=
idtype
,
device
=
F
.
ctx
(),
)
assert
g
.
num
ber_of
_nodes
(
"A"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"B"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"C"
)
==
4
assert
g
.
num_nodes
(
"A"
)
==
3
assert
g
.
num_nodes
(
"B"
)
==
4
assert
g
.
num_nodes
(
"C"
)
==
4
g
=
dgl
.
heterograph
(
{(
"A"
,
"AC"
,
"C"
):
([
0
,
1
,
2
],
[
1
,
2
,
3
])},
...
...
@@ -2210,9 +2210,9 @@ def test_isolated_ntype(idtype):
idtype
=
idtype
,
device
=
F
.
ctx
(),
)
assert
g
.
num
ber_of
_nodes
(
"A"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"B"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"C"
)
==
4
assert
g
.
num_nodes
(
"A"
)
==
3
assert
g
.
num_nodes
(
"B"
)
==
4
assert
g
.
num_nodes
(
"C"
)
==
4
G
=
dgl
.
graph
(
([
0
,
1
,
2
],
[
4
,
5
,
6
]),
num_nodes
=
11
,
idtype
=
idtype
,
device
=
F
.
ctx
()
...
...
@@ -2222,9 +2222,9 @@ def test_isolated_ntype(idtype):
)
G
.
edata
[
dgl
.
ETYPE
]
=
F
.
tensor
([
0
,
0
,
0
],
dtype
=
F
.
int64
)
g
=
dgl
.
to_heterogeneous
(
G
,
[
"A"
,
"B"
,
"C"
],
[
"AB"
])
assert
g
.
num
ber_of
_nodes
(
"A"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"B"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"C"
)
==
4
assert
g
.
num_nodes
(
"A"
)
==
3
assert
g
.
num_nodes
(
"B"
)
==
4
assert
g
.
num_nodes
(
"C"
)
==
4
@
parametrize_idtype
...
...
@@ -2346,13 +2346,13 @@ def test_bipartite(idtype):
assert
g1
.
etypes
==
[
"AB"
]
assert
g1
.
srctypes
==
[
"A"
]
assert
g1
.
dsttypes
==
[
"B"
]
assert
g1
.
num
ber_of
_nodes
(
"A"
)
==
2
assert
g1
.
num
ber_of
_nodes
(
"B"
)
==
6
assert
g1
.
num_nodes
(
"A"
)
==
2
assert
g1
.
num_nodes
(
"B"
)
==
6
assert
g1
.
number_of_src_nodes
(
"A"
)
==
2
assert
g1
.
number_of_src_nodes
()
==
2
assert
g1
.
number_of_dst_nodes
(
"B"
)
==
6
assert
g1
.
number_of_dst_nodes
()
==
6
assert
g1
.
num
ber_of
_edges
()
==
3
assert
g1
.
num_edges
()
==
3
g1
.
srcdata
[
"h"
]
=
F
.
randn
((
2
,
5
))
assert
F
.
array_equal
(
g1
.
srcnodes
[
"A"
].
data
[
"h"
],
g1
.
srcdata
[
"h"
])
assert
F
.
array_equal
(
g1
.
nodes
[
"A"
].
data
[
"h"
],
g1
.
srcdata
[
"h"
])
...
...
@@ -2375,9 +2375,9 @@ def test_bipartite(idtype):
assert
g2
.
is_unibipartite
assert
g2
.
srctypes
==
[
"A"
]
assert
set
(
g2
.
dsttypes
)
==
{
"B"
,
"C"
}
assert
g2
.
num
ber_of
_nodes
(
"A"
)
==
2
assert
g2
.
num
ber_of
_nodes
(
"B"
)
==
6
assert
g2
.
num
ber_of
_nodes
(
"C"
)
==
1
assert
g2
.
num_nodes
(
"A"
)
==
2
assert
g2
.
num_nodes
(
"B"
)
==
6
assert
g2
.
num_nodes
(
"C"
)
==
1
assert
g2
.
number_of_src_nodes
(
"A"
)
==
2
assert
g2
.
number_of_src_nodes
()
==
2
assert
g2
.
number_of_dst_nodes
(
"B"
)
==
6
...
...
@@ -2578,8 +2578,8 @@ def test_reverse(idtype):
gidx
=
g
.
_graph
r_gidx
=
gidx
.
reverse
()
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2591,8 +2591,8 @@ def test_reverse(idtype):
r_gidx
=
gidx
.
reverse
()
assert
"csr"
in
gidx
.
formats
()[
"created"
]
assert
"csc"
in
r_gidx
.
formats
()[
"created"
]
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2604,8 +2604,8 @@ def test_reverse(idtype):
r_gidx
=
gidx
.
reverse
()
assert
"csc"
in
gidx
.
formats
()[
"created"
]
assert
"csr"
in
r_gidx
.
formats
()[
"created"
]
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2636,12 +2636,12 @@ def test_reverse(idtype):
assert
mg
.
find_edge
(
etype
)
==
r_mg
.
find_edge
(
etype
)[::
-
1
]
# three node types and three edge types
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_nodes
(
1
)
==
r_gidx
.
num
ber_of
_nodes
(
1
)
assert
gidx
.
num
ber_of
_nodes
(
2
)
==
r_gidx
.
num
ber_of
_nodes
(
2
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num
ber_of
_edges
(
1
)
==
r_gidx
.
num
ber_of
_edges
(
1
)
assert
gidx
.
num
ber_of
_edges
(
2
)
==
r_gidx
.
num
ber_of
_edges
(
2
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_nodes
(
1
)
==
r_gidx
.
num_nodes
(
1
)
assert
gidx
.
num_nodes
(
2
)
==
r_gidx
.
num_nodes
(
2
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
assert
gidx
.
num_edges
(
1
)
==
r_gidx
.
num_edges
(
1
)
assert
gidx
.
num_edges
(
2
)
==
r_gidx
.
num_edges
(
2
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2662,12 +2662,12 @@ def test_reverse(idtype):
# three node types and three edge types
assert
"csr"
in
gidx
.
formats
()[
"created"
]
assert
"csc"
in
r_gidx
.
formats
()[
"created"
]
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_nodes
(
1
)
==
r_gidx
.
num
ber_of
_nodes
(
1
)
assert
gidx
.
num
ber_of
_nodes
(
2
)
==
r_gidx
.
num
ber_of
_nodes
(
2
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num
ber_of
_edges
(
1
)
==
r_gidx
.
num
ber_of
_edges
(
1
)
assert
gidx
.
num
ber_of
_edges
(
2
)
==
r_gidx
.
num
ber_of
_edges
(
2
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_nodes
(
1
)
==
r_gidx
.
num_nodes
(
1
)
assert
gidx
.
num_nodes
(
2
)
==
r_gidx
.
num_nodes
(
2
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
assert
gidx
.
num_edges
(
1
)
==
r_gidx
.
num_edges
(
1
)
assert
gidx
.
num_edges
(
2
)
==
r_gidx
.
num_edges
(
2
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2688,12 +2688,12 @@ def test_reverse(idtype):
# three node types and three edge types
assert
"csc"
in
gidx
.
formats
()[
"created"
]
assert
"csr"
in
r_gidx
.
formats
()[
"created"
]
assert
gidx
.
num
ber_of
_nodes
(
0
)
==
r_gidx
.
num
ber_of
_nodes
(
0
)
assert
gidx
.
num
ber_of
_nodes
(
1
)
==
r_gidx
.
num
ber_of
_nodes
(
1
)
assert
gidx
.
num
ber_of
_nodes
(
2
)
==
r_gidx
.
num
ber_of
_nodes
(
2
)
assert
gidx
.
num
ber_of
_edges
(
0
)
==
r_gidx
.
num
ber_of
_edges
(
0
)
assert
gidx
.
num
ber_of
_edges
(
1
)
==
r_gidx
.
num
ber_of
_edges
(
1
)
assert
gidx
.
num
ber_of
_edges
(
2
)
==
r_gidx
.
num
ber_of
_edges
(
2
)
assert
gidx
.
num_nodes
(
0
)
==
r_gidx
.
num_nodes
(
0
)
assert
gidx
.
num_nodes
(
1
)
==
r_gidx
.
num_nodes
(
1
)
assert
gidx
.
num_nodes
(
2
)
==
r_gidx
.
num_nodes
(
2
)
assert
gidx
.
num_edges
(
0
)
==
r_gidx
.
num_edges
(
0
)
assert
gidx
.
num_edges
(
1
)
==
r_gidx
.
num_edges
(
1
)
assert
gidx
.
num_edges
(
2
)
==
r_gidx
.
num_edges
(
2
)
g_s
,
g_d
,
_
=
gidx
.
edges
(
0
)
rg_s
,
rg_d
,
_
=
r_gidx
.
edges
(
0
)
assert
F
.
array_equal
(
g_s
,
rg_d
)
...
...
@@ -2715,8 +2715,8 @@ def test_clone(idtype):
g
.
edata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
1
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
new_g
=
g
.
clone
()
assert
g
.
num
ber_of
_nodes
()
==
new_g
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
new_g
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
new_g
.
num_nodes
()
assert
g
.
num_edges
()
==
new_g
.
num_edges
()
assert
g
.
device
==
new_g
.
device
assert
g
.
idtype
==
new_g
.
idtype
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
new_g
.
ndata
[
"h"
])
...
...
@@ -2728,15 +2728,15 @@ def test_clone(idtype):
assert
F
.
array_equal
(
g
.
edata
[
"h"
],
new_g
.
edata
[
"h"
])
==
False
# graph structure change
g
.
add_nodes
(
1
)
assert
g
.
num
ber_of
_nodes
()
!=
new_g
.
num
ber_of
_nodes
()
assert
g
.
num_nodes
()
!=
new_g
.
num_nodes
()
new_g
.
add_edges
(
1
,
1
)
assert
g
.
num
ber_of
_edges
()
!=
new_g
.
num
ber_of
_edges
()
assert
g
.
num_edges
()
!=
new_g
.
num_edges
()
# zero data graph
g
=
dgl
.
graph
(([],
[]),
num_nodes
=
0
,
idtype
=
idtype
,
device
=
F
.
ctx
())
new_g
=
g
.
clone
()
assert
g
.
num
ber_of
_nodes
()
==
new_g
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
new_g
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
new_g
.
num_nodes
()
assert
g
.
num_edges
()
==
new_g
.
num_edges
()
# heterograph
g
=
create_test_heterograph3
(
idtype
)
...
...
@@ -2744,11 +2744,11 @@ def test_clone(idtype):
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
new_g
=
g
.
clone
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
new_g
.
num
ber_of
_nodes
(
"user"
)
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
new_g
.
num
ber_of
_nodes
(
"game"
)
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
new_g
.
num
ber_of
_nodes
(
"developer"
)
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
new_g
.
num
ber_of
_edges
(
"plays"
)
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
new_g
.
num
ber_of
_edges
(
"develops"
)
assert
g
.
num_nodes
(
"user"
)
==
new_g
.
num_nodes
(
"user"
)
assert
g
.
num_nodes
(
"game"
)
==
new_g
.
num_nodes
(
"game"
)
assert
g
.
num_nodes
(
"developer"
)
==
new_g
.
num_nodes
(
"developer"
)
assert
g
.
num_edges
(
"plays"
)
==
new_g
.
num_edges
(
"plays"
)
assert
g
.
num_edges
(
"develops"
)
==
new_g
.
num_edges
(
"develops"
)
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
new_g
.
nodes
[
"user"
].
data
[
"h"
]
)
...
...
@@ -2793,20 +2793,20 @@ def test_add_edges(idtype):
v
=
1
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
3
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
3
u
=
[
0
]
v
=
[
1
]
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
4
u
=
F
.
tensor
(
u
,
dtype
=
idtype
)
v
=
F
.
tensor
(
v
,
dtype
=
idtype
)
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
5
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
5
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
0
,
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
1
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -2816,8 +2816,8 @@ def test_add_edges(idtype):
u
=
F
.
tensor
([
0
,
1
],
dtype
=
idtype
)
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
.
add_edges
(
u
,
v
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2833,8 +2833,8 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
.
add_edges
(
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2851,8 +2851,8 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
.
add_edges
(
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
2
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
,
2
],
dtype
=
idtype
))
...
...
@@ -2869,23 +2869,23 @@ def test_add_edges(idtype):
v
=
1
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
3
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
3
u
=
[
0
]
v
=
[
1
]
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
4
u
=
F
.
tensor
(
u
,
dtype
=
idtype
)
v
=
F
.
tensor
(
v
,
dtype
=
idtype
)
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
5
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
5
u
,
v
=
g
.
edges
(
form
=
"uv"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
0
,
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
1
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -2900,9 +2900,9 @@ def test_add_edges(idtype):
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
.
add_edges
(
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2927,9 +2927,9 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
.
add_edges
(
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2947,11 +2947,11 @@ def test_add_edges(idtype):
u
=
F
.
tensor
([
0
,
2
],
dtype
=
idtype
)
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
.
add_edges
(
u
,
v
,
etype
=
"plays"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
6
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
g
.
num_edges
(
"plays"
)
==
6
assert
g
.
num_edges
(
"develops"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"plays"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
1
,
2
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
0
,
1
,
1
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2973,11 +2973,11 @@ def test_add_edges(idtype):
F
.
tensor
([
2
,
2
,
1
,
1
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
.
add_edges
(
u
,
v
,
data
=
e_feat
,
etype
=
"develops"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
3
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
6
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
3
assert
g
.
num_edges
(
"plays"
)
==
6
assert
g
.
num_edges
(
"develops"
)
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"develops"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -2998,7 +2998,7 @@ def test_add_nodes(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
add_nodes
(
1
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num_nodes
()
==
4
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
0
],
dtype
=
idtype
))
# zero node graph
...
...
@@ -3007,7 +3007,7 @@ def test_add_nodes(idtype):
g
.
add_nodes
(
1
,
data
=
{
"h"
:
F
.
copy_to
(
F
.
tensor
([
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())}
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num_nodes
()
==
4
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
2
],
dtype
=
idtype
))
# bipartite graph
...
...
@@ -3021,12 +3021,12 @@ def test_add_nodes(idtype):
data
=
{
"h"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())},
ntype
=
"user"
,
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
4
assert
g
.
num_nodes
(
"user"
)
==
4
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
0
,
0
,
2
,
2
],
dtype
=
idtype
)
)
g
.
add_nodes
(
2
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
5
assert
g
.
num_nodes
(
"game"
)
==
5
# heterogeneous graph
g
=
create_test_heterograph3
(
idtype
)
...
...
@@ -3037,9 +3037,9 @@ def test_add_nodes(idtype):
ntype
=
"game"
,
)
g
.
add_nodes
(
0
,
ntype
=
"developer"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
4
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
0
],
dtype
=
idtype
)
)
...
...
@@ -3058,33 +3058,33 @@ def test_remove_edges(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
e
=
0
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
e
=
[
0
]
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
e
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_edges
()
==
0
# has node data
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
remove_edges
(
1
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
))
# has edge data
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
edata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
remove_edges
(
0
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
edata
[
"h"
],
F
.
tensor
([
2
],
dtype
=
idtype
))
# invalid eid
...
...
@@ -3103,7 +3103,7 @@ def test_remove_edges(idtype):
)
e
=
0
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
...
...
@@ -3114,13 +3114,13 @@ def test_remove_edges(idtype):
)
e
=
[
0
]
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
e
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
.
remove_edges
(
e
)
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_edges
()
==
0
# has data
g
=
dgl
.
heterograph
(
...
...
@@ -3136,7 +3136,7 @@ def test_remove_edges(idtype):
)
g
.
edata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
remove_edges
(
1
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
],
dtype
=
idtype
)
)
...
...
@@ -3151,7 +3151,7 @@ def test_remove_edges(idtype):
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
.
remove_edges
(
1
,
etype
=
"plays"
)
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
3
assert
g
.
num_edges
(
"plays"
)
==
3
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"plays"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -3160,7 +3160,7 @@ def test_remove_edges(idtype):
)
# remove all edges of 'develops'
g
.
remove_edges
([
0
,
1
],
etype
=
"develops"
)
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
g
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
)
)
...
...
@@ -3178,21 +3178,21 @@ def test_remove_nodes(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
0
g
.
remove_nodes
(
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
[
1
]
g
.
remove_nodes
(
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
0
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
F
.
tensor
([
2
],
dtype
=
idtype
)
g
.
remove_nodes
(
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -3210,8 +3210,8 @@ def test_remove_nodes(idtype):
g
.
ndata
[
"hv"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
edata
[
"he"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
remove_nodes
(
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -3226,9 +3226,9 @@ def test_remove_nodes(idtype):
)
n
=
0
g
.
remove_nodes
(
n
,
ntype
=
"user"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
(
"user"
)
==
1
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
...
...
@@ -3239,9 +3239,9 @@ def test_remove_nodes(idtype):
)
n
=
[
1
]
g
.
remove_nodes
(
n
,
ntype
=
"user"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
(
"user"
)
==
1
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -3252,9 +3252,9 @@ def test_remove_nodes(idtype):
)
n
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
.
remove_nodes
(
n
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
()
==
2
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
()
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
...
...
@@ -3265,11 +3265,11 @@ def test_remove_nodes(idtype):
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
.
remove_nodes
(
0
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
1
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
1
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
g
.
num_edges
(
"plays"
)
==
2
assert
g
.
num_edges
(
"develops"
)
==
1
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
)
)
...
...
tests/python/common/test_readout.py
View file @
5b409bf7
...
...
@@ -30,8 +30,8 @@ def test_sum_case1(idtype):
@
pytest
.
mark
.
parametrize
(
"reducer"
,
[
"sum"
,
"max"
,
"mean"
])
def
test_reduce_readout
(
g
,
idtype
,
reducer
):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
3
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
2
))
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num_nodes
(),
3
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num_edges
(),
2
))
# Test.1: node readout
x
=
dgl
.
readout_nodes
(
g
,
"h"
,
op
=
reducer
)
...
...
@@ -77,10 +77,10 @@ def test_reduce_readout(g, idtype, reducer):
@
pytest
.
mark
.
parametrize
(
"reducer"
,
[
"sum"
,
"max"
,
"mean"
])
def
test_weighted_reduce_readout
(
g
,
idtype
,
reducer
):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
3
))
g
.
ndata
[
"w"
]
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
1
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
2
))
g
.
edata
[
"w"
]
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
1
))
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num_nodes
(),
3
))
g
.
ndata
[
"w"
]
=
F
.
randn
((
g
.
num_nodes
(),
1
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num_edges
(),
2
))
g
.
edata
[
"w"
]
=
F
.
randn
((
g
.
num_edges
(),
1
))
# Test.1: node readout
x
=
dgl
.
readout_nodes
(
g
,
"h"
,
"w"
,
op
=
reducer
)
...
...
@@ -126,7 +126,7 @@ def test_weighted_reduce_readout(g, idtype, reducer):
@
pytest
.
mark
.
parametrize
(
"descending"
,
[
True
,
False
])
def
test_topk
(
g
,
idtype
,
descending
):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
g
.
ndata
[
"x"
]
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
3
))
g
.
ndata
[
"x"
]
=
F
.
randn
((
g
.
num_nodes
(),
3
))
# Test.1: to test the case where k > number of nodes.
dgl
.
topk_nodes
(
g
,
"x"
,
100
,
sortby
=-
1
)
...
...
@@ -158,7 +158,7 @@ def test_topk(g, idtype, descending):
# Test.3: sorby=None
dgl
.
topk_nodes
(
g
,
"x"
,
k
,
sortby
=
None
)
g
.
edata
[
"x"
]
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
3
))
g
.
edata
[
"x"
]
=
F
.
randn
((
g
.
num_edges
(),
3
))
# Test.4: topk edges where k > number of edges.
dgl
.
topk_edges
(
g
,
"x"
,
100
,
sortby
=-
1
)
...
...
@@ -192,8 +192,8 @@ def test_topk(g, idtype, descending):
@
pytest
.
mark
.
parametrize
(
"g"
,
get_cases
([
"homo"
],
exclude
=
[
"dglgraph"
]))
def
test_softmax
(
g
,
idtype
):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
3
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
2
))
g
.
ndata
[
"h"
]
=
F
.
randn
((
g
.
num_nodes
(),
3
))
g
.
edata
[
"h"
]
=
F
.
randn
((
g
.
num_edges
(),
2
))
# Test.1: node readout
x
=
dgl
.
softmax_nodes
(
g
,
"h"
)
...
...
@@ -224,7 +224,7 @@ def test_broadcast(idtype, g):
for
i
,
sg
in
enumerate
(
subg
):
assert
F
.
allclose
(
sg
.
ndata
[
"h"
],
F
.
repeat
(
F
.
reshape
(
gfeat
[
i
],
(
1
,
3
)),
sg
.
num
ber_of
_nodes
(),
dim
=
0
),
F
.
repeat
(
F
.
reshape
(
gfeat
[
i
],
(
1
,
3
)),
sg
.
num_nodes
(),
dim
=
0
),
)
# Test.1: broadcast_edges
...
...
@@ -233,5 +233,5 @@ def test_broadcast(idtype, g):
for
i
,
sg
in
enumerate
(
subg
):
assert
F
.
allclose
(
sg
.
edata
[
"h"
],
F
.
repeat
(
F
.
reshape
(
gfeat
[
i
],
(
1
,
3
)),
sg
.
num
ber_of
_edges
(),
dim
=
0
),
F
.
repeat
(
F
.
reshape
(
gfeat
[
i
],
(
1
,
3
)),
sg
.
num_edges
(),
dim
=
0
),
)
tests/python/common/test_subgraph.py
View file @
5b409bf7
...
...
@@ -43,15 +43,15 @@ def test_edge_subgraph():
sg
.
ndata
[
dgl
.
NID
],
F
.
tensor
([
0
,
2
,
4
,
5
,
1
,
9
],
g
.
idtype
)
)
assert
F
.
array_equal
(
sg
.
edata
[
dgl
.
EID
],
F
.
tensor
(
eid
,
g
.
idtype
))
sg
.
ndata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num
ber_of
_nodes
())
sg
.
edata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num
ber_of
_edges
())
sg
.
ndata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num_nodes
())
sg
.
edata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num_edges
())
# relabel=False
sg
=
g
.
edge_subgraph
(
eid
,
relabel_nodes
=
False
)
assert
g
.
num
ber_of
_nodes
()
==
sg
.
num
ber_of
_nodes
()
assert
g
.
num_nodes
()
==
sg
.
num_nodes
()
assert
F
.
array_equal
(
sg
.
edata
[
dgl
.
EID
],
F
.
tensor
(
eid
,
g
.
idtype
))
sg
.
ndata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num
ber_of
_nodes
())
sg
.
edata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num
ber_of
_edges
())
sg
.
ndata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num_nodes
())
sg
.
edata
[
"h"
]
=
F
.
arange
(
0
,
sg
.
num_edges
())
def
test_subgraph
():
...
...
@@ -192,8 +192,8 @@ def test_subgraph_mask(idtype):
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"wishes"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
idtype
)
)
assert
sg
.
num
ber_of
_nodes
(
"developer"
)
==
0
assert
sg
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
sg
.
num_nodes
(
"developer"
)
==
0
assert
sg
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
sg
.
nodes
[
"user"
].
data
[
"h"
],
g
.
nodes
[
"user"
].
data
[
"h"
][
1
:
3
]
)
...
...
@@ -250,8 +250,8 @@ def test_subgraph1(idtype):
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"wishes"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
g
.
idtype
)
)
assert
sg
.
num
ber_of
_nodes
(
"developer"
)
==
0
assert
sg
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
sg
.
num_nodes
(
"developer"
)
==
0
assert
sg
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
sg
.
nodes
[
"user"
].
data
[
"h"
],
g
.
nodes
[
"user"
].
data
[
"h"
][
1
:
3
]
)
...
...
@@ -307,7 +307,7 @@ def test_subgraph1(idtype):
)
else
:
for
ntype
in
sg
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
sg
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
sg
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"follows"
].
data
[
dgl
.
EID
]),
F
.
tensor
([
1
],
g
.
idtype
)
...
...
@@ -337,7 +337,7 @@ def test_subgraph1(idtype):
)
else
:
for
ntype
in
sg
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
sg
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
sg
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
F
.
tensor
(
sg
.
edges
[
"plays"
].
data
[
dgl
.
EID
]),
...
...
@@ -361,7 +361,7 @@ def test_subgraph1(idtype):
assert
set
(
sg
.
ntypes
)
==
{
"user"
,
"game"
}
assert
set
(
sg
.
etypes
)
==
{
"follows"
,
"plays"
,
"wishes"
}
for
ntype
in
sg
.
ntypes
:
assert
sg
.
num
ber_of
_nodes
(
ntype
)
==
g
.
num
ber_of
_nodes
(
ntype
)
assert
sg
.
num_nodes
(
ntype
)
==
g
.
num_nodes
(
ntype
)
for
etype
in
sg
.
etypes
:
src_sg
,
dst_sg
=
sg
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
src_g
,
dst_g
=
g
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
...
...
@@ -390,7 +390,7 @@ def test_subgraph1(idtype):
assert
set
(
sg
.
ntypes
)
==
{
"developer"
,
"game"
}
assert
set
(
sg
.
etypes
)
==
{
"develops"
}
for
ntype
in
sg
.
ntypes
:
assert
sg
.
num
ber_of
_nodes
(
ntype
)
==
g
.
num
ber_of
_nodes
(
ntype
)
assert
sg
.
num_nodes
(
ntype
)
==
g
.
num_nodes
(
ntype
)
for
etype
in
sg
.
etypes
:
src_sg
,
dst_sg
=
sg
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
src_g
,
dst_g
=
g
.
all_edges
(
etype
=
etype
,
order
=
"eid"
)
...
...
@@ -454,7 +454,7 @@ def test_in_subgraph(idtype):
hg
[
"liked-by"
].
edge_ids
(
u
,
v
),
subg
[
"liked-by"
].
edata
[
dgl
.
EID
]
)
assert
edge_set
==
{(
2
,
0
),
(
2
,
1
),
(
1
,
0
),
(
0
,
0
)}
assert
subg
[
"flips"
].
num
ber_of
_edges
()
==
0
assert
subg
[
"flips"
].
num_edges
()
==
0
for
ntype
in
subg
.
ntypes
:
assert
dgl
.
NID
not
in
subg
.
nodes
[
ntype
].
data
...
...
tests/python/common/test_traversal.py
View file @
5b409bf7
...
...
@@ -73,7 +73,7 @@ def test_topological_nodes(idtype, n=100):
adjmat
=
g
.
adjacency_matrix
(
transpose
=
True
)
def
tensor_topo_traverse
():
n
=
g
.
num
ber_of
_nodes
()
n
=
g
.
num_nodes
()
mask
=
F
.
copy_to
(
F
.
ones
((
n
,
1
)),
F
.
cpu
())
degree
=
F
.
spmm
(
adjmat
,
mask
)
while
F
.
reduce_sum
(
mask
)
!=
0.0
:
...
...
tests/python/common/transforms/test_functional-sort.py
View file @
5b409bf7
...
...
@@ -65,7 +65,7 @@ def check_sort(spm, tag_arr=None, tag_pos=None):
def
test_sort_with_tag
(
idtype
):
num_nodes
,
num_adj
,
num_tags
=
200
,
[
20
,
50
],
5
g
=
create_test_heterograph
(
num_nodes
,
num_adj
,
idtype
=
idtype
)
tag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num
ber_of
_nodes
()))
tag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num_nodes
()))
src
,
dst
=
g
.
edges
()
edge_tag_dst
=
F
.
gather_row
(
tag
,
F
.
tensor
(
dst
))
edge_tag_src
=
F
.
gather_row
(
tag
,
F
.
tensor
(
src
))
...
...
@@ -99,8 +99,8 @@ def test_sort_with_tag_bipartite(idtype):
num_nodes
,
num_adj
,
num_tags
=
200
,
[
20
,
50
],
5
g
=
create_test_heterograph
(
num_nodes
,
num_adj
,
idtype
=
idtype
)
g
=
dgl
.
heterograph
({(
"_U"
,
"_E"
,
"_V"
):
g
.
edges
()})
utag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num
ber_of
_nodes
(
"_U"
)))
vtag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num
ber_of
_nodes
(
"_V"
)))
utag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num_nodes
(
"_U"
)))
vtag
=
F
.
tensor
(
np
.
random
.
choice
(
num_tags
,
g
.
num_nodes
(
"_V"
)))
new_g
=
dgl
.
sort_csr_by_tag
(
g
,
vtag
)
old_csr
=
g
.
adjacency_matrix
(
scipy_fmt
=
"csr"
)
...
...
tests/python/common/transforms/test_to_block.py
View file @
5b409bf7
...
...
@@ -26,7 +26,7 @@ def test_to_block(idtype):
def
check
(
g
,
bg
,
ntype
,
etype
,
dst_nodes
,
include_dst_in_src
=
True
):
if
dst_nodes
is
not
None
:
assert
F
.
array_equal
(
bg
.
dstnodes
[
ntype
].
data
[
dgl
.
NID
],
dst_nodes
)
n_dst_nodes
=
bg
.
num
ber_of
_nodes
(
"DST/"
+
ntype
)
n_dst_nodes
=
bg
.
num_nodes
(
"DST/"
+
ntype
)
if
include_dst_in_src
:
assert
F
.
array_equal
(
bg
.
srcnodes
[
ntype
].
data
[
dgl
.
NID
][:
n_dst_nodes
],
...
...
@@ -136,21 +136,21 @@ def test_to_block(idtype):
bg
=
dgl
.
to_block
(
g_ab
)
assert
bg
.
idtype
==
idtype
assert
bg
.
num
ber_of
_nodes
(
"SRC/B"
)
==
4
assert
bg
.
num_nodes
(
"SRC/B"
)
==
4
assert
F
.
array_equal
(
bg
.
srcnodes
[
"B"
].
data
[
dgl
.
NID
],
bg
.
dstnodes
[
"B"
].
data
[
dgl
.
NID
]
)
assert
bg
.
num
ber_of
_nodes
(
"DST/A"
)
==
0
assert
bg
.
num_nodes
(
"DST/A"
)
==
0
checkall
(
g_ab
,
bg
,
None
)
check_features
(
g_ab
,
bg
)
dst_nodes
=
{
"B"
:
F
.
tensor
([
5
,
6
,
3
,
1
],
dtype
=
idtype
)}
bg
=
dgl
.
to_block
(
g
,
dst_nodes
)
assert
bg
.
num
ber_of
_nodes
(
"SRC/B"
)
==
4
assert
bg
.
num_nodes
(
"SRC/B"
)
==
4
assert
F
.
array_equal
(
bg
.
srcnodes
[
"B"
].
data
[
dgl
.
NID
],
bg
.
dstnodes
[
"B"
].
data
[
dgl
.
NID
]
)
assert
bg
.
num
ber_of
_nodes
(
"DST/A"
)
==
0
assert
bg
.
num_nodes
(
"DST/A"
)
==
0
checkall
(
g
,
bg
,
dst_nodes
)
check_features
(
g
,
bg
)
...
...
tests/python/common/transforms/test_transform.py
View file @
5b409bf7
...
...
@@ -132,7 +132,7 @@ def test_line_graph1():
G
=
dgl
.
DGLGraph
(
nx
.
star_graph
(
N
)).
to
(
F
.
ctx
())
G
.
edata
[
"h"
]
=
F
.
randn
((
2
*
N
,
D
))
L
=
G
.
line_graph
(
shared
=
True
)
assert
L
.
num
ber_of
_nodes
()
==
2
*
N
assert
L
.
num_nodes
()
==
2
*
N
assert
F
.
allclose
(
L
.
ndata
[
"h"
],
G
.
edata
[
"h"
])
assert
G
.
device
==
F
.
ctx
()
...
...
@@ -144,15 +144,15 @@ def test_line_graph2(idtype):
idtype
=
idtype
,
)
lg
=
dgl
.
line_graph
(
g
)
assert
lg
.
num
ber_of
_nodes
()
==
5
assert
lg
.
num
ber_of
_edges
()
==
8
assert
lg
.
num_nodes
()
==
5
assert
lg
.
num_edges
()
==
8
row
,
col
=
lg
.
edges
()
assert
np
.
array_equal
(
F
.
asnumpy
(
row
),
np
.
array
([
0
,
0
,
1
,
2
,
2
,
3
,
4
,
4
]))
assert
np
.
array_equal
(
F
.
asnumpy
(
col
),
np
.
array
([
3
,
4
,
0
,
3
,
4
,
0
,
1
,
2
]))
lg
=
dgl
.
line_graph
(
g
,
backtracking
=
False
)
assert
lg
.
num
ber_of
_nodes
()
==
5
assert
lg
.
num
ber_of
_edges
()
==
4
assert
lg
.
num_nodes
()
==
5
assert
lg
.
num_edges
()
==
4
row
,
col
=
lg
.
edges
()
assert
np
.
array_equal
(
F
.
asnumpy
(
row
),
np
.
array
([
0
,
1
,
2
,
4
]))
assert
np
.
array_equal
(
F
.
asnumpy
(
col
),
np
.
array
([
4
,
0
,
3
,
1
]))
...
...
@@ -161,8 +161,8 @@ def test_line_graph2(idtype):
idtype
=
idtype
,
).
formats
(
"csr"
)
lg
=
dgl
.
line_graph
(
g
)
assert
lg
.
num
ber_of
_nodes
()
==
5
assert
lg
.
num
ber_of
_edges
()
==
8
assert
lg
.
num_nodes
()
==
5
assert
lg
.
num_edges
()
==
8
row
,
col
=
lg
.
edges
()
assert
np
.
array_equal
(
F
.
asnumpy
(
row
),
np
.
array
([
0
,
0
,
1
,
2
,
2
,
3
,
4
,
4
]))
assert
np
.
array_equal
(
F
.
asnumpy
(
col
),
np
.
array
([
3
,
4
,
0
,
3
,
4
,
0
,
1
,
2
]))
...
...
@@ -172,8 +172,8 @@ def test_line_graph2(idtype):
idtype
=
idtype
,
).
formats
(
"csc"
)
lg
=
dgl
.
line_graph
(
g
)
assert
lg
.
num
ber_of
_nodes
()
==
5
assert
lg
.
num
ber_of
_edges
()
==
8
assert
lg
.
num_nodes
()
==
5
assert
lg
.
num_edges
()
==
8
row
,
col
,
eid
=
lg
.
edges
(
"all"
)
row
=
F
.
asnumpy
(
row
)
col
=
F
.
asnumpy
(
col
)
...
...
@@ -187,7 +187,7 @@ def test_no_backtracking():
N
=
5
G
=
dgl
.
DGLGraph
(
nx
.
star_graph
(
N
))
L
=
G
.
line_graph
(
backtracking
=
False
)
assert
L
.
num
ber_of
_nodes
()
==
2
*
N
assert
L
.
num_nodes
()
==
2
*
N
for
i
in
range
(
1
,
N
):
e1
=
G
.
edge_ids
(
0
,
i
)
e2
=
G
.
edge_ids
(
i
,
0
)
...
...
@@ -209,8 +209,8 @@ def test_reverse(idtype):
assert
g
.
is_multigraph
==
rg
.
is_multigraph
assert
g
.
num
ber_of
_nodes
()
==
rg
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
rg
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
rg
.
num_nodes
()
assert
g
.
num_edges
()
==
rg
.
num_edges
()
assert
F
.
allclose
(
F
.
astype
(
rg
.
has_edges_between
([
1
,
2
,
1
],
[
0
,
1
,
2
]),
F
.
float32
),
F
.
ones
((
3
,)),
...
...
@@ -225,8 +225,8 @@ def test_reverse(idtype):
g
.
ndata
[
"h"
]
=
F
.
tensor
([[
0.0
],
[
1.0
],
[
2.0
]])
g
.
edata
[
"h"
]
=
F
.
tensor
([[
3.0
],
[
4.0
],
[
5.0
]])
g_r
=
dgl
.
reverse
(
g
)
assert
g
.
num
ber_of
_nodes
()
==
g_r
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
g_r
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
g_r
.
num_nodes
()
assert
g
.
num_edges
()
==
g_r
.
num_edges
()
u_g
,
v_g
,
eids_g
=
g
.
all_edges
(
form
=
"all"
)
u_rg
,
v_rg
,
eids_rg
=
g_r
.
all_edges
(
form
=
"all"
)
assert
F
.
array_equal
(
u_g
,
v_rg
)
...
...
@@ -237,15 +237,15 @@ def test_reverse(idtype):
# without share ndata
g_r
=
dgl
.
reverse
(
g
,
copy_ndata
=
False
)
assert
g
.
num
ber_of
_nodes
()
==
g_r
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
g_r
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
g_r
.
num_nodes
()
assert
g
.
num_edges
()
==
g_r
.
num_edges
()
assert
len
(
g_r
.
ndata
)
==
0
assert
len
(
g_r
.
edata
)
==
0
# with share ndata and edata
g_r
=
dgl
.
reverse
(
g
,
copy_ndata
=
True
,
copy_edata
=
True
)
assert
g
.
num
ber_of
_nodes
()
==
g_r
.
num
ber_of
_nodes
()
assert
g
.
num
ber_of
_edges
()
==
g_r
.
num
ber_of
_edges
()
assert
g
.
num_nodes
()
==
g_r
.
num_nodes
()
assert
g
.
num_edges
()
==
g_r
.
num_edges
()
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
g_r
.
ndata
[
"h"
])
assert
F
.
array_equal
(
g
.
edata
[
"h"
],
g_r
.
edata
[
"h"
])
...
...
@@ -286,9 +286,9 @@ def test_reverse(idtype):
assert
etype_g
[
0
]
==
etype_gr
[
2
]
assert
etype_g
[
1
]
==
etype_gr
[
1
]
assert
etype_g
[
2
]
==
etype_gr
[
0
]
assert
g
.
num
ber_of
_edges
(
etype_g
)
==
g_r
.
num
ber_of
_edges
(
etype_gr
)
assert
g
.
num_edges
(
etype_g
)
==
g_r
.
num_edges
(
etype_gr
)
for
ntype
in
g
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
g_r
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
g_r
.
num_nodes
(
ntype
)
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
g_r
.
nodes
[
"user"
].
data
[
"h"
])
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"hh"
],
g_r
.
nodes
[
"user"
].
data
[
"hh"
]
...
...
@@ -327,9 +327,9 @@ def test_reverse(idtype):
assert
etype_g
[
0
]
==
etype_gr
[
2
]
assert
etype_g
[
1
]
==
etype_gr
[
1
]
assert
etype_g
[
2
]
==
etype_gr
[
0
]
assert
g
.
num
ber_of
_edges
(
etype_g
)
==
g_r
.
num
ber_of
_edges
(
etype_gr
)
assert
g
.
num_edges
(
etype_g
)
==
g_r
.
num_edges
(
etype_gr
)
for
ntype
in
g
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
g_r
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
g_r
.
num_nodes
(
ntype
)
assert
len
(
g_r
.
nodes
[
"user"
].
data
)
==
0
assert
len
(
g_r
.
nodes
[
"game"
].
data
)
==
0
...
...
@@ -339,7 +339,7 @@ def test_reverse(idtype):
assert
etype_g
[
0
]
==
etype_gr
[
2
]
assert
etype_g
[
1
]
==
etype_gr
[
1
]
assert
etype_g
[
2
]
==
etype_gr
[
0
]
assert
g
.
num
ber_of
_edges
(
etype_g
)
==
g_r
.
num
ber_of
_edges
(
etype_gr
)
assert
g
.
num_edges
(
etype_g
)
==
g_r
.
num_edges
(
etype_gr
)
assert
F
.
array_equal
(
g
.
edges
[
"follows"
].
data
[
"h"
],
g_r
.
edges
[
"follows"
].
data
[
"h"
]
)
...
...
@@ -384,7 +384,7 @@ def test_to_bidirected():
elist
.
append
((
1
,
2
))
elist
=
set
(
elist
)
big
=
dgl
.
to_bidirected
(
g
)
assert
big
.
num
ber_of
_edges
()
==
num_edges
assert
big
.
num_edges
()
==
num_edges
src
,
dst
=
big
.
edges
()
eset
=
set
(
zip
(
list
(
F
.
asnumpy
(
src
)),
list
(
F
.
asnumpy
(
dst
))))
assert
eset
==
set
(
elist
)
...
...
@@ -404,8 +404,8 @@ def test_to_bidirected():
elist2
.
append
((
1
,
0
))
elist2
=
set
(
elist2
)
big
=
dgl
.
to_bidirected
(
g
)
assert
big
.
num
ber_of
_edges
(
"wins"
)
==
7
assert
big
.
num
ber_of
_edges
(
"follows"
)
==
3
assert
big
.
num_edges
(
"wins"
)
==
7
assert
big
.
num_edges
(
"follows"
)
==
3
src
,
dst
=
big
.
edges
(
etype
=
"wins"
)
eset
=
set
(
zip
(
list
(
F
.
asnumpy
(
src
)),
list
(
F
.
asnumpy
(
dst
))))
assert
eset
==
set
(
elist1
)
...
...
@@ -528,7 +528,7 @@ def test_add_reverse_edges():
g
.
ndata
[
"h"
]
=
F
.
tensor
([[
0.0
],
[
1.0
],
[
2.0
],
[
1.0
],
[
1.0
],
[
1.0
]])
g
.
edata
[
"h"
]
=
F
.
tensor
([[
3.0
],
[
4.0
],
[
5.0
],
[
6.0
]])
bg
=
dgl
.
add_reverse_edges
(
g
,
copy_ndata
=
True
,
copy_edata
=
True
)
assert
g
.
num
ber_of
_nodes
()
==
bg
.
num
ber_of
_nodes
()
assert
g
.
num_nodes
()
==
bg
.
num_nodes
()
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
bg
.
ndata
[
"h"
])
assert
F
.
array_equal
(
F
.
cat
([
g
.
edata
[
"h"
],
g
.
edata
[
"h"
]],
dim
=
0
),
bg
.
edata
[
"h"
]
...
...
@@ -558,8 +558,8 @@ def test_add_reverse_edges():
bg
=
dgl
.
add_reverse_edges
(
g
,
copy_ndata
=
True
,
copy_edata
=
True
,
ignore_bipartite
=
True
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
bg
.
num
ber_of
_nodes
(
"user"
)
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
bg
.
num
ber_of
_nodes
(
"game"
)
assert
g
.
num_nodes
(
"user"
)
==
bg
.
num_nodes
(
"user"
)
assert
g
.
num_nodes
(
"game"
)
==
bg
.
num_nodes
(
"game"
)
assert
F
.
array_equal
(
g
.
nodes
[
"game"
].
data
[
"hv"
],
bg
.
nodes
[
"game"
].
data
[
"hv"
]
)
...
...
@@ -592,7 +592,7 @@ def test_simple_graph():
assert
g
.
is_multigraph
sg
=
dgl
.
to_simple_graph
(
g
)
assert
not
sg
.
is_multigraph
assert
sg
.
num
ber_of
_edges
()
==
3
assert
sg
.
num_edges
()
==
3
src
,
dst
=
sg
.
edges
()
eset
=
set
(
zip
(
list
(
F
.
asnumpy
(
src
)),
list
(
F
.
asnumpy
(
dst
))))
assert
eset
==
set
(
elist
)
...
...
@@ -607,7 +607,7 @@ def _test_bidirected_graph():
elist
.
append
((
1
,
2
))
elist
=
set
(
elist
)
big
=
dgl
.
to_bidirected_stale
(
g
,
out_readonly
)
assert
big
.
num
ber_of
_edges
()
==
num_edges
assert
big
.
num_edges
()
==
num_edges
src
,
dst
=
big
.
edges
()
eset
=
set
(
zip
(
list
(
F
.
asnumpy
(
src
)),
list
(
F
.
asnumpy
(
dst
))))
assert
eset
==
set
(
elist
)
...
...
@@ -697,7 +697,7 @@ def create_large_graph(num_nodes, idtype=F.int64):
@
unittest
.
skipIf
(
F
.
_default_context_str
==
"gpu"
,
reason
=
"GPU not implemented"
)
def
test_partition_with_halo
():
g
=
create_large_graph
(
1000
)
node_part
=
np
.
random
.
choice
(
4
,
g
.
num
ber_of
_nodes
())
node_part
=
np
.
random
.
choice
(
4
,
g
.
num_nodes
())
subgs
,
_
,
_
=
dgl
.
transforms
.
partition_graph_with_halo
(
g
,
node_part
,
2
,
reshuffle
=
True
)
...
...
@@ -739,9 +739,9 @@ def test_metis_partition(idtype):
def
check_metis_partition_with_constraint
(
g
):
ntypes
=
np
.
zeros
((
g
.
num
ber_of
_nodes
(),),
dtype
=
np
.
int32
)
ntypes
[
0
:
int
(
g
.
num
ber_of
_nodes
()
/
4
)]
=
1
ntypes
[
int
(
g
.
num
ber_of
_nodes
()
*
3
/
4
)
:]
=
2
ntypes
=
np
.
zeros
((
g
.
num_nodes
(),),
dtype
=
np
.
int32
)
ntypes
[
0
:
int
(
g
.
num_nodes
()
/
4
)]
=
1
ntypes
[
int
(
g
.
num_nodes
()
*
3
/
4
)
:]
=
2
subgs
=
dgl
.
transforms
.
metis_partition
(
g
,
4
,
extra_cached_hops
=
1
,
balance_ntypes
=
ntypes
)
...
...
@@ -779,8 +779,8 @@ def check_metis_partition(g, extra_hops):
assert
np
.
sum
(
F
.
asnumpy
(
subg
.
ndata
[
"part_id"
])
==
part_id
)
==
len
(
lnode_ids
)
assert
num_inner_nodes
==
g
.
num
ber_of
_nodes
()
print
(
g
.
num
ber_of
_edges
()
-
num_inner_edges
)
assert
num_inner_nodes
==
g
.
num_nodes
()
print
(
g
.
num_edges
()
-
num_inner_edges
)
if
extra_hops
==
0
:
return
...
...
@@ -791,7 +791,7 @@ def check_metis_partition(g, extra_hops):
)
num_inner_nodes
=
0
num_inner_edges
=
0
edge_cnts
=
np
.
zeros
((
g
.
num
ber_of
_edges
(),))
edge_cnts
=
np
.
zeros
((
g
.
num_edges
(),))
if
subgs
is
not
None
:
for
part_id
,
subg
in
subgs
.
items
():
lnode_ids
=
np
.
nonzero
(
F
.
asnumpy
(
subg
.
ndata
[
"inner_node"
]))[
0
]
...
...
@@ -816,7 +816,7 @@ def check_metis_partition(g, extra_hops):
orig_ids
=
subg
.
ndata
[
"orig_id"
]
inner_node
=
F
.
asnumpy
(
subg
.
ndata
[
"inner_node"
])
for
nid
in
range
(
subg
.
num
ber_of
_nodes
()):
for
nid
in
range
(
subg
.
num_nodes
()):
neighs
=
subg
.
predecessors
(
nid
)
old_neighs1
=
F
.
gather_row
(
orig_ids
,
neighs
)
old_nid
=
F
.
asnumpy
(
orig_ids
[
nid
])
...
...
@@ -830,8 +830,8 @@ def check_metis_partition(g, extra_hops):
# Normally, local edges are only counted once.
assert
np
.
all
(
edge_cnts
==
1
)
assert
num_inner_nodes
==
g
.
num
ber_of
_nodes
()
print
(
g
.
num
ber_of
_edges
()
-
num_inner_edges
)
assert
num_inner_nodes
==
g
.
num_nodes
()
print
(
g
.
num_edges
()
-
num_inner_edges
)
@
unittest
.
skipIf
(
...
...
@@ -839,7 +839,7 @@ def check_metis_partition(g, extra_hops):
)
def
test_reorder_nodes
():
g
=
create_large_graph
(
1000
)
new_nids
=
np
.
random
.
permutation
(
g
.
num
ber_of
_nodes
())
new_nids
=
np
.
random
.
permutation
(
g
.
num_nodes
())
# TODO(zhengda) we need to test both CSR and COO.
new_g
=
dgl
.
partition
.
reorder_nodes
(
g
,
new_nids
)
new_in_deg
=
new_g
.
in_degrees
()
...
...
@@ -851,14 +851,14 @@ def test_reorder_nodes():
assert
np
.
all
(
F
.
asnumpy
(
new_in_deg
==
new_in_deg1
))
assert
np
.
all
(
F
.
asnumpy
(
new_out_deg
==
new_out_deg1
))
orig_ids
=
F
.
asnumpy
(
new_g
.
ndata
[
"orig_id"
])
for
nid
in
range
(
g
.
num
ber_of
_nodes
()):
for
nid
in
range
(
g
.
num_nodes
()):
neighs
=
F
.
asnumpy
(
g
.
successors
(
nid
))
new_neighs1
=
new_nids
[
neighs
]
new_nid
=
new_nids
[
nid
]
new_neighs2
=
new_g
.
successors
(
new_nid
)
assert
np
.
all
(
np
.
sort
(
new_neighs1
)
==
np
.
sort
(
F
.
asnumpy
(
new_neighs2
)))
for
nid
in
range
(
new_g
.
num
ber_of
_nodes
()):
for
nid
in
range
(
new_g
.
num_nodes
()):
neighs
=
F
.
asnumpy
(
new_g
.
successors
(
nid
))
old_neighs1
=
orig_ids
[
neighs
]
old_nid
=
orig_ids
[
nid
]
...
...
@@ -1110,7 +1110,7 @@ def test_to_simple(idtype):
sg
=
dgl
.
to_simple
(
g
,
writeback_mapping
=
False
,
copy_ndata
=
False
)
for
ntype
in
g
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
sg
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
sg
.
num_nodes
(
ntype
)
assert
"h"
not
in
sg
.
nodes
[
"user"
].
data
assert
"hh"
not
in
sg
.
nodes
[
"user"
].
data
...
...
@@ -1211,20 +1211,20 @@ def test_add_edges(idtype):
v
=
1
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
3
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
3
u
=
[
0
]
v
=
[
1
]
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
4
u
=
F
.
tensor
(
u
,
dtype
=
idtype
)
v
=
F
.
tensor
(
v
,
dtype
=
idtype
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
5
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
5
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
0
,
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
1
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -1232,8 +1232,8 @@ def test_add_edges(idtype):
g
=
dgl
.
add_edges
(
g
,
0
,
[])
g
=
dgl
.
add_edges
(
g
,
[],
0
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
5
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
5
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
0
,
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
1
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -1243,8 +1243,8 @@ def test_add_edges(idtype):
u
=
F
.
tensor
([
0
,
1
],
dtype
=
idtype
)
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1260,8 +1260,8 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
=
dgl
.
add_edges
(
g
,
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
()
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1278,8 +1278,8 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
=
dgl
.
add_edges
(
g
,
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
2
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
,
2
],
dtype
=
idtype
))
...
...
@@ -1296,23 +1296,23 @@ def test_add_edges(idtype):
v
=
1
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
3
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
3
u
=
[
0
]
v
=
[
1
]
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
4
u
=
F
.
tensor
(
u
,
dtype
=
idtype
)
v
=
F
.
tensor
(
v
,
dtype
=
idtype
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
5
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
5
u
,
v
=
g
.
edges
(
form
=
"uv"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
0
,
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
1
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -1327,9 +1327,9 @@ def test_add_edges(idtype):
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
)
assert
g
.
device
==
F
.
ctx
()
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1354,9 +1354,9 @@ def test_add_edges(idtype):
"hh"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()),
}
g
=
dgl
.
add_edges
(
g
,
u
,
v
,
e_feat
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_edges
()
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_edges
()
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
,
2
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1374,11 +1374,11 @@ def test_add_edges(idtype):
u
=
F
.
tensor
([
0
,
2
],
dtype
=
idtype
)
v
=
F
.
tensor
([
2
,
3
],
dtype
=
idtype
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
,
etype
=
"plays"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
6
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
g
.
num_edges
(
"plays"
)
==
6
assert
g
.
num_edges
(
"develops"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"plays"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
1
,
2
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
0
,
1
,
1
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1400,11 +1400,11 @@ def test_add_edges(idtype):
F
.
tensor
([
2
,
2
,
1
,
1
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
=
dgl
.
add_edges
(
g
,
u
,
v
,
data
=
e_feat
,
etype
=
"develops"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
3
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
6
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
4
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
3
assert
g
.
num_edges
(
"plays"
)
==
6
assert
g
.
num_edges
(
"develops"
)
==
4
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"develops"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
0
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
2
,
3
],
dtype
=
idtype
))
...
...
@@ -1425,8 +1425,8 @@ def test_add_nodes(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
new_g
=
dgl
.
add_nodes
(
g
,
1
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
new_g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num_nodes
()
==
3
assert
new_g
.
num_nodes
()
==
4
assert
F
.
array_equal
(
new_g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
0
],
dtype
=
idtype
))
# zero node graph
...
...
@@ -1435,7 +1435,7 @@ def test_add_nodes(idtype):
g
=
dgl
.
add_nodes
(
g
,
1
,
data
=
{
"h"
:
F
.
copy_to
(
F
.
tensor
([
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())}
)
assert
g
.
num
ber_of
_nodes
()
==
4
assert
g
.
num_nodes
()
==
4
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
2
],
dtype
=
idtype
))
# bipartite graph
...
...
@@ -1450,14 +1450,14 @@ def test_add_nodes(idtype):
data
=
{
"h"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())},
ntype
=
"user"
,
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num_nodes
(
"user"
)
==
4
assert
g
.
num_nodes
(
"game"
)
==
3
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
0
,
0
,
2
,
2
],
dtype
=
idtype
)
)
g
=
dgl
.
add_nodes
(
g
,
2
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
5
assert
g
.
num_nodes
(
"user"
)
==
4
assert
g
.
num_nodes
(
"game"
)
==
5
# heterogeneous graph
g
=
create_test_heterograph3
(
idtype
)
...
...
@@ -1468,9 +1468,9 @@ def test_add_nodes(idtype):
data
=
{
"h"
:
F
.
copy_to
(
F
.
tensor
([
2
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())},
ntype
=
"game"
,
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
4
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
4
assert
g
.
num_nodes
(
"game"
)
==
4
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
,
0
],
dtype
=
idtype
)
)
...
...
@@ -1485,33 +1485,33 @@ def test_remove_edges(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
e
=
0
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
e
=
[
0
]
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
e
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_edges
()
==
0
# has node data
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
remove_edges
(
g
,
1
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
ndata
[
"h"
],
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
))
# has edge data
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
edata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
remove_edges
(
g
,
0
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
edata
[
"h"
],
F
.
tensor
([
2
],
dtype
=
idtype
))
# invalid eid
...
...
@@ -1530,7 +1530,7 @@ def test_remove_edges(idtype):
)
e
=
0
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
...
...
@@ -1541,13 +1541,13 @@ def test_remove_edges(idtype):
)
e
=
[
0
]
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
e
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
=
dgl
.
remove_edges
(
g
,
e
)
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_edges
()
==
0
# has data
g
=
dgl
.
heterograph
(
...
...
@@ -1563,7 +1563,7 @@ def test_remove_edges(idtype):
)
g
.
edata
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
remove_edges
(
g
,
1
)
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_edges
()
==
1
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
],
dtype
=
idtype
)
)
...
...
@@ -1578,7 +1578,7 @@ def test_remove_edges(idtype):
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
=
dgl
.
remove_edges
(
g
,
1
,
etype
=
"plays"
)
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
3
assert
g
.
num_edges
(
"plays"
)
==
3
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"plays"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
1
],
dtype
=
idtype
))
...
...
@@ -1587,7 +1587,7 @@ def test_remove_edges(idtype):
)
# remove all edges of 'develops'
g
=
dgl
.
remove_edges
(
g
,
[
0
,
1
],
etype
=
"develops"
)
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
0
assert
g
.
num_edges
(
"develops"
)
==
0
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
)
)
...
...
@@ -1729,21 +1729,21 @@ def test_remove_nodes(idtype):
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
0
g
=
dgl
.
remove_nodes
(
g
,
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
[
1
]
g
=
dgl
.
remove_nodes
(
g
,
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
0
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
0
g
=
dgl
.
graph
(([
0
,
1
],
[
1
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
n
=
F
.
tensor
([
2
],
dtype
=
idtype
)
g
=
dgl
.
remove_nodes
(
g
,
n
)
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -1761,8 +1761,8 @@ def test_remove_nodes(idtype):
g
.
ndata
[
"hv"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
.
edata
[
"he"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
remove_nodes
(
g
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
g
.
num
ber_of
_nodes
()
==
2
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
()
==
2
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -1777,9 +1777,9 @@ def test_remove_nodes(idtype):
)
n
=
0
g
=
dgl
.
remove_nodes
(
g
,
n
,
ntype
=
"user"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
(
"user"
)
==
1
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
],
dtype
=
idtype
))
...
...
@@ -1790,9 +1790,9 @@ def test_remove_nodes(idtype):
)
n
=
[
1
]
g
=
dgl
.
remove_nodes
(
g
,
n
,
ntype
=
"user"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
3
assert
g
.
num
ber_of
_edges
()
==
1
assert
g
.
num_nodes
(
"user"
)
==
1
assert
g
.
num_nodes
(
"game"
)
==
3
assert
g
.
num_edges
()
==
1
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
1
],
dtype
=
idtype
))
...
...
@@ -1803,9 +1803,9 @@ def test_remove_nodes(idtype):
)
n
=
F
.
tensor
([
0
],
dtype
=
idtype
)
g
=
dgl
.
remove_nodes
(
g
,
n
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
2
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
()
==
2
assert
g
.
num_nodes
(
"user"
)
==
2
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
()
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
...
...
@@ -1816,11 +1816,11 @@ def test_remove_nodes(idtype):
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
=
dgl
.
remove_nodes
(
g
,
0
,
ntype
=
"game"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
1
assert
g
.
num
ber_of
_nodes
(
"developer"
)
==
2
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num
ber_of
_edges
(
"develops"
)
==
1
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
1
assert
g
.
num_nodes
(
"developer"
)
==
2
assert
g
.
num_edges
(
"plays"
)
==
2
assert
g
.
num_edges
(
"develops"
)
==
1
assert
F
.
array_equal
(
g
.
nodes
[
"user"
].
data
[
"h"
],
F
.
tensor
([
1
,
1
,
1
],
dtype
=
idtype
)
)
...
...
@@ -2006,8 +2006,8 @@ def test_add_selfloop(idtype):
)
g
.
ndata
[
"hn"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
add_self_loop
(
g
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
6
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
6
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
0
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
,
1
,
0
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2036,8 +2036,8 @@ def test_add_selfloop(idtype):
)
g
.
ndata
[
"hn"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
add_self_loop
(
g
,
fill_data
=
1
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
6
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
6
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
0
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
,
1
,
0
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2059,8 +2059,8 @@ def test_add_selfloop(idtype):
)
g
.
ndata
[
"hn"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
add_self_loop
(
g
,
fill_data
=
"sum"
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
6
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
6
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
0
,
0
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
2
,
1
,
0
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2101,10 +2101,10 @@ def test_add_selfloop(idtype):
F
.
tensor
([[
0.0
,
1.0
],
[
1.0
,
2.0
]]),
ctx
=
F
.
ctx
()
)
g
=
dgl
.
add_self_loop
(
g
,
etype
=
"follows"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
5
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
(
"follows"
)
==
5
assert
g
.
num_edges
(
"plays"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"follows"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2125,10 +2125,10 @@ def test_add_selfloop(idtype):
F
.
tensor
([[
0
,
1
],
[
1
,
2
]],
dtype
=
idtype
),
ctx
=
F
.
ctx
()
)
g
=
dgl
.
add_self_loop
(
g
,
fill_data
=
1
,
etype
=
"follows"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
5
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
(
"follows"
)
==
5
assert
g
.
num_edges
(
"plays"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"follows"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2170,10 +2170,10 @@ def test_add_selfloop(idtype):
)
g
.
edges
[
"plays"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
tensor
([
1.0
,
2.0
]),
ctx
=
F
.
ctx
())
g
=
dgl
.
add_self_loop
(
g
,
fill_data
=
"mean"
,
etype
=
"follows"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
5
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
(
"follows"
)
==
5
assert
g
.
num_edges
(
"plays"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"follows"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
,
2
,
0
,
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
,
0
,
1
,
2
],
dtype
=
idtype
))
...
...
@@ -2200,8 +2200,8 @@ def test_remove_selfloop(idtype):
g
=
dgl
.
graph
(([
0
,
0
,
0
,
1
],
[
1
,
0
,
0
,
2
]),
idtype
=
idtype
,
device
=
F
.
ctx
())
g
.
edata
[
"he"
]
=
F
.
copy_to
(
F
.
tensor
([
1
,
2
,
3
,
4
],
dtype
=
idtype
),
ctx
=
F
.
ctx
())
g
=
dgl
.
remove_self_loop
(
g
)
assert
g
.
num
ber_of
_nodes
()
==
3
assert
g
.
num
ber_of
_edges
()
==
2
assert
g
.
num_nodes
()
==
3
assert
g
.
num_edges
()
==
2
assert
F
.
array_equal
(
g
.
edata
[
"he"
],
F
.
tensor
([
1
,
4
],
dtype
=
idtype
))
# bipartite graph
...
...
@@ -2220,10 +2220,10 @@ def test_remove_selfloop(idtype):
g
=
create_test_heterograph4
(
idtype
)
g
=
dgl
.
remove_self_loop
(
g
,
etype
=
"follows"
)
assert
g
.
num
ber_of
_nodes
(
"user"
)
==
3
assert
g
.
num
ber_of
_nodes
(
"game"
)
==
2
assert
g
.
num
ber_of
_edges
(
"follows"
)
==
2
assert
g
.
num
ber_of
_edges
(
"plays"
)
==
2
assert
g
.
num_nodes
(
"user"
)
==
3
assert
g
.
num_nodes
(
"game"
)
==
2
assert
g
.
num_edges
(
"follows"
)
==
2
assert
g
.
num_edges
(
"plays"
)
==
2
u
,
v
=
g
.
edges
(
form
=
"uv"
,
order
=
"eid"
,
etype
=
"follows"
)
assert
F
.
array_equal
(
u
,
F
.
tensor
([
1
,
2
],
dtype
=
idtype
))
assert
F
.
array_equal
(
v
,
F
.
tensor
([
0
,
1
],
dtype
=
idtype
))
...
...
@@ -2250,8 +2250,8 @@ def test_remove_selfloop(idtype):
g
.
set_batch_num_nodes
(
F
.
tensor
([
3
,
2
],
dtype
=
F
.
int64
))
g
.
set_batch_num_edges
(
F
.
tensor
([
4
,
3
],
dtype
=
F
.
int64
))
g
=
dgl
.
remove_self_loop
(
g
)
assert
g
.
num
ber_of
_nodes
()
==
5
assert
g
.
num
ber_of
_edges
()
==
3
assert
g
.
num_nodes
()
==
5
assert
g
.
num_edges
()
==
3
assert
F
.
array_equal
(
g
.
batch_num_nodes
(),
F
.
tensor
([
3
,
2
],
dtype
=
F
.
int64
))
assert
F
.
array_equal
(
g
.
batch_num_edges
(),
F
.
tensor
([
2
,
1
],
dtype
=
F
.
int64
))
...
...
tests/python/mxnet/test_nn.py
View file @
5b409bf7
...
...
@@ -198,7 +198,7 @@ def test_gat_conv(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
# test residual connection
gat
=
nn
.
GATConv
(
10
,
out_dim
,
num_heads
,
residual
=
True
)
...
...
@@ -222,7 +222,7 @@ def test_gat_conv_bi(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -291,7 +291,7 @@ def test_gg_conv():
# test#1: basic
h0
=
F
.
randn
((
20
,
10
))
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num
ber_of
_edges
()).
as_in_context
(
ctx
)
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num_edges
()).
as_in_context
(
ctx
)
h1
=
gg_conv
(
g
,
h0
,
etypes
)
assert
h1
.
shape
==
(
20
,
20
)
...
...
@@ -421,7 +421,7 @@ def test_dense_sage_conv(idtype, g, out_dim):
F
.
randn
((
g
.
number_of_dst_nodes
(),
5
)),
)
else
:
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
out_sage
=
sage
(
g
,
feat
)
out_dense_sage
=
dense_sage
(
adj
,
feat
)
...
...
@@ -508,7 +508,7 @@ def test_gmm_conv(g, idtype):
gmm_conv
=
nn
.
GMMConv
(
5
,
2
,
5
,
3
,
"max"
)
gmm_conv
.
initialize
(
ctx
=
ctx
)
h0
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
pseudo
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
5
))
pseudo
=
F
.
randn
((
g
.
num_edges
(),
5
))
h1
=
gmm_conv
(
g
,
h0
,
pseudo
)
assert
h1
.
shape
==
(
g
.
number_of_dst_nodes
(),
2
)
...
...
@@ -523,7 +523,7 @@ def test_gmm_conv_bi(g, idtype):
# test #1: basic
h0
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
hd
=
F
.
randn
((
g
.
number_of_dst_nodes
(),
4
))
pseudo
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
5
))
pseudo
=
F
.
randn
((
g
.
num_edges
(),
5
))
h1
=
gmm_conv
(
g
,
(
h0
,
hd
),
pseudo
)
assert
h1
.
shape
==
(
g
.
number_of_dst_nodes
(),
2
)
...
...
@@ -537,7 +537,7 @@ def test_nn_conv(g, idtype):
nn_conv
.
initialize
(
ctx
=
ctx
)
# test #1: basic
h0
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num
ber_of
_edges
()).
as_in_context
(
ctx
)
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num_edges
()).
as_in_context
(
ctx
)
h1
=
nn_conv
(
g
,
h0
,
etypes
)
assert
h1
.
shape
==
(
g
.
number_of_dst_nodes
(),
2
)
...
...
@@ -552,7 +552,7 @@ def test_nn_conv_bi(g, idtype):
# test #1: basic
h0
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
hd
=
F
.
randn
((
g
.
number_of_dst_nodes
(),
4
))
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num
ber_of
_edges
()).
as_in_context
(
ctx
)
etypes
=
nd
.
random
.
randint
(
0
,
4
,
g
.
num_edges
()).
as_in_context
(
ctx
)
h1
=
nn_conv
(
g
,
(
h0
,
hd
),
etypes
)
assert
h1
.
shape
==
(
g
.
number_of_dst_nodes
(),
2
)
...
...
@@ -568,9 +568,9 @@ def test_sg_conv(out_dim):
print
(
sgc
)
# test #1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
sgc
(
g
,
h0
)
assert
h1
.
shape
==
(
g
.
num
ber_of
_nodes
(),
out_dim
)
assert
h1
.
shape
==
(
g
.
num_nodes
(),
out_dim
)
def
test_set2set
():
...
...
@@ -582,13 +582,13 @@ def test_set2set():
print
(
s2s
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
s2s
(
g
,
h0
)
assert
h1
.
shape
[
0
]
==
1
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
# test#2: batched graph
bg
=
dgl
.
batch
([
g
,
g
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
s2s
(
bg
,
h0
)
assert
h1
.
shape
[
0
]
==
3
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
...
...
@@ -601,13 +601,13 @@ def test_glob_att_pool():
gap
.
initialize
(
ctx
=
ctx
)
print
(
gap
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
gap
(
g
,
h0
)
assert
h1
.
shape
[
0
]
==
1
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
# test#2: batched graph
bg
=
dgl
.
batch
([
g
,
g
,
g
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
gap
(
bg
,
h0
)
assert
h1
.
shape
[
0
]
==
4
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
...
...
@@ -622,7 +622,7 @@ def test_simple_pool():
print
(
sum_pool
,
avg_pool
,
max_pool
,
sort_pool
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
sum_pool
(
g
,
h0
)
check_close
(
F
.
squeeze
(
h1
,
0
),
F
.
sum
(
h0
,
0
))
h1
=
avg_pool
(
g
,
h0
)
...
...
@@ -635,7 +635,7 @@ def test_simple_pool():
# test#2: batched graph
g_
=
dgl
.
from_networkx
(
nx
.
path_graph
(
5
)).
to
(
F
.
ctx
())
bg
=
dgl
.
batch
([
g
,
g_
,
g
,
g_
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
sum_pool
(
bg
,
h0
)
truth
=
mx
.
nd
.
stack
(
F
.
sum
(
h0
[:
15
],
0
),
...
...
@@ -680,7 +680,7 @@ def test_rgcn(O):
g
=
dgl
.
from_scipy
(
sp
.
sparse
.
random
(
100
,
100
,
density
=
0.1
)).
to
(
F
.
ctx
())
# 5 etypes
R
=
5
for
i
in
range
(
g
.
num
ber_of
_edges
()):
for
i
in
range
(
g
.
num_edges
()):
etype
.
append
(
i
%
5
)
B
=
2
I
=
10
...
...
@@ -701,7 +701,7 @@ def test_rgcn(O):
assert
list
(
h_new
.
shape
)
==
[
100
,
O
]
# with norm
norm
=
nd
.
zeros
((
g
.
num
ber_of
_edges
(),
1
),
ctx
=
ctx
)
norm
=
nd
.
zeros
((
g
.
num_edges
(),
1
),
ctx
=
ctx
)
rgc_basis
=
nn
.
RelGraphConv
(
I
,
O
,
R
,
"basis"
,
B
)
rgc_basis
.
initialize
(
ctx
=
ctx
)
...
...
@@ -768,7 +768,7 @@ def test_sequential():
graph
.
ndata
[
"h"
]
=
n_feat
graph
.
update_all
(
fn
.
copy_u
(
"h"
,
"m"
),
fn
.
sum
(
"m"
,
"h"
))
n_feat
+=
graph
.
ndata
[
"h"
]
return
n_feat
.
reshape
(
graph
.
num
ber_of
_nodes
()
//
2
,
2
,
-
1
).
sum
(
1
)
return
n_feat
.
reshape
(
graph
.
num_nodes
()
//
2
,
2
,
-
1
).
sum
(
1
)
g1
=
dgl
.
from_networkx
(
nx
.
erdos_renyi_graph
(
32
,
0.05
)).
to
(
F
.
ctx
())
g2
=
dgl
.
from_networkx
(
nx
.
erdos_renyi_graph
(
16
,
0.2
)).
to
(
F
.
ctx
())
...
...
tests/python/pytorch/distributed/optim/test_dist_optim.py
View file @
5b409bf7
...
...
@@ -96,7 +96,7 @@ def run_client(graph_name, cli_id, part_id, server_count):
)
g
=
DistGraph
(
graph_name
,
gpb
=
gpb
)
policy
=
dgl
.
distributed
.
PartitionPolicy
(
"node"
,
g
.
get_partition_book
())
num_nodes
=
g
.
num
ber_of
_nodes
()
num_nodes
=
g
.
num_nodes
()
emb_dim
=
4
dgl_emb
=
DistEmbedding
(
num_nodes
,
...
...
tests/python/pytorch/nn/test_nn.py
View file @
5b409bf7
...
...
@@ -267,7 +267,7 @@ def test_set2set():
print
(
s2s
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
s2s
(
g
,
h0
)
assert
h1
.
shape
[
0
]
==
1
and
h1
.
shape
[
1
]
==
10
and
h1
.
dim
()
==
2
...
...
@@ -275,7 +275,7 @@ def test_set2set():
g1
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
11
)).
to
(
F
.
ctx
())
g2
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
5
)).
to
(
F
.
ctx
())
bg
=
dgl
.
batch
([
g
,
g1
,
g2
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
s2s
(
bg
,
h0
)
assert
h1
.
shape
[
0
]
==
3
and
h1
.
shape
[
1
]
==
10
and
h1
.
dim
()
==
2
...
...
@@ -293,13 +293,13 @@ def test_glob_att_pool():
th
.
save
(
gap
,
tmp_buffer
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
gap
(
g
,
h0
)
assert
h1
.
shape
[
0
]
==
1
and
h1
.
shape
[
1
]
==
10
and
h1
.
dim
()
==
2
# test#2: batched graph
bg
=
dgl
.
batch
([
g
,
g
,
g
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
gap
(
bg
,
h0
)
assert
h1
.
shape
[
0
]
==
4
and
h1
.
shape
[
1
]
==
10
and
h1
.
dim
()
==
2
...
...
@@ -316,7 +316,7 @@ def test_simple_pool():
print
(
sum_pool
,
avg_pool
,
max_pool
,
sort_pool
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
sum_pool
=
sum_pool
.
to
(
ctx
)
avg_pool
=
avg_pool
.
to
(
ctx
)
max_pool
=
max_pool
.
to
(
ctx
)
...
...
@@ -333,7 +333,7 @@ def test_simple_pool():
# test#2: batched graph
g_
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
5
)).
to
(
F
.
ctx
())
bg
=
dgl
.
batch
([
g
,
g_
,
g
,
g_
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
sum_pool
(
bg
,
h0
)
truth
=
th
.
stack
(
[
...
...
@@ -390,7 +390,7 @@ def test_set_trans():
print
(
st_enc_0
,
st_enc_1
,
st_dec
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
50
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
50
))
h1
=
st_enc_0
(
g
,
h0
)
assert
h1
.
shape
==
h0
.
shape
h1
=
st_enc_1
(
g
,
h0
)
...
...
@@ -402,7 +402,7 @@ def test_set_trans():
g1
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
5
))
g2
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
10
))
bg
=
dgl
.
batch
([
g
,
g1
,
g2
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
50
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
50
))
h1
=
st_enc_0
(
bg
,
h0
)
assert
h1
.
shape
==
h0
.
shape
h1
=
st_enc_1
(
bg
,
h0
)
...
...
@@ -421,14 +421,14 @@ def test_rgcn(idtype, O):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
# 5 etypes
R
=
5
for
i
in
range
(
g
.
num
ber_of
_edges
()):
for
i
in
range
(
g
.
num_edges
()):
etype
.
append
(
i
%
5
)
B
=
2
I
=
10
h
=
th
.
randn
((
100
,
I
)).
to
(
ctx
)
r
=
th
.
tensor
(
etype
).
to
(
ctx
)
norm
=
th
.
rand
((
g
.
num
ber_of
_edges
(),
1
)).
to
(
ctx
)
norm
=
th
.
rand
((
g
.
num_edges
(),
1
)).
to
(
ctx
)
sorted_r
,
idx
=
th
.
sort
(
r
)
sorted_g
=
dgl
.
reorder_graph
(
g
,
...
...
@@ -482,13 +482,13 @@ def test_rgcn_default_nbasis(idtype, O):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
# 5 etypes
R
=
5
for
i
in
range
(
g
.
num
ber_of
_edges
()):
for
i
in
range
(
g
.
num_edges
()):
etype
.
append
(
i
%
5
)
I
=
10
h
=
th
.
randn
((
100
,
I
)).
to
(
ctx
)
r
=
th
.
tensor
(
etype
).
to
(
ctx
)
norm
=
th
.
rand
((
g
.
num
ber_of
_edges
(),
1
)).
to
(
ctx
)
norm
=
th
.
rand
((
g
.
num_edges
(),
1
)).
to
(
ctx
)
sorted_r
,
idx
=
th
.
sort
(
r
)
sorted_g
=
dgl
.
reorder_graph
(
g
,
...
...
@@ -552,7 +552,7 @@ def test_gat_conv(g, idtype, out_dim, num_heads):
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
# test residual connection
gat
=
nn
.
GATConv
(
5
,
out_dim
,
num_heads
,
residual
=
True
)
...
...
@@ -576,7 +576,7 @@ def test_gat_conv_bi(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -598,7 +598,7 @@ def test_gatv2_conv(g, idtype, out_dim, num_heads):
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
# test residual connection
gat
=
nn
.
GATConv
(
5
,
out_dim
,
num_heads
,
residual
=
True
)
...
...
@@ -622,7 +622,7 @@ def test_gatv2_conv_bi(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -640,17 +640,17 @@ def test_egat_conv(g, idtype, out_node_feats, out_edge_feats, num_heads):
out_edge_feats
=
out_edge_feats
,
num_heads
=
num_heads
,
)
nfeat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
10
))
efeat
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
5
))
nfeat
=
F
.
randn
((
g
.
num_nodes
(),
10
))
efeat
=
F
.
randn
((
g
.
num_edges
(),
5
))
egat
=
egat
.
to
(
ctx
)
h
,
f
=
egat
(
g
,
nfeat
,
efeat
)
th
.
save
(
egat
,
tmp_buffer
)
assert
h
.
shape
==
(
g
.
num
ber_of
_nodes
(),
num_heads
,
out_node_feats
)
assert
f
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
out_edge_feats
)
assert
h
.
shape
==
(
g
.
num_nodes
(),
num_heads
,
out_node_feats
)
assert
f
.
shape
==
(
g
.
num_edges
(),
num_heads
,
out_edge_feats
)
_
,
_
,
attn
=
egat
(
g
,
nfeat
,
efeat
,
True
)
assert
attn
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
attn
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -672,16 +672,16 @@ def test_egat_conv_bi(g, idtype, out_node_feats, out_edge_feats, num_heads):
F
.
randn
((
g
.
number_of_src_nodes
(),
10
)),
F
.
randn
((
g
.
number_of_dst_nodes
(),
15
)),
)
efeat
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
7
))
efeat
=
F
.
randn
((
g
.
num_edges
(),
7
))
egat
=
egat
.
to
(
ctx
)
h
,
f
=
egat
(
g
,
nfeat
,
efeat
)
th
.
save
(
egat
,
tmp_buffer
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_node_feats
)
assert
f
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
out_edge_feats
)
assert
f
.
shape
==
(
g
.
num_edges
(),
num_heads
,
out_edge_feats
)
_
,
_
,
attn
=
egat
(
g
,
nfeat
,
efeat
,
True
)
assert
attn
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
attn
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -751,7 +751,7 @@ def test_sgc_conv(g, idtype, out_dim):
# test pickle
th
.
save
(
sgc
,
tmp_buffer
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
sgc
=
sgc
.
to
(
ctx
)
h
=
sgc
(
g
,
feat
)
...
...
@@ -772,7 +772,7 @@ def test_appnp_conv(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
appnp
=
nn
.
APPNPConv
(
10
,
0.1
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
appnp
=
appnp
.
to
(
ctx
)
# test pickle
...
...
@@ -788,7 +788,7 @@ def test_appnp_conv_e_weight(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
appnp
=
nn
.
APPNPConv
(
10
,
0.1
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
eweight
=
F
.
ones
((
g
.
num_edges
(),))
appnp
=
appnp
.
to
(
ctx
)
...
...
@@ -805,7 +805,7 @@ def test_gcn2conv_e_weight(g, idtype, bias):
gcn2conv
=
nn
.
GCN2Conv
(
5
,
layer
=
2
,
alpha
=
0.5
,
bias
=
bias
,
project_initial_features
=
True
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
eweight
=
F
.
ones
((
g
.
num_edges
(),))
gcn2conv
=
gcn2conv
.
to
(
ctx
)
res
=
feat
...
...
@@ -819,7 +819,7 @@ def test_sgconv_e_weight(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
sgconv
=
nn
.
SGConv
(
5
,
5
,
3
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
eweight
=
F
.
ones
((
g
.
num_edges
(),))
sgconv
=
sgconv
.
to
(
ctx
)
h
=
sgconv
(
g
,
feat
,
edge_weight
=
eweight
)
...
...
@@ -833,7 +833,7 @@ def test_tagconv_e_weight(g, idtype):
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
conv
=
nn
.
TAGConv
(
5
,
5
,
bias
=
True
)
conv
=
conv
.
to
(
ctx
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
eweight
=
F
.
ones
((
g
.
num_edges
(),))
conv
=
conv
.
to
(
ctx
)
h
=
conv
(
g
,
feat
,
edge_weight
=
eweight
)
...
...
@@ -938,8 +938,8 @@ def test_gated_graph_conv(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
ggconv
=
nn
.
GatedGraphConv
(
5
,
10
,
5
,
3
)
etypes
=
th
.
arange
(
g
.
num
ber_of
_edges
())
%
3
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
etypes
=
th
.
arange
(
g
.
num_edges
())
%
3
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
ggconv
=
ggconv
.
to
(
ctx
)
etypes
=
etypes
.
to
(
ctx
)
...
...
@@ -954,8 +954,8 @@ def test_gated_graph_conv_one_etype(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
ggconv
=
nn
.
GatedGraphConv
(
5
,
10
,
5
,
1
)
etypes
=
th
.
zeros
(
g
.
num
ber_of
_edges
())
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
etypes
=
th
.
zeros
(
g
.
num_edges
())
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
ggconv
=
ggconv
.
to
(
ctx
)
etypes
=
etypes
.
to
(
ctx
)
...
...
@@ -976,7 +976,7 @@ def test_nn_conv(g, idtype):
edge_func
=
th
.
nn
.
Linear
(
4
,
5
*
10
)
nnconv
=
nn
.
NNConv
(
5
,
10
,
edge_func
,
"mean"
)
feat
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
efeat
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
4
))
efeat
=
F
.
randn
((
g
.
num_edges
(),
4
))
nnconv
=
nnconv
.
to
(
ctx
)
h
=
nnconv
(
g
,
feat
,
efeat
)
# currently we only do shape check
...
...
@@ -992,7 +992,7 @@ def test_nn_conv_bi(g, idtype):
nnconv
=
nn
.
NNConv
((
5
,
2
),
10
,
edge_func
,
"mean"
)
feat
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
feat_dst
=
F
.
randn
((
g
.
number_of_dst_nodes
(),
2
))
efeat
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
4
))
efeat
=
F
.
randn
((
g
.
num_edges
(),
4
))
nnconv
=
nnconv
.
to
(
ctx
)
h
=
nnconv
(
g
,
(
feat
,
feat_dst
),
efeat
)
# currently we only do shape check
...
...
@@ -1005,8 +1005,8 @@ def test_gmm_conv(g, idtype):
g
=
g
.
astype
(
idtype
).
to
(
F
.
ctx
())
ctx
=
F
.
ctx
()
gmmconv
=
nn
.
GMMConv
(
5
,
10
,
3
,
4
,
"mean"
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
pseudo
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
3
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
pseudo
=
F
.
randn
((
g
.
num_edges
(),
3
))
gmmconv
=
gmmconv
.
to
(
ctx
)
h
=
gmmconv
(
g
,
feat
,
pseudo
)
# currently we only do shape check
...
...
@@ -1023,7 +1023,7 @@ def test_gmm_conv_bi(g, idtype):
gmmconv
=
nn
.
GMMConv
((
5
,
2
),
10
,
3
,
4
,
"mean"
)
feat
=
F
.
randn
((
g
.
number_of_src_nodes
(),
5
))
feat_dst
=
F
.
randn
((
g
.
number_of_dst_nodes
(),
2
))
pseudo
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
3
))
pseudo
=
F
.
randn
((
g
.
num_edges
(),
3
))
gmmconv
=
gmmconv
.
to
(
ctx
)
h
=
gmmconv
(
g
,
(
feat
,
feat_dst
),
pseudo
)
# currently we only do shape check
...
...
@@ -1070,7 +1070,7 @@ def test_dense_sage_conv(g, idtype, out_dim):
F
.
randn
((
g
.
number_of_dst_nodes
(),
5
)),
)
else
:
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
sage
=
sage
.
to
(
ctx
)
dense_sage
=
dense_sage
.
to
(
ctx
)
out_sage
=
sage
(
g
,
feat
)
...
...
@@ -1130,7 +1130,7 @@ def test_dotgat_conv(g, idtype, out_dim, num_heads):
h
=
dotgat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
dotgat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -1149,7 +1149,7 @@ def test_dotgat_conv_bi(g, idtype, out_dim, num_heads):
h
=
dotgat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
dotgat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
pytest
.
mark
.
parametrize
(
"out_dim"
,
[
1
,
2
])
...
...
@@ -1216,7 +1216,7 @@ def test_sequential():
graph
.
ndata
[
"h"
]
=
n_feat
graph
.
update_all
(
fn
.
copy_u
(
"h"
,
"m"
),
fn
.
sum
(
"m"
,
"h"
))
n_feat
+=
graph
.
ndata
[
"h"
]
return
n_feat
.
view
(
graph
.
num
ber_of
_nodes
()
//
2
,
2
,
-
1
).
sum
(
1
)
return
n_feat
.
view
(
graph
.
num_nodes
()
//
2
,
2
,
-
1
).
sum
(
1
)
g1
=
dgl
.
DGLGraph
(
nx
.
erdos_renyi_graph
(
32
,
0.05
)).
to
(
F
.
ctx
())
g2
=
dgl
.
DGLGraph
(
nx
.
erdos_renyi_graph
(
16
,
0.2
)).
to
(
F
.
ctx
())
...
...
@@ -1243,8 +1243,8 @@ def test_atomic_conv(g, idtype):
if
F
.
gpu_ctx
():
aconv
=
aconv
.
to
(
ctx
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
1
))
dist
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
1
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
1
))
dist
=
F
.
randn
((
g
.
num_edges
(),
1
))
h
=
aconv
(
g
,
feat
,
dist
)
...
...
@@ -1268,7 +1268,7 @@ def test_cf_conv(g, idtype, out_dim):
cfconv
=
cfconv
.
to
(
ctx
)
src_feats
=
F
.
randn
((
g
.
number_of_src_nodes
(),
2
))
edge_feats
=
F
.
randn
((
g
.
num
ber_of
_edges
(),
3
))
edge_feats
=
F
.
randn
((
g
.
num_edges
(),
3
))
h
=
cfconv
(
g
,
src_feats
,
edge_feats
)
# current we only do shape check
assert
h
.
shape
[
-
1
]
==
out_dim
...
...
tests/python/tensorflow/test_nn.py
View file @
5b409bf7
...
...
@@ -141,7 +141,7 @@ def test_simple_pool():
print
(
sum_pool
,
avg_pool
,
max_pool
,
sort_pool
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
sum_pool
(
g
,
h0
)
assert
F
.
allclose
(
F
.
squeeze
(
h1
,
0
),
F
.
sum
(
h0
,
0
))
h1
=
avg_pool
(
g
,
h0
)
...
...
@@ -154,7 +154,7 @@ def test_simple_pool():
# test#2: batched graph
g_
=
dgl
.
DGLGraph
(
nx
.
path_graph
(
5
)).
to
(
F
.
ctx
())
bg
=
dgl
.
batch
([
g
,
g_
,
g
,
g_
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
sum_pool
(
bg
,
h0
)
truth
=
tf
.
stack
(
[
...
...
@@ -205,13 +205,13 @@ def test_glob_att_pool():
print
(
gap
)
# test#1: basic
h0
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h1
=
gap
(
g
,
h0
)
assert
h1
.
shape
[
0
]
==
1
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
# test#2: batched graph
bg
=
dgl
.
batch
([
g
,
g
,
g
,
g
])
h0
=
F
.
randn
((
bg
.
num
ber_of
_nodes
(),
5
))
h0
=
F
.
randn
((
bg
.
num_nodes
(),
5
))
h1
=
gap
(
bg
,
h0
)
assert
h1
.
shape
[
0
]
==
4
and
h1
.
shape
[
1
]
==
10
and
h1
.
ndim
==
2
...
...
@@ -224,7 +224,7 @@ def test_rgcn(O):
)
# 5 etypes
R
=
5
for
i
in
range
(
g
.
num
ber_of
_edges
()):
for
i
in
range
(
g
.
num_edges
()):
etype
.
append
(
i
%
5
)
B
=
2
I
=
10
...
...
@@ -256,7 +256,7 @@ def test_rgcn(O):
assert
F
.
allclose
(
h_new
,
h_new_low
)
# with norm
norm
=
tf
.
zeros
((
g
.
num
ber_of
_edges
(),
1
))
norm
=
tf
.
zeros
((
g
.
num_edges
(),
1
))
rgc_basis
=
nn
.
RelGraphConv
(
I
,
O
,
R
,
"basis"
,
B
)
rgc_basis_low
=
nn
.
RelGraphConv
(
I
,
O
,
R
,
"basis"
,
B
,
low_mem
=
True
)
...
...
@@ -313,7 +313,7 @@ def test_gat_conv(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
# test residual connection
gat
=
nn
.
GATConv
(
5
,
out_dim
,
num_heads
,
residual
=
True
)
...
...
@@ -335,7 +335,7 @@ def test_gat_conv_bi(g, idtype, out_dim, num_heads):
h
=
gat
(
g
,
feat
)
assert
h
.
shape
==
(
g
.
number_of_dst_nodes
(),
num_heads
,
out_dim
)
_
,
a
=
gat
(
g
,
feat
,
get_attention
=
True
)
assert
a
.
shape
==
(
g
.
num
ber_of
_edges
(),
num_heads
,
1
)
assert
a
.
shape
==
(
g
.
num_edges
(),
num_heads
,
1
)
@
parametrize_idtype
...
...
@@ -397,7 +397,7 @@ def test_sgc_conv(g, idtype, out_dim):
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
# not cached
sgc
=
nn
.
SGConv
(
5
,
out_dim
,
3
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h
=
sgc
(
g
,
feat
)
assert
h
.
shape
[
-
1
]
==
out_dim
...
...
@@ -416,7 +416,7 @@ def test_appnp_conv(g, idtype):
ctx
=
F
.
ctx
()
g
=
g
.
astype
(
idtype
).
to
(
ctx
)
appnp
=
nn
.
APPNPConv
(
10
,
0.1
)
feat
=
F
.
randn
((
g
.
num
ber_of
_nodes
(),
5
))
feat
=
F
.
randn
((
g
.
num_nodes
(),
5
))
h
=
appnp
(
g
,
feat
)
assert
h
.
shape
[
-
1
]
==
5
...
...
tests/tools/test_parmetis.py
View file @
5b409bf7
...
...
@@ -119,10 +119,10 @@ def test_parmetis_postprocessing():
num_chunks
=
2
g
=
create_chunked_dataset
(
root_dir
,
num_chunks
)
num_nodes
=
g
.
num
ber_of
_nodes
()
num_institutions
=
g
.
num
ber_of
_nodes
(
"institution"
)
num_authors
=
g
.
num
ber_of
_nodes
(
"author"
)
num_papers
=
g
.
num
ber_of
_nodes
(
"paper"
)
num_nodes
=
g
.
num_nodes
()
num_institutions
=
g
.
num_nodes
(
"institution"
)
num_authors
=
g
.
num_nodes
(
"author"
)
num_papers
=
g
.
num_nodes
(
"paper"
)
# Generate random parmetis partition ids for the nodes in the graph.
# Replace this code with actual ParMETIS executable when it is ready
...
...
@@ -192,9 +192,9 @@ def test_parmetis_wrapper():
all_ntypes
=
g
.
ntypes
all_etypes
=
g
.
etypes
num_constraints
=
len
(
all_ntypes
)
+
3
num_institutions
=
g
.
num
ber_of
_nodes
(
"institution"
)
num_authors
=
g
.
num
ber_of
_nodes
(
"author"
)
num_papers
=
g
.
num
ber_of
_nodes
(
"paper"
)
num_institutions
=
g
.
num_nodes
(
"institution"
)
num_authors
=
g
.
num_nodes
(
"author"
)
num_papers
=
g
.
num_nodes
(
"paper"
)
# Trigger ParMETIS.
schema_file
=
os
.
path
.
join
(
root_dir
,
"chunked-data/metadata.json"
)
...
...
@@ -211,8 +211,8 @@ def test_parmetis_wrapper():
f
.
write
(
"127.0.0.1
\n
"
)
f
.
write
(
"127.0.0.1
\n
"
)
num_nodes
=
g
.
num
ber_of
_nodes
()
num_edges
=
g
.
num
ber_of
_edges
()
num_nodes
=
g
.
num_nodes
()
num_edges
=
g
.
num_edges
()
stats_file
=
f
"
{
graph_name
}
_stats.txt"
with
open
(
stats_file
,
"w"
)
as
f
:
f
.
write
(
f
"
{
num_nodes
}
{
num_edges
}
{
num_constraints
}
"
)
...
...
tests/utils/checks.py
View file @
5b409bf7
...
...
@@ -21,7 +21,7 @@ def check_fail(fn, *args, **kwargs):
def
assert_is_identical
(
g
,
g2
):
assert
g
.
num
ber_of
_nodes
()
==
g2
.
num
ber_of
_nodes
()
assert
g
.
num_nodes
()
==
g2
.
num_nodes
()
src
,
dst
=
g
.
all_edges
(
order
=
"eid"
)
src2
,
dst2
=
g2
.
all_edges
(
order
=
"eid"
)
assert
F
.
array_equal
(
src
,
src2
)
...
...
@@ -45,7 +45,7 @@ def assert_is_identical_hetero(g, g2, ignore_internal_data=False):
# check if node ID spaces and feature spaces are equal
for
ntype
in
g
.
ntypes
:
assert
g
.
num
ber_of
_nodes
(
ntype
)
==
g2
.
num
ber_of
_nodes
(
ntype
)
assert
g
.
num_nodes
(
ntype
)
==
g2
.
num_nodes
(
ntype
)
if
ignore_internal_data
:
for
k
in
list
(
g
.
nodes
[
ntype
].
data
.
keys
()):
if
is_internal_column
(
k
):
...
...
@@ -91,10 +91,10 @@ def check_graph_equal(g1, g2, *, check_idtype=True, check_feature=True):
assert
g2
.
metagraph
().
edges
(
keys
=
True
)[
edges
]
==
features
for
nty
in
g1
.
ntypes
:
assert
g1
.
num
ber_of
_nodes
(
nty
)
==
g2
.
num
ber_of
_nodes
(
nty
)
assert
g1
.
num_nodes
(
nty
)
==
g2
.
num_nodes
(
nty
)
assert
F
.
allclose
(
g1
.
batch_num_nodes
(
nty
),
g2
.
batch_num_nodes
(
nty
))
for
ety
in
g1
.
canonical_etypes
:
assert
g1
.
num
ber_of
_edges
(
ety
)
==
g2
.
num
ber_of
_edges
(
ety
)
assert
g1
.
num_edges
(
ety
)
==
g2
.
num_edges
(
ety
)
assert
F
.
allclose
(
g1
.
batch_num_edges
(
ety
),
g2
.
batch_num_edges
(
ety
))
src1
,
dst1
,
eid1
=
g1
.
edges
(
etype
=
ety
,
form
=
"all"
)
src2
,
dst2
,
eid2
=
g2
.
edges
(
etype
=
ety
,
form
=
"all"
)
...
...
@@ -109,14 +109,14 @@ def check_graph_equal(g1, g2, *, check_idtype=True, check_feature=True):
if
check_feature
:
for
nty
in
g1
.
ntypes
:
if
g1
.
num
ber_of
_nodes
(
nty
)
==
0
:
if
g1
.
num_nodes
(
nty
)
==
0
:
continue
for
feat_name
in
g1
.
nodes
[
nty
].
data
.
keys
():
assert
F
.
allclose
(
g1
.
nodes
[
nty
].
data
[
feat_name
],
g2
.
nodes
[
nty
].
data
[
feat_name
]
)
for
ety
in
g1
.
canonical_etypes
:
if
g1
.
num
ber_of
_edges
(
ety
)
==
0
:
if
g1
.
num_edges
(
ety
)
==
0
:
continue
for
feat_name
in
g2
.
edges
[
ety
].
data
.
keys
():
assert
F
.
allclose
(
...
...
tests/utils/graph_cases.py
View file @
5b409bf7
...
...
@@ -75,8 +75,8 @@ def graph1():
),
device
=
F
.
cpu
(),
)
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_nodes
(),
2
)),
F
.
cpu
())
g
.
edata
[
"w"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_edges
(),
3
)),
F
.
cpu
())
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num_nodes
(),
2
)),
F
.
cpu
())
g
.
edata
[
"w"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num_edges
(),
3
)),
F
.
cpu
())
return
g
...
...
@@ -89,10 +89,8 @@ def graph1():
),
device
=
F
.
cpu
(),
)
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
number_of_nodes
(),
2
)),
F
.
cpu
())
g
.
edata
[
"scalar_w"
]
=
F
.
copy_to
(
F
.
abs
(
F
.
randn
((
g
.
number_of_edges
(),))),
F
.
cpu
()
)
g
.
ndata
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num_nodes
(),
2
)),
F
.
cpu
())
g
.
edata
[
"scalar_w"
]
=
F
.
copy_to
(
F
.
abs
(
F
.
randn
((
g
.
num_edges
(),))),
F
.
cpu
())
return
g
...
...
@@ -129,19 +127,19 @@ def heterograph0():
device
=
F
.
cpu
(),
)
g
.
nodes
[
"user"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_nodes
(
"user"
),
3
)),
F
.
cpu
()
F
.
randn
((
g
.
num_nodes
(
"user"
),
3
)),
F
.
cpu
()
)
g
.
nodes
[
"game"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_nodes
(
"game"
),
2
)),
F
.
cpu
()
F
.
randn
((
g
.
num_nodes
(
"game"
),
2
)),
F
.
cpu
()
)
g
.
nodes
[
"developer"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_nodes
(
"developer"
),
3
)),
F
.
cpu
()
F
.
randn
((
g
.
num_nodes
(
"developer"
),
3
)),
F
.
cpu
()
)
g
.
edges
[
"plays"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_edges
(
"plays"
),
1
)),
F
.
cpu
()
F
.
randn
((
g
.
num_edges
(
"plays"
),
1
)),
F
.
cpu
()
)
g
.
edges
[
"develops"
].
data
[
"h"
]
=
F
.
copy_to
(
F
.
randn
((
g
.
num
ber_of
_edges
(
"develops"
),
5
)),
F
.
cpu
()
F
.
randn
((
g
.
num_edges
(
"develops"
),
5
)),
F
.
cpu
()
)
return
g
...
...
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment