You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flow/HAL+linalg allocates excessive zero-filled buffer for MobileSSD models. #6826
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.
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
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
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).
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!
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
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
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.
@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)
@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?
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)
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. :)
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.
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
Activity
MaheshRavishankar commentedon Aug 23, 2021
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
so its doing a broadcast of the bias into the full tensor which is then fed to the
outsparameter of the conv operation. This is not exactly wrong, but is not what is currently handled correctly. What is handled better isThat 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).
benvanik commentedon Aug 23, 2021
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!
hcindyl commentedon Aug 25, 2021
From https://coral.ai/models/object-detection/#trained-models. I removed the postprocessing op and save it as float so it can pass TOSA IR.
ssd_mobilenet_v2_face_float.tar.gz
benvanik commentedon Aug 25, 2021
Thanks! After #6868 lands I'm switching back to working on this and that'll be really helpful.
benvanik commentedon Aug 25, 2021
Looking at that file, these are the splats:
which map to 23MB (!) at runtime:
So this will be a good fix :P
[-]IREE codegen allocates excessive zero-filled buffer for MobileSSD models[/-][+]Flow/HAL allocates excessive zero-filled buffer for MobileSSD models.[/+][-]Flow/HAL allocates excessive zero-filled buffer for MobileSSD models.[/-][+]Flow/HAL+linalg allocates excessive zero-filled buffer for MobileSSD models.[/+]hcindyl commentedon Aug 26, 2021
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.
MaheshRavishankar commentedon Aug 26, 2021
@rsuderman could you let me know if the change to lower conv to
fill->conv->bias_addlanded? 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)julianwa commentedon Sep 27, 2021
Pinging this thread for @hcindyl. @MaheshRavishankar are you still waiting on response from @rsuderman?
25 remaining items
hcindyl commentedon Oct 19, 2021
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?
benvanik commentedon Oct 19, 2021
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)
hcindyl commentedon Oct 19, 2021
The 2.8MB usage is reported from
iree_hal_allocator_statistics_fprinton exit, with the input .tflite size of 230K.benvanik commentedon Oct 19, 2021
ok - so we don't know tflite memory consumption (the size on disk doesn't directly relate to memory consumption)
hcindyl commentedon Oct 19, 2021
Correct, we (currently) don't have the tflite memory usage stat collected.
hcindyl commentedon Oct 19, 2021
Use https://www.tensorflow.org/lite/performance/measurement to collect the TFLite memory usage. In X86_64 the public mobilenet SSD model results in
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. :)
benvanik commentedon Oct 19, 2021
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.
hcindyl commentedon Dec 6, 2021
FYI,
ssd_mobilenet_v2_face_quant.tfliteat snapshot 686 results in the peak memory usage of 5.93MBbenvanik commentedon Dec 6, 2021
vs the 13.8MB of tflite? 🤞
hcindyl commentedon Dec 6, 2021
From TFLite benchmark tool
benvanik commentedon Dec 6, 2021
wow, that's fantastic! and it only gets smaller from here :)
MaheshRavishankar commentedon Jan 21, 2022
Is this still an issue?
hcindyl commentedon Jan 21, 2022
We have seen some regression of memory usage since #6826 (comment), but we are tracking it on #7896.
Maybe we can close this issue?
MaheshRavishankar commentedon May 3, 2022
@hcindyl I am closing this bug. Please reopen if needed.