Skip to content

Add generic IR symbol deduplication (vm.rodata, flow.executable, etc) #1144

Open

Description

@benvanik

During all of our compilation phases there are symbols generated that may have duplicates. On the simpler end for example several vm.rodata symbols may have the same exact byte contents (coming from the same UTF8-encoded string, etc). Moving higher in the stack there is duplication that is trickier to determine that would be nice to handle in a generic way. Several flow.executables may contain the same exact IR snippets (and is likely if the input IR was generated by library calls/etc).

A DuplicateSymbolFolding pass that ran on a region (module, etc) that did IR diffing could handle all of these cases.

Activity

  1. added
    compiler/dialects
    enhancement ➕
    good first issue 🌱
    on Mar 19, 2020
  2. added this to the milestone on Mar 26, 2020
  3. self-assigned this
    on Apr 22, 2020
  4. self-assigned this
    on May 6, 2020
  5. stellaraccident commented on Jun 6, 2020

    @stellaraccident
    Collaborator

    I'm not sure that a "generic" pass for all of this stuff is feasible/advisable, but passes that dedup flow.executable ops and vm.rodata ops individually may help in some cases. I don't, however, see a lot of big resnet low hanging fruit here:

    • Of the 53 convolutions, once partitioned, I find 2 that are redundant.
    • Depending on how partitioned, there are maybe ~20 regions (~20% of the whole) of mostly duplicated elementwise/broadcast ops. I haven't analyzed completely, but without further work, few of these are trivial dups; however, ~all of them could be dups if they were de-specialized, replacing static dims with dynamic dims. We'd need a cost model for that, but it would probably be safe to do this in most cases (?)
    • Once compiled down to the vm.rodata level, there are no duplicates. A manual inspection revealed a lot of near dups, differening by two words. I verified a couple of these and they come from the elementwise regions mentioned above.

    It seems that if optimizing on this axis, some level of deduping will bear fruit, although I am dubious as to how much it helps resnet specifically. Surely, though, if we don't have anything to do this deduping, we'll lack the motivation to do other optimizatation (mostly de-specialization) that ultimately yield benefits.

    Given that, there are three levels where we could do this:

    1. flow.executable
    2. hal.executable
    3. vm.rodata

    Number 1 is annoying, because at this level, the executable still contains arbitrary IR, requiring a full recursive equivalence check. Possible, but annoying. Number 2 can be easier because all of the non-trivial parts have been compiled at this point, and all of that just becomes an attribute compare. In both cases, there is some internal symbol variability that, if we made it canonical, we could just directly compare the IR. Right now, there would be several special cases due to non-load-bearing differences.

    Number 3 is probably too late, but valuable as a final fallback (likely useful for any global constants that should be deduped): code has already been emitted to initialize each executable, so simply deduping the symbols might save some binary size but would not help with initialization time/overhead because they would just alias the backing data, not undo the initialization code.

    So leaving out priority of doing this, I would probably implement:

    1. Some canonicalization cleanups to executable symbols so that they do not trivially diff, making matching easier.
    2. A hal.executable or flow.executable deduper (tbd which one is better).
    3. An rodata deduper (which is pretty trivial) to catch any final things that are easily aliases together.
  6. stellaraccident commented on Jun 6, 2020

    @stellaraccident
    Collaborator

    Also, since size/startup overhead is not the primary thing we are losing on right now, I'd be tempted to back burner this until later.

  7. benvanik commented on Jun 6, 2020

    @benvanik
    CollaboratorAuthor

    yeah rodata is pretty much classic link-time deduping -- too late to change things but helps with binary size.

    flow.executable is the easiest to modify/despecialize as we have a good deal of flexibility in shapes and constants (it's trivial to pull them out). A fancy cost function would be nice, but in a lot of cases we can make some simple decisions at the HLO level that we know are safe (making outer dimensions used by convs dynamic, etc). Basically, anything that would end up just changing the invocation count (workgroup count) of the dispatch should be free. If our convs are all of different shapes but similar kernel sizes that should gain much more deduplication.

    One nice thing about running deduplication at the various stages is that in some cases what isn't easily identifiable as a duplicate early on may become a duplicate later - for example, if one has a shape of x4 and another x16 but the backend always pads to x16. That avoids the need for super involved higher level despecialization as just ensuring good canonicalization while lowering will help.

    Something you can check is ensuring the rodata doesn't contain names. For example, I know that the function name is based on the original outlined dispatch region name, which then gets exported as the entry point. That'll cause the rodatas to differ as they then contain the unique name of the source function. Making every flow.executable entry point name "main" or something would be a good sanity check on that.

  8. benvanik commented on Jun 6, 2020

    @benvanik
    CollaboratorAuthor

    Another thing to try is disabling RematerializeDispatchConstantsPass - that is what currently takes a dispatch region with no constant values (only ops acting on input args) and inlines some of the constants - deduping prior to that may yield a better hit rate.

  9. benvanik commented on Jun 6, 2020

    @benvanik
    CollaboratorAuthor

    The startup time is most pronounced on Vulkan (where we need to compile each shader with the driver) - 120ms on desktop for me for resnet and probably much more on mobile. We'll want to multithread that and add caching, but caching isn't guaranteed and it's still a cold start issue. #1587 & co that link executables together will help with LLVM AOT/VMLA (as all will end up in a single shared object/VM module) but in SPIR-V land the shaders will stay separate. Not a high priority vs other things as you say, but will eventually be important and could make use of this if we are getting the higher level parts in place anyway.

  10. stellaraccident commented on Jun 6, 2020

    @stellaraccident
    Collaborator

    Ok, I'll go ahead and implement this, along with some utility functions that may prove useful. I think that as we get sophisticated in other ways, having these passes in there will mean that we see the results, which creates the right ramps to optimize further.

  11. 4 remaining items

  12. added this to the milestone on Sep 12, 2020
  13. self-assigned this
    on Oct 28, 2020
  14. added this to IREE Codegen and removed this from IREE Codegenon May 2, 2022
  15. removed this from the milestone on Apr 8, 2023
  16. moved this from Not Started to Backlog in (Deprecated) IREEon Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compiler/dialectsenhancement ➕good first issue 🌱

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions