README.md 3.38 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
[pypi-image]: https://badge.fury.io/py/torch-sparse.svg
[pypi-url]: https://pypi.python.org/pypi/torch-sparse
[build-image]: https://travis-ci.org/rusty1s/pytorch_sparse.svg?branch=master
[build-url]: https://travis-ci.org/rusty1s/pytorch_sparse
[coverage-image]: https://codecov.io/gh/rusty1s/pytorch_sparse/branch/master/graph/badge.svg
[coverage-url]: https://codecov.io/github/rusty1s/pytorch_sparse?branch=master
rusty1s's avatar
rusty1s committed
7

rusty1s's avatar
rusty1s committed
8
# PyTorch Sparse
rusty1s's avatar
rusty1s committed
9
10
11
12
13
14

[![PyPI Version][pypi-image]][pypi-url]
[![Build Status][build-image]][build-url]
[![Code Coverage][coverage-image]][coverage-url]

--------------------------------------------------------------------------------
rusty1s's avatar
rusty1s committed
15
16

This package consists of a small extension library of optimized sparse matrix operations for the use in [PyTorch](http://pytorch.org/), which are missing and or lack autograd support in the main package.
rusty1s's avatar
typos  
rusty1s committed
17
This package currently consists of the following methods:
rusty1s's avatar
rusty1s committed
18

rusty1s's avatar
docs  
rusty1s committed
19
20
21
* **[Autograd Sparse Tensor Creation](#autograd-sparse-tensor-creation)**
* **[Autograd Sparse Tensor Value Extraction](#autograd-sparse-tensor-value-extraction)**
* **[Sparse Sparse Matrix Multiplication](#sparse-sparse-matrix-multiplication)**
rusty1s's avatar
rusty1s committed
22
23
24
25
26

All included operations work on varying data types and are implemented both for CPU and GPU.

## Installation

rusty1s's avatar
rusty1s committed
27
Ensure that at least PyTorch 0.4.0 is installed and verify that `cuda/bin` and `cuda/install` are in your `$PATH` and `$CPATH` respectively, *e.g.*:
rusty1s's avatar
rusty1s committed
28
29

```
rusty1s's avatar
rusty1s committed
30
31
32
$ python -c "import torch; print(torch.__version__)"
>>> 0.4.0

rusty1s's avatar
rusty1s committed
33
$ echo $PATH
rusty1s's avatar
rusty1s committed
34
>>> /usr/local/cuda/bin:...
rusty1s's avatar
rusty1s committed
35
36

$ echo $CPATH
rusty1s's avatar
rusty1s committed
37
>>> /usr/local/cuda/install:...
rusty1s's avatar
rusty1s committed
38
39
40
41
42
```

Then run:

```
rusty1s's avatar
typo  
rusty1s committed
43
pip install torch-sparse
rusty1s's avatar
rusty1s committed
44
45
```

rusty1s's avatar
typos  
rusty1s committed
46
If you are running into any installation problems, please follow these [instructions](https://rusty1s.github.io/pytorch_geometric/build/html/notes/installation.html) first before creating an [issue](https://github.com/rusty1s/pytorch_sparse/issues).
rusty1s's avatar
links  
rusty1s committed
47

rusty1s's avatar
rusty1s committed
48
49
## Autograd Sparse Tensor Creation

rusty1s's avatar
docs  
rusty1s committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
```
torch_sparse.sparse_coo_tensor(torch.LongTensor, torch.Tensor, torch.Size) -> torch.SparseTensor
```

Constructs a [`torch.SparseTensor`](https://pytorch.org/docs/stable/sparse.html) with autograd capabilities w.r.t. `value`.

```python
from torch_sparse import sparse_coo_tensor

i = torch.tensor([[0, 1, 1],
                  [2, 0, 2]])
v = torch.Tensor([3, 4, 5], requires_grad=True)
A = sparse_coo_tensor(i, v, torch.Size([2,3]))
```

This method may become obsolete in future PyTorch releases (>= 0.4.1) as reported by this [issue](https://github.com/pytorch/pytorch/issues/9674).

rusty1s's avatar
rusty1s committed
67
68
## Autograd Sparse Tensor Value Extraction

rusty1s's avatar
docs  
rusty1s committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
```
torch_sparse.to_value(SparseTensor) --> Tensor
```

Wrapper method to support autograd on values of sparse tensors.

```python
from torch_sparse import to_value

i = torch.tensor([[0, 1, 1],
                  [2, 0, 2]])
v = torch.Tensor([3, 4, 5], requires_grad=True)
A = torch.sparse_coo_tensor(i, v, torch.Size([2,3]), requires_grad=True)
v = to_value(A)
```

This method may become obsolete in future PyTorch releases (>= 0.4.1) as reported by this [issue](https://github.com/pytorch/pytorch/issues/9674).

rusty1s's avatar
rusty1s committed
87
88
## Sparse Sparse Matrix Multiplication

rusty1s's avatar
docs  
rusty1s committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
```
torch_sparse.spspmm(SparseTensor, SparseTensor) --> SparseTensor
```

Sparse matrix product of two sparse tensors with autograd support.

```
from torch_sparse import spspmm

A = torch.sparse_coo_tensor(..., requries_grad=True)
B = torch.sparse_coo_tensor(..., requries_grad=True)

C = spspmm(A, B)
```

rusty1s's avatar
rusty1s committed
104
105
106
107
108
## Running tests

```
python setup.py test
```