You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: rfcs/20200519-csr-sparse-matrix.md
+18-10Lines changed: 18 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Support the [compressed sparse row (CSR)](https://en.wikipedia.org/wiki/Sparse_m
24
24
25
25
## Motivation
26
26
27
-
Sparse tensor representation has a significant impact on performance. Modern architectures are most suited for non-random, bulk memory accesses. Sparse formats that optimize for data locality and reuse can achieve large performance gains. TensorFlow currently stores sparse tensors in [coordinate (COO)](https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor) format, which works well for tensors with very few nonzeroes, and is inefficient otherwise. Deep learning and sparse linear algebra applications typically do not have sufficient sparsities to benefit from the COO format. The compressed sparse row (CSR) format is one of the most commonly used formats. It generally requires less storage and is faster than COO, sometimes by up to orders of magnitudes.
27
+
Sparse tensor representation has a significant impact on performance. Modern architectures are most suited for non-random, bulk memory accesses. Sparse formats that optimize for data locality and reuse can achieve large performance gains. TensorFlow currently stores sparse tensors in [coordinate (COO)](https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor) format, which works well for tensors with very few nonzeroes, and is inefficient otherwise. Deep learning and sparse linear algebra applications typically do not have sufficient sparsities to benefit from the COO format. The compressed sparse row (CSR) format is one of the most commonly used formats. It generally requires less storage and is faster than COO, sometimes by up to orders of magnitude.
28
28
29
29
We propose supporting the CSR format in TensorFlow to accelerate sparse linear algebra and applicable deep learning applications in TensorFlow.
30
30
@@ -46,6 +46,14 @@ All kernel implementations are in C++, with a Python wrapper for Python APIs. Du
46
46
47
47
### Supported Operations
48
48
See APIs in the Detailed Design section.
49
+
* Construction ops:
50
+
* From a dense tensor.
51
+
* From a SparseTensor.
52
+
* From given `batch_pointers`, `row_pointers`, `col_indices`, and `values` arrays.
53
+
* Op that returns CSR components.
54
+
* Conversions ops:
55
+
* Convert to and from dense tensor
56
+
* Convert to and from SparseTensor.
49
57
* Sparse linear algebra ops:
50
58
* Sparse matrix-vector multiplication (SpMV)
51
59
* Sparse-dense matrix multiplication (SpMM)
@@ -55,12 +63,9 @@ See APIs in the Detailed Design section.
55
63
* Sparse Cholesky factorization
56
64
* Sparse LU factorization
57
65
* Sparse QR factorization
58
-
* Conversions ops:
59
-
* Convert to and from dense tensor
60
-
* Convert to and from SparseTensor.
61
66
62
67
General ops, borrowing APIs from numpy and scipy ([sparse](https://docs.scipy.org/doc/scipy/reference/sparse.html), [sparse.csr_matrix](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html)) packages.
63
-
*Construction ops: [eye](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.eye.html) and [rand](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.rand.html)
68
+
*Generation ops: [eye](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.eye.html) and [rand](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.rand.html)
* How this proposal interacts with other parts of the TensorFlow Ecosystem:
195
203
* TFLite: TFLite already supports the CSR format. TensorFlow should be able to pass the format to TFLite without problems.
196
204
* Distribution strategies: Don’t plan on interacting with this in this initial phase.
197
-
* tf.function: Should work just like any other ops.
205
+
* tf.function: Can be made to work with tf.function. Will work straightforwardly if CSRSparseMatrix is a [CompositeTensor](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/python/framework/composite_tensor.py;l=31).
198
206
* GPU: We plan to make all CSRSparseMatrix operations work on GPUs.
199
207
* TPU: We don’t plan on supporting CSRSparseMatrix on TPUs yet.
200
208
* SavedModel: Should work just like any other ops/tensors.
@@ -215,7 +223,7 @@ There are CSRSparseMatrix classes on both C++ and Python sides. The C++ CSRSpars
215
223
216
224
217
225
### C++ Layer
218
-
The C++ [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/master:tensorflow/core/kernels/sparse/sparse_matrix.h;l=35) class has the following properties:
226
+
The C++ [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/core/kernels/sparse/sparse_matrix.h;l=35) class has the following properties:
@@ -275,7 +283,7 @@ The C++ [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/m
275
283
276
284
277
285
### Python Layer
278
-
The Python [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/python/ops/linalg/sparse/sparse_csr_matrix_ops.py;l=315) class is a subclass of [SparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/python/ops/linalg/sparse/sparse_csr_matrix_ops.py;l=248), which stores common sparse matrix properties. Other new sparse formats can be added as subclasses of SparseMatrix. CSRSparseMatrix has the following properties:
286
+
The Python [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/python/ops/linalg/sparse/sparse_csr_matrix_ops.py;l=315) class is a subclass of [SparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/python/ops/linalg/sparse/sparse_csr_matrix_ops.py;l=248), which stores common sparse matrix properties. Other new sparse formats can be added as subclasses of SparseMatrix. CSRSparseMatrix has the following properties:
@@ -285,7 +293,7 @@ The Python [CSRSparseMatrix](https://cs.opensource.google/tensorflow/tensorflow/
285
293
286
294
287
295
### Shape Inference
288
-
`Variant` tensors are perceived as scalars in TensorFlow. For proper shape inference, we store `CSRSparseMatrix`’s shape and data type in a shape inference primitive, [ShapeAndType](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/core/framework/shape_inference.h;l=133), and access them through [input_handle_shapes_and_types](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/core/framework/shape_inference.h;l=584) and [set_output_handle_shapes_and_types](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/core/framework/shape_inference.h;l=588) during shape inference.
296
+
`Variant` tensors are perceived as scalars in TensorFlow. For proper shape inference, we store `CSRSparseMatrix`’s shape and data type in a shape inference primitive, [ShapeAndType](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/core/framework/shape_inference.h;l=133), and access them through [input_handle_shapes_and_types](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/core/framework/shape_inference.h;l=584) and [set_output_handle_shapes_and_types](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/core/framework/shape_inference.h;l=588) during shape inference.
289
297
290
298
291
299
### APIs
@@ -321,7 +329,7 @@ Other sparse APIs follow NumPy and SciPy APIs. See links in [Supported Operation
321
329
* Should we add `CSRSparseMatrix` support to existing standard ops as well, e.g., `tf.math.{add,asin,atan,ceil}`, etc?
322
330
* Would love to hear about more use cases.
323
331
* For neural networks, would CSR be useful for you (while Block CSR is still a future work)?
324
-
* Should we make CSRSparseMatrix a [CompositeTensor](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0-rc4:tensorflow/python/framework/composite_tensor.py;l=31)? Would the effort be worth it since we will transition to the new TFRT/MLIR backend soon? How should this be prioritized?
332
+
* Should we make CSRSparseMatrix a [CompositeTensor](https://cs.opensource.google/tensorflow/tensorflow/+/v2.2.0:tensorflow/python/framework/composite_tensor.py;l=31)? Would the effort be worth it since we will transition to the new TFRT/MLIR backend soon? How should this be prioritized?
0 commit comments