Skip to content
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

[Cuda] Skip FreeDataSpace when CUDA driver is in inconsistent state #16980

Merged

Conversation

Lunderberg
Copy link
Contributor

Prior to this commit, the RAII handler in NDArray would always attempt to free a cuda memory allocation on destruction. However, the call to cudaFree may throw an exception. If this happens during stack unwinding due to a previously-thrown exception, this causes the program to immediately terminate, making it difficult to identify the source of the original error.

This can commonly occur if an async compute kernel performs an illegal memory access. An exception is thrown from the next cuda API call following the asynchronous error, causing the stack to unwind. If the stack contains any NDArray instances which reference cuda allocations, the destructor of these NDArray instances will attempt to free memory, triggering the segfault.

This commit updates the CUDADeviceAPI::FreeDataSpace function to check if the program is currently unwinding the stack due to a thrown exception, while the cuda driver has been left in an unrecoverable state. If this occurs, no attempt to free memory is made, as all cuda API calls will result in an error, and the original exception is allowed to propagate.

If the cuda driver is in an unrecoverable state, but no exception is currently unwinding the stack, then this may be the first cuda API call to occur after the asynchronous error. In this case, the cudaFree call is still performed, which throws the initial exception.

Prior to this commit, the RAII handler in `NDArray` would always
attempt to free a cuda memory allocation on destruction.  However, the
call to `cudaFree` may throw an exception.  If this happens during
stack unwinding due to a previously-thrown exception, this causes the
program to immediately terminate, making it difficult to identify the
source of the original error.

This can commonly occur if an async compute kernel performs an illegal
memory access.  An exception is thrown from the next cuda API call
following the asynchronous error, causing the stack to unwind.  If the
stack contains any `NDArray` instances which reference cuda
allocations, the destructor of these `NDArray` instances will attempt
to free memory, triggering the segfault.

This commit updates the `CUDADeviceAPI::FreeDataSpace` function to
check if the program is currently unwinding the stack due to a thrown
exception, while the cuda driver has been left in an unrecoverable
state.  If this occurs, no attempt to free memory is made, as all cuda
API calls will result in an error, and the original exception is
allowed to propagate.

If the cuda driver is in an unrecoverable state, but no exception is
currently unwinding the stack, then this may be the first cuda API
call to occur after the asynchronous error.  In this case, the
`cudaFree` call is still performed, which throws the initial
exception.
@@ -142,6 +142,24 @@ class CUDADeviceAPI final : public DeviceAPI {
}

void FreeDataSpace(Device dev, void* ptr) final {
if (std::uncaught_exceptions() && cudaPeekAtLastError() == cudaErrorIllegalAddress) {
Copy link
Member

@masahi masahi May 13, 2024

Choose a reason for hiding this comment

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

I assume that uncaught_exceptions() is not going to add any overhead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be minimal compared to the memory allocation/free itself. The exact implementation of std::uncaught_exceptions() is compiler-dependent, but it typically is implemented as a read of a static thread-local variable.

@masahi masahi merged commit 2933744 into apache:main May 13, 2024
20 checks passed
@Lunderberg Lunderberg deleted the cuda_skip_raii_free_if_driver_unrecoverable branch May 13, 2024 20:21
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.

None yet

2 participants