forked from rusty1s/pytorch_sparse
-
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.
Add function for the addition of two matrices (rusty1s#177)
* Create spadd.py Hi, Maybe it's trivial to have this function, but I still think it'll be helpful and it looks neat when applying matrix addition, i.e., C = A + B. Thanks * update * update * fix jit Co-authored-by: rusty1s <matthias.fey@tu-dortmund.de>
- Loading branch information
Showing
5 changed files
with
107 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from itertools import product | ||
|
||
import pytest | ||
import torch | ||
from torch_sparse import SparseTensor, add | ||
|
||
from .utils import dtypes, devices, tensor | ||
|
||
|
||
@pytest.mark.parametrize('dtype,device', product(dtypes, devices)) | ||
def test_add(dtype, device): | ||
rowA = torch.tensor([0, 0, 1, 2, 2], device=device) | ||
colA = torch.tensor([0, 2, 1, 0, 1], device=device) | ||
valueA = tensor([1, 2, 4, 1, 3], dtype, device) | ||
A = SparseTensor(row=rowA, col=colA, value=valueA) | ||
|
||
rowB = torch.tensor([0, 0, 1, 2, 2], device=device) | ||
colB = torch.tensor([1, 2, 2, 1, 2], device=device) | ||
valueB = tensor([2, 3, 1, 2, 4], dtype, device) | ||
B = SparseTensor(row=rowB, col=colB, value=valueB) | ||
|
||
C = A + B | ||
rowC, colC, valueC = C.coo() | ||
|
||
assert rowC.tolist() == [0, 0, 0, 1, 1, 2, 2, 2] | ||
assert colC.tolist() == [0, 1, 2, 1, 2, 0, 1, 2] | ||
assert valueC.tolist() == [1, 2, 5, 4, 1, 1, 5, 4] | ||
|
||
@torch.jit.script | ||
def jit_add(A: SparseTensor, B: SparseTensor) -> SparseTensor: | ||
return add(A, B) | ||
|
||
jit_add(A, B) |
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,18 @@ | ||
import torch | ||
from torch_sparse import coalesce | ||
|
||
|
||
def spadd(indexA, valueA, indexB, valueB, m, n): | ||
"""Matrix addition of two sparse matrices. | ||
Args: | ||
indexA (:class:`LongTensor`): The index tensor of first sparse matrix. | ||
valueA (:class:`Tensor`): The value tensor of first sparse matrix. | ||
indexB (:class:`LongTensor`): The index tensor of second sparse matrix. | ||
valueB (:class:`Tensor`): The value tensor of second sparse matrix. | ||
m (int): The first dimension of the sparse matrices. | ||
n (int): The second dimension of the sparse matrices. | ||
""" | ||
index = torch.cat([indexA, indexB], dim=-1) | ||
value = torch.cat([valueA, valueB], dim=0) | ||
return coalesce(index=index, value=value, m=m, n=n, op='add') |
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