Repository navigation
Adding AMDGPU command buffer implementation. - #21123
Conversation
This contains the host-side `iree_hal_amdgpu_command_buffer_t` responsible for recording and managing the replicas of the device-side `iree_hal_amdgpu_device_command_buffer_t` instances and the device-side implementation for issuing and executing command buffer programs. The comments for `iree_hal_amdgpu_device_command_buffer_t` and its related commands document how the command buffer is represented in device memory and what each command does. Some of the commands are not yet wired up to host recording code as the it is skating ahead of the HAL command buffer API. Even though unconditional branches cannot be recorded directly they are used for segmenting large command buffers to fit within resource limits. Conditional branches are nominally supported but need an API (similar to indirect dispatches) to be plumbed from the compiler to the command buffer vtable. IREE's HAL is intended to optimize for precompiled command buffers that can amortize the recording cost - this implementation leans that direction and should be optimal for meaningful (non-benchmark) programs originating from anything targeting CUDA graphs. There are some provisions for small command buffers but the intent is that single-command buffers end up routed towards device queue operations much earlier. Since the device-side queue scheduler does not yet exist the two methods required (`iree_hal_amdgpu_device_queue_scheduler_*`) are stubbed out to indicate where they'll be used in the future. The API may change in subsequent changes that implement the behavior.
bc655aa to
d650313
Compare
Needed to factor in the trace query ringbuffer capacity.
| // hidden_grid_dims | ||
| uint16_t grid_dims; // + 64 | ||
|
|
||
| // Fixed-size buffer for `-mprintf-kind=buffered` support. |
There was a problem hiding this comment.
Ooooof. Thanks for this big comment block. Regarding the architecture this is working within, can we push for a redo of those APIs? What would that look like? A new alternate API alongside the existing API? A transparent replacement from the perspective of user code, but requiring a compatibility breaking change to the compiler and CLR?
There was a problem hiding this comment.
I think we could - there's already 2 codepaths in LLVM and the flags surfacing it to users, so I think adding a well documented/simple one that was easy to support could go well. The CLR will always need to support the old ones because of legacy code but maybe we could find someone to add a new approach too. Both approaches are entirely synchronous and have a lot of side-effecting behavior (by synchronizing the host/device at or within dispatch boundaries) and a proper streaming implementation would be so much nicer.
| // Command buffers are represented by a host-side wrapper that implements the | ||
| // IREE HAL API and a device-side data structure holding the recorded contents. | ||
| // All information required to execute a command buffer lives on the device and | ||
| // a command buffer can be submitted from the device without host involvement. |
There was a problem hiding this comment.
Checking my understanding: while GPU APIs like Vulkan, Metal, D3D12, WebGPU all have a concept of command buffers, HSA is lower level and does not, so this is bridging that abstraction gap?
There was a problem hiding this comment.
A few things factor in; technically in HSA you can memcpy some precomputed AQL packets into the AQL queue and that's your command buffer. If your workload is entirely static (no kernarg changes, etc) then that's both simple and fast. If you do need to change things, though, you need additional metadata and processing besides memcpy to put the updated packets in the AQL queue. There's two approaches I've played with: in one we precompute all we can and have a fixup vector that says "update the 8 bytes at this offset with the new buffer pointer for binding table slot 8" (repeated a few thousand times) and in the other we have a form that effectively does the updates while it memcpys. The former is a special case of the latter so I went with the more general one to start as it allows full fidelity: we can do arbitrary control flow, dynamic packet expansion (in the future we can specialize which dispatch entry point is called per invocation, instead of re-recording the command buffer), etc. We also want 99% of all buffers to be from the binding table so the amount of work required to do a copy + fixup of nearly everything outweighed just doing the spicy copy.
Probably.
I'm sure there will be a lot of fast paths added in the future :)
There was a problem hiding this comment.
Gotcha. So for our "additional metadata and processing besides memcpy", we have things mappable to push constants like "run this dispatch with these outer loop dimensions" and then the more advanced things like on-device control flow, changing entry points per invocation, etc.
There was a problem hiding this comment.
yeah, and the intent is that our representation here is not 1:1 with the HAL API but more of an acceleration structure to allow us to do the issue as fast as possible (parallelized AQL packet construction, etc) - it's more 1:1 than I want it to be but I figured I'd start on the straightforward side and get more gnarly as we had some good benchmarks. There's a bit of a cycle too with things like control flow: have to support them here, go add the support to the HAL API, add the support to the compiler/lots of passes, etc to get it lit up and then evaluate the best approaches. My brain trying to sequence this:

| // Processing behavior: | ||
| // 1. Initialize iree_hal_amdgpu_device_execution_state_t: |
There was a problem hiding this comment.
This whole comment describing the device side command buffer architecture is excellent.
I know you've been deep down debugging paths with this code and have had discussions with system architects[^1], so I'm wondering what it will take to make more of this understandable and maintainable by other developers (e.g. to adapt to use cases beyond what IREE supports).
Some guiding questions:
- What is the tooling and debugging situation like for observing what this implementation is doing? Are there tools to record the sequence of AQL packets and analyze them, play them back in a simulator, etc.?
- What happens around error conditions? Do we get [helpful] messages from the device over an API or are there logs in some system files? Does the GPU crash?
1: btw, want to request reviews from some of the people you've been speaking with?
There was a problem hiding this comment.
Tooling is a mess so it's hard to know - it was all developed poorly alongside HIP/CLR so it's hard to use it for anything that is not exactly what the CLR implements by convention. HSA/ROCR does have some intercept ability and other fancy features but unlike Vulkan layers those directly reach into the internal C++ implementation in ROCR and thus if you aren't using ROCR (because you're doing device-side enqueue) there's no connection.
At some point I'll build a DAP for debugging command buffers (step through dispatches, etc) like swiftshader had. We also have the full tracy support which makes it possible to see what the scheduler is doing on device. The biggest things to make work then are gdb (but that may not be possible - that is another lovecraftian nightmare of an implementation), ASAN (more reasonable), and tools like threadtrace. The nice thing about IREE is that for dispatch-level stuff is that we can always keep HIP around - the kernels will execute with the same performance on both (since they're the same) so even if we can't enable any of those features the user can just switch their target backend and use HIP for debugging.
I've tried to make it so that errors aren't possible once work has been scheduled - lots of checks around resource limits, forward progress guarantees, etc - so though there are still a few cases it's rare. We can do some propagation back on semaphores for things that are scoped but everything else is a device-loss kind of thing.
There was a problem hiding this comment.
it was all developed poorly alongside HIP/CLR so it's hard to use it for anything that is not exactly what the CLR implements by convention
Yeah :( it'll take continued investment and more diverse uses to increase the maturity level of the stack
| // NOTE: the pointers used in the command are in the host address space. This is | ||
| // wonky, but the host trace buffer translation checks first to see if the | ||
| // address is in the expected range of device pointers and otherwise passes it | ||
| // right through. |
There was a problem hiding this comment.
Any security or data privacy concerns with mixing host and device address spaces? Could one task running on the GPU look at these to get information about other processes running on the CPU?
(I don't think so, but if it's "wonky" then maybe it brushes up against such concerns where "normal" code would not)
There was a problem hiding this comment.
The beautiful thing about HSA is that the virtual address space is unified, meaning that a host and device address for the same HSA-visible memory is the same. Non-local memory that isn't HSA visible can't be accessed by either side (so I can't take an arbitrary malloced pointer and have the GPU use it or an HSA device allocation and have to host use it), so it's effectively like sandboxing. There's a potential for the device code to MITM pointers but only for reads and there's far easier ways to mess things up (HIP's hostcall lets device kernels execute arbitrary host functions by giving it a host code pointer!). Being better than that isn't something to be too proud of, but we're at least better than that :P
There was a problem hiding this comment.
Oooo unified virtual address spaces sound cool :o
There was a problem hiding this comment.
Doesn't mean everything is fast, and apparently doesn't mean everything is coherent either, but it at least makes it easier to deal with pointers :P
| // TODO(benvanik): rework events so that they can be reused. We really should | ||
| // have an events table-like thing or something that allows capture at time of | ||
| // issue (if we even want to allow events to be used across command buffers). | ||
| // Today events are similar to Vulkan ones which don't support concurrent issue | ||
| // and that limits us here. |
There was a problem hiding this comment.
I'm wondering about this host/device split, given that HSA seems lower level than Vulkan. This device code is acting like a "GPU driver" and sits resident on the device, using some number of GPU threads and some amount of fixed overhead storage, right? How large is that overhead? When using an API like Vulkan (or I guess even HSA via ROCr?), does the system Vulkan driver do something similar?
I ask because
- "we should have an events table-like thing" would presumably increase that overhead
- When using a "system driver" compared to one managed by a library like IREE, that overhead may be amortized across multiple applications/processes (or not, if sandboxing)
There was a problem hiding this comment.
We don't keep anything executing resident and only run when there's something to issue or retire. You can think of it like setTimeout in javascript: we setTimeout(scheduler), issue anything we can (like async fetch), and then return. When the issued work retires (async fetch completes) we setTimeout(scheduler) to issue the next set of work. Traditional APIs do that on the host - they'll complete their work, defer back to the host to do the bookkeeping and issue, then wait/poll until the work completes before going again. So it's effectively the same algorithm, just we're doing it local to the device without involving the host. The host CPU is faster than a GPU thread, but the work we do is small/brief enough compared to say a giant LLM matmul that the benefit we're getting is really around timing variance/latency (no risk of hopping back to an oversubscribed host thread and waiting in a kernel schedule queue, less traffic over slow busses, etc). If we were always just launching the same static workloads and could pipeline those deeply and were always on a single device (no cross-device hops) then CPU may still be better, but our hope is to be smarter than that by having loops, conditions, etc that there's really no way to do efficiently with the CPU in the loop. Worst case, the command buffer stays the same but we have both a host and device scheduler that we use depending on where the originating request comes from (though that has other potential pitfalls, like we want to put the AQL queue in device memory and due to hardware limitations we can't have the host touch queues in device memory - we'd have to put the queue in host memory and then make the 99% of all reads/writes to that queue from the device go across the bus... yuck). I've been toying with multiple queues and ways to ensure exclusive access such that we could slipstream some early commands into them from the host while the scheduler then runs async on the device. Gotta get it all running first, though :P
This contains the host-side `iree_hal_amdgpu_command_buffer_t` responsible for recording and managing the replicas of the device-side `iree_hal_amdgpu_device_command_buffer_t` instances and the device-side implementation for issuing and executing command buffer programs. The comments for `iree_hal_amdgpu_device_command_buffer_t` and its related commands document how the command buffer is represented in device memory and what each command does. Some of the commands are not yet wired up to host recording code as the it is skating ahead of the HAL command buffer API. Even though unconditional branches cannot be recorded directly they are used for segmenting large command buffers to fit within resource limits. Conditional branches are nominally supported but need an API (similar to indirect dispatches) to be plumbed from the compiler to the command buffer vtable. IREE's HAL is intended to optimize for precompiled command buffers that can amortize the recording cost - this implementation leans that direction and should be optimal for meaningful (non-benchmark) programs originating from anything targeting CUDA graphs. There are some provisions for small command buffers but the intent is that single-command buffers end up routed towards device queue operations much earlier. Since the device-side queue scheduler does not yet exist the two methods required (`iree_hal_amdgpu_device_queue_scheduler_*`) are stubbed out to indicate where they'll be used in the future. The API may change in subsequent changes that implement the behavior.
This contains the host-side
iree_hal_amdgpu_command_buffer_tresponsible for recording and managing the replicas of the device-sideiree_hal_amdgpu_device_command_buffer_tinstances and the device-side implementation for issuing and executing command buffer programs.The comments for
iree_hal_amdgpu_device_command_buffer_tand its related commands document how the command buffer is represented in device memory and what each command does. Some of the commands are not yet wired up to host recording code as the it is skating ahead of the HAL command buffer API. Even though unconditional branches cannot be recorded directly they are used for segmenting large command buffers to fit within resource limits. Conditional branches are nominally supported but need an API (similar to indirect dispatches) to be plumbed from the compiler to the command buffer vtable.IREE's HAL is intended to optimize for precompiled command buffers that can amortize the recording cost - this implementation leans that direction and should be optimal for meaningful (non-benchmark) programs originating from anything targeting CUDA graphs. There are some provisions for small command buffers but the intent is that single-command buffers end up routed towards device queue operations much earlier.
Since the device-side queue scheduler does not yet exist the two methods required (
iree_hal_amdgpu_device_queue_scheduler_*) are stubbed out to indicate where they'll be used in the future. The API may change in subsequent changes that implement the behavior.