Skip to content

Adding support for #hal.device.optimal<...> through to runtime. - #20879

Merged
benvanik merged 3 commits into
mainfrom
users/benvanik/allocator-select
May 22, 2025
Merged

benvanik merged 3 commits into
mainfrom
users/benvanik/allocator-select

Conversation

@benvanik

Copy link
Copy Markdown
Collaborator

This is an experimental approach for representing a set of device affinities that are able to be resolved at runtime. Currently this is limited to allocation-related ops but may be extended in the future to allow for runtime-provided device rankings based on benchmarked performance.

The new #hal.device.optimal<...> affinity attribute represents a set of affinities that an operation is able to execute with. The expectation is that the exact affinity can be resolved during compilation if sufficient information is available (TBD) or at runtime based on a user-provided policy (iree_hal_module_device_policy_t). As with other treatments of device topology in the runtime this policy is left to the hosting framework or application to decide how they want to configure their overall topology. The common IREE command line tooling adds a --device_lead_allocator=N flag to let the user denote which device ordinal (of --device= flags) is to be chosen when a #hal.device.optimal request makes it all the way to runtime. For simple cases of CPU+accelerator this allows for hack-free ways to indicate the lead allocator that is responsible for any buffer allocation that is used on multiple devices.

The #hal.device.optimal<...> attr lowers to a hal.allocator.select op/runtime call that takes a list of devices/queue masks along with the memory types and buffer usage of the request and returns the selected device/queue mask. Size is not included such that the selection can be memoized across an entire program at initialization time. A pass is added that runs at the same time as device query memoization to hoist the selection into globals. Future changes can enhance the pass to fold the selection when sufficient information has been provided.

Future work on #20855 will result in affinity assignment on resource allocations lowering into this new #hal.device.optimal<...> attr and deallocations switching to either use the recently added origin attr or the same optimal attr (given that it should be consistent and provides more information). Future work on #20856 will ensure that allocations that may be used on multiple devices (regardless of whether runtime-selected) get the appropriate bits set but that may require #20854.

Progress on #20851.
Progress on #20855 (analysis will lower into this attr).
Progress on #20856 (will be used to set bits).
Fixes #20857.

@benvanik
benvanik force-pushed the users/benvanik/allocator-select branch from 2741108 to 934fdff Compare May 21, 2025 21:10
I don't like having the two ops but they're an implementation detail
that can be fixed in the future without changes above/below.

Progress on #20857.
@benvanik
benvanik force-pushed the users/benvanik/allocator-select branch from 934fdff to f43d9cb Compare May 21, 2025 21:40
@benvanik
benvanik marked this pull request as ready for review May 22, 2025 00:07
@benvanik
benvanik requested a review from stellaraccident as a code owner May 22, 2025 00:07
benvanik added a commit that referenced this pull request May 22, 2025
This will be used to lower into the new `#hal.device.optimal` attr
added in #20879.

Progress on #20855.

@ScottTodd ScottTodd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As more multi-device (especially heterogeneous) code lands, we should think more about integration tests. Unit test coverage via lit tests is great, but I'd like to see some tests showing what the user-observable behavior is.

Comment thread runtime/src/iree/tooling/context_util.c Resolved
Comment thread runtime/src/iree/modules/hal/module.c Resolved
benvanik added a commit that referenced this pull request May 22, 2025
This will be used to lower into the new `#hal.device.optimal` attr
added in #20879.

Progress on #20855.
benvanik added a commit that referenced this pull request May 22, 2025
This will be used to lower into the new `#hal.device.optimal` attr
added in #20879.

Progress on #20855.
The `--device_lead_allocator=` flag can be used in tooling to specify
a device that should be preferred as the allocator when a selection is
requested. If the device isn't in the list the first will be returned.
For simple cases of CPU + some accelerator this should work and more
complex situations will need something more designed.

Fixes #20857.
@benvanik

Copy link
Copy Markdown
Collaborator Author

With whatever other non-CPU target we get working as part of #20851 we'll ensure there are e2e tests for it that uses the new flag (they'll all require the flag to work at all, so they'll be easy to spot if it breaks).

@benvanik
benvanik force-pushed the users/benvanik/allocator-select branch from f43d9cb to bbfc44f Compare May 22, 2025 21:21
@benvanik
benvanik enabled auto-merge (squash) May 22, 2025 21:21
benvanik added a commit that referenced this pull request May 22, 2025
This will be used to lower into the new `#hal.device.optimal` attr added
in #20879.

Progress on #20855 (still needs usage, this is just the analysis).
Progress on #20851.
@benvanik
benvanik merged commit eec9316 into main May 22, 2025
@benvanik
benvanik deleted the users/benvanik/allocator-select branch May 22, 2025 21:57
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This will be used to lower into the new `#hal.device.optimal` attr added
in #20879.

Progress on #20855 (still needs usage, this is just the analysis).
Progress on #20851.
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…0879)

This is an experimental approach for representing a set of device
affinities that are able to be resolved at runtime. Currently this is
limited to allocation-related ops but may be extended in the future to
allow for runtime-provided device rankings based on benchmarked
performance.

The new `#hal.device.optimal<...>` affinity attribute represents a set
of affinities that an operation is able to execute with. The expectation
is that the exact affinity can be resolved during compilation if
sufficient information is available (TBD) or at runtime based on a
user-provided policy (`iree_hal_module_device_policy_t`). As with other
treatments of device topology in the runtime this policy is left to the
hosting framework or application to decide how they want to configure
their overall topology. The common IREE command line tooling adds a
`--device_lead_allocator=N` flag to let the user denote which device
ordinal (of `--device=` flags) is to be chosen when a
`#hal.device.optimal` request makes it all the way to runtime. For
simple cases of CPU+accelerator this allows for hack-free ways to
indicate the lead allocator that is responsible for any buffer
allocation that is used on multiple devices.

The `#hal.device.optimal<...>` attr lowers to a `hal.allocator.select`
op/runtime call that takes a list of devices/queue masks along with the
memory types and buffer usage of the request and returns the selected
device/queue mask. Size is not included such that the selection can be
memoized across an entire program at initialization time. A pass is
added that runs at the same time as device query memoization to hoist
the selection into globals. Future changes can enhance the pass to fold
the selection when sufficient information has been provided.

Future work on #20855 will result in affinity assignment on resource
allocations lowering into this new `#hal.device.optimal<...>` attr and
deallocations switching to either use the recently added `origin` attr
or the same optimal attr (given that it should be consistent and
provides more information). Future work on #20856 will ensure that
allocations that may be used on multiple devices (regardless of whether
runtime-selected) get the appropriate bits set but that may require
#20854.

Progress on #20851.
Progress on #20855 (analysis will lower into this attr).
Progress on #20856 (will be used to set bits).
Fixes #20857.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HAL] Add HAL allocator selection based on optimal affinity for a particular usage + API.

2 participants