Repository navigation
Description
JAX inputs have a lot of quirks that need to be fixed at the frontend level (like passing in/returning thousands of tensors to each invocation, not having non-tensor operations, etc). Looking at the large models in #13543 and #13459 there are several patterns we could handle better post-HLO, though, and that would be good to have even if frontend improvements are made.
Some things found in an initial skim through:
Detensoring creates many bad load + arith + splat sequences
Should probably disable detensoring as it was never finished - or a retensoring pass in flow should be able to find slices of load -> splat and move them into dispatch regions
// 476 blocking loads total
%950 = stream.async.transfer %211 : !stream.resource<transient>{%c4} -> !stream.resource<staging>{%c4}
%951 = stream.async.load %950[%c0] : !stream.resource<staging>{%c4} -> f32
%952 = arith.addf %951, %cst_18 : f32
%953 = arith.bitcast %952 : f32 to i32
%8167 = stream.async.splat %953 : i32 -> !stream.resource<external>{%c4}Splats are used to pass scalar values into dispatches that could instead be done via normal dispatch operands
Need a pattern in flow to capture the splat operand and drop the tensor use
%3766 = stream.async.splat %3765 : i32 -> !stream.resource<transient>{%c4}
%3767 = stream.async.splat %3756 : i32 -> !stream.resource<transient>{%c4}
%3768 = stream.async.dispatch @_main_dispatch_2777::@_main_dispatch_2777_generic_1x8192_f32(%3766[%c0 to %c4 for %c4], %3569[%c0 to %c32768 for %c32768], %3767[%c0 to %c4 for %c4], %arg199[%c0 to %c32768 for %c32768]) : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c32768}, !stream.resource<transient>{%c4}, !stream.resource<external>{%c32768}) -> !stream.resource<transient>{%c32768}Splats that later become fills don't coalesce due to resource padding
Could special case pack any allocation under the resource config alignment so these could be dense, then pull all into a single dispatch to perform at once (scatter) instead of one transfer operation per 4 byte splat
%8402 = stream.async.splat %8144 : i32 -> !stream.resource<external>{%c4}
%8403 = stream.async.splat %8149 : i32 -> !stream.resource<external>{%c4}
%8404 = stream.async.splat %8154 : i32 -> !stream.resource<external>{%c4}
->
stream.cmd.fill %3307, %arg922[%c7317683072 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
stream.cmd.fill %3311, %arg922[%c7317683136 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
stream.cmd.fill %3315, %arg922[%c7317683200 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
stream.cmd.fill %3319, %arg922[%c7317683264 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
stream.cmd.fill %3323, %arg922[%c7317683328 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
stream.cmd.fill %3327, %arg922[%c7317683392 for %c4] : i32 -> !stream.resource<external>{%c7317692032}Dispatch region formation puts lots of scalar arithmetic in their own dispatches
Need much more aggressive dispatch region formation, or aggressive detensoring so that retensoring makes its own dispatch regions, etc - these are ~100k to ~1m times more expensive than they should be
// 200 splat + scalar dispatches
%1390 = stream.async.splat %1047 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2133 : index}
%1391 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1390[%c0 to %c4 for %c4], %764[%c0 to %c4 for %c4], %1387) {util.ordinal = 2134 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %764{%c4}
%1392 = stream.async.splat %1058 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2135 : index}
%1393 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1392[%c0 to %c4 for %c4], %765[%c0 to %c4 for %c4], %1387) {util.ordinal = 2136 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %765{%c4}
%1394 = stream.async.splat %1066 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2137 : index}
%1395 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1394[%c0 to %c4 for %c4], %766[%c0 to %c4 for %c4], %1387) {util.ordinal = 2138 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %766{%c4}Dispatch region formation isn't aggressive enough around reductions so 100's of elementwise + reduction dispatches on 2048 elements
Likely because of multiple users of %5564, but all users are reductions of the same input that could be fused together
// elementwise add of 2048 elements
%5564 = stream.async.dispatch @_main_dispatch_4395::@_main_dispatch_4395_generic_2048_f32(%arg121[%c0 to %c8192 for %c8192], %4631[%c0 to %c8192 for %c8192]) : (!stream.resource<external>{%c8192}, !stream.resource<transient>{%c8192}) -> !stream.resource<external>{%c8192}
// sum reduction of 2048 elements -> scalar
%6712 = stream.async.dispatch @_main_dispatch_4578::@_main_dispatch_4578_generic_2048_f32(%5564[%c0 to %c8192 for %c8192], %6711[%c0 to %c4 for %c4]) : (!stream.resource<external>{%c8192}, !stream.resource<transient>{%c4}) -> !stream.resource<transient>{%c4}Some transient allocations end up host-ordered (stream.resource.alloc vs stream.async.alloca)
%632:36 = stream.resource.alloc uninitialized : !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c8192}, !stream.resource<transient>{%c4096}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c8388608}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}This explains why #13440 didn't help.
There's a mysterious tf.aliasing_output attribute on function args that could be useful
// I think this means that output 0 aliases arg0
%arg0: !hal.buffer_view {jax.arg_info = "state.step", mhlo.sharding = "{replicated}", tf.aliasing_output = 0 : i32}
Activity
silvasean commentedon May 16, 2023
Hey Ben, this is great. Right now I'm not seeing this on the profiles though. FYI we're currently 10x off on performance on this workload due to first-order codegen issues mainly. I think the most actionable thing in this vein that I see is a very large amount of time spent in memory allocation (~200-300ms out of ~1.1s, with a strong baseline for this workload being ~100ms).
I filed #13566 for that, but it sounds like we need more ownership and input from higher up in the stack on that one -- I'm wondering if you can think of anything we can do to move that one forward on our end here in IREE, or are we completely blocked without further from higher in the stack? Even something like a concrete decision tree or forward-looking set of options that seem feasible to implement on our end here, that higher up can consider, could be useful.
benvanik commentedon May 17, 2023
Will respond on that issue - next step is capturing a trace and posting it there so we can see the allocation behavior at runtime.
ScottTodd commentedon May 17, 2023
Some of these issues are structural, so simplifying could unlock more of our existing compiler optimizations, or at least make it easier to read the IR to see what to focus on. Directly going after the profiled hotspots might just find a local minimum (e.g. we might not need to optimize a convolution with striding if we can instead convert it into a matmul or skip running it altogether).
benvanik commentedon May 17, 2023
Yep! This is why instrumented tracing is so useful - looking at the top 5 hotspots is useful for driving codegen work but not useful for non-codegen work that is much more about the entire workload instead of any particular work item.
benvanik commentedon May 17, 2023
Good example from immediately before
--pass-pipeline=builtin.module(func.func(iree-flow-form-dispatch-regions)):https://gist.github.com/benvanik/a6846aa7ad6999ce167705fe3f6f0853
As an example here's one where we are going device->host->device but shouldn't be; there's several things we can do but I'll file an issue for the real solution to this particular issue:
Disabling detensoring by default until #6948 can be completed. (#13658)
pjannaty commentedon May 30, 2023
@MaheshRavishankar if there are any parcelable/concrete sub tasks, please let me and @Young768 know.
MaheshRavishankar commentedon May 30, 2023
There is a lot of content here and in other bugs that is a bit hard to parse and categorize... #13545 is a place where we have some tasks... I have to look at everything listed on this and related bugs and organize them a bit. So far I am going through this list here #13545 (comment) . Once I land that I can look at what is needed after that to get a better picture...
For a more deep dive list of tasks waiting for an inference model to help better prioritize.
Increase the PotentialValuesState analysis max value count. (#13862)
Increase the PotentialValuesState analysis max value count. (iree-org…
Disabling detensoring by default until iree-org#6948 can be completed. (
Increase the PotentialValuesState analysis max value count. (iree-org…
Increase the PotentialValuesState analysis max value count. (iree-org…