Skip to content

Flow/HAL+linalg allocates excessive zero-filled buffer for MobileSSD models. #6826

Closed

Description

@hcindyl

While running IREE compiler on MobileSSD models (with TFLite format), the codegen requires a large amount of buffer (4.7MB in our test case) that's all filled in 0s. The intermediate operations could be fused and the buffer could be squashed.

Please see examples of mobilenet SSD models in

In tf1 tensorflow pb format:
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md#coco-trained-models
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md#open-images-trained-models

In tflite format
https://coral.ai/models/object-detection/#trained-models. (notice there is a TFLite_Detection_PostProcess op at the end of the models here and should be stripped before passing into the IREE codegen)

In tf2 savedmodel format
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md

Activity

  1. added
    bug 🐞
    codegen
    on Aug 23, 2021
  2. MaheshRavishankar commented on Aug 23, 2021

    @MaheshRavishankar
    Collaborator

    Thanks Cindy! The immediate fix for these are to change the way the TOSA -> Linalg lowering is done. AFAICS, the TOSA -> Linalg path is currently lowering conv + bias to

    %0 = linalg.generic %bias {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", parallel", "parallel", "parallel"]}
        ins(%bias : tensor<?xf32>) outs(%init : tensor<?x?x?x?xf32>) {
        ^bb0(%arg0 : f32, %arg1 : f32):
          linalg.yield %arg0 : f32
        } -> tensor<?x?x?x?xf32>
    %1 = linalg.*conv* ... outs(%0 : tensor<?x?x?x?xf32>)
    
    

    so its doing a broadcast of the bias into the full tensor which is then fed to the outs parameter of the conv operation. This is not exactly wrong, but is not what is currently handled correctly. What is handled better is

    %cst = constant 0 : f32
    %0 = linalg.fill(%cst, %init) : f32, tensor<?x?x?x?xf32> -> tensor<?x?x?x?xf32>
    %1 = linalg.*conv* ... outs(%0 : tensor<?x?x?x?xf32>
    %2 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", parallel", "parallel", "parallel"]}
        ins(%bias : tensor<?xf32>) outs(%init : tensor<?x?x?x?xf32>) {
        ^bb0(%arg0 : f32, %arg1 : f32):
          linalg.yield %arg0 : f32
        } -> tensor<?x?x?x?xf32>
       
    

    That would be the first thing to fix.

    As a side note, what is done today is still "correct". I have some local changes that can fuse what is currently seen by IREE through the TOSA path as well. I am in process of landing those (Needs https://reviews.llvm.org/D108565 and this commit in IREE MaheshRavishankar@5e36006). With this you get things fused, but it actually didnt help the memory consumption. It seems to me that buffer allocation already is reusing buffers in a way that makes this fusion unnecessary from a memory stand point (still better to do this since it is more efficient).

  3. benvanik commented on Aug 23, 2021

    @benvanik
    Collaborator

    Mahesh's fix is the best one; my workaround in #6815 is going to requires a few workarounds to work so I'm going to take the time to burn down some tech debt before it goes in.

    If someone gets the model in tosa .mlir before me and could attach to the issue that'd be helpful for tracking down other issues too!

  4. hcindyl commented on Aug 25, 2021

    @hcindyl
    ContributorAuthor
  5. benvanik commented on Aug 25, 2021

    @benvanik
    Collaborator

    Thanks! After #6868 lands I'm switching back to working on this and that'll be really helpful.

  6. benvanik commented on Aug 25, 2021

    @benvanik
    Collaborator

    Looking at that file, these are the splats:

      hal.constant_pool.splat @_large_const_0 = dense<0.000000e+00> : tensor<1x3x3x128xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_1 = dense<0.000000e+00> : tensor<1x4x4x64xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_2 = dense<0.000000e+00> : tensor<1x4x4x256xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_3 = dense<0.000000e+00> : tensor<1x5x5x128xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_4 = dense<0.000000e+00> : tensor<1x5x5x256xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_5 = dense<0.000000e+00> : tensor<1x7x7x128xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_6 = dense<0.000000e+00> : tensor<1x7x7x512xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_7 = dense<0.000000e+00> : tensor<1x12x12x256xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_8 = dense<0.000000e+00> : tensor<1x12x12x1280xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_9 = dense<0.000000e+00> : tensor<1x12x12x960xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_10 = dense<0.000000e+00> : tensor<1x22x22x576xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_11 = dense<0.000000e+00> : tensor<1x22x22x384xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_12 = dense<0.000000e+00> : tensor<1x42x42x192xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_13 = dense<0.000000e+00> : tensor<1x82x82x144xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_14 = dense<0.000000e+00> : tensor<1x162x162x96xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_15 = dense<0.000000e+00> : tensor<1x162x162x32xf32> attributes {sym_visibility = "nested"}
      hal.constant_pool.splat @_large_const_16 = dense<0.000000e+00> : tensor<1x322x322x3xf32> attributes {sym_visibility = "nested"}
    

    which map to 23MB (!) at runtime:

        %buffer = hal.allocator.allocate<%allocator : !hal.allocator> type("HostVisible|DeviceVisible|DeviceLocal") usage("Constant|Transfer|Mapping|Dispatch") : !hal.buffer{%c23394096}
        %cmd = hal.command_buffer.create device(%device : !hal.device) mode("OneShot|AllowInlineExecution") categories(Transfer) : !hal.command_buffer
        hal.command_buffer.begin<%cmd : !hal.command_buffer>
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c0, %c4608] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c4608, %c4096] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c8704, %c16384] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c25088, %c12800] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c37888, %c25600] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c63488, %c25088] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c88576, %c100352] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c188928, %c147456] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c336384, %c737280] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c1073664, %c552960] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c1626624, %c1115136] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c2741760, %c743424] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c3485184, %c1354752] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c4839936, %c3873024] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c8712960, %c10077696] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c18790656, %c3359232] pattern(%c0_i32 : i32)
        hal.command_buffer.fill_buffer<%cmd : !hal.command_buffer> target(%buffer : !hal.buffer)[%c22149888, %c1244208] pattern(%c0_i32 : i32)
        hal.command_buffer.end<%cmd : !hal.command_buffer>
        hal.ex.submit_and_wait %device, %cmd

    So this will be a good fix :P

  7. changed the title [-]IREE codegen allocates excessive zero-filled buffer for MobileSSD models[/-] [+]Flow/HAL allocates excessive zero-filled buffer for MobileSSD models.[/+] on Aug 26, 2021
  8. changed the title [-]Flow/HAL allocates excessive zero-filled buffer for MobileSSD models.[/-] [+]Flow/HAL+linalg allocates excessive zero-filled buffer for MobileSSD models.[/+] on Aug 26, 2021
  9. hcindyl commented on Aug 26, 2021

    @hcindyl
    ContributorAuthor

    Upload the 8-bit quantized version of the TOSA IR.
    ssd_mobilenet_v2_face_quant.tar.gz
    Note this may not pass the IREE codegen flow yet as @rsuderman is still working on some kinks w.r.t quant mobilenet.

  10. MaheshRavishankar commented on Aug 26, 2021

    @MaheshRavishankar
    Collaborator

    @rsuderman could you let me know if the change to lower conv to fill -> conv -> bias_add landed? That would tell me if I should land my fix to handle the way TOSA is/was doing (I would rather hold off on that cause there is a better way to do things at linalg fusion that I think would solve a lot of problems and this would essentially be throw away)

  11. julianwa commented on Sep 27, 2021

    @julianwa
    Member

    Pinging this thread for @hcindyl. @MaheshRavishankar are you still waiting on response from @rsuderman?

  12. 25 remaining items

  13. hcindyl commented on Oct 19, 2021

    @hcindyl
    ContributorAuthor

    @hcindyl: where'd you get the tflite quant memory usage number of 230KB? That seems physically impossible - the first conv in the model produces a 311KB tensor. There are also depthwise convs of 1x162x162x96xi8 (2.4MB) - and unless we do that in-place (is that possible?) that means we'll have a lower-bound usage of ~4.5MB. If tflite doesn't have that then either their reporting is wrong or they are doing something major that we need to replicate :P

    The 230KB refers to the original TFLite binary size, not the intermediate activate size. I don't have the number of TFLite kernel internal memory usage. I agree for the public models shared above we could be close to the lower-bound, but for the lower depth-multiplier model that we are testing, the largest intermediate tensor is 1x162x162x16xi8, whereas the memory usage is 2.8MB, so somewhere in the pipe the memory could be saved.

    Anything I can set to test memory usage with the stream branch for different models?

  14. benvanik commented on Oct 19, 2021

    @benvanik
    Collaborator

    Oh, I thought you were talking about intermediates!
    So I don't make the same mistake: the 2.8MB you mention dealing with a 420KB max tensor - is that IREE reporting it used 2.8MB on exit or a number from tflite?
    (trying to know if we need to find a way to get numbers from tflite for comparison - we use the same algorithm so any delta would be the above - would be good to know)

  15. hcindyl commented on Oct 19, 2021

    @hcindyl
    ContributorAuthor

    The 2.8MB usage is reported from iree_hal_allocator_statistics_fprint on exit, with the input .tflite size of 230K.

  16. benvanik commented on Oct 19, 2021

    @benvanik
    Collaborator

    ok - so we don't know tflite memory consumption (the size on disk doesn't directly relate to memory consumption)

  17. hcindyl commented on Oct 19, 2021

    @hcindyl
    ContributorAuthor

    Correct, we (currently) don't have the tflite memory usage stat collected.

  18. hcindyl commented on Oct 19, 2021

    @hcindyl
    ContributorAuthor

    Use https://www.tensorflow.org/lite/performance/measurement to collect the TFLite memory usage. In X86_64 the public mobilenet SSD model results in

    The input model file size (MB): 5.51052
    ...
    Note: as the benchmark tool itself affects memory footprint, the following is only APPROXIMATE to the actual memory footprint of the model at runtime. Take the information at your discretion.
    Memory footprint delta from the start of the tool (MB): init=3 overall=13.8516
    

    While our smaller mobilenet SSD model results in 3.8MB TFLite memory usage (in X86_64). So IREE's current memory usage seems to be more efficient than TFLite. :)

  19. benvanik commented on Oct 19, 2021

    @benvanik
    Collaborator

    Nice! That's really helpful! There's some future things we'll be able to do to reduce the transient memory usage more - like allowing for donation of input buffers would get us 307KB for free.

    Looking at the file sizes, the public model (ssd_mobilenet_v2_face_quant.tflite) is 5.511MB .tflite and 5.651MB .vmfb. These compress to 3.123MB and 3.030MB respectively, meaning there's still a lot of low entropy stuff in there - but that's future work. A quick check shows 206KB being the ELF which compresses to 55KB. Makes sense given we are generating at least 2x as many kernels as we need and not deduplicating nearly any of them.

    So, padding and improvements to deduping should let us close that 140KB gap - there's really only 5-6 unique dispatches but we are generating code for 118 :) There's also a decent chunk of low hanging fruit (constants that are pre-padded we could store unpadded and expand at init time, some zero padding, etc). Next year sometime it'd be interesting to play with constant compression (decoded on the fly at startup) as a storage-size vs. memory-size tradeoff.

  20. hcindyl commented on Dec 6, 2021

    @hcindyl
    ContributorAuthor

    FYI, ssd_mobilenet_v2_face_quant.tflite at snapshot 686 results in the peak memory usage of 5.93MB

    [[ iree_hal_allocator_t memory statistics ]]
      HOST_LOCAL:            0B peak /            0B allocated /            0B freed /            0B live
    DEVICE_LOCAL:      6218144B peak /      6218144B allocated /      6218144B freed /            0B live
    
  21. benvanik commented on Dec 6, 2021

    @benvanik
    Collaborator

    vs the 13.8MB of tflite? 🤞

  22. hcindyl commented on Dec 6, 2021

    @hcindyl
    ContributorAuthor

    From TFLite benchmark tool

    Graph: [mobile_ssd_v2_quant.tflite]
    #threads used for CPU inference: [1]
    Loaded model mobile_ssd_v2_quant.tflite
    The input model file size (MB): 5.51052
    Initialized session in 1.357ms.
    Running benchmark for at least 1 iterations and at least 0.5 seconds but terminate if exceeding 150 seconds.
    count=1 curr=2302523
    
    Running benchmark for at least 50 iterations and at least 1 seconds but terminate if exceeding 150 seconds.
    count=50 first=2293956 curr=2243167 min=2224464 max=2307193 avg=2.2736e+06 std=20999
    
    Inference timings in us: Init: 1357, First inference: 2302523, Warmup (avg): 2.30252e+06, Inference (avg): 2.2736e+06
    Note: as the benchmark tool itself affects memory footprint, the following is only APPROXIMATE to the actual memory footprint of the model at runtime. Take the information at your discretion.
    Memory footprint delta from the start of the tool (MB): init=2.67188 overall=14.3906
    Overall peak memory footprint (MB) via periodic monitoring: 20.4492
  23. benvanik commented on Dec 6, 2021

    @benvanik
    Collaborator

    wow, that's fantastic! and it only gets smaller from here :)

  24. MaheshRavishankar commented on Jan 21, 2022

    @MaheshRavishankar
    Collaborator

    Is this still an issue?

  25. hcindyl commented on Jan 21, 2022

    @hcindyl
    ContributorAuthor

    We have seen some regression of memory usage since #6826 (comment), but we are tracking it on #7896.

    Maybe we can close this issue?

  26. MaheshRavishankar commented on May 3, 2022

    @MaheshRavishankar
    Collaborator

    @hcindyl I am closing this bug. Please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions