Skip to content

Support specialization constants on the HAL API and replace usage of flatbuffers with them. #5662

Closed

Description

@benvanik

Currently CUDA and Metal are both side-channeling their workgroup sizes through flatbuffers:
https://github.com/google/iree/blob/c8cd9b935b11d9c6761727b0af94af17c02660a8/iree/schemas/cuda_executable_def.fbs#L33-L37

Vulkan has specialization constants side-channeled in its flatbuffer:
https://github.com/google/iree/blob/6ebd1b0c5274b5ed5a9b6b1dc44e5a7476341845/iree/schemas/spirv_executable_def.fbs#L21-L43

Instead we can add specialization constants to the HAL API such that the compiler can send down anything it wants. Special values for workgroup size/threadgroup size/block size can satisfy that use case.

Entry point names - the other thing in the flatbuffers - can be replaced with deterministic values (entry point 0 = "entry_0") such that we don't need to side-channel them.

Activity

  1. added
    hal/vulkan
    hal/api
    hal/cuda
    on Apr 28, 2021
  2. self-assigned this
    on Apr 28, 2021
  3. benvanik commented on Mar 2, 2022

    @benvanik
    CollaboratorAuthor

    #8436 adds the plumbing on the CPU side for executables to receive spec constants. The next step is to plumb the constants through the iree_hal_executable_spec_t and up through the HAL module into the compiler.

    The constants are per-executable matching per-pipeline in underlying APIs (and what we have today with the CUDA block sizes/SPIR-V flatbuffer). It's valid for a module to create multiple of the same executable with differing spec constants though we want to avoid that for resource reasons. Executables across backends don't need to agree on the constants; this allows us to plumb through block sizes in CUDA independent from local workgroup sizes in SPIR-V and whatever else we decide to add. Note that the constants are per-executable and that in some implementations like SPIR-V there may be multiple shader modules per executable - today we aren't doing this properly but that also needs to be fixed in #7824.

    Next steps:

    • Add constants to executable spec & plumb through to HAL impls
    • Add constants to hal.executable.create in IR and HAL VM module
    • Add hal.executable.* ops and optimization for defining constants
    • Generate VMVX __set_constants helper
    • Replace SPIR-V flatbuffer usage (+ TargetBackend plumbing)
    • Replace CUDA flatbuffer usage (+ TargetBackend plumbing)
  4. antiagainst commented on Mar 2, 2022

    @antiagainst
    Member

    Related to device characteristics query, here is the list of features/capabilities/limits we found useful for Vulkan side:

    https://github.com/google/iree/blob/main/iree/compiler/Dialect/Vulkan/IR/VulkanAttributes.td

    The GPU vendor and product ID are useful too.

  5. moved this from TODO to Active in @benvanik 👻on Mar 5, 2022
  6. benvanik commented on Apr 18, 2022

    @benvanik
    CollaboratorAuthor

    Planning to add something like:

    hal.executable.variant public @foo {
      hal.executable.initializer(%device: !hal.device) -> (i32, ...) {
        %cst0 = ... // <<some kind of query or logic>>
        hal.return %cst0, ... : i32, ...
      }
      builtin.module {
        func.func @dispatch0() {
          %0 = hal.executable.constant.load[0] : i32
          func.return
        }
      }
    }

    It's up to the target backend to add the initializer with as many constants as it wants and load them. When materializing the host-side executable resources the initializer will be inlined and evaluated to pass the values to hal.executable.create which gets passed to the runtime. Since the initializer is per variant each target can have different constants.

  7. benvanik commented on Jun 23, 2022

    @benvanik
    CollaboratorAuthor

    I have an implementation mostly done of this but unfortunately ran into a hairy issue with linking: because we perform linking after translation we lose the ability to rebase the constant ordinals when merging modules in a generic way. Linking needs to be reworked for things like #7824 anyway and maybe this finally causes it.

    Thought would be to have a TargetBacked::buildLinkingPassPipeline to replace the current linkExecutables call, and each backend can have its own pass pipeline (as it does its own translation pipeline) to perform the linking. This way we could have translation leave things symbolic in whatever backend-specific form things need to be (as it'll be LLVM dialect, SPIR-V dialect, etc) and during linking do fixups before finally combining things. A default pass that does this could be provided for targets that are ok with some out-of-dialect ops remaining in them after translation (hal.executable.constant.lookup or something that returns an i32 that's later resolved). It's effectively like declaring externs that are resolved during linking.

    A complication would be dialects that need the constant ordinals as attributes and such (thinking SPIR-V specialization constants). In those cases the backend would have to perform the rebasing with awareness of the ops that need to be updated. Most (LLVMCPU/LLVMGPU/VMVX) are probably ok but the higher-level targets are going to need special cases.

    So before this can land I need to:

    • Add the buildLinkingPassPipeline method and move the existing linking setup to use it
    • Add a placeholder op for extern constant references
    • Add generic passes that do module merging (what mergeModuleInto does today) and constant rebasing based on the placeholder ops
    • Leave SPIRV/etc TBD, as they aren't linking yet anyway

    This will also improve testability of linking which today is woefully undertested.

  8. benvanik commented on Jun 30, 2022

    @benvanik
    CollaboratorAuthor

    This just won't work :/
    Needs a redesign.

  9. benvanik commented on Oct 24, 2022

    @benvanik
    CollaboratorAuthor

    Landed in #10607 (CPU/VMVX only).

  10. Repository owner moved this from Active to Done in @benvanik 👻on Oct 24, 2022
  11. moved this to Done in IREEon Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

cleanup 🧹hal/apihal/cudahal/vulkan

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions