You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[CPU] CPU backend produces unfused IR in mmt4d->unpack->elem dispatch #20786
The distribution using scf.forall creates extract_slice between unpack and generic op. It may be the reason that generic op is not fused in later parallel loops tileAndFuse.
I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.
The reason that extra extract_slice op is there is because of the tile sizes on the generic op. The tile sizes of the generic op does not take into account the layout transformation by the unpack op in the middle. Therefore, although you've computed a result of shape [M0 x m1, N0 x n1] where [m1, n1] are your inner tile sizes, you try to extract a [M0, N0] slice from it and therefore the overall forall loop calculates that same result m1 * n1 times. For better visualization, I'm attaching an example with static sizes:
As I said, you calculate the same matmul result multiple times. This happens with dynamic sizes in your example as well, it's just harder to see.
On the other hand, if we were to switch to choosing the root operation and fusing producers+consumers instead of the last compute op and then fusing the producers while tiling forall, it gets proper tiling without duplicated computations. But that one fails compilation with other dispatches, e.g. generic -> pack ones like this one. I think by improving the consumer fusion and then doing the above switch is the way to handle this. I'd like to take a look.
I see, thanks for the sharing! The static cases make it easier to think. This is what I'd like to discuss in the data-tiling fusion codegen. In the beginning, I'm thinking that if this is the time to revisit local data-layout propagation. I'd like to avoid this as a first approach because it introduces pack ops when the consumer has other input operands -- and codegen may be struggled about it.
After taking a second look, I think we can kick in the unpack -> extract_slice folding because the unpack op has slicing semantic. We implemented the FoldUnpackWithExtractSliceOp pattern in upstream, and maybe we can populate it after tiling. This way, we'd have a sequence of compute ops. There are no slicing ops in between.
I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.
@pashu123 please chime in if you have context. You claimed this in a private conversation, and it'd be good if you can provide more context. It is okay if it is not the solution; it'd save others time to look into your PR. Thanks!
I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.
@pashu123 please chime in if you have context. You claimed this in a private conversation, and it'd be good if you can provide more context. It is okay if it is not the solution; it'd save others time to look into your PR. Thanks!
Let's not discuss {linalg.generic | maxpool} + linalg.pack fusion here. That's the problem of consumer fusion not happening, and we'll have to implement that.
We are selecting the wrong tile sizes from the unpack op (last op), which the distribution pass uses. Later, tileRootFuseProducerConsumer is confused because it chooses the root op mmt4d in our case. We must be consistent in choosing which op should be selected as the root across the pipeline. I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.
#19821 borrows logic from TileDispatchUsingForall, which does (producer + consumer + producer) fusion in TileRootFuseProducerConsumer pass.
The log is not easy to read because the IRs are interleaved. Can you update it with using --mlir-disable-threading flag? E.g.,
// -----// IR Dump After OptimizeTensorInsertExtractSlicesPass (iree-codegen-optimize-tensor-insert-extract-slices) //----- //
func.func @_encoding_1_encode_DxDxf32_to_DxDxf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDataTiling>} {
// .... and the next dump is for other dispatch
// -----// IR Dump After LLVMCPUTileRootAndFuseProducerConsumerPass (iree-llvmcpu-tile-root-and-fuse-producer-consumer) //----- //
func.func @foo_dispatch_0_matmul_DxDxD_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
We must be consistent in choosing which op should be selected as the root across the pipeline.
I think you meant across the passes? Did you dump the IRs with your branch? ukernel is a different story that we might need a temporary buffer, so we still see stack allocation in the dump. Can you try with disabling ukernels, so we can verify that there are no stack allocations?
I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.
Yes, we discussed this last week, and it is doable. Now I remember the old approach that I discussed with you last year, and could you remind me what the status of that? In the past, we discussed disabling lowering config propagation for the Mmt4D pipeline, so the dispatch can always pick mmt4d op as a root op. This way, we don't need the selectRootOp flag, and we can move the pipeline to do this type of tile and fuse. Did we try that before?
The log is not easy to read because the IRs are interleaved. Can you update it with using --mlir-disable-threading flag? E.g.,
// -----// IR Dump After OptimizeTensorInsertExtractSlicesPass (iree-codegen-optimize-tensor-insert-extract-slices) //----- //
func.func @_encoding_1_encode_DxDxf32_to_DxDxf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDataTiling>} {
// .... and the next dump is for other dispatch
// -----// IR Dump After LLVMCPUTileRootAndFuseProducerConsumerPass (iree-llvmcpu-tile-root-and-fuse-producer-consumer) //----- //
func.func @foo_dispatch_0_matmul_DxDxD_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
We must be consistent in choosing which op should be selected as the root across the pipeline.
I think you meant across the passes? Did you dump the IRs with your branch? ukernel is a different story that we might need a temporary buffer, so we still see stack allocation in the dump. Can you try with disabling ukernels, so we can verify that there are no stack allocations?
I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.
Yes, we discussed this last week, and it is doable. Now I remember the old approach that I discussed with you last year, and could you remind me what the status of that? In the past, we discussed disabling lowering config propagation for the Mmt4D pipeline, so the dispatch can always pick mmt4d op as a root op. This way, we don't need the selectRootOp flag, and we can move the pipeline to do this type of tile and fuse. Did we try that before?
No, we didn't try disabling the lowering_config propagation. I wasn't actively working on this and was pulled into different things.
Activity
hanhanW commentedon May 12, 2025
IR dump: https://gist.github.com/hanhanW/c76837b22276d8acfe760ba968e06c45
The distribution using scf.forall creates extract_slice between unpack and generic op. It may be the reason that generic op is not fused in later parallel loops tileAndFuse.
hanhanW commentedon May 15, 2025
I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.
egebeysel commentedon May 20, 2025
The reason that extra extract_slice op is there is because of the tile sizes on the generic op. The tile sizes of the generic op does not take into account the layout transformation by the unpack op in the middle. Therefore, although you've computed a result of shape
[M0 x m1, N0 x n1]where[m1, n1]are your inner tile sizes, you try to extract a[M0, N0]slice from it and therefore the overall forall loop calculates that same resultm1 * n1times. For better visualization, I'm attaching an example with static sizes:to compile:
iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic ~/repro.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after=iree-codegen-tile-and-distribute-to-workgroups-using-forall-op --compile-to=hal 2> ~/repro_logs.mlirYou get something like this one:
As I said, you calculate the same matmul result multiple times. This happens with dynamic sizes in your example as well, it's just harder to see.
On the other hand, if we were to switch to choosing the root operation and fusing producers+consumers instead of the last compute op and then fusing the producers while tiling forall, it gets proper tiling without duplicated computations. But that one fails compilation with other dispatches, e.g.
generic -> packones like this one. I think by improving the consumer fusion and then doing the above switch is the way to handle this. I'd like to take a look.hanhanW commentedon May 20, 2025
I see, thanks for the sharing! The static cases make it easier to think. This is what I'd like to discuss in the data-tiling fusion codegen. In the beginning, I'm thinking that if this is the time to revisit local data-layout propagation. I'd like to avoid this as a first approach because it introduces pack ops when the consumer has other input operands -- and codegen may be struggled about it.
After taking a second look, I think we can kick in the
unpack -> extract_slicefolding because the unpack op has slicing semantic. We implemented the FoldUnpackWithExtractSliceOp pattern in upstream, and maybe we can populate it after tiling. This way, we'd have a sequence of compute ops. There are no slicing ops in between.hanhanW commentedon May 21, 2025
@pashu123 please chime in if you have context. You claimed this in a private conversation, and it'd be good if you can provide more context. It is okay if it is not the solution; it'd save others time to look into your PR. Thanks!
pashu123 commentedon May 21, 2025
Sure, I'll go over the IRs and let you know.
pashu123 commentedon May 21, 2025
I am experimenting with https://github.com/pashu123/iree/tree/test_cpu, and many mixed issues are floating around, so I'll tackle them one by one.
~/iree-build/tools/iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic test2.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after-all 2> repro_logs.txtHere's the mlir: https://gist.github.com/pashu123/7b5b7be862ad49e7dbe85c8ad51ed505
Here's the dump: https://gist.github.com/pashu123/77be9c734f96c3ddd2e22369863f7cc9
Looking at the IRs, it does the right thing, as mentioned in the previous comments.
We are selecting the wrong tile sizes from the unpack op (last op), which the distribution pass uses. Later, tileRootFuseProducerConsumer is confused because it chooses the root op mmt4d in our case. We must be consistent in choosing which op should be selected as the root across the pipeline. I think we discussed this, we can have a flag
selectRootOporselectLastOpin the pass that chooses the root op according to the flag and pipeline.#19821 borrows logic from TileDispatchUsingForall, which does (producer + consumer + producer) fusion in TileRootFuseProducerConsumer pass.
hanhanW commentedon May 21, 2025
The log is not easy to read because the IRs are interleaved. Can you update it with using
--mlir-disable-threadingflag? E.g.,I think you meant across the passes? Did you dump the IRs with your branch? ukernel is a different story that we might need a temporary buffer, so we still see stack allocation in the dump. Can you try with disabling ukernels, so we can verify that there are no stack allocations?
Yes, we discussed this last week, and it is doable. Now I remember the old approach that I discussed with you last year, and could you remind me what the status of that? In the past, we discussed disabling lowering config propagation for the Mmt4D pipeline, so the dispatch can always pick mmt4d op as a root op. This way, we don't need the
selectRootOpflag, and we can move the pipeline to do this type of tile and fuse. Did we try that before?hanhanW commentedon May 21, 2025
Is it something different from what we've discussed how to choose root op? Is it an improvement for the pass, which solves a different problem?
pashu123 commentedon May 21, 2025
It solves a different problem.
pashu123 commentedon May 21, 2025
No, we didn't try disabling the lowering_config propagation. I wasn't actively working on this and was pulled into different things.
pashu123 commentedon May 21, 2025
Running with
~/iree-build/tools/iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic test.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false -iree-llvmcpu-enable-ukernels=none --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after-all 2> repro_logs.txt -mlir-disable-threadingIR Dump: https://gist.github.com/pashu123/2a35cfb322e0fdd7e71f1564fd9f047a
[CPU] Disable lowering_config propagation for Mmt4dTilingExpert pipeline
[CPU] Disable lowering_config propagation for Mmt4dTilingExpert pipeline
[CPU] Disable lowering_config propagation for Mmt4dTilingExpert pipel…
[CPU] Disable lowering_config propagation for Mmt4dTilingExpert pipel…