Skip to content

Improve hal.device to model device archetypes and propagate compile-time information #3148

Open

Description

@benvanik

Initial sketch, the implementation of which will come in stages. Overall this is trying to solve for composition in multi-device and multi-target modes as well as plan for how devices are enumerated/selected and how we effectively deduplicate/fold code to reduce program complexity.

Requirements:

  • enable multiple devices in use at the same time
  • enable each device to be a different device type or have different properties
  • enable dce/cse/folding to reduce binary size and runtime overhead
  • enable both automatic and manual enumeration/selection of devices
  • enable multi-targeting of executables for different device types independent of the number/mix of devices used

The rough idea is to setup an environment that specifies which/how many devices are available, constrain those devices, and define targets for codegen lowering. By doing this in IR we get better inspectability but most importantly we allow all of the above in a way that is fully integrated with the MLIR pipeline: no need for extremely complex side-channel options and instead end up with normal ops with canonicalizers/cse/dce/dfa/etc.


Starting at the top: the user specifies one or more target execution environments defining device availability requirements. The count and mix of devices is load-bearing to the scheduling work done during flow->hal lowering so we make that explicit here:

// The `@embedded` environment has a single device available named `@dev_local_cpu`:
hal.environment @embedded = [@dev_local_cpu]
// The `@desktop_gpu` environment has a single GPU available:
hal.environment @desktop_gpu = [@dev_gpu_0]
// The `@big_server_multi_gpu` environment has several GPUs available:
hal.environment @big_server_multi_gpu = [@dev_gpu_0, @dev_gpu_1, @dev_gpu_2, @dev_gpu_3]
// The `@hybrid_cpu_gpu` environment mixes device types:
hal.environment @hybrid_cpu_gpu = [@dev_local_cpu, @dev_gpu_0]

By default the environment would be selected automatically based on device availability/compatibility, but we could extend this to support custom logic for determining whether an environment matches or not:

hal.environment @modern_vulkan = [@dev_gpu] {
  func @select(%dev_gpu : !hal.device) -> index {
    // Do device enumeration/queries/etc to decide whether to use this env.
    // We can use priorities to allow for a ranked selection across environments/fallbacks/etc.
    // It's just code, and the kind of code one would normally write by hand when trying to select devices.
    return %priority : index
  }
}

The devices specified represent instances and (at this level) are like global variables containing a device handle:

// A CPU device instance with some properties:
hal.device @dev_local_cpu : !hal.device targets = [@cpu_aarch64] {
  // Known statically based on the device archetype:
  hal.device.property "hal.device.type" = #hal.device.type<"cpu">
  hal.device.property "hal.device.memory_model" = #hal.device.memory_model<"unified">
  // Not known statically, so this would end up as a runtime query:
  hal.device.property "hal.buffer.max_allocation_size" : index
  // But we can hint for things we know hold in certain configurations so that
  // we can propagate that information as required:
  hal.device.property "hal.cpu.worker_count" = #hal.device.limit<1..4>
}
...
// Devices are dereferenced as needed:
%dev = hal.device.load @dev_local_cpu : !hal.device
// Code can be generic to what kind of device it takes, allowing for reuse
// across device types/environments:
%1 = call @do_some_work(%dev, %0) : (!hal.device, tensor...) -> tensor...
...
// Properties can be dynamically queried from the device:
%t = hal.device.query<%dev : !hal.device> key("hal.device.memory_model") -> index
// Which, DFA+constant propagation can elide when possible:
%t = constant 0 : index

As with environments the selection/enumeration is performed based on the specified device properties (like device type), but we can also add custom code to do it too (for allow/denylists, etc):

hal.device @vulkan_dev_we_like : !hal.device targets = [@spv_with_fancy_exts] {
  func @select(%info : !hal.device.info) -> index {
    // Query device information before selecting and creating the device.
    return %priority : index
  }
}

Finally, we have device targets that specify the parameters needed to target executables for the device. These may be reused by many devices:

hal.device.target @cpu_aarch64 attributes {
  // Lowering information for codegen/etc (HAL_LoweringConfigAttr).
  ...
} {
  // Can have properties used to select the target at runtime.
  // As with the environment/device a custom select() function could be used
  // too (querying compatible subarchitectures/etc).
  hal.device.property "hal.cpu.arch" = #hal.cpu.arch<"aarch64">
}

All together this lets us say "I want to run in an environment with these devices available, each of which has these properties, and use this information to target them". We also have very fine control over device selection by allowing it to be either very general (good for portability) or very specific (good for complex configurations/predictability in constrained environments).

Some examples:

hal.device.target @target_aarch64 ...
  hal.device.property "hal.cpu.arch" = #hal.cpu.arch<"aarch64">
}
hal.device.target @target_x86_64 ...
hal.device.target @target_vmvx ...
hal.environment @local_cpu = [@local_dev]
hal.device @local_dev targets = [@target_aarch64, @target_x86_64, @target_vmvx] {
  hal.device.property "hal.device.type" = #hal.device.type<"cpu">
  hal.device.property "hal.device.memory_model" = #hal.device.memory_model<"unified">
}
// Has cooperative matrix support:
hal.device.target @target_vkspv_cooperative_matrix {
  hal.device.property "hal.device.type" = #hal.device.type<"vulkan">
  hal.device.property "hal.vulkan.cooperative_matrix" = true : i1
}
// Fallback:
hal.device.target @target_vkspv {
  hal.device.property "hal.device.type" = #hal.device.type<"vulkan">
}
hal.environment @multi_gpu = [@gpu0, @gpu1]
hal.device @gpu0 targets=[@target_vkspv_cooperative_matrix, @target_vkspv] {
  hal.device.property "hal.device.memory_model" = #hal.device.memory_model<"discrete">
}

Since this is just IR and each of the ops has a region we can mix in device/backend-specific ops as well. We could have a hal.device.extension op that indicates the presence (optional or required) of a Vulkan extension, etc. The more we can avoid there today the better but I do like that we have that option - if not going directly to the escape hatch of custom select() functions. There's also opportunities here for dynamic environment switching: how one transitions between an environment with one set of devices and another can be automated, such as pulling buffers off one device when it is being disabled on an environment switch but another device common between environments is still active.

When performing scheduling/setting up executable translation we have all the information we need and afterward can lower these constructs down to normal IR. For example, a hal.device becomes a hal.variable, each hal.device.property becomes a hal.variable with an initializer that issues a runtime hal.device.query (if needed), the environment/device selection logic becomes just a func that calls out to each selection function/ranks/performs the initialization, etc. All the rest of our infra for deduping variables, CSE'ing variable loads, propagating constant variables, etc just works. By the time we are lowering out of the HAL dialect and into VM all of this melts away into some runtime queries (if required) and variable loads/stores.

Producing the IR for this (the hal.environment/etc) is left to the higher levels of the stack. Having some more generic configuration facing up that is then lowered into this ops will let us vary this over time, and to start we are just going to be taking the command line flags we already have and turning them into this such that no flags are used in the rest of the HAL pipeline. Extending this beyond our current flags would involve a flag that specifies one or more environment description .mlir files with this IR, which could then also have a yaml representation if we wanted something more loose that gets converted to the IR/verified/etc. I do think there's some real power in being able to define code for some of these things in the more complex scenarios, though, and IR will be the best route for people doing crazy things. It doesn't preclude us from having some builtin environments with friendly names and command line flags, only gives us a good layer to build the rest of the compiler infra on.


My rough plan of attack is to start with hal.device and finally remove hal.ex.shared_device. From there, adding hal.device.target to replace the current TargetBackend::declareTargetOps C++ will enable us to start multi-targeting. Later on as needed multiple devices can come in (this requires larger reworks to streams for scheduling and has to be ordered after that) and finally multiple environments can be layered on top of that. There's lots of little details to work through and I think this ordering and composition makes it possible to make progress incrementally. Hopefully: I've been spinning on this for a few weeks :)

I'm still debating about the shape of hal.device.property. It could be turned into attributes instead, however attributes are much more difficult to modify during transformation. We want to be able to perform transformations like "all targets of this device have the same value for this property, so hoist it into the device itself" or "all devices in all environments have this same property so hoist it to the top level module". Aggressively hoisting/folding/etc early will allow us to emit much less IR during scheduling so that we'd not have to just hope we could clean it up later. The property ops could be symbolic, which would get us some benefits but may fight against other systems (we need the properties to setup enumeration even if they aren't referenced anywhere else in the module, etc).

Activity

  1. added
    enhancement ➕
    compiler/dialects
    on Sep 15, 2020
  2. added this to the milestone on Sep 15, 2020
  3. self-assigned this
    on Sep 15, 2020
  4. removed this from the milestone on Nov 22, 2020
  5. moved this from Not Started to Backlog in (Deprecated) IREEon Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compiler/dialectsenhancement ➕

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions