-
Notifications
You must be signed in to change notification settings - Fork 13.9k
CUDA: add stream-based concurrency #16991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
1e97a91 to
1c4d8f3
Compare
|
Sorry, I wanted to tell you this but I forgot: a long time ago I tried something similar, see #4719 . There the performance did not improve, I think the reason was the lack of CUDA graphs to reduce the overhead. |
|
Yeah, I think CUDA graphs are essential for this to work (hence this PR only looks at batch_size=1) |
1c4d8f3 to
70a5a01
Compare
|
Minimal changes to make this work on hip: If used for real, cudaStreamWaitEvent error needs to handled of course with
|
|
The almost exact same numbers make me think that this change is not launching the streams. I would expect a shift in performance either for the worse or the better. |
|
yeah ill run a trace on it later. |
70a5a01 to
2c3cfa9
Compare
12d5f82 to
d3a8d93
Compare
|
@ggerganov would you mind testing this on your DGX spark? I want to see if the low memory bandwidth GPUs benefit from this change |
I'm not really clear on what the problem is here you're trying to solve. If the order is: MUL_MAT+ADD+MUL_MAT+ADD+MUL_MAT+ADD, then you have the nodes conveniently consecutive (for fusion), the intermediate MUL_MAT outputs aren't needed and the ADDs will all have different outputs. This is the order ggml-vulkan will use and it gets both fusion and concurrency. |
|
My problem is that the buffer gets reused in this case. The graph assumes serial execution, and thinks the first mul-mats buffer is no longer required. (Assume the no fusion case for now) |
|
If you're not doing fusion, then you'd want graph_optimize to reorder these to MUL_MAT+MUL_MAT+MUL_MAT+ADD+ADD+ADD. Then the MUL_MAT results will stay live until the ADDs. |
|
Yeah that's what the current re-order does. But that doesn't allow for fusion. I don't want these two things to be intertwined. Ideally want something that just lets me extend the lifetime for a particular output till a certain node |
|
IMO they are fundamentally intertwined. The code that detects fusions looks for specific sequences of operations, and graph_optimize should generate or preserve those sequences. If the backend supports fusing something, then graph_optimize should make them consecutive both to make the fusion logic simpler and to shorten the lifetime of transient allocations. |
|
I think it will be good to isolate these two behaviours. If you see the graph above it can launch a concurrent graph from the mul-mat till set rows. We don't have fusion for that entire sequence, and reasoning about which output stays alive would involve inspecting the graph in any case. Secondly fusion is a common source of bugs in the cuda backend, I don't want to add another layer of complexity on top of it. |
This comment was marked as outdated.
This comment was marked as outdated.
|
Did you pass the env flag? |
Why of course not 🫠
Updated numbers
|
If my current understanding of ggml is correct, we should be able to get the same behavior (fusion + concurrency) on both vulkan + cuda, as Q should go out of life after flash-attention (and K + V go out of life after being inserted into the KV-cache). Have we root-caused this? |
1677786 to
25565c3
Compare
| return; | ||
| } | ||
|
|
||
| GGML_ASSERT(ggml_backend_cuda_get_device_count() == 1 && "compute graph optimization is only supported on single GPU in the CUDA backend"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this restriction actually needed? I could see an incompatibility with --split-mode row since that also uses multiple CUDA streams but when running multiple GPUs concurrently I think it should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes specifically for pipeline parallelism (since that also also uses events to synchronize)
| } | ||
| } | ||
|
|
||
| if (num_joins >= 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the scenario where you have a 1 -> 3 fanout, then join branches 1 and 2 to a branch "1.5", then execute more nodes on both 1.5 and 3, then join 1.5 and 3. If I understand the code correctly it is assuming that there is a single node for the fanout and a single node for the join.
Co-authored-by: Johannes Gäßler <[email protected]>
Co-authored-by: Johannes Gäßler <[email protected]>
|
Consider the following implementation:
@ggerganov @am17an would a design like that be acceptable to you as the long-term goal for how to handle out-of-order execution in ggml? |
|
Pipeline parallelism already does this in a way for multiple CUDA GPUs, re-orders the graph to run things concurrently. I wonder if we can repurpose that to also mark the graph like you mentioned (via some stream-id) |
|
What just happened? |
e1b6274 to
2c9c3c2
Compare
My git just tweaked in the middle of auto-compaction |
Not sure if you have a specific logic in mind for assigning the Atm, I don't have a good feeling how difficult would be to modify the allocator to respect the stream ids. |
|
IMO ggml_visit_parents should just assign everything to stream 0 and then graph_optimize can reassign to streams based on what the backend wants. |
We need to ensure that nodes are only recycled after they've been used for the last time and after their results have "become visible", meaning that we need to ensure that we don't recycle nodes where the execution can be in the future. I would suggest allocating an array like
I would also be fine with determining stream ids in the graph optimization step. |
ggml/src/ggml-cuda/ggml-cuda.cu
Outdated
| bool should_launch_concurrent_events = true; | ||
| for (const auto & [tensor, event] : stream_ctx.concurrent_events) { | ||
| should_launch_concurrent_events &= event.is_valid(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote previously:
I think the outer should_launch_concurrent_events being shadowed here is a bug. Also please avoid using the bitwise |= and &= operators for booleans.
You marked this as resolved without further comment. Can you provide some context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops sorry, I had fixed this but forgot to commit


Possibly supersede #16813.
This PR adds support to run concurrent CUDA streams on single GPU setups.
At the moment this only targets the Q, K, V branch. I feel this is the "correct" approach in case the Q, K, V tensors are of different types/not in the same place in memory. The downside is that this approach doesn't come for free and there's some complexity involved, but I'm not an expert at the ggml graph and I feel it could be simplified.
Currently this is hidden by an env variable flag. To run you can use
GGML_CUDA_GRAPH_OPT=1TG Performance gain is more than the previous PR (1-9% gain depending on the model/GPU), probably because we parallelize MUL_MAT + NORM + ROPE rather than just MUL_MAT. At the moment we leave some performance on the table where we don't fuse operations in the parallel streams themselves (e.g. MUL_MAT + BIAS, RMS_NORM + MUL etc.), I couldn't find a simple enough way to enable fusion there.
Performance details:
5090:
4090:
And just for comparison, this is without fusing ops inside a stream
5090:
4090:
TODO: