Skip to content

Commit 28b9277

Browse files
committed
[mlir][sparse] add some documentation to storage layout (NFC)
in particular, the trailing COO optimization was not desribed in the general layout description Reviewed By: Peiming Differential Revision: https://reviews.llvm.org/D143284
1 parent d60ef93 commit 28b9277

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorStorageLayout.h

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This header file defines utilities for lowering and accessing sparse tensor
10-
// types.
9+
// This header file defines utilities for the sparse memory layout.
1110
//
1211
//===----------------------------------------------------------------------===//
1312

@@ -23,13 +22,15 @@ namespace mlir {
2322
namespace sparse_tensor {
2423

2524
//===----------------------------------------------------------------------===//
26-
// SparseTensorDescriptor and helpers, manage the sparse tensor memory layout
27-
// scheme.
25+
// SparseTensorDescriptor and helpers that manage the sparse tensor memory
26+
// layout scheme during "direct code generation" (i.e. when sparsification
27+
// generates the buffers as part of actual IR, in constrast with the library
28+
// approach where data structures are hidden behind opaque pointers).
2829
//
29-
// Sparse tensor storage scheme for rank-dimensional tensor is organized
30-
// as a single compound type with the following fields. Note that every
31-
// memref with ? size actually behaves as a "vector", i.e. the stored
32-
// size is the capacity and the used size resides in the memSizes array.
30+
// The sparse tensor storage scheme for a rank-dimensional tensor is organized
31+
// as a single compound type with the following fields. Note that every memref
32+
// with ? size actually behaves as a "vector", i.e. the stored size is the
33+
// capacity and the used size resides in the storage_specifier struct.
3334
//
3435
// struct {
3536
// ; per-dimension d:
@@ -40,15 +41,37 @@ namespace sparse_tensor {
4041
// memref<? x idx> indices-d ; indices for sparse dim d
4142
// ; if singleton:
4243
// memref<? x idx> indices-d ; indices for singleton dim d
44+
//
4345
// memref<? x eltType> values ; values
4446
//
45-
// ; sparse tensor metadata
46-
// struct {
47+
// struct sparse_tensor.storage_specifier {
4748
// array<rank x int> dimSizes ; sizes for each dimension
4849
// array<n x int> memSizes; ; sizes for each data memref
4950
// }
5051
// };
5152
//
53+
// In addition, for a "trailing COO region", defined as a compressed
54+
// dimension followed by one ore more singleton dimensions, the default
55+
// SOA storage that is inherent to the TACO format is optimized into an
56+
// AOS storage where all indices of a stored element appear consecutively.
57+
// In such cases, a special operation (sparse_tensor.indices_buffer) must
58+
// be used to access the AOS index array. In the code below, the method
59+
// `getCOOStart` is used to find the start of the "trailing COO region".
60+
//
61+
// Examples.
62+
//
63+
// #CSR storage of 2-dim matrix yields
64+
// memref<?xindex> ; pointers-1
65+
// memref<?xindex> ; indices-1
66+
// memref<?xf64> ; values
67+
// struct<(array<2 x i64>, array<3 x i64>)>) ; dim0, dim1, 3xsizes
68+
//
69+
// #COO storage of 2-dim matrix yields
70+
// memref<?xindex>, ; pointers-0, essentially [0,sz]
71+
// memref<?xindex> ; AOS index storage
72+
// memref<?xf64> ; values
73+
// struct<(array<2 x i64>, array<3 x i64>)>) ; dim0, dim1, 3xsizes
74+
//
5275
//===----------------------------------------------------------------------===//
5376

5477
enum class SparseTensorFieldKind : uint32_t {
@@ -198,11 +221,11 @@ class SparseTensorSpecifier {
198221
TypedValue<StorageSpecifierType> specifier;
199222
};
200223

201-
/// A helper class around an array of values that corresponding to a sparse
202-
/// tensor, provides a set of meaningful APIs to query and update a particular
203-
/// field in a consistent way.
204-
/// Users should not make assumption on how a sparse tensor is laid out but
205-
/// instead relies on this class to access the right value for the right field.
224+
/// A helper class around an array of values that corresponds to a sparse
225+
/// tensor. This class provides a set of meaningful APIs to query and update
226+
/// a particular field in a consistent way. Users should not make assumptions
227+
/// on how a sparse tensor is laid out but instead rely on this class to access
228+
/// the right value for the right field.
206229
template <typename ValueArrayRef>
207230
class SparseTensorDescriptorImpl {
208231
protected:

0 commit comments

Comments
 (0)