Description
To clean up the IR and reduce startup cost it'd be nice to link all VMLA executables together. This will require completing the TargetBackend linking support, which means solving how executables are selected based on backend. Right now the command buffer conversion is performing executable lookup but ideally we'd insert hal.device.switch ops based on what the linker remaps things to - or make that part of a data set provided to the target backend during recordDispatch.
We could enable this by moving the executable lookup and descriptor set management into the recordDispatch. We'd then need to rely on command buffer-specific optimizations to CSE(ish) things (like deduping two blocks that dispatch the same way, etc), which is something we want anyway. In the short term it would mean inserting duplicate work in the case of multi-device builds, but that seems acceptable as for models like resnet50 we just want to see it for a single device at a time and this would clean things up a ton.
From there, linking in the VMLA case becomes just globbing all targets/entry points together and there's no requirement that entry points have the same interfaces so we can also move all those in. There should then be a single executable with N entry points. Any symbol deduplication can happen within this module and entry points can be combined, which is nice.
This is needed for the LLVM AOT case so proving it out now with VMLA will make that significantly easier.
Activity
Move references to HAL executables inside hal.device.switch ops. (#3250)
ScottTodd commentedon Oct 6, 2020
One of the details to figure out here is how to support multiple interfaces within a single executable, so we can link across executables with different interfaces. (If we actually need to do that - the savings seem worth it?)
I think this relates to
We currently declare a single
hal.interfaceop per executable (independent of the number of target backends in use): https://github.com/google/iree/blob/73b3e1e9e9897982c6d7e4f1172473086c7931b6/iree/compiler/Dialect/HAL/Transforms/MaterializeInterfaces.cpp#L370-L376I think that can stay, since the mapping from
Flow::ExecutableOp->HAL::ExecutableOpis straightforward.During other IR transformations, I was thinking we could support multiple interfaces by listing them side-by-side and using entry points to associate them with translated functions:
However, a few steps later, after linking will happen, we create
ExecutableCachePrepareOps that require a "layout", derived from the singleinterfaceOp: https://github.com/google/iree/blob/73b3e1e9e9897982c6d7e4f1172473086c7931b6/iree/compiler/Dialect/HAL/Transforms/MaterializeResourceCaches.cpp#L234-L247that goes all the way down to the runtime HAL: https://github.com/google/iree/blob/ef7f00260e0c4f5f1edda1832a9cd915be0060b3/iree/hal/api.cc#L1972-L1974
though the layout is only used by the Vulkan backend: https://github.com/google/iree/blob/ef7f00260e0c4f5f1edda1832a9cd915be0060b3/iree/hal/vulkan/pipeline_cache.cc#L52-L59
so now I'm wondering:
ScottTodd commentedon Oct 6, 2020
Another option could be to enable linking between executables with different interfaces in VMLA and LLVM backends, but not in Vulkan. It isn't immediately clear to me though how we'd implement that cleanly:
ExecutableCache::PrepareExecutablestill take anExecutableLayoutparameter?VulkanSPIRVTarget's implementation ofTargetBackend::linkExecutablesthat only links between compatible executables would work as a start, but relaxing restrictions onHAL::ExecutableOpand other related changes may have some ripple effects.ScottTodd commentedon Oct 6, 2020
Discussed with @benvanik and there's a hack we can use:
ExecutableOp::getInterfaceOptoExecutableOp::getFirstInterfaceOpIt's a sharp edge, and continuing gap between GPU and CPU backends, but it will unblock linking on CPU backends.
Longer term, we can rework how executables are cached (to batch them and make them async). The detail to consider here for Vulkan is that we create a vkpipeline, which is shader + pipeline state + pipeline layout + entry point (see VkComputePipelineCreateInfo?), unlike metal and d3d which don't require the entry point/layout to be tied together in the cache.
antiagainst commentedon Oct 7, 2020
Question: for Vulkan/SPIR-V side, shouldn't we merge all
hal.interfaceops into one by reassigning the set/binding numbers? This will make sure we have one pipeline layout for the whole combined module and we just need to create one pipeline object.benvanik commentedon Oct 7, 2020
we can do that to an extent, but we wouldn't want to pass 800 shaders as a single pipeline (I don't think, anyway) - so we could link together executables with similar interfaces or lifetime, but likely not the same as with llvm/vmla where everything can be in a single executable.
interfaces -> pipeline layouts are already deduped at runtime
antiagainst commentedon Oct 7, 2020
You mean as a single shader module? For a compute pipeline I think we just specify one entry point from the shader module, but can certainly use a pipeline layout that has binding slots for an aggregation of entry points.
I'm asking because we have @KareemErgawy helping us right now to implement a SPIR-V combiner, so we will see SPIR-V "linking" utilities in MLIR working soon. :)
benvanik commentedon Oct 7, 2020
You need one pipeline per entry point and that's where all the work happens. So one shader module with 100 entry points means creating 100 pipelines, and as such the benefits of keeping things in the same shader module are trickier to balance. If the SPIR-V combiner can get us code deduplication (where the size of one shader module of 100 entry points is less than the size of 100 individual shader modules with one entry point) then it's worth it. That'll mean outlining/sharing blocks/etc.
antiagainst commentedon Oct 8, 2020
Yup, certainly. There is no way to trim down the number of pipeline objects. My previous comment is more on that by combining all the shader modules into one and remap the descriptor set/binding numbers, we can reduce the number of descriptor set and pipeline layout objects. But I understand there is a balance to dance as we can see small number of maxDescriptorSetStorageBuffers.
That's the goal! :) But it may take some time to come.
Implement executable linking in VMLA. (#3283)
ScottTodd commentedon Oct 13, 2020
Linking in VMLA is "done", with some room for further cleanup and optimizations. I'll close this and we can follow up in other issues.
I'll look at #3137 next, and also update this comment: https://github.com/google/iree/blob/0a225f92503f493eba5c1c93aa153a2b44d5ed1b/iree/compiler/Dialect/HAL/Target/TargetBackend.h#L283-L289