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
resnet_fake_weights.mlir doesn't work in Linalg on tensors path #5152
Do you have an IR dump? a single dispatch taking 37 buffers is not going to have a good time (we are limited to 8 on some android devices). This sounds like a pathological case.
Looks like constant loads may be the major source - which is good news as the fix is relatively easy now (it wasn't before due to the old codegen lowerings using iree.placeholder and such instead of the HAL interfaces). The root issue here is that there are 400 some constants of size 256 to 2048 (and then a bunch larger) and many are used in each dispatch region. Today those constants each end up as an individual binding even though they share the same underlying storage thanks to constant pooling. MaterializeInterfaces needs to be updated to use the same binding for all constants sharing storage with an offset being used to load each packed constant.
The thing to look for is that there are a ton of hal.constant.subspan ops getting each individual constant and each one is then being passed in to the dispatches individually. The HAL constant pooling logic is going through all the effort to combine them all into a single buffer and then not using that :P
// one single buffer to store all the constants, cool!hal.variable@_const_pool_splatsinit(@_const_pool_splats_initializer) : !hal.bufferattributes {sym_visibility = "private"}
I can look at updating MaterializeInterfaces to make use of this to reuse the constant binding (this is part of the @legacy_io cleanup being done as part of allocation anyway).
One thing this highlights, though, is that this test file is not a very useful approximation of resnet inference: a real resnet has all these 256-2048b constants inlined and constant folded/propagated, whereas our test file here marks every single one as non-optimizable. This is much more of a worst-case training example where every single variable can change every single invocation. We should update the model to only have the large weights be non-optimizable (2048x1000xf32, 3x3x512x512xf32, etc) while all the usually-0 or 1 splats for masking should be allowed to fold. That may be the immediate workaround, and would be a big improvement to the applicability of this test.
(the training case is still very interesting, and buffer allocation will help with that as just like we can share constant storage to reduce the binding count we'll be sharing transient buffers to reduce the binding count)
(the small constant inlining @MaheshRavishankar just added/is adding would also help here for those constants that could not be folded earlier on, but the do-no-optimize bits on these in the test model would prevent that from working too)
These changes have some nontrivial interactions with the legacy code paths using iree.placeholder that are tricky to work around so I'm not going to rush until we are closer to the cut over to linalg-on-tensors.
I'll be working on this as part of the new hal.stream set of ops which are coming to support allocation: hal.constant.subspan will get pulled into the hal.streams, and then hal.interface materialization will make use of them to plan the buffer bindings. There's some bits here that are required for allocation anyway, such as having materialize interfaces inspect all dispatch sites of each entry point to find the compatible bindings (some sites may not be using constants, etc).
I was hoping that the non-splat constants being inlined will help a bit here. If not, we need to add a mechanism here to control fusion. Right now the elementwise operations fuse everything it can, but that has a cost.
One way to control this is to have a callback function to the elementwise operation fusion pattern, that once it has decided that a producer can be fused with the consumer will invoke a callback to check if it should go ahead with the fusion. Here we could bail if we hit some limits. The mechanism goes into core. The actual callback will live in IREE. I can file an issue for tracking this.
Any takers?
I think it depends on how soon we want to start tracking/optimizing the performance in Linalg on tensors. I'd like to address other issues first, so Ben can start to work on this issue.
If this support is what we really want in the new path, we can prioritize it.
Seems like this is being hit on MobileBert too. Maybe just increasing the limit to 512 from 256 might help? Though probably better to add a flag to control the limit.
Good thinking - I'd give that a shot (upping limit) - could reasonably be much larger as a workaround and we can lower it later. It'll still be defeated if the model itself is preventing the inlining (unfoldable_constants, noinline). The bert checked into e2e/models/ has noinline on every single variable (128xf32 etc) - we should remove it from all but the larger ones
@benvanik this seems to be a blocker. I can change the fusion upstream to provide a way to not fuse as much. Its something that will probably help upstream anyway, so I can take that path. Question though is how does this limit come about?
Fixing things so that all constants come from the same binding will help a lot - then there's still a potential issue if there are too many unique transient inputs but that will be less likely to get hit (hopefully). The transient buffer allocation that I'll be adding causes a large number of those to end up in the same storage buffer (and thus the same binding), so long as they are actual transients and not variables/inputs to the program/etc.
We'll still likely want a way to limit the fusion - but maybe with the constant and transient stuff worked out it'll be much rarer and we can deal with it then.
Sounds good. In the meantime, I will add upstream mechanism to try to control when fusion happens. It is something that was always needed, but didnt have a motivating example. Now I do.
Activity
hanhanW commentedon Mar 18, 2021
cc @antiagainst @ScottTodd @MaheshRavishankar
The reason could be that we use too many descriptors(?) during codegen?
benvanik commentedon Mar 18, 2021
Do you have an IR dump? a single dispatch taking 37 buffers is not going to have a good time (we are limited to 8 on some android devices). This sounds like a pathological case.
hanhanW commentedon Mar 18, 2021
The whole log file is too big, I have the IR after OrdinalAllocationPass https://gist.github.com/hanhanW/5c6badfa299ec757fa3e38f45aad753c
The whole log file can be downloaded from https://drive.google.com/file/d/1Tb8_ANEpS3NFFDjRN9bEnosabBHpgeNL/view?usp=sharing
benvanik commentedon Mar 18, 2021
Looks like constant loads may be the major source - which is good news as the fix is relatively easy now (it wasn't before due to the old codegen lowerings using iree.placeholder and such instead of the HAL interfaces). The root issue here is that there are 400 some constants of size 256 to 2048 (and then a bunch larger) and many are used in each dispatch region. Today those constants each end up as an individual binding even though they share the same underlying storage thanks to constant pooling. MaterializeInterfaces needs to be updated to use the same binding for all constants sharing storage with an offset being used to load each packed constant.
The thing to look for is that there are a ton of
hal.constant.subspanops getting each individual constant and each one is then being passed in to the dispatches individually. The HAL constant pooling logic is going through all the effort to combine them all into a single buffer and then not using that :PI can look at updating MaterializeInterfaces to make use of this to reuse the constant binding (this is part of the
@legacy_iocleanup being done as part of allocation anyway).One thing this highlights, though, is that this test file is not a very useful approximation of resnet inference: a real resnet has all these 256-2048b constants inlined and constant folded/propagated, whereas our test file here marks every single one as non-optimizable. This is much more of a worst-case training example where every single variable can change every single invocation. We should update the model to only have the large weights be non-optimizable (
2048x1000xf32,3x3x512x512xf32, etc) while all the usually-0 or 1 splats for masking should be allowed to fold. That may be the immediate workaround, and would be a big improvement to the applicability of this test.(the training case is still very interesting, and buffer allocation will help with that as just like we can share constant storage to reduce the binding count we'll be sharing transient buffers to reduce the binding count)
benvanik commentedon Mar 18, 2021
(the small constant inlining @MaheshRavishankar just added/is adding would also help here for those constants that could not be folded earlier on, but the do-no-optimize bits on these in the test model would prevent that from working too)
benvanik commentedon Mar 19, 2021
These changes have some nontrivial interactions with the legacy code paths using iree.placeholder that are tricky to work around so I'm not going to rush until we are closer to the cut over to linalg-on-tensors.
I'll be working on this as part of the new
hal.streamset of ops which are coming to support allocation:hal.constant.subspanwill get pulled into thehal.streams, and thenhal.interfacematerialization will make use of them to plan the buffer bindings. There's some bits here that are required for allocation anyway, such as having materialize interfaces inspect all dispatch sites of each entry point to find the compatible bindings (some sites may not be using constants, etc).hanhanW commentedon Mar 23, 2021
@MaheshRavishankar Let's move the discussion to here instead of #5191
I think it depends on how soon we want to start tracking/optimizing the performance in Linalg on tensors. I'd like to address other issues first, so Ben can start to work on this issue.
If this support is what we really want in the new path, we can prioritize it.
MaheshRavishankar commentedon Mar 23, 2021
Seems like this is being hit on MobileBert too. Maybe just increasing the limit to 512 from 256 might help? Though probably better to add a flag to control the limit.
benvanik commentedon Mar 23, 2021
Good thinking - I'd give that a shot (upping limit) - could reasonably be much larger as a workaround and we can lower it later. It'll still be defeated if the model itself is preventing the inlining (unfoldable_constants, noinline). The bert checked into e2e/models/ has
noinlineon every single variable (128xf32 etc) - we should remove it from all but the larger onesMaheshRavishankar commentedon Mar 23, 2021
Ah, we should remove that first. @ThomasRaoux was looking at adding the flag, but maybe worth just changing the model and see if that helps?
Actually make MobileBERT run in the test.
Actually make MobileBERT run in the test.
MaheshRavishankar commentedon Mar 31, 2021
@benvanik this seems to be a blocker. I can change the fusion upstream to provide a way to not fuse as much. Its something that will probably help upstream anyway, so I can take that path. Question though is how does this limit come about?
benvanik commentedon Mar 31, 2021
Argh. I'll take a look after all my meetings today and see if I can get the constant storage improvement in.
The limit is from https://vulkan.gpuinfo.org/displaydevicelimit.php?name=maxDescriptorSetStorageBuffers&platform=android
You'll note that it's currently higher than 24 - that was because we raised it once before already :)
Fixing things so that all constants come from the same binding will help a lot - then there's still a potential issue if there are too many unique transient inputs but that will be less likely to get hit (hopefully). The transient buffer allocation that I'll be adding causes a large number of those to end up in the same storage buffer (and thus the same binding), so long as they are actual transients and not variables/inputs to the program/etc.
We'll still likely want a way to limit the fusion - but maybe with the constant and transient stuff worked out it'll be much rarer and we can deal with it then.
benvanik commentedon Mar 31, 2021
(oh, and the real limit is https://vulkan.gpuinfo.org/displaydevicelimit.php?name=maxPerStageDescriptorStorageBuffers&platform=android, but we don't care about those 30% of devices that only support 4 bindings - no useful compute can be done with that, so 24 is what I consider a somewhat reasonable limit)
Actually make MobileBERT run in the test. (#5264)
MaheshRavishankar commentedon Mar 31, 2021
Sounds good. In the meantime, I will add upstream mechanism to try to control when fusion happens. It is something that was always needed, but didnt have a motivating example. Now I do.
MaheshRavishankar commentedon May 4, 2021
This doesnt seem to be an issue anymore. Closing