MLIR

Multi-Level IR Compiler Framework

'sparse_tensor' Dialect

The SparseTensor dialect supports all the attributes, types, operations, and passes that are required to make sparse tensor types first class citizens within the MLIR compiler infrastructure. The dialect forms a bridge between high-level operations on sparse tensors types and lower-level operations on the actual sparse storage schemes consisting of positions, coordinates, and values. Lower-level support may consist of fully generated code or may be provided by means of a small sparse runtime support library.

The concept of treating sparsity as a property, not a tedious implementation detail, by letting a sparsifier generate sparse code automatically was pioneered for linear algebra by [Bik96] in MT1 (see https://www.aartbik.com/sparse.php) and formalized to tensor algebra by [Kjolstad17,Kjolstad20] in the Sparse Tensor Algebra Compiler (TACO) project (see http://tensor-compiler.org). Please note that we started to prefer the term “sparsifier” over the also commonly used “sparse compiler” terminology to refer to such a pass to make it clear that the sparsifier pass is not a separate compiler, but should be an integral part of any compiler pipeline that is built with the MLIR compiler infrastructure

The MLIR implementation [Biketal22] closely follows the “sparse iteration theory” that forms the foundation of TACO. A rewriting rule is applied to each tensor expression in the Linalg dialect (MLIR’s tensor index notation) where the sparsity of tensors is indicated using the per-level level-types (e.g., dense, compressed, singleton) together with a specification of the order on the levels (see [Chou18] for an in-depth discussions and possible extensions to these level-types). Subsequently, a topologically sorted iteration graph, reflecting the required order on coordinates with respect to the levels of each tensor, is constructed to ensure that all tensors are visited in natural level-coordinate order. Next, iteration lattices are constructed for the tensor expression for every index in topological order. Each iteration lattice point consists of a conjunction of tensor coordinates together with a tensor (sub)expression that needs to be evaluated for that conjunction. Within the lattice, iteration points are ordered according to the way coordinates are exhausted. As such these iteration lattices drive actual sparse code generation, which consists of a relatively straightforward one-to-one mapping from iteration lattices to combinations of for-loops, while-loops, and if-statements. Sparse tensor outputs that materialize uninitialized are handled with direct insertions if all parallel loops are outermost or insertions that indirectly go through a 1-dimensional access pattern expansion (a.k.a. workspace) where feasible [Gustavson72,Bik96,Kjolstad19].

  • [Bik96] Aart J.C. Bik. Compiler Support for Sparse Matrix Computations. PhD thesis, Leiden University, May 1996.
  • [Biketal22] Aart J.C. Bik, Penporn Koanantakool, Tatiana Shpeisman, Nicolas Vasilache, Bixia Zheng, and Fredrik Kjolstad. Compiler Support for Sparse Tensor Computations in MLIR. ACM Transactions on Architecture and Code Optimization, June, 2022. See: https://dl.acm.org/doi/10.1145/3544559
  • [Chou18] Stephen Chou, Fredrik Berg Kjolstad, and Saman Amarasinghe. Format Abstraction for Sparse Tensor Algebra Compilers. Proceedings of the ACM on Programming Languages, October 2018.
  • [Chou20] Stephen Chou, Fredrik Berg Kjolstad, and Saman Amarasinghe. Automatic Generation of Efficient Sparse Tensor Format Conversion Routines. Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation, June, 2020.
  • [Gustavson72] Fred G. Gustavson. Some basic techniques for solving sparse systems of linear equations. In Sparse Matrices and Their Applications, pages 41–52. Plenum Press, New York, 1972.
  • [Kjolstad17] Fredrik Berg Kjolstad, Shoaib Ashraf Kamil, Stephen Chou, David Lugato, and Saman Amarasinghe. The Tensor Algebra Compiler. Proceedings of the ACM on Programming Languages, October 2017.
  • [Kjolstad19] Fredrik Berg Kjolstad, Peter Ahrens, Shoaib Ashraf Kamil, and Saman Amarasinghe. Tensor Algebra Compilation with Workspaces, Proceedings of the IEEE/ACM International Symposium on Code Generation and Optimization, 2019.
  • [Kjolstad20] Fredrik Berg Kjolstad. Sparse Tensor Algebra Compilation. PhD thesis, MIT, February, 2020.

Operations 

source

sparse_tensor.assemble (sparse_tensor::AssembleOp) 

Returns a sparse tensor assembled from the given levels and values

Syntax:

operation ::= `sparse_tensor.assemble` ` ` `(` $levels       `)` `,` $values attr-dict `:`    `(` type($levels) `)` `,` type($values) `to` type($result)

Assembles the per-level position and coordinate arrays together with the values arrays into a sparse tensor. The order and types of the provided levels must be consistent with the actual storage layout of the returned sparse tensor described below.

  • levels: [tensor<? x iType>, ...] supplies the sparse tensor position and coordinate arrays of the sparse tensor for the corresponding level as specifed by sparse_tensor::StorageLayout.
  • values : tensor<? x V> supplies the values array for the stored elements in the sparse tensor.

This operation can be used to assemble a sparse tensor from an external source; e.g., by passing numpy arrays from Python. It is the user’s responsibility to provide input that can be correctly interpreted by the sparsifier, which does not perform any sanity test to verify data integrity.

Example:

%pos    = arith.constant dense<[0, 3]>                : tensor<2xindex>
%index  = arith.constant dense<[[0,0], [1,2], [1,3]]> : tensor<3x2xindex>
%values = arith.constant dense<[ 1.1,   2.2,   3.3 ]> : tensor<3xf64>
%s = sparse_tensor.assemble (%pos, %index), %values
   : (tensor<2xindex>, tensor<3x2xindex>), tensor<3xf64> to tensor<3x4xf64, #COO>
// yields COO format |1.1, 0.0, 0.0, 0.0|
//     of 3x4 matrix |0.0, 0.0, 2.2, 3.3|
//                   |0.0, 0.0, 0.0, 0.0|

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
levelsvariadic of tensor of signless integer or index values
valuestensor of any type values

Results: 

ResultDescription
resultsparse tensor of any type values

sparse_tensor.binary (sparse_tensor::BinaryOp) 

Binary set operation utilized within linalg.generic

Syntax:

operation ::= `sparse_tensor.binary` $x `,` $y `:` attr-dict type($x) `,` type($y) `to` type($output) `\n`
              `overlap` `=` $overlapRegion `\n`
              `left` `=` (`identity` $left_identity^):($leftRegion)? `\n`
              `right` `=` (`identity` $right_identity^):($rightRegion)?

Defines a computation within a linalg.generic operation that takes two operands and executes one of the regions depending on whether both operands or either operand is nonzero (i.e. stored explicitly in the sparse storage format).

Three regions are defined for the operation and must appear in this order:

  • overlap (elements present in both sparse tensors)
  • left (elements only present in the left sparse tensor)
  • right (element only present in the right sparse tensor)

Each region contains a single block describing the computation and result. Every non-empty block must end with a sparse_tensor.yield and the return type must match the type of output. The primary region’s block has two arguments, while the left and right region’s block has only one argument.

A region may also be declared empty (i.e. left={}), indicating that the region does not contribute to the output. For example, setting both left={} and right={} is equivalent to the intersection of the two inputs as only the overlap region will contribute values to the output.

As a convenience, there is also a special token identity which can be used in place of the left or right region. This token indicates that the return value is the input value (i.e. func(%x) => return %x). As a practical example, setting left=identity and right=identity would be equivalent to a union operation where non-overlapping values in the inputs are copied to the output unchanged.

Due to the possibility of empty regions, i.e. lack of a value for certain cases, the result of this operation may only feed directly into the output of the linalg.generic operation or into into a custom reduction sparse_tensor.reduce operation that follows in the same region.

Example of isEqual applied to intersecting elements only:

%C = tensor.empty(...)
%0 = linalg.generic #trait
  ins(%A: tensor<?xf64, #SparseVector>,
      %B: tensor<?xf64, #SparseVector>)
  outs(%C: tensor<?xi8, #SparseVector>) {
  ^bb0(%a: f64, %b: f64, %c: i8) :
    %result = sparse_tensor.binary %a, %b : f64, f64 to i8
      overlap={
        ^bb0(%arg0: f64, %arg1: f64):
          %cmp = arith.cmpf "oeq", %arg0, %arg1 : f64
          %ret_i8 = arith.extui %cmp : i1 to i8
          sparse_tensor.yield %ret_i8 : i8
      }
      left={}
      right={}
    linalg.yield %result : i8
} -> tensor<?xi8, #SparseVector>

Example of A+B in upper triangle, A-B in lower triangle:

%C = tensor.empty(...)
%1 = linalg.generic #trait
  ins(%A: tensor<?x?xf64, #CSR>, %B: tensor<?x?xf64, #CSR>
  outs(%C: tensor<?x?xf64, #CSR> {
  ^bb0(%a: f64, %b: f64, %c: f64) :
    %row = linalg.index 0 : index
    %col = linalg.index 1 : index
    %result = sparse_tensor.binary %a, %b : f64, f64 to f64
      overlap={
        ^bb0(%x: f64, %y: f64):
          %cmp = arith.cmpi "uge", %col, %row : index
          %upperTriangleResult = arith.addf %x, %y : f64
          %lowerTriangleResult = arith.subf %x, %y : f64
          %ret = arith.select %cmp, %upperTriangleResult, %lowerTriangleResult : f64
          sparse_tensor.yield %ret : f64
      }
      left=identity
      right={
        ^bb0(%y: f64):
          %cmp = arith.cmpi "uge", %col, %row : index
          %lowerTriangleResult = arith.negf %y : f64
          %ret = arith.select %cmp, %y, %lowerTriangleResult : f64
          sparse_tensor.yield %ret : f64
      }
    linalg.yield %result : f64
} -> tensor<?x?xf64, #CSR>

Example of set difference. Returns a copy of A where its sparse structure is not overlapped by B. The element type of B can be different than A because we never use its values, only its sparse structure:

%C = tensor.empty(...)
%2 = linalg.generic #trait
  ins(%A: tensor<?x?xf64, #CSR>, %B: tensor<?x?xi32, #CSR>
  outs(%C: tensor<?x?xf64, #CSR> {
  ^bb0(%a: f64, %b: i32, %c: f64) :
    %result = sparse_tensor.binary %a, %b : f64, i32 to f64
      overlap={}
      left=identity
      right={}
    linalg.yield %result : f64
} -> tensor<?x?xf64, #CSR>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
left_identity::mlir::UnitAttrunit attribute
right_identity::mlir::UnitAttrunit attribute

Operands: 

OperandDescription
xany type
yany type

Results: 

ResultDescription
outputany type

sparse_tensor.compress (sparse_tensor::CompressOp) 

Compressed an access pattern for insertion

Syntax:

operation ::= `sparse_tensor.compress` $values `,` $filled `,` $added `,` $count `into` $tensor `[` $lvlCoords `]` attr-dict `:` type($values) `,` type($filled) `,` type($added) `,` type($tensor)

Finishes a single access pattern expansion by moving inserted elements into the sparse storage scheme of the given tensor with the given level-coordinates. The arity of lvlCoords is one less than the level-rank of the tensor, with the coordinate of the innermost level defined through the added array. The values and filled arrays are reset in a sparse fashion by only iterating over set elements through an indirection using the added array, so that the operations are kept proportional to the number of nonzeros. See the sparse_tensor.expand operation for more details.

Note that this operation is “impure” in the sense that even though the result is modeled through an SSA value, the insertion is eventually done “in place”, and referencing the old SSA value is undefined behavior.

Example:

%result = sparse_tensor.compress %values, %filled, %added, %count into %tensor[%i]
  : memref<?xf64>, memref<?xi1>, memref<?xindex>, tensor<4x4xf64, #CSR>

Interfaces: InferTypeOpInterface

Operands: 

OperandDescription
valuesstrided memref of any type values of rank 1
filled1D memref of 1-bit signless integer values
added1D memref of index values
countindex
tensorsparse tensor of any type values
lvlCoordsvariadic of index

Results: 

ResultDescription
resultsparse tensor of any type values

sparse_tensor.concatenate (sparse_tensor::ConcatenateOp) 

Concatenates a list of tensors into a single tensor.

Syntax:

operation ::= `sparse_tensor.concatenate` $inputs attr-dict `:` type($inputs) `to` type($result)

Concatenates a list input tensors and the output tensor with the same dimension-rank. The concatenation happens on the specified dimension (0 <= dimension < dimRank). The resulting dimension size is the sum of all the input sizes for that dimension, while all the other dimensions should have the same size in the input and output tensors.

Only statically-sized input tensors are accepted, while the output tensor can be dynamically-sized.

Example:

%0 = sparse_tensor.concatenate %1, %2 { dimension = 0 : index }
  : tensor<64x64xf64, #CSR>, tensor<64x64xf64, #CSR> to tensor<128x64xf64, #CSR>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), StageWithSortSparseOpInterface

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
dimension::mlir::IntegerAttrdimension attribute

Operands: 

OperandDescription
inputsvariadic of ranked tensor of any type values

Results: 

ResultDescription
resultranked tensor of any type values

sparse_tensor.convert (sparse_tensor::ConvertOp) 

Converts between different tensor types

Syntax:

operation ::= `sparse_tensor.convert` $source attr-dict `:` type($source) `to` type($dest)

Converts one sparse or dense tensor type to another tensor type. The rank of the source and destination types must match exactly, and the dimension sizes must either match exactly or relax from a static to a dynamic size. The sparse encoding of the two types can obviously be completely different. The name convert was preferred over cast, since the operation may incur a non-trivial cost.

When converting between two different sparse tensor types, only explicitly stored values are moved from one underlying sparse storage format to the other. When converting from an unannotated dense tensor type to a sparse tensor type, an explicit test for nonzero values is used. When converting to an unannotated dense tensor type, implicit zeroes in the sparse storage format are made explicit. Note that the conversions can have non-trivial costs associated with them, since they may involve elaborate data structure transformations. Also, conversions from sparse tensor types into dense tensor types may be infeasible in terms of storage requirements.

Trivial dense-to-dense convert will be removed by canonicalization while trivial sparse-to-sparse convert will be removed by the sparse codegen. This is because we use trivial sparse-to-sparse convert to tell bufferization that the sparse codegen will expand the tensor buffer into sparse tensor storage.

Examples:

%0 = sparse_tensor.convert %a : tensor<32x32xf32> to tensor<32x32xf32, #CSR>
%1 = sparse_tensor.convert %a : tensor<32x32xf32> to tensor<?x?xf32, #CSR>
%2 = sparse_tensor.convert %b : tensor<8x8xi32, #CSC> to tensor<8x8xi32, #CSR>
%3 = sparse_tensor.convert %c : tensor<4x8xf64, #CSR> to tensor<4x?xf64, #CSC>

// The following conversion is not allowed (since it would require a
// runtime assertion that the source's dimension size is actually 100).
%4 = sparse_tensor.convert %d : tensor<?xf64> to tensor<100xf64, #SV>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), StageWithSortSparseOpInterface

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
sourcetensor of any type values

Results: 

ResultDescription
desttensor of any type values

sparse_tensor.coordinates (sparse_tensor::ToCoordinatesOp) 

Extracts the level-th coordinates array of the tensor

Syntax:

operation ::= `sparse_tensor.coordinates` $tensor attr-dict `:` type($tensor) `to` type($result)

Returns the coordinates array of the tensor’s storage at the given level. This is similar to the bufferization.to_memref operation in the sense that it provides a bridge between a tensor world view and a bufferized world view. Unlike the bufferization.to_memref operation, however, this sparse operation actually lowers into code that extracts the coordinates array from the sparse storage itself (either by calling a support library or through direct code).

Writing into the result of this operation is undefined behavior.

Example:

%1 = sparse_tensor.coordinates %0 { level = 1 : index }
   : tensor<64x64xf64, #CSR> to memref<?xindex>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
level::mlir::IntegerAttrlevel attribute

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resultnon-0-ranked.memref of any type values

sparse_tensor.coordinates_buffer (sparse_tensor::ToCoordinatesBufferOp) 

Extracts the linear coordinates array from a tensor

Syntax:

operation ::= `sparse_tensor.coordinates_buffer` $tensor attr-dict `:` type($tensor) `to` type($result)

Returns the linear coordinates array for a sparse tensor with a trailing COO region with at least two levels. It is an error if the tensor doesn’t contain such a COO region. This is similar to the bufferization.to_memref operation in the sense that it provides a bridge between a tensor world view and a bufferized world view. Unlike the bufferization.to_memref operation, however, this operation actually lowers into code that extracts the linear coordinates array from the sparse storage scheme that stores the coordinates for the COO region as an array of structures. For example, a 2D COO sparse tensor with two non-zero elements at coordinates (1, 3) and (4, 6) are stored in a linear buffer as (1, 4, 3, 6) instead of two buffer as (1, 4) and (3, 6).

Writing into the result of this operation is undefined behavior.

Example:

%1 = sparse_tensor.coordinates_buffer %0
   : tensor<64x64xf64, #COO> to memref<?xindex>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resultnon-0-ranked.memref of any type values

sparse_tensor.crd_translate (sparse_tensor::CrdTranslateOp) 

Performs coordinate translation between level and dimension coordinate space.

Syntax:

operation ::= `sparse_tensor.crd_translate` $direction `[` $in_crds `]` `as` $encoder attr-dict `:` type($out_crds)

Performs coordinate translation between level and dimension coordinate space according to the affine maps defined by $encoder.

Example:

%l0, %l1, %l2, %l3 = sparse_tensor.crd_translate dim_to_lvl [%d0, %d1] as #BSR
                   : index, index, index, index

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
direction::mlir::sparse_tensor::CrdTransDirectionKindAttr
sparse tensor coordinate translation direction

Enum cases:

  • dim_to_lvl (dim2lvl)
  • lvl_to_dim (lvl2dim)
encoder::mlir::sparse_tensor::SparseTensorEncodingAttr
An attribute to encode information on sparsity properties of tensors, inspired
by the TACO formalization of sparse tensors. This encoding is eventually used
by a **sparsifier** pass to generate sparse code fully automatically from a
sparsity-agnostic representation of the computation, i.e., an implicit sparse
representation is converted to an explicit sparse representation where co-iterating
loops operate on sparse storage formats rather than tensors with a sparsity
encoding. Compiler passes that run before this sparsifier pass need to be aware
of the semantics of tensor types with such a sparsity encoding.

In this encoding, we use dimension to refer to the axes of the semantic tensor, and level to refer to the axes of the actual storage format, i.e., the operational representation of the sparse tensor in memory. The number of dimensions is usually the same as the number of levels (such as CSR storage format). However, the encoding can also map dimensions to higher-order levels (for example, to encode a block-sparse BSR storage format) or to lower-order levels (for example, to linearize dimensions as a single level in the storage).

The encoding contains a map that provides the following:

  • An ordered sequence of dimension specifications, each of which defines:
    • the dimension-size (implicit from the tensor’s dimension-shape)
    • a dimension-expression
  • An ordered sequence of level specifications, each of which includes a required level-type, which defines how the level should be stored. Each level-type consists of:
    • a level-expression, which defines what is stored
    • a level-format
    • a collection of level-properties that apply to the level-format

Each level-expression is an affine expression over dimension-variables. Thus, the level-expressions collectively define an affine map from dimension-coordinates to level-coordinates. The dimension-expressions collectively define the inverse map, which only needs to be provided for elaborate cases where it cannot be inferred automatically.

Each dimension could also have an optional SparseTensorDimSliceAttr. Within the sparse storage format, we refer to indices that are stored explicitly as coordinates and offsets into the storage format as positions.

The supported level-formats are the following:

  • dense : all entries along this level are stored and linearized.
  • batch : all entries along this level are stored but not linearized.
  • compressed : only nonzeros along this level are stored
  • loose_compressed : as compressed, but allows for free space between regions
  • singleton : a variant of the compressed format, where coordinates have no siblings
  • structured[n, m] : the compression uses a n:m encoding (viz. n out of m consecutive elements are nonzero)

For a compressed level, each position interval is represented in a compact way with a lowerbound pos(i) and an upperbound pos(i+1) - 1, which implies that successive intervals must appear in order without any "holes" in between them. The loose compressed format relaxes these constraints by representing each position interval with a lowerbound lo(i) and an upperbound hi(i), which allows intervals to appear in arbitrary order and with elbow room between them.

By default, each level-type has the property of being unique (no duplicate coordinates at that level) and ordered (coordinates appear sorted at that level). For singleton levels, the coordinates are fused with its parents in AoS (array of structures) scheme. The following properties can be added to a level-format to change this default behavior:

  • nonunique : duplicate coordinates may appear at the level
  • nonordered : coordinates may appear in arbribratry order
  • soa : only applicable to singleton levels, fuses the singleton level in SoA (structure of arrays) scheme.

In addition to the map, the following fields are optional:

  • The required bitwidth for position storage (integral offsets into the sparse storage scheme). A narrow width reduces the memory footprint of overhead storage, as long as the width suffices to define the total required range (viz. the maximum number of stored entries over all indirection levels). The choices are 8, 16, 32, 64, or, the default, 0 to indicate the native bitwidth.

  • The required bitwidth for coordinate storage (the coordinates of stored entries). A narrow width reduces the memory footprint of overhead storage, as long as the width suffices to define the total required range (viz. the maximum value of each tensor coordinate over all levels). The choices are 8, 16, 32, 64, or, the default, 0 to indicate a native bitwidth.

  • The explicit value for the sparse tensor. If explicitVal is set, then all the non-zero values in the tensor have the same explicit value. The default value Attribute() indicates that it is not set. This is useful for binary-valued sparse tensors whose values can either be an implicit value (0 by default) or an explicit value (such as 1). In this approach, we don’t store explicit/implicit values, and metadata (such as position and coordinate arrays) alone fully defines the original tensor. This yields additional savings for the storage requirements, as well as for the computational time, since we skip operating on implicit values and can constant fold the explicit values where they are used.

  • The implicit value for the sparse tensor. If implicitVal is set, then the "zero" value in the tensor is equal to the implicit value. For now, we only support 0 as the implicit value but it could be extended in the future. The default value Attribute() indicates that the implicit value is 0 (same type as the tensor element type).

Examples:

// Sparse vector.
#SparseVector = #sparse_tensor.encoding&lt;{
  map = (i) -&gt; (i : compressed)
}&gt;
... tensor&lt;?xf32, #SparseVector&gt; ...

// Sorted coordinate scheme (arranged in AoS format by default).
#SortedCOO = #sparse_tensor.encoding&lt;{
  map = (i, j) -&gt; (i : compressed(nonunique), j : singleton)
}&gt;
// coordinates = {x_crd, y_crd}[nnz]
... tensor&lt;?x?xf64, #SortedCOO&gt; ...

// Sorted coordinate scheme (arranged in SoA format).
#SortedCOO = #sparse_tensor.encoding&lt;{
  map = (i, j) -&gt; (i : compressed(nonunique), j : singleton(soa))
}&gt;
// coordinates = {x_crd[nnz], y_crd[nnz]}
... tensor&lt;?x?xf64, #SortedCOO&gt; ...

// Batched sorted coordinate scheme, with high encoding.
#BCOO = #sparse_tensor.encoding&lt;{
  map = (i, j, k) -&gt; (i : dense, j : compressed(nonunique, high), k : singleton)
}&gt;
... tensor&lt;10x10xf32, #BCOO&gt; ...

// Compressed sparse row.
#CSR = #sparse_tensor.encoding&lt;{
  map = (i, j) -&gt; (i : dense, j : compressed)
}&gt;
... tensor&lt;100x100xbf16, #CSR&gt; ...

// Doubly compressed sparse column storage with specific bitwidths.
#DCSC = #sparse_tensor.encoding&lt;{
  map = (i, j) -&gt; (j : compressed, i : compressed),
  posWidth = 32,
  crdWidth = 8
}&gt;
... tensor&lt;8x8xf64, #DCSC&gt; ...

// Doubly compressed sparse column storage with specific
// explicit and implicit values.
#DCSC = #sparse_tensor.encoding&lt;{
  map = (i, j) -&gt; (j : compressed, i : compressed),
  explicitVal = 1 : i64,
  implicitVal = 0 : i64
}&gt;
... tensor&lt;8x8xi64, #DCSC&gt; ...

// Block sparse row storage (2x3 blocks).
#BSR = #sparse_tensor.encoding&lt;{
  map = ( i, j ) -&gt;
  ( i floordiv 2 : dense,
    j floordiv 3 : compressed,
    i mod 2      : dense,
    j mod 3      : dense
  )
}&gt;
... tensor&lt;20x30xf32, #BSR&gt; ...

// Same block sparse row storage (2x3 blocks) but this time
// also with a redundant reverse mapping, which can be inferred.
#BSR_explicit = #sparse_tensor.encoding&lt;{
  map = { ib, jb, ii, jj }
        ( i = ib * 2 + ii,
          j = jb * 3 + jj) -&gt;
  ( ib = i floordiv 2 : dense,
    jb = j floordiv 3 : compressed,
    ii = i mod 2 : dense,
    jj = j mod 3 : dense)
}&gt;
... tensor&lt;20x30xf32, #BSR_explicit&gt; ...

// ELL format.
// In the simple format for matrix, one array stores values and another
// array stores column indices. The arrays have the same number of rows
// as the original matrix, but only have as many columns as
// the maximum number of nonzeros on a row of the original matrix.
// There are many variants for ELL such as jagged diagonal scheme.
// To implement ELL, map provides a notion of &quot;counting a
// dimension&quot;, where every stored element with the same coordinate
// is mapped to a new slice. For instance, ELL storage of a 2-d
// tensor can be defined with the mapping (i, j) -&gt; (#i, i, j)
// using the notation of [Chou20]. Lacking the # symbol in MLIR's
// affine mapping, we use a free symbol c to define such counting,
// together with a constant that denotes the number of resulting
// slices. For example, the mapping [c](i, j) -&gt; (c * 3 * i, i, j)
// with the level-types [&quot;dense&quot;, &quot;dense&quot;, &quot;compressed&quot;] denotes ELL
// storage with three jagged diagonals that count the dimension i.
#ELL = #sparse_tensor.encoding&lt;{
  map = [c](i, j) -&gt; (c * 3 * i : dense, i : dense, j : compressed)
}&gt;
... tensor&lt;?x?xf64, #ELL&gt; ...

// CSR slice (offset = 0, size = 4, stride = 1 on the first dimension;
// offset = 0, size = 8, and a dynamic stride on the second dimension).
#CSR_SLICE = #sparse_tensor.encoding&lt;{
  map = (i : #sparse_tensor&lt;slice(0, 4, 1)&gt;,
         j : #sparse_tensor&lt;slice(0, 8, ?)&gt;) -&gt;
        (i : dense, j : compressed)
}&gt;
... tensor&lt;?x?xf64, #CSR_SLICE&gt; ...

Operands: 

OperandDescription
in_crdsvariadic of index

Results: 

ResultDescription
out_crdsvariadic of index

sparse_tensor.disassemble (sparse_tensor::DisassembleOp) 

Copies the levels and values of the given sparse tensor

Syntax:

operation ::= `sparse_tensor.disassemble` $tensor attr-dict `:` type($tensor)`out_lvls` `(` $out_levels `:` type($out_levels) `)` `out_vals` `(` $out_values `:` type($out_values) `)` `->``(` type($ret_levels) `)` `,` type($ret_values) `,` `(` type($lvl_lens)   `)` `,` type($val_len)

The disassemble operation is the inverse of sparse_tensor::assemble. It copies the per-level position and coordinate arrays together with the values array of the given sparse tensor into the user-supplied buffers along with the actual length of the memory used in each returned buffer.

This operation can be used for returning a disassembled MLIR sparse tensor; e.g., copying the sparse tensor contents into pre-allocated numpy arrays back to Python. It is the user’s responsibility to allocate large enough buffers of the appropriate types to hold the sparse tensor contents. The sparsifier simply copies all fields of the sparse tensor into the user-supplied buffers without any sanity test to verify data integrity.

Example:

// input COO format |1.1, 0.0, 0.0, 0.0|
//    of 3x4 matrix |0.0, 0.0, 2.2, 3.3|
//                  |0.0, 0.0, 0.0, 0.0|
%p, %c, %v, %p_len, %c_len, %v_len =
  sparse_tensor.disassemble %s : tensor<3x4xf64, #COO>
     out_lvls(%op, %oi : tensor<2xindex>, tensor<3x2xindex>)
     out_vals(%od : tensor<3xf64>) ->
       (tensor<2xindex>, tensor<3x2xindex>), tensor<3xf64>, (index, index), index
// %p = arith.constant dense<[ 0,              3 ]> : tensor<2xindex>
// %c = arith.constant dense<[[0,0], [1,2], [1,3]]> : tensor<3x2xindex>
// %v = arith.constant dense<[ 1.1,   2.2,   3.3 ]> : tensor<3xf64>
// %p_len = 2
// %c_len = 6 (3x2)
// %v_len = 3

Traits: AlwaysSpeculatableImplTrait, SameVariadicResultSize

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
tensorsparse tensor of any type values
out_levelsvariadic of tensor of signless integer or index values
out_valuestensor of any type values

Results: 

ResultDescription
ret_levelsvariadic of tensor of signless integer or index values
ret_valuestensor of any type values
lvl_lensvariadic of scalar like
val_lenscalar like

sparse_tensor.expand (sparse_tensor::ExpandOp) 

Expands an access pattern for insertion

Syntax:

operation ::= `sparse_tensor.expand` $tensor attr-dict `:` type($tensor) `to` type($values) `,` type($filled) `,` type($added)

Performs an access pattern expansion for the innermost levels of the given tensor. This operation is useful to implement kernels in which a sparse tensor appears as output. This technique is known under several different names and using several alternative implementations, for example, phase counter [Gustavson72], expanded or switch array [Pissanetzky84], in phase scan [Duff90], access pattern expansion [Bik96], and workspaces [Kjolstad19].

The values and filled arrays must have lengths equal to the level-size of the innermost level (i.e., as if the innermost level were dense). The added array and count are used to store new level-coordinates when a false value is encountered in the filled array. All arrays should be allocated before the loop (possibly even shared between loops in a future optimization) so that their dense initialization can be amortized over many iterations. Setting and resetting the dense arrays in the loop nest itself is kept sparse by only iterating over set elements through an indirection using the added array, so that the operations are kept proportional to the number of nonzeros.

Note that this operation is “impure” in the sense that even though the results are modeled through SSA values, the operation relies on a proper side-effecting context that sets and resets the expanded arrays.

Example:

%values, %filled, %added, %count = sparse_tensor.expand %tensor
  : tensor<4x4xf64, #CSR> to memref<?xf64>, memref<?xi1>, memref<?xindex>

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
valuesstrided memref of any type values of rank 1
filled1D memref of 1-bit signless integer values
added1D memref of index values
countindex

sparse_tensor.extract_iteration_space (sparse_tensor::ExtractIterSpaceOp) 

Extracts an iteration space from a sparse tensor between certain levels

Syntax:

operation ::= `sparse_tensor.extract_iteration_space` $tensor (`at` $parentIter^)? `lvls` `=` custom<LevelRange>($loLvl, $hiLvl)  attr-dict `:` type($tensor) (`,` type($parentIter)^)?

Extracts a !sparse_tensor.iter_space from a sparse tensor between certain (consecutive) levels. For sparse levels, it is usually done by loading a postion range from the underlying sparse tensor storage. E.g., for a compressed level, the iteration space is extracted by [pos[i], pos[i+1]) supposing the the parent iterator points at i.

tensor: the input sparse tensor that defines the iteration space. parentIter: the iterator for the previous level, at which the iteration space at the current levels will be extracted. loLvl, hiLvl: the level range between [loLvl, hiLvl) in the input tensor that the returned iteration space covers. hiLvl - loLvl defines the dimension of the iteration space.

The type of returned the value is automatically inferred to !sparse_tensor.iter_space<#INPUT_ENCODING, lvls = $loLvl to $hiLvl>. The returned iteration space can then be iterated over by sparse_tensor.iterate operations to visit every stored element (usually nonzeros) in the input sparse tensor.

Example:

// Extracts a 1-D iteration space from a COO tensor at level 1.
%space = sparse_tensor.iteration.extract_space %sp at %it1 lvls = 1
  : tensor<4x8xf32, #COO>, !sparse_tensor.iterator<#COO, lvls = 0>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
loLvl::mlir::IntegerAttrlevel attribute
hiLvl::mlir::IntegerAttrlevel attribute

Operands: 

OperandDescription
tensorsparse tensor of any type values
parentItersparse iterator

Results: 

ResultDescription
resultSpacesparse iteration space

sparse_tensor.foreach (sparse_tensor::ForeachOp) 

Iterates over elements in a tensor

Syntax:

operation ::= `sparse_tensor.foreach` `in` $tensor (`init``(`$initArgs^`)`)? attr-dict    `:` type($tensor) (`,` type($initArgs)^)?  (`->` type($results)^)?  `do` $region

Iterates over stored elements in a tensor (which are typically, but not always, non-zero for sparse tensors) and executes the block.

tensor: the input tensor to iterate over. initArgs: the initial loop argument to carry and update during each iteration. order: an optional permutation affine map that specifies the order in which the dimensions are visited (e.g., row first or column first). This is only applicable when the input tensor is a non-annotated dense tensor.

For an input tensor with dim-rank n, the block must take n + 1 arguments (plus additional loop-carried variables as described below). The first n arguments provide the dimension-coordinates of the element being visited, and must all have index type. The (n+1)-th argument provides the element’s value, and must have the tensor’s element type.

sparse_tensor.foreach can also operate on loop-carried variables and returns the final values after loop termination. The initial values of the variables are passed as additional SSA operands to the “sparse_tensor.foreach” following the n + 1 SSA values mentioned above (n coordinates and 1 value).

The region must terminate with a “sparse_tensor.yield” that passes the current values of all loop-carried variables to the next iteration, or to the result, if at the last iteration. The number and static types of loop-carried variables may not change with iterations.

For example:

%c0 = arith.constant 0 : i32
%ret = sparse_tensor.foreach in %0 init(%c0): tensor<?x?xi32, #DCSR>, i32 -> i32 do {
 ^bb0(%arg1: index, %arg2: index, %arg3: i32, %iter: i32):
   %sum = arith.add %iter, %arg3
   sparse_tensor.yield %sum
}

It is important to note that the generated loop iterates over elements in their storage order. However, regardless of the storage scheme used by the tensor, the block is always given the dimension-coordinates.

For example:

#COL_MAJOR = #sparse_tensor.encoding<{
  map = (d0, d1) -> (d1 : compressed, d0 : compressed)
}>

// foreach on a column-major sparse tensor
sparse_tensor.foreach in %0 : tensor<2x3xf64, #COL_MAJOR> do {
 ^bb0(%row: index, %col: index, %arg3: f64):
    // [%row, %col] -> [0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1]
}

#ROW_MAJOR = #sparse_tensor.encoding<{
  map = (d0, d1) -> (d0 : compressed, d1 : compressed)
}>

// foreach on a row-major sparse tensor
sparse_tensor.foreach in %0 : tensor<2x3xf64, #ROW_MAJOR> do {
 ^bb0(%row: index, %col: index, %arg3: f64):
    // [%row, %col] -> [0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]
}

// foreach on a row-major dense tensor but visit column first
sparse_tensor.foreach in %0 {order=affine_map<(i,j)->(j,i)>}: tensor<2x3xf64> do {
 ^bb0(%row: index, %col: index, %arg3: f64):
    // [%row, %col] -> [0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1]
}

Traits: SingleBlockImplicitTerminator<YieldOp>, SingleBlock

Attributes: 

AttributeMLIR TypeDescription
order::mlir::AffineMapAttrAffineMap attribute

Operands: 

OperandDescription
tensortensor of any type values
initArgsvariadic of any type

Results: 

ResultDescription
resultsvariadic of any type

sparse_tensor.has_runtime_library (sparse_tensor::HasRuntimeLibraryOp) 

Indicates whether running in runtime/codegen mode

Syntax:

operation ::= `sparse_tensor.has_runtime_library` attr-dict

Returns a boolean value that indicates whether the sparsifier runs in runtime library mode or not. For testing only! This operation is useful for writing test cases that require different code depending on runtime/codegen mode.

Example:

%has_runtime = sparse_tensor.has_runtime_library
scf.if %has_runtime {
  ...
}

Interfaces: InferTypeOpInterface

Results: 

ResultDescription
result1-bit signless integer

sparse_tensor.load (sparse_tensor::LoadOp) 

Rematerializes tensor from underlying sparse storage format

Syntax:

operation ::= `sparse_tensor.load` $tensor (`hasInserts` $hasInserts^)? attr-dict `:` type($tensor)

Rematerializes a tensor from the underlying sparse storage format of the given tensor. This is similar to the bufferization.to_tensor operation in the sense that it provides a bridge between a bufferized world view and a tensor world view. Unlike the bufferization.to_tensor operation, however, this sparse operation is used only temporarily to maintain a correctly typed intermediate representation during progressive bufferization.

The hasInserts attribute denote whether insertions to the underlying sparse storage format may have occurred, in which case the underlying sparse storage format needs to be finalized. Otherwise, the operation simply folds away.

Note that this operation is “impure” in the sense that even though the result is modeled through an SSA value, the operation relies on a proper context of materializing and inserting the tensor value.

Examples:

%result = sparse_tensor.load %tensor : tensor<8xf64, #SV>

%1 = sparse_tensor.load %0 hasInserts : tensor<16x32xf32, #CSR>

Traits: SameOperandsAndResultType

Interfaces: InferTypeOpInterface

Attributes: 

AttributeMLIR TypeDescription
hasInserts::mlir::UnitAttrunit attribute

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resulttensor of any type values

sparse_tensor.lvl (sparse_tensor::LvlOp) 

Level index operation

Syntax:

operation ::= `sparse_tensor.lvl` attr-dict $source `,` $index `:` type($source)

The sparse_tensor.lvl behaves similar to tensor.dim operation. It takes a sparse tensor and a level operand of type index and returns the size of the requested level of the given sparse tensor. If the sparse tensor has an identity dimension to level mapping, it returns the same result as tensor.dim. If the level index is out of bounds, the behavior is undefined.

Example:

#BSR = #sparse_tensor.encoding<{
  map = ( i, j ) ->
    ( i floordiv 2 : dense,
      j floordiv 3 : compressed,
      i mod 2      : dense,
      j mod 3      : dense
    )
}>

// Always returns 2 (4 floordiv 2), can be constant folded:
%c0 = arith.constant 0 : index
%x = sparse_tensor.lvl %A, %c0 : tensor<4x?xf32, #BSR>

// Return the dynamic dimension of %A computed by %j mod 3.
%c1 = arith.constant 1 : index
%y = sparse_tensor.lvl %A, %c1 : tensor<4x?xf32, #BSR>

// Always return 3 (since j mod 3 < 3), can be constant fold
%c3 = arith.constant 3 : index
%y = sparse_tensor.lvl %A, %c3 : tensor<4x?xf32, #BSR>

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
sourcesparse tensor of any type values
indexindex

Results: 

ResultDescription
resultindex

sparse_tensor.new (sparse_tensor::NewOp) 

Materializes a new sparse tensor from given source

Syntax:

operation ::= `sparse_tensor.new` $source attr-dict `:` type($source) `to` type($result)

Materializes a sparse tensor with contents taken from an opaque pointer provided by source. For targets that have access to a file system, for example, this pointer may be a filename (or file) of a sparse tensor in a particular external storage format. The form of the operation is kept deliberately very general to allow for alternative implementations in the future, such as pointers to buffers or runnable initialization code. The operation is provided as an anchor that materializes a properly typed sparse tensor with inital contents into a computation.

Reading in a symmetric matrix will result in just the lower/upper triangular part of the matrix (so that only relevant information is stored). Proper symmetry support for operating on symmetric matrices is still TBD.

Example:

sparse_tensor.new %source : !Source to tensor<1024x1024xf64, #CSR>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
sourceany type

Results: 

ResultDescription
resultsparse tensor of any type values

sparse_tensor.number_of_entries (sparse_tensor::NumberOfEntriesOp) 

Returns the number of entries that are stored in the tensor.

Syntax:

operation ::= `sparse_tensor.number_of_entries` $tensor attr-dict `:` type($tensor)

Returns the number of entries that are stored in the given sparse tensor. Note that this is typically the number of nonzero elements in the tensor, but since explicit zeros may appear in the storage formats, the more accurate nomenclature is used.

Example:

%noe = sparse_tensor.number_of_entries %tensor : tensor<64x64xf64, #CSR>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resultindex

sparse_tensor.out (sparse_tensor::OutOp) 

Outputs a sparse tensor to the given destination

Syntax:

operation ::= `sparse_tensor.out` $tensor `,` $dest attr-dict `:` type($tensor) `,` type($dest)

Outputs the contents of a sparse tensor to the destination defined by an opaque pointer provided by dest. For targets that have access to a file system, for example, this pointer may specify a filename (or file) for output. The form of the operation is kept deliberately very general to allow for alternative implementations in the future, such as sending the contents to a buffer defined by a pointer.

Note that this operation is “impure” in the sense that its behavior is solely defined by side-effects and not SSA values.

Example:

sparse_tensor.out %t, %dest : tensor<1024x1024xf64, #CSR>, !Dest

Operands: 

OperandDescription
tensorsparse tensor of any type values
destany type

sparse_tensor.positions (sparse_tensor::ToPositionsOp) 

Extracts the level-th positions array of the tensor

Syntax:

operation ::= `sparse_tensor.positions` $tensor attr-dict `:` type($tensor) `to` type($result)

Returns the positions array of the tensor’s storage at the given level. This is similar to the bufferization.to_memref operation in the sense that it provides a bridge between a tensor world view and a bufferized world view. Unlike the bufferization.to_memref operation, however, this sparse operation actually lowers into code that extracts the positions array from the sparse storage itself (either by calling a support library or through direct code).

Writing into the result of this operation is undefined behavior.

Example:

%1 = sparse_tensor.positions %0 { level = 1 : index }
   : tensor<64x64xf64, #CSR> to memref<?xindex>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
level::mlir::IntegerAttrlevel attribute

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resultnon-0-ranked.memref of any type values

sparse_tensor.print (sparse_tensor::PrintOp) 

Prints a sparse tensor (for testing and debugging)

Syntax:

operation ::= `sparse_tensor.print` $tensor attr-dict `:` type($tensor)

Prints the individual components of a sparse tensors (the positions, coordinates, and values components) to stdout for testing and debugging purposes. This operation lowers to just a few primitives in a light-weight runtime support to simplify supporting this operation on new platforms.

Example:

sparse_tensor.print %tensor : tensor<1024x1024xf64, #CSR>

Operands: 

OperandDescription
tensorsparse tensor of any type values

sparse_tensor.push_back (sparse_tensor::PushBackOp) 

Pushes a value to the back of a given buffer

Syntax:

operation ::= `sparse_tensor.push_back` (`inbounds` $inbounds^)? $curSize `,` $inBuffer `,` $value (`,` $n^ )?  attr-dict `:` type($curSize) `,` type($inBuffer) `,` type($value) (`,` type($n)^ )?

Pushes value to the end of the given sparse tensor storage buffer inBuffer as indicated by the value of curSize and returns the new size of the buffer in newSize (newSize = curSize + n). The capacity of the buffer is recorded in the memref type of inBuffer. If the current buffer is full, then inBuffer.realloc is called before pushing the data to the buffer. This is similar to std::vector push_back.

The optional input n specifies the number of times to repeately push the value to the back of the tensor. When n is a compile-time constant, its value can’t be less than 1. If n is a runtime value that is less than 1, the behavior is undefined. Although using input n is semantically equivalent to calling push_back n times, it gives compiler more chances to to optimize the memory reallocation and the filling of the memory with the same value.

The inbounds attribute tells the compiler that the insertion won’t go beyond the current storage buffer. This allows the compiler to not generate the code for capacity check and reallocation. The typical usage will be for “dynamic” sparse tensors for which a capacity can be set beforehand.

Note that this operation is “impure” in the sense that even though the result is modeled through an SSA value, referencing the memref through the old SSA value after this operation is undefined behavior.

Example:

%buf, %newSize = sparse_tensor.push_back %curSize, %buffer, %val
   : index, memref<?xf64>, f64
%buf, %newSize = sparse_tensor.push_back inbounds %curSize, %buffer, %val
   : xindex, memref<?xf64>, f64
%buf, %newSize = sparse_tensor.push_back inbounds %curSize, %buffer, %val, %n
   : xindex, memref<?xf64>, f64

Interfaces: InferTypeOpInterface

Attributes: 

AttributeMLIR TypeDescription
inbounds::mlir::UnitAttrunit attribute

Operands: 

OperandDescription
curSizeindex
inBuffer1D memref of any type values
valueany type
nindex

Results: 

ResultDescription
outBuffer1D memref of any type values
newSizeindex

sparse_tensor.reduce (sparse_tensor::ReduceOp) 

Custom reduction operation utilized within linalg.generic

Syntax:

operation ::= `sparse_tensor.reduce` $x `,` $y `,` $identity attr-dict `:` type($output) $region

Defines a computation with a linalg.generic operation that takes two operands and an identity value and reduces all stored values down to a single result based on the computation in the region.

The region must contain exactly one block taking two arguments. The block must end with a sparse_tensor.yield and the output must match the input argument types.

Note that this operation is only required for custom reductions beyond the standard reduction operations (add, sub, or, xor) that can be sparsified by merely reducing the stored values. More elaborate reduction operations (mul, and, min, max, etc.) would need to account for implicit zeros as well. They can still be handled using this custom reduction operation. The linalg.generic iterator_types defines which indices are being reduced. When the associated operands are used in an operation, a reduction will occur. The use of this explicit reduce operation is not required in most cases.

Example of Matrix->Vector reduction using max(product(x_i), 100):

%cf1 = arith.constant 1.0 : f64
%cf100 = arith.constant 100.0 : f64
%C = tensor.empty(...)
%0 = linalg.generic #trait
   ins(%A: tensor<?x?xf64, #SparseMatrix>)
  outs(%C: tensor<?xf64, #SparseVector>) {
  ^bb0(%a: f64, %c: f64) :
    %result = sparse_tensor.reduce %c, %a, %cf1 : f64 {
        ^bb0(%arg0: f64, %arg1: f64):
          %0 = arith.mulf %arg0, %arg1 : f64
          %cmp = arith.cmpf "ogt", %0, %cf100 : f64
          %ret = arith.select %cmp, %cf100, %0 : f64
          sparse_tensor.yield %ret : f64
      }
    linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>

Traits: AlwaysSpeculatableImplTrait, SameOperandsAndResultType

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
xany type
yany type
identityany type

Results: 

ResultDescription
outputany type

sparse_tensor.reinterpret_map (sparse_tensor::ReinterpretMapOp) 

Reinterprets the dimension/level maps of the source tensor

Syntax:

operation ::= `sparse_tensor.reinterpret_map` $source attr-dict `:` type($source) `to` type($dest)

Reinterprets the dimension-to-level and level-to-dimension map specified in source according to the type of dest. reinterpret_map is a no-op and is introduced merely to resolve type conflicts. It does not make any modification to the source tensor and source/dest tensors are considered to be aliases.

source and dest tensors are “reinterpretable” if and only if they have the exactly same storage at a low level. That is, both source and dest has the same number of levels and level types, and their shape is consistent before and after reinterpret_map.

Example:

#CSC = #sparse_tensor.encoding<{
  map = (d0, d1) -> (d1: dense, d0: compressed)
}>
#CSR = #sparse_tensor.encoding<{
  map = (d0, d1) -> (d0: dense, d1: compressed)
}>
%t1 = sparse_tensor.reinterpret_map %t0 : tensor<3x4xi32, #CSC> to tensor<4x3xi32, #CSR>

#BSR = #sparse_tensor.encoding<{
  map = ( i, j ) -> ( i floordiv 2 : dense,
                      j floordiv 3 : compressed,
                      i mod 2      : dense,
                      j mod 3      : dense
  )
}>
#DSDD = #sparse_tensor.encoding<{
  map = (i, j, k, l) -> (i: dense, j: compressed, k: dense, l: dense)
}>
%t1 = sparse_tensor.reinterpret_map %t0 : tensor<6x12xi32, #BSR> to tensor<3x4x2x3xi32, #DSDD>

Interfaces: NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
sourcesparse tensor of any type values

Results: 

ResultDescription
destsparse tensor of any type values

sparse_tensor.reorder_coo (sparse_tensor::ReorderCOOOp) 

Reorder the input COO such that it has the the same order as the output COO

Syntax:

operation ::= `sparse_tensor.reorder_coo` $algorithm $input_coo attr-dict`:` type($input_coo) `to` type($result_coo)

Reorders the input COO to the same order as specified by the output format. E.g., reorder an unordered COO into an ordered one.

The input and result COO tensor must have the same element type, position type and coordinate type. At the moment, the operation also only supports ordering input and result COO with the same dim2lvl map.

Example:

%res = sparse_tensor.reorder_coo quick_sort %coo : tensor<?x?xf64 : #Unordered_COO> to
                                                   tensor<?x?xf64 : #Ordered_COO>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
algorithm::mlir::sparse_tensor::SparseTensorSortKindAttr
sparse tensor sort algorithm

Enum cases:

  • hybrid_quick_sort (HybridQuickSort)
  • insertion_sort_stable (InsertionSortStable)
  • quick_sort (QuickSort)
  • heap_sort (HeapSort)

Operands: 

OperandDescription
input_coosparse tensor of any type values

Results: 

ResultDescription
result_coosparse tensor of any type values

sparse_tensor.select (sparse_tensor::SelectOp) 

Select operation utilized within linalg.generic

Syntax:

operation ::= `sparse_tensor.select` $x attr-dict `:` type($x) $region

Defines an evaluation within a linalg.generic operation that takes a single operand and decides whether or not to keep that operand in the output.

A single region must contain exactly one block taking one argument. The block must end with a sparse_tensor.yield and the output type must be boolean.

Value threshold is an obvious usage of the select operation. However, by using linalg.index, other useful selection can be achieved, such as selecting the upper triangle of a matrix.

Example of selecting A >= 4.0:

%C = tensor.empty(...)
%0 = linalg.generic #trait
   ins(%A: tensor<?xf64, #SparseVector>)
  outs(%C: tensor<?xf64, #SparseVector>) {
  ^bb0(%a: f64, %c: f64) :
    %result = sparse_tensor.select %a : f64 {
        ^bb0(%arg0: f64):
          %cf4 = arith.constant 4.0 : f64
          %keep = arith.cmpf "uge", %arg0, %cf4 : f64
          sparse_tensor.yield %keep : i1
      }
    linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>

Example of selecting lower triangle of a matrix:

%C = tensor.empty(...)
%1 = linalg.generic #trait
   ins(%A: tensor<?x?xf64, #CSR>)
  outs(%C: tensor<?x?xf64, #CSR>) {
  ^bb0(%a: f64, %c: f64) :
    %row = linalg.index 0 : index
    %col = linalg.index 1 : index
    %result = sparse_tensor.select %a : f64 {
        ^bb0(%arg0: f64):
          %keep = arith.cmpf "olt", %col, %row : f64
          sparse_tensor.yield %keep : i1
      }
    linalg.yield %result : f64
} -> tensor<?x?xf64, #CSR>

Traits: AlwaysSpeculatableImplTrait, SameOperandsAndResultType

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
xany type

Results: 

ResultDescription
outputany type

sparse_tensor.slice.offset (sparse_tensor::ToSliceOffsetOp) 

Extracts the offset of the sparse tensor slice at the given dimension

Syntax:

operation ::= `sparse_tensor.slice.offset` $slice `at` $dim attr-dict `:` type($slice)

Extracts the offset of the sparse tensor slice at the given dimension.

Currently, sparse tensor slices are still a work in progress, and only works when runtime library is disabled (i.e., running the sparsifier with enable-runtime-library=false).

Example:

%0 = tensor.extract_slice %s[%v1, %v2][64, 64][1, 1] : tensor<128x128xf64, #DCSR>
                                                    to tensor<64x64xf64, #Slice>

%1 = sparse_tensor.slice.offset %0 at 0 : tensor<64x64xf64, #Slice>
%2 = sparse_tensor.slice.offset %0 at 1 : tensor<64x64xf64, #Slice>
// %1 = %v1
// %2 = %v2

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
dim::mlir::IntegerAttrindex attribute

Operands: 

OperandDescription
slicesparse tensor slice of any type values

Results: 

ResultDescription
offsetindex

sparse_tensor.slice.stride (sparse_tensor::ToSliceStrideOp) 

Extracts the stride of the sparse tensor slice at the given dimension

Syntax:

operation ::= `sparse_tensor.slice.stride` $slice `at` $dim attr-dict `:` type($slice)

Extracts the stride of the sparse tensor slice at the given dimension.

Currently, sparse tensor slices are still a work in progress, and only works when runtime library is disabled (i.e., running the sparsifier with enable-runtime-library=false).

Example:

%0 = tensor.extract_slice %s[%v1, %v2][64, 64][%s1, %s2] : tensor<128x128xf64, #DCSR>
                                                        to tensor<64x64xf64, #Slice>

%1 = sparse_tensor.slice.stride %0 at 0 : tensor<64x64xf64, #Slice>
%2 = sparse_tensor.slice.stride %0 at 1 : tensor<64x64xf64, #Slice>
// %1 = %s1
// %2 = %s2

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
dim::mlir::IntegerAttrindex attribute

Operands: 

OperandDescription
slicesparse tensor slice of any type values

Results: 

ResultDescription
strideindex

sparse_tensor.sort (sparse_tensor::SortOp) 

Sorts the arrays in xs and ys lexicographically on the integral values found in the xs list

Syntax:

operation ::= `sparse_tensor.sort` $algorithm $n`,`$xy (`jointly` $ys^)? attr-dict`:` type($xy) (`jointly` type($ys)^)?

Sorts the xs values along with some ys values that are put in a single linear buffer xy. The affine map attribute perm_map specifies the permutation to be applied on the xs before comparison, the rank of the permutation map also specifies the number of xs values in xy. The optional index attribute ny provides the number of ys values in xy. When ny is not explicitly specified, its value is 0. This instruction supports a more efficient way to store the COO definition in sparse tensor type.

The buffer xy should have a dimension not less than n * (rank(perm_map) + ny) while the buffers in ys should have a dimension not less than n. The behavior of the operator is undefined if this condition is not met.

Example:

sparse_tensor.sort insertion_sort_stable %n, %x { perm_map = affine_map<(i,j) -> (j,i)> }
  : memref<?xindex>

Attributes: 

AttributeMLIR TypeDescription
perm_map::mlir::AffineMapAttrAffineMap attribute
ny::mlir::IntegerAttrindex attribute
algorithm::mlir::sparse_tensor::SparseTensorSortKindAttr
sparse tensor sort algorithm

Enum cases:

  • hybrid_quick_sort (HybridQuickSort)
  • insertion_sort_stable (InsertionSortStable)
  • quick_sort (QuickSort)
  • heap_sort (HeapSort)

Operands: 

OperandDescription
nindex
xy1D memref of integer or index values
ysvariadic of 1D memref of any type values

sparse_tensor.storage_specifier.get (sparse_tensor::GetStorageSpecifierOp) 

Syntax:

operation ::= `sparse_tensor.storage_specifier.get` $specifier $specifierKind (`at` $level^)? attr-dict`:` qualified(type($specifier))

Returns the requested field of the given storage_specifier.

Example of querying the size of the coordinates array for level 0:

%0 = sparse_tensor.storage_specifier.get %arg0 crd_mem_sz at 0
     : !sparse_tensor.storage_specifier<#COO>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
specifierKind::mlir::sparse_tensor::StorageSpecifierKindAttr
sparse tensor storage specifier kind

Enum cases:

  • lvl_sz (LvlSize)
  • pos_mem_sz (PosMemSize)
  • crd_mem_sz (CrdMemSize)
  • val_mem_sz (ValMemSize)
  • dim_offset (DimOffset)
  • dim_stride (DimStride)
level::mlir::IntegerAttrlevel attribute

Operands: 

OperandDescription
specifiermetadata

Results: 

ResultDescription
resultindex

sparse_tensor.storage_specifier.init (sparse_tensor::StorageSpecifierInitOp) 

Syntax:

operation ::= `sparse_tensor.storage_specifier.init` attr-dict (`with` $source^)? `:` (`from` qualified(type($source))^ `to`)? qualified(type($result))

Returns an initial storage specifier value. A storage specifier value holds the level-sizes, position arrays, coordinate arrays, and the value array. If this is a specifier for slices, it also holds the extra strides/offsets for each tensor dimension.

TODO: The sparse tensor slice support is currently in a unstable state, and is subject to change in the future.

Example:

#CSR = #sparse_tensor.encoding<{
  map = (i, j) -> (i : dense, j : compressed)
}>
#CSR_SLICE = #sparse_tensor.encoding<{
  map = (d0 : #sparse_tensor<slice(1, 4, 1)>,
         d1 : #sparse_tensor<slice(1, 4, 2)>) ->
        (d0 : dense, d1 : compressed)
}>

%0 = sparse_tensor.storage_specifier.init :  !sparse_tensor.storage_specifier<#CSR>
%1 = sparse_tensor.storage_specifier.init with %src
     : !sparse_tensor.storage_specifier<#CSR> to
       !sparse_tensor.storage_specifier<#CSR_SLICE>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
sourcemetadata

Results: 

ResultDescription
resultmetadata

sparse_tensor.storage_specifier.set (sparse_tensor::SetStorageSpecifierOp) 

Syntax:

operation ::= `sparse_tensor.storage_specifier.set` $specifier $specifierKind (`at` $level^)? `with` $value attr-dict `:` qualified(type($result))

Set the field of the storage specifier to the given input value. Returns the updated storage_specifier as a new SSA value.

Example of updating the sizes of the coordinates array for level 0:

%0 = sparse_tensor.storage_specifier.set %arg0 crd_mem_sz at 0 with %new_sz
   : !sparse_tensor.storage_specifier<#COO>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Attributes: 

AttributeMLIR TypeDescription
specifierKind::mlir::sparse_tensor::StorageSpecifierKindAttr
sparse tensor storage specifier kind

Enum cases:

  • lvl_sz (LvlSize)
  • pos_mem_sz (PosMemSize)
  • crd_mem_sz (CrdMemSize)
  • val_mem_sz (ValMemSize)
  • dim_offset (DimOffset)
  • dim_stride (DimStride)
level::mlir::IntegerAttrlevel attribute

Operands: 

OperandDescription
specifiermetadata
valueindex

Results: 

ResultDescription
resultmetadata

sparse_tensor.unary (sparse_tensor::UnaryOp) 

Unary set operation utilized within linalg.generic

Syntax:

operation ::= `sparse_tensor.unary` $x attr-dict `:` type($x) `to` type($output) `\n`
              `present` `=` $presentRegion `\n`
              `absent` `=` $absentRegion

Defines a computation with a linalg.generic operation that takes a single operand and executes one of two regions depending on whether the operand is nonzero (i.e. stored explicitly in the sparse storage format).

Two regions are defined for the operation must appear in this order:

  • present (elements present in the sparse tensor)
  • absent (elements not present in the sparse tensor)

Each region contains a single block describing the computation and result. A non-empty block must end with a sparse_tensor.yield and the return type must match the type of output. The primary region’s block has one argument, while the missing region’s block has zero arguments. The absent region may only generate constants or values already computed on entry of the linalg.generic operation.

A region may also be declared empty (i.e. absent={}), indicating that the region does not contribute to the output.

Due to the possibility of empty regions, i.e. lack of a value for certain cases, the result of this operation may only feed directly into the output of the linalg.generic operation or into into a custom reduction sparse_tensor.reduce operation that follows in the same region.

Example of A+1, restricted to existing elements:

%C = tensor.empty(...) : tensor<?xf64, #SparseVector>
%0 = linalg.generic #trait
   ins(%A: tensor<?xf64, #SparseVector>)
  outs(%C: tensor<?xf64, #SparseVector>) {
  ^bb0(%a: f64, %c: f64) :
    %result = sparse_tensor.unary %a : f64 to f64
      present={
      ^bb0(%arg0: f64):
        %cf1 = arith.constant 1.0 : f64
        %ret = arith.addf %arg0, %cf1 : f64
        sparse_tensor.yield %ret : f64
      }
      absent={}
    linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>

Example returning +1 for existing values and -1 for missing values:

%p1 = arith.constant  1 : i32
%m1 = arith.constant -1 : i32
%C = tensor.empty(...) : tensor<?xi32, #SparseVector>
%1 = linalg.generic #trait
   ins(%A: tensor<?xf64, #SparseVector>)
  outs(%C: tensor<?xi32, #SparseVector>) {
  ^bb0(%a: f64, %c: i32) :
    %result = sparse_tensor.unary %a : f64 to i32
      present={
      ^bb0(%x: f64):
        sparse_tensor.yield %p1 : i32
      }
      absent={
        sparse_tensor.yield %m1 : i32
      }
    linalg.yield %result : i32
} -> tensor<?xi32, #SparseVector>

Example showing a structural inversion (existing values become missing in the output, while missing values are filled with 1):

%c1 = arith.constant 1 : i64
%C = tensor.empty(...) : tensor<?xi64, #SparseVector>
%2 = linalg.generic #trait
   ins(%A: tensor<?xf64, #SparseVector>)
  outs(%C: tensor<?xi64, #SparseVector>) {
  ^bb0(%a: f64, %c: i64) :
    %result = sparse_tensor.unary %a : f64 to i64
      present={}
      absent={
        sparse_tensor.yield %c1 : i64
      }
    linalg.yield %result : i64
} -> tensor<?xi64, #SparseVector>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
xany type

Results: 

ResultDescription
outputany type

sparse_tensor.values (sparse_tensor::ToValuesOp) 

Extracts numerical values array from a tensor

Syntax:

operation ::= `sparse_tensor.values` $tensor attr-dict `:` type($tensor) `to` type($result)

Returns the values array of the sparse storage format for the given sparse tensor, independent of the actual dimension. This is similar to the bufferization.to_memref operation in the sense that it provides a bridge between a tensor world view and a bufferized world view. Unlike the bufferization.to_memref operation, however, this sparse operation actually lowers into code that extracts the values array from the sparse storage scheme (either by calling a support library or through direct code).

Writing into the result of this operation is undefined behavior.

Example:

%1 = sparse_tensor.values %0 : tensor<64x64xf64, #CSR> to memref<?xf64>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
tensorsparse tensor of any type values

Results: 

ResultDescription
resultnon-0-ranked.memref of any type values

sparse_tensor.yield (sparse_tensor::YieldOp) 

Yield from sparse_tensor set-like operations

Syntax:

operation ::= `sparse_tensor.yield` $results attr-dict `:` type($results)

Yields a value from within a binary, unary, reduce, select or foreach block.

Example:

%0 = sparse_tensor.unary %a : i64 to i64 {
  present={
    ^bb0(%arg0: i64):
      %cst = arith.constant 1 : i64
      %ret = arith.addi %arg0, %cst : i64
      sparse_tensor.yield %ret : i64
  }
}

Traits: AlwaysSpeculatableImplTrait, HasParent<BinaryOp, UnaryOp, ReduceOp, SelectOp, ForeachOp>, Terminator

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
resultsvariadic of any type

Attributes 

CrdTransDirectionKindAttr 

sparse tensor coordinate translation direction

Syntax:

#sparse_tensor.CrdTransDirection<
  ::mlir::sparse_tensor::CrdTransDirectionKind   # value
>

Enum cases:

  • dim_to_lvl (dim2lvl)
  • lvl_to_dim (lvl2dim)

Parameters: 

ParameterC++ typeDescription
value::mlir::sparse_tensor::CrdTransDirectionKindan enum of type CrdTransDirectionKind

SparseTensorDimSliceAttr 

An attribute to encode slice information of a sparse tensor on a particular dimension (a tuple of offset, size, stride).

Parameters: 

ParameterC++ typeDescription
offsetint64_t
sizeint64_t
strideint64_t

SparseTensorEncodingAttr 

An attribute to encode information on sparsity properties of tensors, inspired by the TACO formalization of sparse tensors. This encoding is eventually used by a sparsifier pass to generate sparse code fully automatically from a sparsity-agnostic representation of the computation, i.e., an implicit sparse representation is converted to an explicit sparse representation where co-iterating loops operate on sparse storage formats rather than tensors with a sparsity encoding. Compiler passes that run before this sparsifier pass need to be aware of the semantics of tensor types with such a sparsity encoding.

In this encoding, we use dimension to refer to the axes of the semantic tensor, and level to refer to the axes of the actual storage format, i.e., the operational representation of the sparse tensor in memory. The number of dimensions is usually the same as the number of levels (such as CSR storage format). However, the encoding can also map dimensions to higher-order levels (for example, to encode a block-sparse BSR storage format) or to lower-order levels (for example, to linearize dimensions as a single level in the storage).

The encoding contains a map that provides the following:

  • An ordered sequence of dimension specifications, each of which defines:
    • the dimension-size (implicit from the tensor’s dimension-shape)
    • a dimension-expression
  • An ordered sequence of level specifications, each of which includes a required level-type, which defines how the level should be stored. Each level-type consists of:
    • a level-expression, which defines what is stored
    • a level-format
    • a collection of level-properties that apply to the level-format

Each level-expression is an affine expression over dimension-variables. Thus, the level-expressions collectively define an affine map from dimension-coordinates to level-coordinates. The dimension-expressions collectively define the inverse map, which only needs to be provided for elaborate cases where it cannot be inferred automatically.

Each dimension could also have an optional SparseTensorDimSliceAttr. Within the sparse storage format, we refer to indices that are stored explicitly as coordinates and offsets into the storage format as positions.

The supported level-formats are the following:

  • dense : all entries along this level are stored and linearized.
  • batch : all entries along this level are stored but not linearized.
  • compressed : only nonzeros along this level are stored
  • loose_compressed : as compressed, but allows for free space between regions
  • singleton : a variant of the compressed format, where coordinates have no siblings
  • structured[n, m] : the compression uses a n:m encoding (viz. n out of m consecutive elements are nonzero)

For a compressed level, each position interval is represented in a compact way with a lowerbound pos(i) and an upperbound pos(i+1) - 1, which implies that successive intervals must appear in order without any “holes” in between them. The loose compressed format relaxes these constraints by representing each position interval with a lowerbound lo(i) and an upperbound hi(i), which allows intervals to appear in arbitrary order and with elbow room between them.

By default, each level-type has the property of being unique (no duplicate coordinates at that level) and ordered (coordinates appear sorted at that level). For singleton levels, the coordinates are fused with its parents in AoS (array of structures) scheme. The following properties can be added to a level-format to change this default behavior:

  • nonunique : duplicate coordinates may appear at the level
  • nonordered : coordinates may appear in arbribratry order
  • soa : only applicable to singleton levels, fuses the singleton level in SoA (structure of arrays) scheme.

In addition to the map, the following fields are optional:

  • The required bitwidth for position storage (integral offsets into the sparse storage scheme). A narrow width reduces the memory footprint of overhead storage, as long as the width suffices to define the total required range (viz. the maximum number of stored entries over all indirection levels). The choices are 8, 16, 32, 64, or, the default, 0 to indicate the native bitwidth.

  • The required bitwidth for coordinate storage (the coordinates of stored entries). A narrow width reduces the memory footprint of overhead storage, as long as the width suffices to define the total required range (viz. the maximum value of each tensor coordinate over all levels). The choices are 8, 16, 32, 64, or, the default, 0 to indicate a native bitwidth.

  • The explicit value for the sparse tensor. If explicitVal is set, then all the non-zero values in the tensor have the same explicit value. The default value Attribute() indicates that it is not set. This is useful for binary-valued sparse tensors whose values can either be an implicit value (0 by default) or an explicit value (such as 1). In this approach, we don’t store explicit/implicit values, and metadata (such as position and coordinate arrays) alone fully defines the original tensor. This yields additional savings for the storage requirements, as well as for the computational time, since we skip operating on implicit values and can constant fold the explicit values where they are used.

  • The implicit value for the sparse tensor. If implicitVal is set, then the “zero” value in the tensor is equal to the implicit value. For now, we only support 0 as the implicit value but it could be extended in the future. The default value Attribute() indicates that the implicit value is 0 (same type as the tensor element type).

Examples:

// Sparse vector.
#SparseVector = #sparse_tensor.encoding<{
  map = (i) -> (i : compressed)
}>
... tensor<?xf32, #SparseVector> ...

// Sorted coordinate scheme (arranged in AoS format by default).
#SortedCOO = #sparse_tensor.encoding<{
  map = (i, j) -> (i : compressed(nonunique), j : singleton)
}>
// coordinates = {x_crd, y_crd}[nnz]
... tensor<?x?xf64, #SortedCOO> ...

// Sorted coordinate scheme (arranged in SoA format).
#SortedCOO = #sparse_tensor.encoding<{
  map = (i, j) -> (i : compressed(nonunique), j : singleton(soa))
}>
// coordinates = {x_crd[nnz], y_crd[nnz]}
... tensor<?x?xf64, #SortedCOO> ...

// Batched sorted coordinate scheme, with high encoding.
#BCOO = #sparse_tensor.encoding<{
  map = (i, j, k) -> (i : dense, j : compressed(nonunique, high), k : singleton)
}>
... tensor<10x10xf32, #BCOO> ...

// Compressed sparse row.
#CSR = #sparse_tensor.encoding<{
  map = (i, j) -> (i : dense, j : compressed)
}>
... tensor<100x100xbf16, #CSR> ...

// Doubly compressed sparse column storage with specific bitwidths.
#DCSC = #sparse_tensor.encoding<{
  map = (i, j) -> (j : compressed, i : compressed),
  posWidth = 32,
  crdWidth = 8
}>
... tensor<8x8xf64, #DCSC> ...

// Doubly compressed sparse column storage with specific
// explicit and implicit values.
#DCSC = #sparse_tensor.encoding<{
  map = (i, j) -> (j : compressed, i : compressed),
  explicitVal = 1 : i64,
  implicitVal = 0 : i64
}>
... tensor<8x8xi64, #DCSC> ...

// Block sparse row storage (2x3 blocks).
#BSR = #sparse_tensor.encoding<{
  map = ( i, j ) ->
  ( i floordiv 2 : dense,
    j floordiv 3 : compressed,
    i mod 2      : dense,
    j mod 3      : dense
  )
}>
... tensor<20x30xf32, #BSR> ...

// Same block sparse row storage (2x3 blocks) but this time
// also with a redundant reverse mapping, which can be inferred.
#BSR_explicit = #sparse_tensor.encoding<{
  map = { ib, jb, ii, jj }
        ( i = ib * 2 + ii,
          j = jb * 3 + jj) ->
  ( ib = i floordiv 2 : dense,
    jb = j floordiv 3 : compressed,
    ii = i mod 2 : dense,
    jj = j mod 3 : dense)
}>
... tensor<20x30xf32, #BSR_explicit> ...

// ELL format.
// In the simple format for matrix, one array stores values and another
// array stores column indices. The arrays have the same number of rows
// as the original matrix, but only have as many columns as
// the maximum number of nonzeros on a row of the original matrix.
// There are many variants for ELL such as jagged diagonal scheme.
// To implement ELL, map provides a notion of "counting a
// dimension", where every stored element with the same coordinate
// is mapped to a new slice. For instance, ELL storage of a 2-d
// tensor can be defined with the mapping (i, j) -> (#i, i, j)
// using the notation of [Chou20]. Lacking the # symbol in MLIR's
// affine mapping, we use a free symbol c to define such counting,
// together with a constant that denotes the number of resulting
// slices. For example, the mapping [c](i, j) -> (c * 3 * i, i, j)
// with the level-types ["dense", "dense", "compressed"] denotes ELL
// storage with three jagged diagonals that count the dimension i.
#ELL = #sparse_tensor.encoding<{
  map = [c](i, j) -> (c * 3 * i : dense, i : dense, j : compressed)
}>
... tensor<?x?xf64, #ELL> ...

// CSR slice (offset = 0, size = 4, stride = 1 on the first dimension;
// offset = 0, size = 8, and a dynamic stride on the second dimension).
#CSR_SLICE = #sparse_tensor.encoding<{
  map = (i : #sparse_tensor<slice(0, 4, 1)>,
         j : #sparse_tensor<slice(0, 8, ?)>) ->
        (i : dense, j : compressed)
}>
... tensor<?x?xf64, #CSR_SLICE> ...

Parameters: 

ParameterC++ typeDescription
lvlTypes::llvm::ArrayRef<::mlir::sparse_tensor::LevelType>level-types
dimToLvlAffineMap
lvlToDimAffineMap
posWidthunsigned
crdWidthunsigned
explicitVal::mlir::Attribute
implicitVal::mlir::Attribute
dimSlices::llvm::ArrayRef<::mlir::sparse_tensor::SparseTensorDimSliceAttr>per dimension slice metadata

SparseTensorSortKindAttr 

sparse tensor sort algorithm

Syntax:

#sparse_tensor.SparseTensorSortAlgorithm<
  ::mlir::sparse_tensor::SparseTensorSortKind   # value
>

Enum cases:

  • hybrid_quick_sort (HybridQuickSort)
  • insertion_sort_stable (InsertionSortStable)
  • quick_sort (QuickSort)
  • heap_sort (HeapSort)

Parameters: 

ParameterC++ typeDescription
value::mlir::sparse_tensor::SparseTensorSortKindan enum of type SparseTensorSortKind

StorageSpecifierKindAttr 

sparse tensor storage specifier kind

Syntax:

#sparse_tensor.kind<
  ::mlir::sparse_tensor::StorageSpecifierKind   # value
>

Enum cases:

  • lvl_sz (LvlSize)
  • pos_mem_sz (PosMemSize)
  • crd_mem_sz (CrdMemSize)
  • val_mem_sz (ValMemSize)
  • dim_offset (DimOffset)
  • dim_stride (DimStride)

Parameters: 

ParameterC++ typeDescription
value::mlir::sparse_tensor::StorageSpecifierKindan enum of type StorageSpecifierKind

Types 

IterSpaceType 

Syntax:

!sparse_tensor.iter_space<
  ::mlir::sparse_tensor::SparseTensorEncodingAttr,   # encoding
  Level,   # loLvl
  Level   # hiLvl
>

A sparse iteration space that represents an abstract N-D (sparse) iteration space extracted from a sparse tensor, i.e., a set of (crd_0, crd_1, …, crd_N) for every stored element (usually nonzeros) in a sparse tensor between the specified [$loLvl, $hiLvl) levels.

Examples:

// An iteration space extracted from a CSR tensor between levels [0, 2).
!iter_space<#CSR, lvls = 0 to 2>

Parameters: 

ParameterC++ typeDescription
encoding::mlir::sparse_tensor::SparseTensorEncodingAttr
loLvlLevel
hiLvlLevel

IteratorType 

Syntax:

!sparse_tensor.iterator<
  ::mlir::sparse_tensor::SparseTensorEncodingAttr,   # encoding
  Level,   # loLvl
  Level   # hiLvl
>

An iterator that points to the current element in the corresponding iteration space.

Examples:

// An iterator that iterates over a iteration space of type `!iter_space<#CSR, lvls = 0 to 2>`
!iterator<#CSR, lvls = 0 to 2>

Parameters: 

ParameterC++ typeDescription
encoding::mlir::sparse_tensor::SparseTensorEncodingAttr
loLvlLevel
hiLvlLevel

StorageSpecifierType 

Structured metadata for sparse tensor low-level storage scheme

Syntax:

!sparse_tensor.storage_specifier<
  ::mlir::sparse_tensor::SparseTensorEncodingAttr   # encoding
>

Values with storage_specifier types represent aggregated storage scheme metadata for the given sparse tensor encoding. It currently holds a set of values for level-sizes, coordinate arrays, position arrays, and value array. Note that the type is not yet stable and subject to change in the near future.

Examples:

// A storage specifier that can be used to store storage scheme metadata from CSR matrix.
!storage_specifier<#CSR>

Parameters: 

ParameterC++ typeDescription
encoding::mlir::sparse_tensor::SparseTensorEncodingAttr