-
Notifications
You must be signed in to change notification settings - Fork 11.5k
CUDA: HIP: maintain_cuda_graph use of cudaGraphKernelNodeGetParams is incorrect. #12152
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
Comments
pageing @aendk from nv, @slaren and @JohannesGaessler |
Also tagging @agray3 here |
I agree that the above change is better, and confirmed it also works for CUDA. I'll check with our CUDA graphs team to get their comments on it. Note that we can bypass this altogether with #9017 which still needs some work as per comment #9017 (comment) - if there is a desire for this I can resurrect it. |
Maybe you can submit feedback to them that this interface seams poorly thought out. Either:
or:
Otherwise this 'here is a non-const pointer to our internal memory structure but please dont modify anything' behavior of cudaGraphKernelNodeGetParams seams like a unnescary footgun. |
The CUDA graphs team have confirmed that these solutions which directly modify the parameters are not strictly compliant with the docs. It is understood why they work at the moment, and there is no specific reason to believe anything will change, but of course there can be no guarantees in the future. Therefore I think it would make sense to move away from this via indirection (with the updated parameters copied to the GPU rather than inserted in the graph) as mentioned above. I'll work on this. |
ok i will open a pr with the patch above as a stop gap. Please do consider my feedback on the gaph interface. |
Sounds good. Yes, I have already also forwarded your feedback and the team are considering it. |
…ointers Previously there was complexity in the CUDA graphs implementation due frequently changing parameters to copy kernels associated with K and V cache pointers. This patch simplifies by using indirection to avoid such parameters frequently changing, avoiding the need for frequent graph updates. Fixes ggml-org#12152
May I ask a related question? My code occasionally hangs during the execution of When this occurs, the model does not return and enters an endless loop, continuously logging this message without stopping. Could you please advise on how to break out of this loop and handle the error more gracefully, such as throwing an error or exiting the loop? I believe this might be a bug, and I am looking for a workaround until it is resolved. Thank you for your assistance! |
One more peace of information: If I switch OFF cuda graph in the compilation of llama.cpp, then I get occasionally this with the same endless loop:
This and the formerly reported "CUDA graph update failed" are most probably pointing to the same BUG. |
…ointers Previously there was complexity in the CUDA graphs implementation due frequently changing parameters to copy kernels associated with K and V cache pointers. This patch simplifies by using indirection to avoid such parameters frequently changing, avoiding the need for frequent graph updates. Fixes ggml-org#12152
According to cuda documentation the memory of the cudaKernelNodeParams struct as returned by cudaGraphKernelNodeGetParams is owned by the associated node. In the case here
llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu
Line 2546 in 14dec0c
we later modify this struct by replacing one of its pointer members with the address inside a block of memory we own:
llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu
Line 2565 in 14dec0c
We have thus modified the node by simply replacing a pointer to a member with memory owned by the runtime with a pointer to memory we own. There is no way this results in well defined behavior and indeed the cuda documentation prohibits this action, see the link to the documentation above:
Presumably this happens to work on cuda right now either because the runtime happens to allocate the pointer we are updating as part of a larger block separately malloced and the pointer happens to not be the first address in the allocated block, or because the runtime simply is leaking this memory.
On hip, the runtime mallocs() memory all the kernel pointers separately and then frees() the memory when the node is destroyed. This of course causses an invalid free() when the runtime encounters the pointer we changed to point to our memory.
We could avoid this by not changing the pointer to memory we own, but to instead simply update the value it holds:
I have verfied by looking at the hip runtime code and discussion with an amd engeneer that this is fine to do for hip, but this still violates the provision to not modify the parameters given by the cuda documentation and i have no idea if this is safe to do there. The only way to not violate the constraints given by the documentation would be assemble a cudaKernelNodeParams struct by hand from scratch in this possition:
llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu
Line 2564 in 14dec0c
The text was updated successfully, but these errors were encountered: