Skip to content

Investigate better measurement on dispatch binary sizes #9436

Closed

Description

@pzread

Request description

To count the total size of dispatches in a module, currently we simply add up all dispatch binaries (from plolyglot). As the example in #9227 (comment), the raw size of the dynamic library might not provide enough granularity to the actual code size.

We might have similar issues on other formats of dispatches, such as SPIR-V flatbuffer.

What component(s) does this issue relate to?

Other

Additional context

No response

Activity

  1. added
    enhancement ➕
    infrastructure/benchmark
    on Jun 10, 2022
  2. benvanik commented on Jun 10, 2022

    @benvanik
    Collaborator

    If you want precise sizes per dispatch region your best bet is to do an --iree-hal-dump-executable-sources-to= and then compile each using iree-compile --compile-mode=hal-executable - that'll spit out each file on its own without any linking/combining that we do in the normal pipelines. The tradeoff here is of course then that we aren't doing any linking/combining so it doesn't give you an accurate representation of the real size of the final output - we'll want to continue to track both.

  3. self-assigned this
    on Jun 10, 2022
  4. pzread commented on Jun 10, 2022

    @pzread
    ContributorAuthor

    I think we want to measure the total size of all dispatches (tracking individual size is hard as the number of dispatches is always changing). This issue is the paddings in the ELF section, which reduces the precision on the real code size. The code size might increase but still in the range of padding.

  5. benvanik commented on Jun 10, 2022

    @benvanik
    Collaborator

    Ah - this is a fun topic - the TL;DR is that anything at whole-program scale within the range of padding is noise and not something we care about outside of specific investigations :)

    The padding is a function of the code size and if you're looking at the aggregate it's relevant: if a +1B change causes another 4KB page to be required that's actually a 4KB change as it impacts users, not a 1B change - it's still going to take 4KB on disk and 4KB+ in memory. Knowing that only 1B changed is not actionable, and frankly neither is knowing about the 4KB change - when talking about machine code for an entire program it's not the right scale of measurement. Imagine a user making a change to something in mhlo and seeing that the total model size goes from 78712 to 78713 - there's nothing that can be done about that - we want to be looking for % deltas like 79KB to 94KB. We can work through some of these scenarios to help narrow things down, but starting from a place of "who is looking at this number, how frequently are they looking at it, and what can they possibly do if it changes" is useful when it comes to stuff like this. For the smaller creep we only care over large time frames - and if over a large enough time frame the size only grows 200 bytes and that's hidden inside of some padding it's fine: we weren't going to stress about 200 bytes anyway.

    We're also sitting at the tail end of a massive pipeline with hundreds of thousands of lines of code in-between us and the output and it's a struggle to get actionable results from that - a single change of a single register selected for a single instruction in a single dispatch for a single target with a specific instruction encoding can cause 10-100's of bytes difference (switching instruction encodings, spilling to the stack, etc). Every change that lands in LLVM is likely to change our byte counts so we need to be sure that whatever we're trying to surface has the right precision to not be invalidated on every integration. In other words, we're not going to be changing linalg patterns based on +/- N B but +/- NN KB - the rest is noise that just leads to complacency ("eh, it always is +/-5% so I'll just ignore anything not >+/-20%") or whack-a-mole ("I regressed model X on platform Y by 1KB but if I try to fix that then I regress model Y on platform Z by 10KB").

    The other issue here is that ELF is not the only thing adding padding - and padding is not a bad thing. In fact, in a lot of cases we'll want additional internal padding and LLVM is going to be inserting that for us. The topic of code alignment as it relates to performance is an extensive one (https://easyperf.net/blog/2018/01/18/Code_alignment_issues is a good writeup hinting at how deep that rabbit hole goes - I'm hopeful we can eventually be wiring up llvm-mca and such and actually tuning these things!), but the important thing is that adding bytes can be extremely important and at the scales we're working at it's in the noise from a perspective of whole program code size. That is to say, even if we started measuring exact per-function sizes after code generation that's still going to have padding in it we can't (or don't want to) remove. Properly deduplicating a single dispatch can make up for the cost of all internal padding in all dispatches within a model, and you don't need to know the precise byte size of all of the dispatches to know that :)

    When it comes to other targets we have even less control; for example, the size of a SPIR-V/wasm/etc module does not strongly correlate to the size of the actual machine code emitted by the driver at runtime - you could have a 2KB SPIR-V module that is unrolled to 400KB, etc. Even though in some situations we can query the driver at runtime to ask it how much code it generated that's adding even more layers between us and the final result: just as every LLVM integrate is likely to change our CPU output so too will every driver version from every vendor on every platform with every set of options/unique hardware combination - the only thing we can measure effectively at scale is file size and that's a naturally noisy number.

    The detailed information can still be informative but only in specific situations: we don't need to track that fidelity of information for all models on all targets but do want to make it available if someone is digging into a specific model on a specific target - that's what the dump-executable-sources-to flag can help with (compile to obj and then objdump to get exact function sizes, disassemble, etc). If you really do want to try to track detailed aggregate sizes you can take the ELFs and run them through something like readelf and sum the section sizes (.data/.text/etc) - but again that's not going to be a great metric in aggregate and still will have factors outside of our control. If you're counting bytes you're going to want to know precisely which dispatches they come from for detailed situational analysis or you're going to want to count the actual size as it matters to users (file size on disk + size in memory once loaded). Trying to do both with the same metrics will give you something not actionable to the team and not representative of what users care about.

  6. bjacob commented on Jun 13, 2022

    @bjacob
    Collaborator

    In the past, I have come across situations where ther granularity of file size measurements was much larger than 4k. I seem to remember 64k for Android aarch64 builds of TFLite. I can dig up the ~ 3 year old data as needed (google internal). I never understood why the granularity was so high, but it was an empirical fact. I had a theory that it might be related to how relocations are implemented (and TFLite probably had lots more than we ever do in IREE runtime or module code).

    Wanting a fine granularity metric does not imply wanting to act on tiny size changes. It's merely wanting to be able to tell tiny changes from larger changes. When the granularity is 64k, and a change makes us bump by 64k, all we know is the change was anywhere between 1 bytes and 128k minus 1 byte.

  7. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    The granularity is not 64K - we're generating the ELFs and it's 4K :)

  8. bjacob commented on Jun 13, 2022

    @bjacob
    Collaborator

    Is there a specific place in the embedded-elf linker code that controls that granularity? really curious!

  9. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    We don't set it and let the default of 4096 kick in:
    https://code.woboq.org/llvm/lld/ELF/Target.h.html#lld::elf::TargetInfo::defaultCommonPageSize

    (I'm not saying it's not useful to see the changes - just that we can already do that with readelf/objdump and anything we were to build in addition to what we can already do is not going to make things easier)

  10. bjacob commented on Jun 13, 2022

    @bjacob
    Collaborator

    i guess i was wondering if something else than "page size" could contribute to the effective final file size granularity - after all, I doubt that page size was 64k when i observed 64k file size granularity across tflite android/aarch64 changes!

  11. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    I didn't pick those two constants at random :)

    https://sourcegraph.com/github.com/llvm/llvm-project/-/blob/lld/ELF/Driver.cpp?L2678-2688
    // All important alignment decisions must use this value.

  12. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    (if you saw 64K it was because someone override that value)

  13. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    Looks like toolchains do override it, and there's been some discussion about making it the default:
    https://reviews.llvm.org/D77330

    Our loader can align things in memory at any alignment we need and currently does the padding: https://github.com/google/iree/blob/857e15593718b74c15a104925bad1bab50fa7d9b/runtime/src/iree/hal/local/elf/elf_module.c#L244-L245
    If the default does change we can override it (-z max-page-size=4096).

  14. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    Confirmed an aarch64 ELF is not padded - in fact, not even padded to 4096;
    image
    Total size on disk is 3,968B.

  15. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    oh, that had debug info - stripped is 2,144B on disk:
    image
    image

    Counting .text and .rodata is going to get you a pretty accurate code size number, and the rest of the total file size is relatively fixed (we only export a single symbol and we barely use relocations).

  16. pzread commented on Jun 13, 2022

    @pzread
    ContributorAuthor

    I see. So the IREE ELF loader uses memcpy to load segments and the padding to page size is not required.
    https://github.com/google/iree/blob/857e15593718b74c15a104925bad1bab50fa7d9b/runtime/src/iree/hal/local/elf/elf_module.c#L274-L277

    I remembered ld and Linux kernel [1] can load the segments with mmap to reduce memory copy [1]. In that case the padding in file is required. For example, by default the clang -fPIC -shared gives an ELF like this:

    Program Headers:                                                                                                                                  
      Type           Offset             VirtAddr           PhysAddr                                                                                   
                     FileSiz            MemSiz              Flags  Align                                                                              
      LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000                                                                         
                     0x0000000000005850 0x0000000000005850  R      0x1000                                                                             
      LOAD           0x0000000000006000 0x0000000000006000 0x0000000000006000                                                                         
                     0x00000000000024b5 0x00000000000024b5  R E    0x1000                                                                             
      LOAD           0x0000000000009000 0x0000000000009000 0x0000000000009000                                                                         
                     0x0000000000001454 0x0000000000001454  R      0x1000                                                                             
      LOAD           0x000000000000add0 0x000000000000bdd0 0x000000000000bdd0                                                                         
                     0x0000000000000658 0x0000000000000660  RW     0x1000    
    

    Note that although the file offset is not aligned, they have enough gap between them with zeros filled. This might the reason we see a big gap in the example #9227 (comment).

    1. https://elixir.bootlin.com/linux/latest/source/fs/binfmt_elf.c#L365
  17. pzread commented on Jun 13, 2022

    @pzread
    ContributorAuthor

    Back to the original topic, if the ELF dispatch doesn't have padding between segments and the other non-code segments are fixed and relative small, then directly counting on the dispatch binary size seems not a bad idea to me. I measured the rodata + code vs. the binary size on mobilenetv3 x86 embedded ELF and the difference is only 2%. And as Ben said, small size changes will be buried in the noise. We can confirm the noise level after starting tracking the binary size.

    Although this probably won't happen soon, I'm thinking if there is a way to track the bad IREE codegen (IREE generates bad LLVM IR) at the IR level, which suffers from less noise. For #8849, it looks like a histogram to count the vector sizes/types might help. And from my experience, this can also track the bad tiling config. @dcaballe WDYT?

  18. bjacob commented on Jun 13, 2022

    @bjacob
    Collaborator

    Looks like toolchains do override it, and there's been some discussion about making it the default:
    https://reviews.llvm.org/D77330

    Ha! OK, that clears it up. Thanks for digging this. Also relieved that "it wasn't just me" and 64k granularity was really a thing, and now I totally buy the rationale why IREE is not concerned 🙂

  19. benvanik commented on Jun 13, 2022

    @benvanik
    Collaborator

    Yep! Since we don't mmap the files we can keep them lightly padded - usually just the interior .text/.rodata padding for cache line alignment. Since page granularity for access permissions varies across platforms/configurations we'd have to take the max of all possible platforms in all modes if we wanted to map things, but because we exclusively are putting dispatch code in the ELFs there's generally not much we'd need to map. At the point that our (eventual, ideal) ELF sizes are ~32-128KB vs the 100MB-1GB of the model data itself stored outside we just don't bother :P

  20. dcaballe commented on Jun 13, 2022

    @dcaballe
    Contributor

    Although this probably won't happen soon, I'm thinking if there is a way to track the bad IREE codegen (IREE generates bad LLVM IR) at the IR level, which suffers from less noise. For #8849, it looks like a histogram to count the vector sizes/types might help. And from my experience, this can also track the bad tiling config. @dcaballe WDYT?

    It would be hard. For #8849, the LLVM IR looks good until it's lowered to RISC-V machine instructions, which happens at the very end of the pipeline and it's target specific. It would be like inspecting assembly.

  21. added
    infrastructure
    on Jun 22, 2022
  22. pzread commented on Nov 21, 2022

    @pzread
    ContributorAuthor

    From the discussions above and the records in the past few months (we caught the regressions on sizes at the correct PRs), it looks like using the size of each dispatch binary is okay at this moment. Close for now.

  23. moved this to Done in IREEon Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement ➕infrastructureinfrastructure/benchmark

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions