This is an archive of the discontinued LLVM Phabricator instance.

[mlir][sparse] add init sparse tensor operation
ClosedPublic

Authored by aartbik on Oct 12 2021, 3:10 PM.

Details

Summary

This is the first step towards supporting general sparse tensors as output
of operations. The init sparse tensor is used to materialize an empty sparse
tensor of given shape and sparsity into a subsequent computation (similar to
the dense tensor init operation counterpart).

Example:

%c = sparse_tensor.init %d1, %d2 : tensor<?x?xf32, #SparseMatrix>
%0 = linalg.matmul
  ins(%a, %b: tensor<?x?xf32>, tensor<?x?xf32>)
  outs(%c: tensor<?x?xf32, #SparseMatrix>) -> tensor<?x?xf32, #SparseMatrix>

Diff Detail

Event Timeline

aartbik created this revision.Oct 12 2021, 3:10 PM
aartbik requested review of this revision.Oct 12 2021, 3:10 PM
bixia accepted this revision.Oct 13 2021, 8:10 AM
bixia added inline comments.
mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
228–231

So we don't allow "dynamic value" in "sizes" if the corresponding dimension in the tensor type is static, something like this:

%0 = sparse_tensor.init [%d1, %d2] : tensor<?x10xf32, #SparseMatrix>. // not allow? %d1 %d2 aren't ConstantOp

%0 = sparse_tensor.init [%d1, %d2] : tensor<?x?xf32, #SparseMatrix>. // allow

Will we have runtime checking to verify the consistency of the dimensions? I think the needed runtime checking for both of the above cases are similar and we probably should support the first case as well.

This revision is now accepted and ready to land.Oct 13 2021, 8:10 AM
aartbik marked an inline comment as done.Oct 13 2021, 9:38 AM
aartbik added inline comments.
mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
228–231

No, we only allow runtime SSA values for the dynamic case (?), similar to e.g. the alloc operation.

I thought about not specifying the static cases at all (ie. 10x?xf32 only takes one argument), similar to alloc, but what I have now feels a bit more readable.

There could be some value in allowing dynamic SSA values on the static case and then asserting that it matches at runtime, but we have no precedent for this. For example, linalg generic operations check static sizes to make sure the shape matches, but no runtime checks are ever generated.

But I am okay to relax the requirements in follow up revisions if you see value (I also suspect we will probably merge the "init" between linalg, tensor, sparse_tensor at some point). In any form, however, I need something like this to get the sparse output started.

This revision was automatically updated to reflect the committed changes.
aartbik marked an inline comment as done.