Repository navigation
[metal] Implement a Metal HAL driver - #12713
Conversation
|
Woah awesome. Time to try stable diffusion 😀 |
54b7dfd to
890d82d
Compare
47693aa to
cf99485
Compare
ead5ff2 to
0d5318c
Compare
|
Listing some bigger questions/tasks mentioned in offline discussions: General
Compiler
HAL allocator / buffer
HAL command buffer
HAL device / queue
HAL executable
HAL semaphore
HAL event
|
|
There are non Apple metal devices ? Would it be ok to land this and address the issues listed? Or are they needed before landing (trying to get rid of moltenvk support in SHARK which only works on version 216.) |
I was under the impression that Metal offline toolchain is only available on Apple platforms. Ben pointed out that they are also available on Windows. So we can also use them to do MSL to
Not blocking thus far. Just listing these relative major points so we don't forget them. |
6d62849 to
d22d684
Compare
08f7e5f to
d65b254
Compare
Unlike Vulkan, Metal adopts a multi-level command recording model-- memory/dispatch commands are not directly recorded into a command buffer; rather, they must go through the additional level of blit/compute encoders. IREE's HAL follows the flat Vulkan command buffer recording model, so we have a mismatch here. Implementing IREE's HAL using Metal would require switching encoders for interleaved memory and dispatch commands. Additionally, certain IREE HAL API features do not have direct mapping in Metal APIs, e.g., various forms of IREE HAL execution/memory barriers. Translating them would require looking at both previous and next commands to decide the proper mapping. Due to these reasons, it's beneficial to have a complete view of the full command buffer and extra flexibility during recording, in order to fixup past commands, or inspect future commands. Therefore, to implement IREE HAL command buffers using Metal, we perform two steps using a linked list of command segments. First we create segments (iree_hal_metal_command_buffer_prepare_* and iree_hal_metal_command_segment_create_*) to keep track of all IREE HAL commands and the associated data, and then, when finalizing the command buffer, we iterate through all the segments and record their contents (iree_hal_metal_command_segment_record_*) into a proper Metal command buffer . A linked list gives us the flexibility to organize command sequence in low overhead; and a deferred recording gives us the complete picture of the command buffer when really started recording.
These checks should not happen with compilers and runtime validation. So just turn them into asserts.
This is in preparation of upcoming changes to use one single device buffer for holding all argument buffers.
This commit switches the Metal HAL driver to use a staging buffer for recording argument buffers and uploading buffer update source data. This avoids creating lots of small-sized buffers like we did previously, and avoids using command buffer completion callback to handle their lifetime.
This makes the `iree_hal_metal_command_buffer_t` structure more clear and readable.
This simplifies the logic for descriptor tracking in command buffer states. Now we don't need to find the proper starting index and sort.
We want to have `host_allocator` directly ahead of the `out_` parameters.
For managed storage mode, we need to issue GPU commands to explicitly make data visible to CPU. This would require having a handle to a MTLCommandQueue. Instead of creating a new one internally, give the queue to the buffer during construction time.
This avoids the needs to create the buffer and clean it up later.
This saves us one level of indent to make the code easier to follow.
Change it to follow device struct allocation, initialization, and subresource creation. This way we can handle destruction on failures better.
By this way, we can avoid tracking the current pipeline layout.
This avoids the potential refcount mismatches due to that we can have command buffers created but never submitted and such.
This is an performance improvement over the previous case where we always create a new command buffer for semaphore signaling.
MTLFunction and MTLComputePipelineState creation are common between these two paths; so we can have one function for them.
This avoids mutating the `active_count` in the command buffer state.
We need to check the status and free the resource set if any error happens.
We need to check the return status of resource set allcoation and properly destroy the command buffer if any error happens.
125fcb5 to
028acfb
Compare
This pull request resurrects the previously removed Metal
HAL driver due to HAL revamping. It now implements the
HAL driver using Objective-C, instead of Objective-C++
like previously. We also explicitly track resource lifetimes
and manage dependencies, following IREE explicitness.
Right now the following features are implemented:
allocation (we expect common layer for those later)
direct and indirect dispatches
.metallibstream-ordered allocation yet
The implementation right now assumes Metal3 devices.
It passes all existing CTS tests, end-to-end StableHLO
and TOSA op and model tests in tree for Apple M1
MacBook.
Fixes #4370