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
7f8aac48
Commit
7f8aac48
authored
Apr 14, 2020
by
rusty1s
Browse files
final clean up
parent
c6ca46d8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
52 deletions
+32
-52
test/test_metis.py
test/test_metis.py
+13
-15
torch_sparse/metis.py
torch_sparse/metis.py
+19
-37
No files found.
test/test_metis.py
View file @
7f8aac48
...
@@ -7,21 +7,19 @@ from .utils import devices
...
@@ -7,21 +7,19 @@ 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
))
value1
=
torch
.
randn
(
6
*
6
,
device
=
device
).
view
(
6
,
6
)
mat
,
partptr
,
perm
=
weighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
sort_strategy
=
True
)
value2
=
torch
.
arange
(
6
*
6
,
dtype
=
torch
.
long
,
device
=
device
).
view
(
6
,
6
)
assert
partptr
.
numel
()
==
3
value3
=
torch
.
ones
(
6
*
6
,
device
=
device
).
view
(
6
,
6
)
assert
perm
.
numel
()
==
6
mat
,
partptr
,
perm
=
weighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
sort_strategy
=
False
)
for
value
in
[
value1
,
value2
,
value3
]:
assert
partptr
.
numel
()
==
3
mat
=
SparseTensor
.
from_dense
(
value
)
assert
perm
.
numel
()
==
6
unweighted_mat
=
SparseTensor
.
from_dense
(
torch
.
ones
((
6
,
6
),
device
=
device
))
_
,
partptr
,
perm
=
mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
mat
,
partptr
,
perm
=
unweighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
True
,
sort_strategy
=
True
)
weighted
=
True
)
assert
partptr
.
numel
()
==
3
assert
partptr
.
numel
()
==
3
assert
perm
.
numel
()
==
6
assert
perm
.
numel
()
==
6
unweighted_mat
=
unweighted_mat
.
set_value
(
None
)
_
,
partptr
,
perm
=
mat
.
partition
(
num_parts
=
2
,
recursive
=
False
,
mat
,
partptr
,
perm
=
unweighted_mat
.
partition
(
num_parts
=
2
,
recursive
=
Tru
e
)
weighted
=
Fals
e
)
assert
partptr
.
numel
()
==
3
assert
partptr
.
numel
()
==
3
assert
perm
.
numel
()
==
6
assert
perm
.
numel
()
==
6
torch_sparse/metis.py
View file @
7f8aac48
from
typing
import
Tuple
from
typing
import
Tuple
,
Optional
import
torch
import
torch
from
torch_sparse.tensor
import
SparseTensor
from
torch_sparse.tensor
import
SparseTensor
from
torch_sparse.permute
import
permute
from
torch_sparse.permute
import
permute
def
cartesian1d
(
x
,
y
):
def
weight2metis
(
weight
:
torch
.
Tensor
)
->
Optional
[
torch
.
Tensor
]:
a1
,
a2
=
torch
.
meshgrid
([
x
,
y
])
sorted_weight
=
weight
.
sort
()[
0
]
coos
=
torch
.
stack
([
a1
,
a2
]).
T
.
reshape
(
-
1
,
2
)
diff
=
sorted_weight
[
1
:]
-
sorted_weight
[:
-
1
]
return
coos
.
split
(
1
,
dim
=
1
)
def
metis_weight1
(
x
):
sorted_x
=
x
.
sort
()[
0
]
diff
=
sorted_x
[
1
:]
-
sorted_x
[:
-
1
]
if
diff
.
sum
()
==
0
:
if
diff
.
sum
()
==
0
:
return
None
return
None
xmin
,
x
max
=
sorted_
x
[
[
0
,
-
1
]
]
weight_min
,
weight_
max
=
sorted_
weight
[
0
]
,
sorted_weight
[
-
1
]
srange
=
xmax
-
x
min
srange
=
weight_max
-
weight_
min
min_diff
=
diff
.
min
()
min_diff
=
diff
.
min
()
scale
=
(
min_diff
/
srange
).
item
()
scale
=
(
min_diff
/
srange
).
item
()
tick
,
arange
=
scale
.
as_integer_ratio
()
tick
,
arange
=
scale
.
as_integer_ratio
()
x_ratio
=
(
x
-
xmin
)
/
srange
weight_ratio
=
(
weight
-
weight_min
).
div_
(
srange
).
mul_
(
arange
).
add_
(
tick
)
return
(
x_ratio
*
arange
+
tick
).
long
()
return
weight_ratio
.
to
(
torch
.
long
)
def
metis_weight2
(
x
):
t1
,
t2
=
cartesian1d
(
x
,
x
)
diff
=
t1
-
t2
diff
=
diff
[
diff
!=
0
]
if
len
(
diff
)
==
0
:
return
None
xmin
,
xmax
=
x
.
min
(),
x
.
max
()
srange
=
xmax
-
xmin
min_diff
=
diff
.
abs
().
min
()
scale
=
(
min_diff
/
srange
).
item
()
tick
,
arange
=
scale
.
as_integer_ratio
()
x_ratio
=
(
x
-
xmin
)
/
srange
return
(
x_ratio
*
arange
+
tick
).
long
()
def
partition
(
src
:
SparseTensor
,
num_parts
:
int
,
recursive
:
bool
=
False
,
def
metis_weight
(
x
,
sort_strategy
=
True
):
weighted
=
False
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
:
value
=
value
.
detach
().
cpu
()
if
value
is
not
None
and
weighted
:
value
=
metis_weight
(
value
,
sort_strategy
)
assert
value
.
numel
()
==
col
.
numel
()
value
=
value
.
view
(
-
1
).
detach
().
cpu
()
if
value
.
is_floating_point
():
value
=
weight2metis
(
value
)
else
:
value
=
None
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