Skip to content

Implement VMLA linking and the rest of the TargetBackend linker interface #1587

Closed

Description

@benvanik

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

  1. added
    enhancement ➕
    compiler/dialects
    performance ⚡
    on Apr 21, 2020
  2. added this to the milestone on Apr 21, 2020
  3. self-assigned this
    on Apr 21, 2020
  4. modified the milestones: , on May 12, 2020
  5. removed their assignment
    on May 15, 2020
  6. modified the milestones: , on Oct 1, 2020
  7. ScottTodd commented on Oct 6, 2020

    @ScottTodd
    Member

    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 could enable this by moving the executable lookup and descriptor set management into the recordDispatch.


    We currently declare a single hal.interface op 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-L376

    I think that can stay, since the mapping from Flow::ExecutableOp -> HAL::ExecutableOp is 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:

      hal.executable @main_ex_dispatch_0 attributes {sym_visibility = "private"} {
        hal.interface @legacy_io_0 {
          hal.interface.binding @arg0, set=0, binding=0, type="StorageBuffer", access="Read"
          hal.interface.binding @arg1, set=0, binding=1, type="StorageBuffer", access="Read"
          hal.interface.binding @ret0, set=0, binding=2, type="StorageBuffer", access="Write|Discard"
        }
        hal.interface @legacy_io_1 {
          hal.interface.binding @arg0, set=0, binding=0, type="StorageBuffer", access="Read"
          hal.interface.binding @ret0, set=0, binding=1, type="StorageBuffer", access="Write|Discard"
        }
        hal.executable.target @vmla, filter="vmla" {
          hal.executable.entry_point @main_ex_dispatch_0 attributes {interface = @legacy_io_0, ordinal = 0 : i32, signature = (tensor<1x1xf32>, tensor<1x1xf32>) -> tensor<1x1xf32>}
          hal.executable.entry_point @main_ex_dispatch_1 attributes {interface = @legacy_io_1, ordinal = 1 : i32, signature = (tensor<1x1xf32>) -> tensor<1x1xf32>}
          ...

    However, a few steps later, after linking will happen, we create ExecutableCachePrepareOps that require a "layout", derived from the single interfaceOp: https://github.com/google/iree/blob/73b3e1e9e9897982c6d7e4f1172473086c7931b6/iree/compiler/Dialect/HAL/Transforms/MaterializeResourceCaches.cpp#L234-L247

    that 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:

    1. Does it still make sense to link executables with different interfaces together?
    2. If so, what changes should be made to the HAL (particularly the Vulkan HAL)?
  8. ScottTodd commented on Oct 6, 2020

    @ScottTodd
    Member

    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:

  9. ScottTodd commented on Oct 6, 2020

    @ScottTodd
    Member

    Discussed with @benvanik and there's a hack we can use:

    • Change ExecutableOp::getInterfaceOp to ExecutableOp::getFirstInterfaceOp
    • In the VulkanSPIRV target and Vulkan HAL, continue using a single interface per executable and continue to pass the layout down to executable caching / pipeline creation
    • In other targets / HALs, support multiple interfaces per executable, and be sure to not introduce a dependency on the layout during executable caching

    It'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.

  10. antiagainst commented on Oct 7, 2020

    @antiagainst
    Member

    Question: for Vulkan/SPIR-V side, shouldn't we merge all hal.interface ops 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.

  11. benvanik commented on Oct 7, 2020

    @benvanik
    CollaboratorAuthor

    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

  12. antiagainst commented on Oct 7, 2020

    @antiagainst
    Member

    pass 800 shaders as a single pipeline

    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. :)

  13. benvanik commented on Oct 7, 2020

    @benvanik
    CollaboratorAuthor

    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.

  14. antiagainst commented on Oct 8, 2020

    @antiagainst
    Member

    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.

    If the SPIR-V combiner can get us code deduplication

    That's the goal! :) But it may take some time to come.

  15. ScottTodd commented on Oct 13, 2020

    @ScottTodd
    Member

    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

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

Metadata

Metadata

Assignees

Labels

compiler/dialectsenhancement ➕performance ⚡

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions