Repository navigation
Tie operands and results when creating DispatchWorkgroupsOp - #5102
Conversation
For each resultant tensor, DispatchWorkgroupsOp now can indicate to which operand it ties to, in order to share the underlying buffer storage. This can be used for subtensor_insert ops, where we insert into the target tensor and also return the target tensor as the result, which can happen as in-place update on the buffer down the layers.
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Wow! just changing that worked and took care of bufferization too? Could you upload the dump of ir after all for this. I am surprised that it worked out of the box.
Otherwise this LGTM!
benvanik
left a comment
There was a problem hiding this comment.
awesome - lgtm besides tile->tie. thanks so much for working through this and sorry there were some rough edges!
| func @subtensor_insert(%arg0: tensor<1x224x224x3xf32>) -> tensor<1x225x225x3xf32> { | ||
| %cst = constant 0.000000e+00 : f32 | ||
| %0 = linalg.init_tensor [1, 225, 225, 3] : tensor<1x225x225x3xf32> | ||
| %1 = linalg.fill(%0, %cst) : tensor<1x225x225x3xf32>, f32 -> tensor<1x225x225x3xf32> |
There was a problem hiding this comment.
this made me think of something Mahesh and I were chatting about a bit the other day: we should have some identification of DMA operations like fills and copies, as turning those into the corresponding flow ops will be significantly better (especially on GPUs). Maybe it'd be enough to have a canonicalizer on flow.dispatch.workgroups: if the body is linalg.fill -> tensor.store then turn that into a flow.tensor.splat, or if tensor.load -> tensor.store then turn into a flow.tensor.clone/etc.
Do either you or @MaheshRavishankar know if there's an issue tracking something like that, or should I file one?
There was a problem hiding this comment.
+1 on having such patterns to turn standalone fill/copy into corresponding DMA operations. Fill ops are used for initializing the output buffer and normally they will be fused together with the real producer ops and after a bunch of transformations those fill ops will be killed with vector store to load forwarding. Here it's a special case: the padding happened after has a different workload, so we cannot go down that route and have the fill "for free". Having DMA operations is better for such cases!
I don't recall we have such an issue to track; so please feel free to create one! (And I might get to it soon). :)
For the pad_test in iree/test/e2e/xla_ops/pad.mlir, here is the IR dump before this patch, and here is the IR dump after this patch. I think the interesting part is we are seeing the following before bufferization, with this patch: func @pad_test_dispatch_1() {
%c0 = constant 0 : index
%0 = hal.interface.binding.subspan @legacy_io::@ro0[%c0] : !flow.dispatch.tensor<readonly:2x3xi32>
%1 = hal.interface.binding.subspan @legacy_io::@rw1[%c0] : !flow.dispatch.tensor<readwrite:3x9xi32>
%2 = flow.dispatch.tensor.load %0 : !flow.dispatch.tensor<readonly:2x3xi32> -> tensor<2x3xi32>
%3 = flow.dispatch.tensor.load %1 : !flow.dispatch.tensor<readwrite:3x9xi32> -> tensor<3x9xi32>
%4 = subtensor_insert %2 into %3[0, 1] [2, 3] [1, 1] : tensor<2x3xi32> into tensor<3x9xi32>
flow.dispatch.tensor.store %4, %1 : tensor<3x9xi32> -> !flow.dispatch.tensor<readwrite:3x9xi32>
return
}Given that |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Awesome! works as expected. Still not sure how bufferization picked that up, but it seems to be doing the right thing here.
For each resultant tensor, DispatchWorkgroupsOp now can indicate
to which operand it ties to, in order to share the underlying
buffer storage. This can be used for subtensor_insert ops, where
we insert into the target tensor and also return the target
tensor as the result, which can happen as in-place update on
the buffer down the layers.
Addresses #5042