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
torch-sparse
Commits
c6ca46d8
Commit
c6ca46d8
authored
Apr 14, 2020
by
bowendeng
Browse files
add one optional arg to 'partition'
parent
fb3459ad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
9 deletions
+32
-9
test/test_metis.py
test/test_metis.py
+6
-2
torch_sparse/metis.py
torch_sparse/metis.py
+26
-7
No files found.
test/test_metis.py
View file @
c6ca46d8
...
@@ -8,12 +8,16 @@ from .utils import devices
...
@@ -8,12 +8,16 @@ from .utils import devices
@
pytest
.
mark
.
parametrize
(
'device'
,
devices
)
@
pytest
.
mark
.
parametrize
(
'device'
,
devices
)
def
test_metis
(
device
):
def
test_metis
(
device
):
weighted_mat
=
SparseTensor
.
from_dense
(
torch
.
randn
((
6
,
6
),
device
=
device
))
weighted_mat
=
SparseTensor
.
from_dense
(
torch
.
randn
((
6
,
6
),
device
=
device
))
mat
,
partptr
,
perm
=
weighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
False
)
mat
,
partptr
,
perm
=
weighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
sort_strategy
=
True
)
assert
partptr
.
numel
()
==
3
assert
perm
.
numel
()
==
6
mat
,
partptr
,
perm
=
weighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
sort_strategy
=
False
)
assert
partptr
.
numel
()
==
3
assert
partptr
.
numel
()
==
3
assert
perm
.
numel
()
==
6
assert
perm
.
numel
()
==
6
unweighted_mat
=
SparseTensor
.
from_dense
(
torch
.
ones
((
6
,
6
),
device
=
device
))
unweighted_mat
=
SparseTensor
.
from_dense
(
torch
.
ones
((
6
,
6
),
device
=
device
))
mat
,
partptr
,
perm
=
unweighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
True
)
mat
,
partptr
,
perm
=
unweighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
True
,
sort_strategy
=
True
)
assert
partptr
.
numel
()
==
3
assert
partptr
.
numel
()
==
3
assert
perm
.
numel
()
==
6
assert
perm
.
numel
()
==
6
...
...
torch_sparse/metis.py
View file @
c6ca46d8
...
@@ -11,27 +11,46 @@ def cartesian1d(x, y):
...
@@ -11,27 +11,46 @@ def cartesian1d(x, y):
return
coos
.
split
(
1
,
dim
=
1
)
return
coos
.
split
(
1
,
dim
=
1
)
def
metis_weight
(
x
):
def
metis_weight1
(
x
):
sorted_x
=
x
.
sort
()[
0
]
diff
=
sorted_x
[
1
:]
-
sorted_x
[:
-
1
]
if
diff
.
sum
()
==
0
:
return
None
xmin
,
xmax
=
sorted_x
[[
0
,
-
1
]]
srange
=
xmax
-
xmin
min_diff
=
diff
.
min
()
scale
=
(
min_diff
/
srange
).
item
()
tick
,
arange
=
scale
.
as_integer_ratio
()
x_ratio
=
(
x
-
xmin
)
/
srange
return
(
x_ratio
*
arange
+
tick
).
long
()
def
metis_weight2
(
x
):
t1
,
t2
=
cartesian1d
(
x
,
x
)
t1
,
t2
=
cartesian1d
(
x
,
x
)
diff
=
t1
-
t2
diff
=
t1
-
t2
diff
=
diff
[
diff
!=
0
]
diff
=
diff
[
diff
!=
0
]
if
len
(
diff
)
==
0
:
if
len
(
diff
)
==
0
:
return
None
return
None
res
=
diff
.
abs
().
min
()
xmin
,
xmax
=
x
.
min
(),
x
.
max
()
bod
=
x
.
max
()
-
x
.
min
()
srange
=
xmax
-
xmin
scale
=
(
res
/
bod
).
item
()
min_diff
=
diff
.
abs
().
min
()
scale
=
(
min_diff
/
srange
).
item
()
tick
,
arange
=
scale
.
as_integer_ratio
()
tick
,
arange
=
scale
.
as_integer_ratio
()
x_ratio
=
(
x
-
x
.
min
()
)
/
bod
x_ratio
=
(
x
-
xmin
)
/
srange
return
(
x_ratio
*
arange
+
tick
).
long
()
return
(
x_ratio
*
arange
+
tick
).
long
()
def
partition
(
src
:
SparseTensor
,
num_parts
:
int
,
recursive
:
bool
=
False
def
metis_weight
(
x
,
sort_strategy
=
True
):
return
metis_weight1
(
x
)
if
sort_strategy
else
metis_weight2
(
x
)
def
partition
(
src
:
SparseTensor
,
num_parts
:
int
,
recursive
:
bool
=
False
,
sort_strategy
=
True
,
)
->
Tuple
[
SparseTensor
,
torch
.
Tensor
,
torch
.
Tensor
]:
)
->
Tuple
[
SparseTensor
,
torch
.
Tensor
,
torch
.
Tensor
]:
rowptr
,
col
,
value
=
src
.
csr
()
rowptr
,
col
,
value
=
src
.
csr
()
rowptr
,
col
=
rowptr
.
cpu
(),
col
.
cpu
()
rowptr
,
col
=
rowptr
.
cpu
(),
col
.
cpu
()
if
value
is
not
None
and
value
.
dim
()
==
1
:
if
value
is
not
None
and
value
.
dim
()
==
1
:
value
=
value
.
detach
().
cpu
()
value
=
value
.
detach
().
cpu
()
value
=
metis_weight
(
value
)
value
=
metis_weight
(
value
,
sort_strategy
)
cluster
=
torch
.
ops
.
torch_sparse
.
partition
(
rowptr
,
col
,
value
,
num_parts
,
cluster
=
torch
.
ops
.
torch_sparse
.
partition
(
rowptr
,
col
,
value
,
num_parts
,
recursive
)
recursive
)
cluster
=
cluster
.
to
(
src
.
device
())
cluster
=
cluster
.
to
(
src
.
device
())
...
...
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