Repository navigation
Conversation
e19749e to
a328ba9
Compare
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/IR/Visitors.h" | ||
| #include "mlir/Interfaces/DataLayoutInterfaces.h" | ||
| #include "mlir/Support/LogicalResult.h" |
There was a problem hiding this comment.
Oops, I need to remove a lot of includes from all the files. I'll clean that up in the first review
|
There is still a lot of code to add to this to actually implement the propagation piece. I think my plan is to make a new PR for each op to support propagation for. For now, all I have locally is I also have some preprocessing patterns to add to this, and a pass to fold unit dims on global ops. I will make separate PRs for each of those as well. The global unit dim folding review can happen in parallel with this, so I'll open that now. |
|
Love what it's doing, but terrified by the PackOp and static data layout coupling. We really are going to need to stop using PackOp at the global opt level - it's not good that it ever landed there, and I was promised we wouldn't be building out more stuff around it being there without a proper symbolic solution in place. If this is something where the PackOp use can be replaced with an interface and the data layout can be symbolic that's ok (stuff we can clean up without a rewrite) but otherwise it's tech debt and any code tied to it is going to end up getting thrown away or effectively forking the compiler. The litmus test is disabling MaterializeHomogeneousEncodings - if something doesn't work then it's tech debt. There's shades of tech debt and pragmatism but the more we build on this path the more we're forking and the longer it's going to take to recover. |
One of the things I had in mind when writing this was to be able to eventually extend this to some propagation of an encoding. I have left most implementation details semi-coupled to the PackOp, but I needed something material to go off of to start. Ultimately, the analysis flow should work if we swap out the implementations of the DataLayoutTransformations in this PR with something less coupled to PackOp. The propagation implementations (which aren't part of this PR) are a little more tightly coupled to specifically PackOp, but I wasn't exactly sure how to get around that, since they have to rewrite the IR. It would be great to get feedback on the analysis about core pieces that seem a little too coupled to static packing layouts, though, because I 100% agree that we should try not to dig this hole deeper. |
a328ba9 to
6277308
Compare
So couple of comments from my side
|
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Looking through this. More comments coming.
| /// a `__foldable_pack_unpack__` attribute, which indicates that the op came | ||
| /// from a given data layout, and can be folded with the inverse of the op as | ||
| /// long as it also has the same attribute. | ||
| void setFoldablePackUnPackAttribute(Operation *op); |
There was a problem hiding this comment.
Such attributes shouldnt leak pass boundaries. So it is better to avoid having them in a header file, but rather make them local to a pass implementation.
There was a problem hiding this comment.
This attribute is used in the util functions and also the pass patterns, and I wanted to keep the util stuff out of the pass implementation. Would it be fine to just rename the util files to something like PropagateDataLayoutUtils so it is more clear that they are specific to this pass?
There was a problem hiding this comment.
Maybe create a folder for pack propagation and move everything into that.
IIUC, currently the most important thing MaterializeHomogenousEncodings does is drop all encodings when we statically know that no target is going to set anything other than a default encoding. Not saying this is the end state we want, but if we drop the pass now there will be noticeable regressions on other (non-dt) backends because there is no propagation of encodings, and fusion of encodings isn't what it needs to be. I think that the litmus test for whether we can drop that pass is, do we generate (almost) the exact same code with and without that pass for a target that does not use them. |
Ok, My bad, Was thinking of a different pass (I think MaterializeUpperBoundTileSizePass). Ok now this makes more sense. So this was added so that you could use consteval to fuse some pack with globals. In my mind to make this all work without too much a fragile pass, const eval and this pack propagation pass needs to move to Stream, potentially after things have been mapped to devices. I understand how that makes things difficult, you have already found dispatches etc, and this makes pack propoagation effectively a global data flow pass. It is a much more well defined path, but that is in "just need to write code" category. Propagation on encodings seems harder, and much more fragile. |
Ok I'm definitely missing details around how that would work on Stream (especially w/ padding), but that does sound like a nice forcing solution for all of the fusion concerns. |
|
Yeah, I've got some ideas - TLDR is encodings should be SSA values associated with tensors just like we do with dynamic dimensions, and just like we propagate/fold/inline dynamic dimensions across the program we can propagate encodings, and only after that's all done bake them out in codegen and synthesize builtins for repacking if needed. That's why I think what's being done here is not durable, but I don't think it should block it landing to get us to a better state with the current non-durable MaterializeHomogeneousEncodings pass - they'll likely both go away together. |
Would love to have a chat about that or see a proposal for how that works, because I'm not seeing how it connects without looking inside the contents of the executables (and potentially replicating executables with different encodings if there is a conflict). (Unless that's what you mean) |
|
Definitely don't need to peer inside executables - this is all feed-forward (symbolic encodings are assigned and kept consistent all the way until we know what devices they are used on). Think of it just like a Tensor type in a normal eager runtime: it'll store shape, type, and encoding/strides and pass it around to allow different code acting on it to query those attributes and handle them like I don't think this will end up being difficult, and to codegen it'll be the same (this will all happen before we run translation). Anything relying on packs and static shapes at the whole-program level, though, will have issues and that's why we need to make sure we quarantine anything added that does for easy removal. |
|
I'm either not understanding what "propagate encoding" means then, or I'm not understanding what we're expected to produce in Flow. Say I do something like this in Flow/GlobalOptimization which then forms dispatches approximately like the following Now I want to propagate the encoding on the input to the matmul to its producer (after conversion to Stream and such), however I can't do that because the only valid padding value for the matmul is 0, and I can't introduce divide by 0 in the first dispatch by propagating. |
|
The encoding should be propagated before then. Think of encoding changes like casts or reshapes. I think w.r.t. this PR that's the interesting thing it's doing we may be able to preserve. |
|
Ok that makes sense then, but I'm still a little lost on details... (not looking for a response now, can cross that bridge once we come to it, but the progress here is good!) |
|
Yep - details are what we have to work through - it won't be possible to fully articulate them without actually solving the problem :) |
6277308 to
2070cc2
Compare
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Looking at this, it seems like a bit of overkill to use DFX. Ill keep reviewing but just leaving the comment there.
| /// a layout. | ||
| /// - All of the node's users can assume any layout or define a layout. | ||
| static bool isIntermediateNode(Value value) { | ||
| if (auto definingOp = value.getDefiningOp()) { |
There was a problem hiding this comment.
Nit: restructure to
auto definingOp = value.getDefiningOp();
if (!definingOp) return false;
if (!isLayoutFlexibleOp(...) && !isLayoutDefiningOp(...)) return false;
return llvm::any_of(value.getUsers(), [&](...){...});
| } | ||
|
|
||
| void annotateDataLayoutNodes() { | ||
| auto walkFn = [&](Value val) -> WalkResult { |
There was a problem hiding this comment.
If you are not doing anything with WalkResult returned here, you can just make this a walk with void return type (and also remove the WalkResult::advace() below)
| // Propagation pattern definitions | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class FoldCancellingUnPackPackOps final |
There was a problem hiding this comment.
We should just add this upstream as a folding/canonicalization to pack/unpack operations.
There was a problem hiding this comment.
It's just calling tensor::UnPackOp::canonicalize, why not just populate with tensor::UnPackOp::getCanonicalizationPatterns.
This adds a new pass to do propagation of data layouts all the way to mutable GlobalOps. This is just the basic analysis flow for now, only capable of handling some trivial cases, but this will be extended. The pass does an analysis of the full program, and finds subgraphs of values that can all have the same layout. The subgraphs are seeded on loads and stores to GlobalOps, and then transformations are computed from the source layout to the layout at propagation barriers. Then, if one of the barrier layouts is better than the original source layout, the globals are rewritten to the barrier layout, and the layout is propagated through the subgraph. This PR sets in place the full program analysis, but does not implement any propagations or transformation logic for ops other than PackOp. Future PRs will extend this by adding propagation patterns and logic for computing transformations through different ops.
2070cc2 to
bf1cbfd
Compare
hanhanW
left a comment
There was a problem hiding this comment.
The DataLayoutUtils is a fairly complicated class. I actually did not go through all the codes because I realize that we should have better testing for DataLayoutUtils. To help review process, I think we can start with adding unit tests! This reminds me the days not working on LLVM/MLIR compiler. My experience is that it usually helps us design the class better. You will think more about it when you are writing unit tests. It forces you to make utils more isolated when possible; hopefully it can address some smart pointer and ownership issues. For the pattern and pass part, we still need lit tests. For utils/class, I think we can introduce some unit tests. What do you think?
Here is the documentation about googletest: https://google.github.io/googletest/primer.html
And here is an example that we have in IREE: https://github.com/openxla/iree/blob/main/compiler/src/iree/compiler/Utils/test/UtilsTest.cpp
| LLVM_DEBUG({ | ||
| llvm::dbgs() << "\n--- After propagating data layout through DAGs ---\n"; | ||
| moduleOp->print(llvm::dbgs(), OpPrintingFlags().useLocalScope()); | ||
| llvm::dbgs() << "\n\n"; | ||
| }); |
There was a problem hiding this comment.
Perhaps we don't need this because the *-opt just print the IR at the end.
| // Propagation pattern definitions | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class FoldCancellingUnPackPackOps final |
| if (auto unPackOp = packOp.getSource().getDefiningOp<tensor::UnPackOp>()) { | ||
| if (!hasFoldablePackUnPackAttribute(unPackOp)) { | ||
| return failure(); | ||
| } | ||
| if (unPackOp.getSourceType() != packOp.getDestType()) | ||
| return failure(); | ||
| if (packOp.getInnerDimsPos() != unPackOp.getInnerDimsPos() || | ||
| packOp.getOuterDimsPerm() != unPackOp.getOuterDimsPerm() || | ||
| !haveSameTiles(packOp, unPackOp)) | ||
| return failure(); | ||
| rewriter.replaceOp(packOp, unPackOp.getSource()); | ||
| return success(); | ||
| } |
There was a problem hiding this comment.
nit: restructure this to below to save some level of indents.
auto unPackOp = packOp.getSounrce().getDefiningOp<...>();
if (!unPackOp) {
return failure();
}
...
| if (!neighborLayout->hasValidTransform()) { | ||
| continue; | ||
| } | ||
| auto *newLayout = new DataLayoutTransformation(*neighborLayout); |
There was a problem hiding this comment.
nit: use smart pointer (i.e., std::unique_ptr<>).
| DataLayoutTransformation *newLayout = | ||
| DataLayoutTransformation::getIdentityTransformation(layoutType); | ||
| setDataLayoutTransformation(id, newLayout); |
There was a problem hiding this comment.
nit: here might have a memory leak. We should really use smart pointers and have better ownership..
This adds a new pass to do propagation of data layouts all the way to mutable GlobalOps. This is just the basic analysis flow for now, only capable of handling some trivial cases, but this will be extended.
The pass does an analysis of the full program, and finds subgraphs of values that can all have the same layout. The subgraphs are seeded on loads and stores to GlobalOps, and then transformations are computed from the source layout to the layout at propagation barriers. Then, if one of the barrier layouts is better than the original source layout, the globals are rewritten to the barrier layout, and the layout is propagated through the subgraph.
This PR sets in place the full program analysis, but does not implement any propagations or transformation logic for ops other than PackOp. Future PRs will extend this by adding propagation patterns and logic for computing transformations through different ops.