This is an archive of the discontinued LLVM Phabricator instance.

[mlir][sparse] Add new utility class to help generates loop structures over sparse tensors; Implement foreach operator.
ClosedPublic

Authored by Peiming on Sep 27 2022, 7:07 PM.

Diff Detail

Event Timeline

Peiming created this revision.Sep 27 2022, 7:07 PM
Herald added a project: Restricted Project. · View Herald Transcript
Peiming requested review of this revision.Sep 27 2022, 7:07 PM
Peiming updated this revision to Diff 463397.Sep 27 2022, 7:09 PM

Update comments

Peiming added inline comments.Sep 27 2022, 7:11 PM
mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_codegen_foreach.mlir
110

Note how this sequence is printed in col-major order

Peiming added inline comments.Sep 28 2022, 8:54 AM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
366

Should we always lowering foreach? or only when RT is disabled? I think the operator is general enough to be used for conversion as well.

Peiming updated this revision to Diff 463581.Sep 28 2022, 8:56 AM

try fixing windows fail...

Peiming updated this revision to Diff 463586.Sep 28 2022, 9:29 AM

code cleanup.

Peiming updated this revision to Diff 463611.Sep 28 2022, 10:30 AM

Update comments.

Peiming updated this revision to Diff 463667.Sep 28 2022, 1:26 PM

rebase + remove dup utils

bixia added inline comments.Sep 28 2022, 2:48 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
346–362

Can you use RewriterBase::MergeBlocks to avoid manually remap like this?

bixia added inline comments.Sep 28 2022, 2:59 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
307–311

The style guide actually discourage the use of "auto" unless the type is obvious from the context (line 309) or it makes the code safer.

aartbik added inline comments.Sep 28 2022, 3:27 PM
mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.cpp
26

structure
over sparse tensors

Note that this also shows the danger of repeating comments. In general, google style says that if something is documented in the header, it does not need to be repeated in cc file. I would just pick a shorter "header" comment here to separate the parts

65

period

138

note that with an eye on more dim level types, I like to make these asserts very defensive.
So
assert(isCompressed || isDenseDim()) would capture future omissions

246

you can remove this one

mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
30

yeah, as you suggested yourself, you can merge these with the sparsetensor.h ones

50

loop structure

to (co-iterate) over sparse tensors?

53

relies -> rely

58

oh nice, I just did this, and renamed it to toOrigDim

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
334

if we don't do this, do we break debugging path?

If it is not expensive, shall we always do it, just to avoid inconsistent paths under prod/debug?

366

Yeah, if it is there, we lower (right now when RT is enabled, we won't see it).
You can add an assert if you are worried?

bixia added inline comments.Sep 28 2022, 3:50 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
299

the foreach

Peiming updated this revision to Diff 463704.Sep 28 2022, 4:08 PM
Peiming marked 8 inline comments as done.

address comments

Peiming updated this revision to Diff 463705.Sep 28 2022, 4:09 PM
Peiming marked an inline comment as done.

fix comments

Peiming added inline comments.Sep 28 2022, 4:10 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
334

No, it affects nothing in this case.

I can remove it.

366

Okay, I will then make it accessible universally (I just found that my integrate test did not disableRT either.

Peiming updated this revision to Diff 463708.Sep 28 2022, 4:21 PM

remove spaces

Peiming updated this revision to Diff 463714.Sep 28 2022, 4:41 PM

rebase + remove dup utils.

Peiming marked 4 inline comments as done.Sep 28 2022, 4:41 PM
Peiming added inline comments.Sep 28 2022, 4:44 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
346–362

Thanks! It does make the code simpler!

Peiming updated this revision to Diff 463716.Sep 28 2022, 4:52 PM

fix errors.

Peiming updated this revision to Diff 463719.Sep 28 2022, 5:00 PM

move assert into if.

aartbik added inline comments.Sep 28 2022, 5:10 PM
mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
31–54

Emitter (one m in comment)

60

Add a doc on what constructor expects (the list of tensors)

68

period at end?

76

period at end?

80

I just talked with the IREE team and we may find a better separation of codegen concerns by migrating the vectorization and parallelization to a downstream pass. If it simplifies things for you know, I would omit the parallel/vector capabilities from these utils for now

96

period at end?

142

sparis -> sparsification

148

for each tensor

152

A lot of these are taken from the codegen structured (and sometimes renamed, which makes "recognition" a bit harder for me)
But can you at least copy the comments from the codegen struct here, so that each section has a bit of a explanation

E.g.

/ Sparse iteration information (by tensor and index). These arrays
/ are updated to remain current within the current loop.

was useful to explain the vector of vector

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
314

I like how this reads now!

334

int64_t when dealing with rank?

345

same

Peiming updated this revision to Diff 463726.Sep 28 2022, 5:24 PM

update comments.

Peiming added inline comments.Sep 28 2022, 9:18 PM
mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
152

One thing that need to be mentioned is that all the variables here now are indexed by tensor+dim instead of tensor+loop idx.

Peiming updated this revision to Diff 463937.Sep 29 2022, 9:34 AM
Peiming marked 12 inline comments as done.

Address comments

mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
80

Sounds good!

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
314

;-)

Peiming updated this revision to Diff 463941.Sep 29 2022, 9:39 AM

rebase against main.

aartbik added inline comments.Sep 29 2022, 2:34 PM
mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.cpp
74

I like the idea of encforcing proper API usage through asserts and other means a lot.
Can we add some more? E.g. here you can check that nothing has been called yet, so you don't try to use this method again later.

Same for methods below.

mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
57

Please document the class also. In general, showing "usage" is very illustrative. So you can give a very short pseudo-code example of starting entering exiting and leaving a loop using the API of the class.

78

typo: generated

103

This is not really a getter. Just give it its own doc stating that it will return the coordinate lists in the passed array

108

this is a lot of code for a header, and it distracts from reading the API

why not move this block into the cpp file?

132

I am not sure I am a big fan of using two completely different data structures for debug/prod, since that has the risk of introducing bugs on one path and not the other. Can't we just simply always keep all the bookkeeping with proper detection of wrong usage?

mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorRewriting.cpp
333

Actually, I would remove this comment completely. The API of the emitter requires every loop to be opened/closed properly (and asserting on this behavior is a good thing). There is really no good benefit telling the reader here that a "shortcut" usage is possible ;-)

mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_codegen_foreach.mlir
2

This seems a copy and paste error (kDirect only impacts sparse2sparse)

16

Can we use the common names for this, DCSC etc.

23

Use common comment format with capital/period at end

25

such a nifty test for this construct!

Peiming updated this revision to Diff 464083.Sep 29 2022, 4:19 PM
Peiming marked 10 inline comments as done.

address comments.

Peiming updated this revision to Diff 464089.Sep 29 2022, 4:27 PM

update names

mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.cpp
74

see line.90.

mlir/lib/Dialect/SparseTensor/Transforms/CodegenUtils.h
78

Really need to configure a spell checker on my editor ;-(

132

Make sense, yet another pre-mature optimization ;-(

aartbik accepted this revision.Sep 30 2022, 1:26 PM
This revision is now accepted and ready to land.Sep 30 2022, 1:26 PM
This revision was landed with ongoing or failed builds.Sep 30 2022, 2:42 PM
This revision was automatically updated to reflect the committed changes.