Repository navigation
Create dispatches for scalar computation using program slices. - #13711
Conversation
139d808 to
73cfc9b
Compare
…atch region. Adding all values of the operation moved into a dispatch as results can lead to wasted memory usage if the result is not used. This patch avoids doing that and refactors the methods as a precursor to iree-org#13711.
d8c9b1a to
7cd173a
Compare
7cd173a to
0186ec7
Compare
benvanik
left a comment
There was a problem hiding this comment.
Excited to see the IR cleanup with this! Is it something I could test now or still WIP?
| /// Return `true` for operations that are to be treated as compute operations. | ||
| static bool isComputeOperation(Operation *op) { | ||
| MLIRContext *context = op->getContext(); | ||
| if (op->getDialect() == context->getLoadedDialect<linalg::LinalgDialect>()) { |
There was a problem hiding this comment.
probably should have arith, complex, etc in here as well
There was a problem hiding this comment.
Yes... Ill make iterative... pushing too many things here can just create a false dependency between dispatches (sometimes it is just better to clone an operation into a dispatch, like affine.apply operations). arith and complex do make sense... but just being deliberate.
There was a problem hiding this comment.
I left this as linalg/tensor for now. We can extend it to arith/math if needed, but I think they are all actually converted to Linalg to funnel it through that. If we move away from that, then we fix this.
| return false; | ||
| } | ||
|
|
||
| // 3. Do not move operations that are cloned into the dispatch region. |
There was a problem hiding this comment.
what's this mean? isScalarOperation(op) is true if it's (NOT clonable into dispatch OR has one use) feels odd to me? (things clonable into dispatch regions should be put into dispatch regions, and multi-use seems pretty common from what I saw?)
maybe add a test for this specific case showing why it's not expected to work?
There was a problem hiding this comment.
Basically we want to undo CSE of operations that really should be cloned in each dispatch (tensor.empty, affine.* ops, even some linalg.fill operations). If we just move them into dispatch, they will unnecessarily become outputs of a dispatch and need allocation etc.... So operations that are not really "compute" are clonable (linalg.fill is on the margins here...). This will avoid false SSA dependency between operations.
| } | ||
|
|
||
| // Set the workgroup count to {1, 1, 1} since this is to be executed | ||
| // sequentially (at leats for now) |
There was a problem hiding this comment.
| // sequentially (at leats for now) | |
| // sequentially (at least for now) |
| %arg0 : tensor<f32>, %arg1 : tensor<f32>, %arg2 : tensor<f32>, %arg3 : tensor<f32>) | ||
| -> (tensor<f32>, tensor<f32>) { | ||
| %0 = tensor.empty() : tensor<f32> | ||
| %1 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = []} |
There was a problem hiding this comment.
will be nice when we turn detensoring on and this pass can also pick up arith ops - shame that we have to keep all this linalg goo around for what's a couple of arith ops.
There was a problem hiding this comment.
yup, possible.... just going one step at a time.
It is sort of WIP... no tests failed.... I am still looking at the big model... Was planning to do it today and look deeper, just haven't done it yet. Also for now I only set the workload to 1. I have a few examples for where the workload of 5 would actually make it better (though for that the backend might run into issues). |
0186ec7 to
f14f10a
Compare
6a2cf5d to
971e583
Compare
971e583 to
3f92e7b
Compare
3f92e7b to
c7b8ae1
Compare
c7b8ae1 to
9cda29e
Compare
|
@benvanik I fixed the issues with this PR. It does "something" with the big model from Sean, but ideally #13747 and this together will do a better job for scalar dispatches... Ill work on debugging that before I land this so I get what I expect for that input IR. This is ready for review by itself though (and you can try it on that model as well to see what it does). |
c0641b7 to
7b70aa7
Compare
Current dispatch region formation logic is geared towards "large" tensors. For computations on small tensors, these just need to be moved into a dispatch and executed sequentially on the device. Unlike the current dispatch region formation that is centered around tile and fuse approach, the scalar computations just need to be lowered to loops (which might be small trip loops that can be canonicalized/unrolled). So the approach used here uses backward slice to group together a DAG of operations that can be moved into a sequential dispatch for execution on the device. It also does some basic "horizontal fusion" to group together operations that are not producer->consumer but are still scalar computations.
7b70aa7 to
cdfd12e
Compare
…org#13711) Current dispatch region formation logic is geared towards "large" tensors. For computations on small tensors, these just need to be moved into a dispatch and executed sequentially on the device. Unlike the current dispatch region formation that is centered around tile and fuse approach, the scalar computations just need to be lowered to loops (which might be small trip loops that can be canonicalized/unrolled). So the approach used here uses backward slice to group together a DAG of operations that can be moved into a sequential dispatch for execution on the device. It also does some basic "horizontal fusion" to group together operations that are not producer->consumer but are still scalar computations. Fixes iree-org#13545
…atch region. (iree-org#13798) Adding all values of the operation moved into a dispatch as results can lead to wasted memory usage if the result is not used. This patch avoids doing that and refactors the methods as a precursor to iree-org#13711.
See #11054 (comment) - this `multiple_results.mlir` test program uses scalars, and #13711 changed the code path that scalar dispatches go down. This alternate code path does not appear to have the same SPIR-V -> WGSL compatibility patches as the tensor path, so we see compilation errors with messages like `entry point 'd0' references multiple variables that use the same resource binding`. We should fix that (and add tests so it doesn't regress again), but I want to keep webgpu runtime debugging work unblocked. This modified program exhibits the same runtime-related issue as reported on #13809 / #14163: * The first invocation incorrectly returns ``` 2xf32=2.23 2.45 2xf32=0 0 ``` * The second invocation correctly returns ``` 2xf32=1.23 1.45 2xf32=2.23 2.45 ```
…atch region. (iree-org#13798) Adding all values of the operation moved into a dispatch as results can lead to wasted memory usage if the result is not used. This patch avoids doing that and refactors the methods as a precursor to iree-org#13711.
…org#13711) Current dispatch region formation logic is geared towards "large" tensors. For computations on small tensors, these just need to be moved into a dispatch and executed sequentially on the device. Unlike the current dispatch region formation that is centered around tile and fuse approach, the scalar computations just need to be lowered to loops (which might be small trip loops that can be canonicalized/unrolled). So the approach used here uses backward slice to group together a DAG of operations that can be moved into a sequential dispatch for execution on the device. It also does some basic "horizontal fusion" to group together operations that are not producer->consumer but are still scalar computations. Fixes iree-org#13545
See iree-org#11054 (comment) - this `multiple_results.mlir` test program uses scalars, and iree-org#13711 changed the code path that scalar dispatches go down. This alternate code path does not appear to have the same SPIR-V -> WGSL compatibility patches as the tensor path, so we see compilation errors with messages like `entry point 'd0' references multiple variables that use the same resource binding`. We should fix that (and add tests so it doesn't regress again), but I want to keep webgpu runtime debugging work unblocked. This modified program exhibits the same runtime-related issue as reported on iree-org#13809 / iree-org#14163: * The first invocation incorrectly returns ``` 2xf32=2.23 2.45 2xf32=0 0 ``` * The second invocation correctly returns ``` 2xf32=1.23 1.45 2xf32=2.23 2.45 ```
Current dispatch region formation logic is geared towards "large"
tensors. For computations on small tensors, these just need to be
moved into a dispatch and executed sequentially on the device. Unlike
the current dispatch region formation that is centered around tile and
fuse approach, the scalar computations just need to be lowered to
loops (which might be small trip loops that can be
canonicalized/unrolled). So the approach used here uses backward slice
to group together a DAG of operations that can be moved into a
sequential dispatch for execution on the device. It also does some
basic "horizontal fusion" to group together operations that are not
producer->consumer but are still scalar computations.
Issue #13545