Skip to content

Create dispatches for scalar computation using program slices. - #13711

Merged
MaheshRavishankar merged 1 commit into
iree-org:mainfrom
MaheshRavishankar:scalar_dispatches
Jun 22, 2023
Merged

MaheshRavishankar merged 1 commit into
iree-org:mainfrom
MaheshRavishankar:scalar_dispatches

Conversation

@MaheshRavishankar

@MaheshRavishankar MaheshRavishankar commented May 20, 2023 •

Copy link
Copy Markdown
Collaborator

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

MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request May 26, 2023
…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.
MaheshRavishankar added a commit that referenced this pull request May 26, 2023
…atch region. (#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 #13711.
@MaheshRavishankar
MaheshRavishankar force-pushed the scalar_dispatches branch 2 times, most recently from d8c9b1a to 7cd173a Compare May 30, 2023 23:23
@MaheshRavishankar MaheshRavishankar changed the title [WIP] Enable creation of scalar dispatches. Create dispatches for scalar computation using program slices. May 30, 2023
@MaheshRavishankar
MaheshRavishankar marked this pull request as ready for review May 30, 2023 23:24

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably should have arith, complex, etc in here as well

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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 = []}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, possible.... just going one step at a time.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Excited to see the IR cleanup with this! Is it something I could test now or still WIP?

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).

@MaheshRavishankar
MaheshRavishankar force-pushed the scalar_dispatches branch 2 times, most recently from 6a2cf5d to 971e583 Compare June 8, 2023 05:18
@MaheshRavishankar MaheshRavishankar added benchmarks:cuda Run default CUDA benchmarks benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:comp-stats Run default compilation statistics benchmarks benchmarks:android-cpu Run default Android CPU benchmarks benchmarks:android-gpu Run default Android GPU benchmarks benchmarks:vulkan-nvidia Run default Vulkan benchmarks on NVIDIA GPU labels Jun 8, 2023
@github-actions

github-actions Bot commented Jun 8, 2023 •

Copy link
Copy Markdown

Abbreviated Benchmark Summary

@ commit 27561e0cea552e5ed5881536fe26f88dbb952e58 (vs. base 5757feba749787870aca120f01338d9d0677ed5d)

Regressed Latencies 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
PoseNet\_fp32(tflite) [armv8.2-a-generic-linux\_android29-llvm\_cpu][default-flags] local\_task(embedded\_elf)[4-thread,full-inference,system-scheduling] with zeros @ pixel-4[big-core] 80.641 (vs. 74.481, 8.27%↑) 80.329 1.066
MobileNetV3Small\_fp32(tflite) [qualcomm-adreno-vulkan\_android31-vulkan\_spirv][default-flags] vulkan(none)[full-inference,default-flags] with zeros @ moto-edge-x30[gpu] 4.966 (vs. 4.701, 5.62%↑) 5.083 0.375
PersonDetect\_int8(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][default-flags] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 2.685 (vs. 2.544, 5.53%↑) 2.722 0.106

Improved Latencies 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileNetV3Small\_fp32(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][default-flags] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 7.299 (vs. 9.073, 19.55%↓) 7.293 0.059
EfficientNet\_int8(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][default-flags] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 13.416 (vs. 15.441, 13.11%↓) 13.483 0.376
MobileNetV3Small\_fp32(tflite) [arm-valhall-vulkan\_android31-vulkan\_spirv][experimental-flags,fuse-padding,max-concurrency] vulkan(none)[full-inference,default-flags] with zeros @ pixel-6-pro[gpu] 7.979 (vs. 8.924, 10.59%↓) 7.978 0.024

[Top 3 out of 19 results showed]

No improved or regressed compilation metrics 🏖️

For more information:

Source Workflow Run

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

@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).

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@MaheshRavishankar
MaheshRavishankar force-pushed the scalar_dispatches branch 2 times, most recently from c0641b7 to 7b70aa7 Compare June 10, 2023 01:58
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.
@MaheshRavishankar
MaheshRavishankar enabled auto-merge (squash) June 22, 2023 02:15
@MaheshRavishankar
MaheshRavishankar merged commit 3459833 into iree-org:main Jun 22, 2023
jvstokes pushed a commit to jvstokes/iree that referenced this pull request Jun 25, 2023
…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
NatashaKnk pushed a commit to NatashaKnk/iree that referenced this pull request Jul 6, 2023
…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.
ScottTodd added a commit that referenced this pull request Aug 10, 2023
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
  ```
nhasabni pushed a commit to plaidml/iree that referenced this pull request Aug 24, 2023
…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.
nhasabni pushed a commit to plaidml/iree that referenced this pull request Aug 24, 2023
…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
nhasabni pushed a commit to plaidml/iree that referenced this pull request Aug 24, 2023
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
  ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmarks:android-cpu Run default Android CPU benchmarks benchmarks:android-gpu Run default Android GPU benchmarks benchmarks:comp-stats Run default compilation statistics benchmarks benchmarks:cuda Run default CUDA benchmarks benchmarks:vulkan-nvidia Run default Vulkan benchmarks on NVIDIA GPU benchmarks:x86_64 Run default x86_64 benchmarks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants