forked from dmlc/dgl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sparse] Add compact operator (dmlc#6352)
Co-authored-by: Ubuntu <ubuntu@ip-172-31-24-117.ap-northeast-1.compute.internal>
- Loading branch information
1 parent
d3cb636
commit 892ab4a
Showing
7 changed files
with
175 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import backend as F | ||
import pytest | ||
import torch | ||
|
||
from .utils import ( | ||
rand_coo, | ||
rand_csc, | ||
rand_csr, | ||
rand_diag, | ||
sparse_matrix_to_dense, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"create_func", [rand_diag, rand_csr, rand_csc, rand_coo] | ||
) | ||
@pytest.mark.parametrize("dim", [0, 1]) | ||
@pytest.mark.parametrize("index", [None, (1, 3), (4, 0, 2)]) | ||
def test_compact(create_func, dim, index): | ||
ctx = F.ctx() | ||
shape = (5, 5) | ||
ans_idx = [] | ||
if index is not None: | ||
ans_idx = list(dict.fromkeys(index)) | ||
index = torch.tensor(index).to(ctx) | ||
|
||
A = create_func(shape, 8, ctx) | ||
|
||
A_compact, ret_id = A.compact(dim, index) | ||
A_compact_dense = sparse_matrix_to_dense(A_compact) | ||
|
||
A_dense = sparse_matrix_to_dense(A) | ||
|
||
for i in range(shape[dim]): | ||
if dim == 0: | ||
row = list(A_dense[i, :].nonzero().reshape(-1)) | ||
else: | ||
row = list(A_dense[:, i].nonzero().reshape(-1)) | ||
if (i not in list(ans_idx)) and len(row) > 0: | ||
ans_idx.append(i) | ||
if len(ans_idx): | ||
ans_idx = torch.tensor(ans_idx).to(ctx) | ||
A_dense_select = sparse_matrix_to_dense(A.index_select(dim, ans_idx)) | ||
|
||
assert A_compact_dense.shape == A_dense_select.shape | ||
assert torch.allclose(A_compact_dense, A_dense_select) | ||
assert torch.allclose(ans_idx, ret_id) |