This is an archive of the discontinued LLVM Phabricator instance.

[mlir][sparse] Add sparse_tensor.sort operator.
ClosedPublic

Authored by bixia on Sep 22 2022, 2:33 PM.

Event Timeline

bixia created this revision.Sep 22 2022, 2:33 PM
Herald added a project: Restricted Project. · View Herald Transcript
bixia requested review of this revision.Sep 22 2022, 2:33 PM
aartbik added inline comments.Sep 22 2022, 2:44 PM
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
395

I would not present this as a COO case per se.

What it really does is sorting parallel arrays, with one array of one type, and all other arrays of index type.
But what is missing here is a criteria on how to compare elements. Is it always lexicographic order in coordinates?

For example, in compress, we will need to sort a *single* array of type index, so that can only be expressed as

sparse_tensor.sort %n, %values

with index typed %values. But will that work too?

400

Please add the usual disclaimer (note that we have two sections, pure ops, and ops that are (for now) defined with side effects.
This is currently the latter

Note that this operation is "impure" in the sense that its behavior
 is solely defined by side-effects and not SSA values. The semantics
 may be refined over time as our sparse abstractions evolve.
404

coordinates

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
457

ironically, our errors do not follow our comment rule,
so start without capital letter, no period at end

462

Please use

for (size_t i = 0, e = getCoordinates().size(); i < e; i++)

idiom

bixia updated this revision to Diff 462474.Sep 23 2022, 7:20 AM
bixia marked 4 inline comments as done.

Revise the operator format, fix comments to address review comments.

bixia marked an inline comment as done.Sep 23 2022, 7:21 AM
bixia added inline comments.
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
395

Revise the op per offline discussion.

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
457

Fix here and other places.

bixia updated this revision to Diff 462601.Sep 23 2022, 4:00 PM

Revise the operator syntax per offline discussion.

Peiming added inline comments.Sep 23 2022, 4:32 PM
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
398

It is ambiguous, how about "so that the resulting coordinates are in lexicographical order when putting together". (or something better)

for example:
[1, 3, 2]
[1, 2, 3]

are sorted into
[1, 2, 3]
[1, 3, 2] (note that this array is not in non-decreasing order)

not
[1, 2, 3]
[1, 2, 3]
right?

403

Maybe change it to "This operator updates coordinates and values in place"?

416

Is it the trick to omit jointly when value is empty?

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
517

why not for (Value t : operands)? since you are not using i other than operands[i]

Peiming added inline comments.Sep 23 2022, 4:41 PM
mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
522

This reminds me of the debate I had with Wren about whether or not support dynamic dimension for concat.

While I believe here dynamic support is crucial, but can we add a disclaimer that it is UB when dynamic sized array have unmatched dimensions?

bixia updated this revision to Diff 462811.Sep 25 2022, 10:27 PM
bixia marked 4 inline comments as done.

Use xs and ys instead of coordinates and values per offline discussion.
Address review comment.

Yeah, looking good. A few nits on doc and verification

mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
395

Please update short summary as well, something like

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

399

Please make a note that, thus, the order in which arrays appear in the xs list matter, before going into the example

408

Don't we want to relax this to, all arrays should have length >= n
?

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
522

I think it is safe to state that all arrays should have length >= n or otherwise UB results.
We can check the static sizes, but skip the dynamic sizes

526

as above, do we really want same length, or just >= n?

mlir/test/Dialect/SparseTensor/invalid.mlir
505

I don't think all custom verification errors are covered yet?
e.g. need at least one xs buffer?

bixia updated this revision to Diff 463044.Sep 26 2022, 4:08 PM
bixia marked 4 inline comments as done.

Relax the requirement of dimension.

bixia added inline comments.Sep 26 2022, 4:11 PM
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
398

Revise this part per offline discussion and also add an example. PTAL.

408

Yes, we can.

416

Yes.

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
517

Thanks!

522

Add comment and description in the td definition.

526

Change to allow >= n.

mlir/test/Dialect/SparseTensor/invalid.mlir
505

The test doesn't work possible due to some parse issue. Add a TODO.

wrengr added inline comments.Sep 26 2022, 4:49 PM
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
391

Can you check to see if there's a way to use the SameOperandsShape and SameOperandsElementType traits but restricting them to the memrefs in $xs (rather than for all operands to the op)? If we can, that'd save a lot of code in the verifier. Though last time I checked there wasn't a clean way to do this

393

As mentioned in chat, you should use Variadic<...> {let minSize = 1;} for this. I don't think you can do the let inline, so you can just float out the definition class NonemptyVariadic<Type type> : Variadic<type> { let minSize = 1; } and then use NonemptyVariadic<...>:$xs

397–405

It might help clarify things to say that the relationship between memrefs is as if we did zip(zip(xs), zip(ys)) to obtain a list of pairs of lists, and then lexicographically sort the outer list on the first component of the pair.

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
512

Minor nit, but I'd rather this be named xsType (or xsTp or xtp)

bixia updated this revision to Diff 463238.Sep 27 2022, 8:09 AM
bixia marked 2 inline comments as done.

Address review comments.

bixia added inline comments.Sep 27 2022, 8:15 AM
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
391

We now allow different dims for xs and ys. SameOperandsElementType applies to all operands, but we only need it for xs. So we can't use this trait here.

393

I add a TODO for adding this in tablegen and improve here after.

aartbik accepted this revision.Sep 27 2022, 10:18 AM
aartbik added inline comments.
mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.td
403

put `` on the zip part, so that it looks like code in our online doc as well

415

perhaps mention UB if this condition is not met (since we don't have runtime checks for that)

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
522

Nit: if n == 0, you can skip the whole loop that tests this

This revision is now accepted and ready to land.Sep 27 2022, 10:18 AM
bixia updated this revision to Diff 463298.Sep 27 2022, 11:46 AM
bixia marked 3 inline comments as done.

Fix comments to address review feedback.

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
522

Per offline discussion, I will keep the current loop which does two checks.

This revision was landed with ongoing or failed builds.Sep 27 2022, 12:00 PM
This revision was automatically updated to reflect the committed changes.
NathanielMcVicar added inline comments.
mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
522

I believe MSVC is unhappy with the signed comparison here. https://lab.llvm.org/buildbot/#/builders/13/builds/26316

C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Dialect\SparseTensor\IR\SparseTensorDialect.cpp(523): error C2220: the following warning is treated as an error
C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Dialect\SparseTensor\IR\SparseTensorDialect.cpp(523): warning C4018: '<': signed/unsigned mismatch