Skip to content

Roll-up of tweaks and enhancements made to the core compiler dialects. - #7312

Merged
benvanik merged 17 commits into
mainfrom
benvanik-utils-mix
Oct 15, 2021
Merged

benvanik merged 17 commits into
mainfrom
benvanik-utils-mix

Conversation

@benvanik

@benvanik benvanik commented Oct 12, 2021 •

Copy link
Copy Markdown
Collaborator

These are standalone updates made to the util dialect as part of the streams work. For context of where these are used see the full branch... or use imagination.

@benvanik benvanik added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Oct 12, 2021
@google-cla google-cla Bot added the cla: yes label Oct 12, 2021
@benvanik
benvanik force-pushed the benvanik-utils-mix branch 2 times, most recently from 06278e2 to 3127872 Compare October 12, 2021 21:39
@benvanik
benvanik marked this pull request as ready for review October 12, 2021 23:01
@benvanik
benvanik requested a review from ScottTodd October 12, 2021 23:02
@iree-github-actions-bot

iree-github-actions-bot commented Oct 13, 2021 •

Copy link
Copy Markdown
Contributor

Abbreviated Benchmark Summary

@ commit fcfac913c70706fcc20175a5098e605103abb339 (vs. base ef53a6c4602493914d8a131130a1a2edb6487d58)

Regressed Benchmarks 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
PoseNet [fp32] (TFLite) kernel-execution with IREE-Vulkan @ SM-G980F (GPU-Mali-G77) 41 (vs. 23, 78.26%↑) 41 0
MobileSSD [fp32] (TFLite) full-inference with IREE-Vulkan @ SM-G980F (GPU-Mali-G77) 111 (vs. 73, 52.05%↑) 112 1
MobileBertSquad [fp32] (TensorFlow) big-core,full-inference with IREE-Dylib-Sync @ Pixel-4 (CPU-ARMv8.2-A) 840 (vs. 670, 25.37%↑) 839 1

Improved Benchmarks 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileNetV2 [fp32,imagenet] (TensorFlow) big-core,full-inference with IREE-Dylib-Sync @ SM-G980F (CPU-ARMv8.2-A) 80 (vs. 105, 23.81%↓) 79 1
MobileNetV2 [fp32,imagenet] (TensorFlow) 1-thread,big-core,full-inference with IREE-Dylib @ SM-G980F (CPU-ARMv8.2-A) 80 (vs. 104, 23.08%↓) 80 0
MobileNetV2 [fp32,imagenet] (TensorFlow) 3-thread,big-core,full-inference with IREE-Dylib @ SM-G980F (CPU-ARMv8.2-A) 57 (vs. 61, 6.56%↓) 56 1

For more information:

@benvanik

Copy link
Copy Markdown
Collaborator Author

(the failure here is #7323)

Comment on lines +158 to +159
Returns the set of results that may be tied to operands as an
(index, length) pair ala getODSResultIndexAndLength.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just dropping a comment to point out that I didn't really understand what "tied" ops were until I looked above this at the comment for IREE::Util::TiedOpInterface. The examples there are really useful :D

return;
}
p << "{";
llvm::interleaveComma(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TIL. Nice util method :D

... then below this, if (i < resultTypes.size() - 1) p << ", "; is still needed :P

return true;
} else {
// See if block the value is defined in dominates the forOp block.
// TODO(benvanik): optimize this, it's terribly expensive to recompute.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not for this in particular, but have you profiled the compiler with your set of changes? I'm curious about the general performance and if the changes uncovered any slow paths in MLIR core.

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.

not yet - I'm sure that some of these passes need to be split up so they can parallelize better but it's hard to get a feel for just how bad something is until you have real IR flowing through. My plan before doing the flag flip to the new streams path (that uses these quite a bit) is to ensure the time comes down to ~match the old path (it'll definitely be slower because it's running more passes, but it shouldn't be an order of magnitude slower).

return didChange;
}

class FoldGlobalsPass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This pass looks great - lots of nice optimizations that I expect we'll see having use in real progarms, with how much globals are used in the programs we've seen so far. It also could report some statistics like https://github.com/google/iree/blob/df4e156da5b575e2721e9f363d3f56cd761cdf1d/iree/compiler/Dialect/Flow/Transforms/DeduplicateExecutables.cpp#L305-L314

Ideas:

  • Original number of globals
  • Number of globals [inlined, erased, deduplicated]
  • Number of globals changed from immutable to mutable

I'm also happy to add those myself to sate my own curiosity :)

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.

that'd be great! lots of the folding/fusing could use those stats. I still don't use the stats because they get compiled out - if we made all our builds have them then I'd be more excited to add them!

Comment on lines +152 to +157
// Propagate constant into the initial value. Note that there may have been
// a previous initial value that is being replaced.
global.op.initial_valueAttr(constantValue);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this correct in the case of a load happening before the first store?

initial value: 5
load value -> 5
store value: 6
store value: 6
load value -> 6

or does that fail to match since the initial value is checked?

Maybe the comment is just confusing me - the previous "initial value" resolves to the same numeric value but has a different MLIR SSA value?

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.

good catch - originally this did have that bug (probably when I wrote the comment) - then I added the L134 initial value for constantValue - so now this only kicks in if there's either no default initializer or all stores match that initializer. I still think it may not be right though, as an uninitialized value is defaulted to 0 so the load before store could still observe 0. Currently we don't rely on that though and it's just a safety thing to avoid uninitialized memory. A proper DFA using the DFX stuff would solve all these issues as it would propagate the value at specific times around the program. A single DFA pass for optimizing globals could replace nearly all these passes (SimplifyGlobalAccess and FoldGlobals for sure). I'll fix the comment and add a TODO.

Comment on lines +286 to +291
// Deduplicates immutable globals with constant initial values.
// This is a simplified and safer version of the global fusion pass.
static bool deduplicateConstantGlobals(GlobalTable &globalTable) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another case where our test models with weights removed may have quite different behavior from real models (after optimizations).

Have we thought about grouping passes like this into IREE-specific -O0, -O2, etc. optimization levels? There's probably no substitute for real models in benchmarking, but some grouping for debugging might be helpful.

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.

the only solution is to move away from improper emulation and use proxies that match the behavior. This would be things like #util.random that behave as if the data is actually unique and would have all the proper behavior with mapping/staging/etc. Splatting out uniform values is just not the right way to simulate things.

Comment on lines 755 to +756
for (auto i : llvm::seq<unsigned>(0, numElems / 2)) {
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
constexpr float kTwoPi = 2.0 * M_PI;
constexpr float kTwoPi = static_cast<float>(2.0 * M_PI);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks for the warnings fixes, they were bugging me too :P

Comment on lines +50 to +58
def Util_CmpEQOp : Util_PureOp<"cmp.eq", [
AllTypesMatch<["lhs", "rhs"]>,
Commutative,
]> {
let summary = [{compares two values for equality}];
let description = [{
Compares two operands for equality. This is intended for comparing IREE
reference types (like !util.byte_buffer) that cannot be used with std.cmpi.
}];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how do you feel about cmp.ref.eq to disambiguate even further?

then no need for

return failure();  // not used for non-ref types currently

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 may want to use it for more in the future - especially now that stuff is getting microsharded to different dialects. For example, list comparison, pointer value comparison, etc.

Comment on lines +30 to +31
// TODO(#7277): kill nearly all tensor related mappings from HAL when using
// streams. Only tensors that get turned into buffer views on boundaries should

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

I tried to spot which ops should be allowed to use tensor types after flow in #6883 and found many places that were using them still

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.

totally missed that PR! yeah, we don't want allowlists like that on intermediate dialects meant to compose with other user-defined dialects - we only want denylists on dialects we should have removed (like ensuring there are no tensor.* ops remaining as we should have converted them all), but even that is difficult to do in the case of dialects that may have their own requirements - if something needs a tensor.dim and throws a do_not_optimize on it then it's going to live to HAL turns it into a buffer_view.dim, and that's totally fine.

Future changes will move tensor size calculations out of HAL and
this makes that easier to remove by hiding the implementations.
The stream dialect heavily uses size-aware types and tied ops and
exposed some assumptions around the tie storage that needn't be the
case.
This doesn't trigger today but does in the new streams dialect that
performs more sophisticated casts.
…n't.

IREE relies on the use-def chain being complete to model all potential
hazards.
This allows the inlineRegion helper to be used.
This allows us to run it on util.initializer.
Running this early on after initializers are materialized
allows for better folding/CSE/etc as many initializers end up
doing similar things.
Today only branches/blocks are optimized but the intent is that
we'll do function/call/return optimization in the same way.

(these predate the explorer but could be changed to use that in the
future to work more generically)
This does a lot of heavily lifting for simplification of globals.
The flow dialect today benefits from this after shape dimensions
are expanded, but the stream dialect does several types of global
expansion and leans on this to make most of the globals fold
away.

(this predates the explorer but could be changed to use that in the
future to work more generically)
There's no way outside of vm to compare a ref type to null today.
This can be extended to support more types but is kept limited now
for this use case.
Shape aware ops can return dynamic shape dimensions without
materializing any shape ops. This avoids quite a bit of shape dialect
IR during dynamic tensor handling and avoids the need to rely on
shape dialect canonicalizers throughout the pipelines.
This finds globals that are correlated across the program (always
set to the same values) and fuses them together. This
dramatically reduces the global count for things like dynamic shape
dimensions or resources sizes/timepoints/subviews (as seen in the
stream dialect).
There's only one place it's valid to have this op - before the
flow dialect - so being in util is just a footgun. This move is
mandatory in the streams branch as the current shady HAL handling
of this op is all dead code.
These allow for efficient emission and canonicalization of ranges
of offsets/lengths.
This allows stream formation to be disabled for when the stream dialect
is added.
@benvanik
benvanik merged commit 3d8a059 into main Oct 15, 2021
@benvanik
benvanik deleted the benvanik-utils-mix branch October 15, 2021 19:26
ScottTodd added a commit that referenced this pull request Nov 4, 2021
Following up on #7312 (comment), this adds more [statistics](https://mlir.llvm.org/docs/PassManagement/#pass-statistics) to IREE's compiler passes. I personally build with the CMake option `-DLLVM_FORCE_ENABLE_STATS=ON` so the `-pass-statistics` flag works in `RelWithDebInfo` builds, but we don't yet have anything like that configured on our CI or recommended in our developer documentation. See also #6161 (comment) for how this could be useful.

Sample output on [bert_encoder_unrolled_fake_weights.mlir](https://github.com/google/iree/blob/main/iree/test/e2e/models/bert_encoder_unrolled_fake_weights.mlir) (fake weights are folded aggressively 😛):

```
mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass
  (S) 1113 global ops before folding - Number of util.global ops before folding
  (S)    7 global ops after folding  - Number of util.global ops after folding
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants