Repository navigation
[WIP] Graceful cuda assert (no device-side assert triggered` error) - #26776
andreaskoepf wants to merge 23 commits into
Conversation
|
The central 'async' check for assert-error-conditions happens in every |
|
Don't forget to add reviewers! |
There was a problem hiding this comment.
It feels a little goofy that we literally copy the const char* pointer into assert state, when in principle it's just a string literal and the pointer should be good enough. I wonder if there's a way to share a constant between CUDA and CPU. Maybe we should just have a fixed set of error codes...
There was a problem hiding this comment.
It is true, the impl can be simplified in a couple of ways, e.g. by using only a fixed set of well-known error states. I am open to change the code in any direction. I wanted to keep generic / comparable to a CPU assert at first - but that might not be necessary.
I am also not really happy with copying strings in device code. I have not yet tested if it is possible to pass on pointers of string-constants defined in a cuda kernel. I will check this later today.
There was a problem hiding this comment.
Naw man, you can't do this! Now you're synchronizing after every kernel again. The point is that checking for failures has to be opportunistic in the same way that cudaGetLastError is: if the error flag happens to be set, now you can do a synchronize and grab all the information you need--if it's not, you gotta wait until next time to try.
(One complication is making sure, if we manage to queue more kernels before we discover the previous one is dead, we do something reasonable in that case...)
There was a problem hiding this comment.
The cudaDeviceSynchronize() call is only made when error != 0, e.g. after a device side assert has failed and after the write operation to the pinned memory becomes visible to the host. The device sync is done to wait for the (failed) kernels to complete in order to get all information and throw an exception. E.g. otherwise the effect of the atomicCAS operation might become 'visible' before the other data has been transfered, e.g. the device-side string-copy operations for the. It would be possible to do everything completely without device-sync but that would get a bit more complicated (e.g. similar to writing a custom host/device spanning critical-section).
There was a problem hiding this comment.
(It should definitely be possible to launch kernels in parallel as it was before, the sync is only done in those cases that before ended up in the evil "Device-side assert triggered" state...)
|
This looks promising but there are still some important problems. |
|
The last commit adds index-out-of range output that matches the cpu version and I added a first experiment for cuda division by zero (for integral types). This was a bit complicated since the kernels were |
There was a problem hiding this comment.
A comment on this file explaining what this does would be pretty nice
There was a problem hiding this comment.
This is a reasonable hack, but it should be documented as such in the code
There was a problem hiding this comment.
This file could also use some high level comments explaining what it does to uninformed readers (it's pretty clear to me, but I also know what you're doing...)
There was a problem hiding this comment.
I guess it will be good when you do your performance testing, but I wonder what the perf implication of this atomic access is. Surely it can't be good...
There was a problem hiding this comment.
I don't think this condition is right. AT_DISPATCH_ALL_TYPES_AND seems to imply floating point types are also hit here. We shouldn't do the condition test for those kernels.
There was a problem hiding this comment.
CPU multithreaded execution of CUDA kernels is a thing. What's the multithreading discipline here?
There was a problem hiding this comment.
You don't actually end up calling this macro anywhere. Maybe we should just drop it; that would resolve my concern with the atomic access in success case.
|
Very cool! Needs two things: (1) multithreading, and (2) tests. Also curious to see what the perf impact is. |
|
@ezyang Regarding C string literals in a kernel code: Those char arrays live in device memory, dereferencing such pointers in host code results in segmentation fault. I have tomorrow a whole day to work on this issue to test & polish. Maybe after the speed tests we could decide if string-copying is fine or if we better go back to just storing error-code + additional data. If the decision is made for error-codes it might be a good idea to offer a error-handler registration mechanism, so that central code in CUDAStream is not affected if somebody wants to introduce a new error-code with corresponding handler/output formatting function. |
pietern
left a comment
There was a problem hiding this comment.
I wonder, check_assert_state is only called from CUDAStream_fromInternals. When is that called?
There was a problem hiding this comment.
You'll likely define all the detail structs in this file as well, so you can turn this into a union of those structs to avoid accidentally overflowing and excessive reinterpret_cast'ing.
There was a problem hiding this comment.
I thought about using an union. My first concern was that c10 should not be changed whenever new/special assert functionality is added, e.g. originally I wanted to create something like a registry for formatters/handlers. But if there are only a handful of different cases and we basically 'seal' the set of assert/error functions for users, it might be reasonable to specify them in a c10 header. I guess that would also solve alignment issues, e.g. currently if the _reserved member would be removed the field-writes would trigger 'misaligned address' errors in the graceful_index_error_...
There was a problem hiding this comment.
You could do a check for nullptr and opportunistically allocate followed by a CAS and potential free.
There was a problem hiding this comment.
The break is not needed here, but if you have one here, add them to the other cases as well.
There was a problem hiding this comment.
I wonder,
check_assert_stateis only called fromCUDAStream_fromInternals. When is that called?
@pietern from_Internals is called for example for every call to getDefaultCUDAStream(). This is a quite central place for the check but will be executed mostly once during setup of a kernel launch. I did not expose the check_assert_state function in the prototype, but it could simply become a member of CUDAStream to be called explicitly, e.g. by C++ clients.
I must admit that I do not yet fully understand the architecture of c10, e.g. I had no contact to HIP etc. In this context it is for example unclear to me whether Stream sub-classes are allowed to expose new functionality like cuda-specific assert/error handling.
|
Can we somehow have a mechanism that does the following:
Does that sound better? (all in all, we are trying to avoid cudaSynchronize ) Basically that will still result in deviceAsserts caught by C10_CUDA_CHECK, but we will have additional and controlled information about the error message to rethrow it better |
|
@soumith The name Regarding For a single stream this is already working well. A problematic case are p2p device operations with multiple streams. I currently think those could be addressed by registering a cuda-callback function that would asynchronously cascade a detected assert state to another stream. I could test this next week. |
|
@soumith: Currently my impl allocates an assert_state struct per cuda stream. It has to be passed by host code to the kernel via a lambda-capture, e.g. see the use here for the check in IndexKernel: https://github.com/pytorch/pytorch/blob/50e2a87b89362134a1f4a802dc45b28488268f9c/aten/src/ATen/native/cuda/IndexKernel.cu#L54-L67 The assert_state struct contains the arrays that stores the information about the assert, e.g. file name, line number, error-message etc. In order to create something that looks as beautiful as |
|
This is a simple solution with two macros |
|
I made an major overhaul and implemented functionality in a way similar to what @soumith suggested. I hope tomorrow I have time for a speed test. As this PR has become a bit of a mess I think it would be best to close it and create a fresh one. |
|
|
||
| CUDAAssert* CUDAStream::assert_state() const { | ||
| auto ptr = CUDAStream_internals(*this); | ||
| AT_ASSERT(ptr); |
There was a problem hiding this comment.
(AT_ASSERT is the old name but we had a lot of people misusing it to do error checking for conditions that users could trigger. TORCH_INTERNAL_ASSERT is the "if this ever is false, that means we the library authors fucked up")
|
|
||
| // lazy create assert state | ||
| CUDAAssert* assert_state = ptr->assert_state.load(); | ||
| if (assert_state == nullptr) { |
There was a problem hiding this comment.
Hmm... this feels racy too. What if two threads initialize the assert state at the same time? Then you'll just leak the allocated host memory in this case, right?
I see, you handle it later!
| C10_CUDA_CHECK(cudaHostAlloc( | ||
| (void**)&new_instance, sizeof(CUDAAssert), cudaHostAllocMapped)); | ||
| memset(new_instance, 0, sizeof(CUDAAssert)); // clear memory | ||
| new_instance->mutex = new std::mutex(); |
There was a problem hiding this comment.
Technically this is UB, and you need to placement-new the mutex here.
There was a problem hiding this comment.
here mutex is just a pointer, if it was a regular member I would need placement new. I thought about using placement new for the whole CUDAAssert struct and use a regular mutex member but I decided against it because this struct lies in pinned memory, e.g. I thought it might make sense to use a simple plain C object and hide the C++ stuff behind a pointer (to memory that is not accessible from within a cuda kernel).
| assert_state = new_instance; | ||
| } else { | ||
| // we lost the race: deallocate & load effective value | ||
| delete new_instance->mutex; |
| } | ||
|
|
||
| size_t size = ret + 1; // ret does not include the terminating '\0' | ||
| auto buff_ptr = std::unique_ptr<char[]>(new char[size]); |
There was a problem hiding this comment.
One thing that does make me a little nervous here is that this is all error testing code, which in practice means we're not going to be exercising it as rigorously as other code. So if there are any bugs added by people later, it may be hard to notice (unless we've written tests for the error condition in question.)
There was a problem hiding this comment.
(And it's pretty easy to, e.g., use the wrong format specifier, or also use the right format specifier, but only on Linux (because the format specifiers on Windows are different)). And speaking of tests... we're going to need some here :)
| bool persistent; // whether a non-recoverable device side assert was triggered | ||
| uint32_t length; // number of bytes in buffer (captured arguments) | ||
| std::mutex* mutex; // synchronize host side decoding & reset | ||
| alignas(C10_ASSERT_ARG_ALIGN_SIZE) char buffer[C10_ASSERT_BUFFER_SIZE]; |
There was a problem hiding this comment.
Would be good to mention in a comment here that the assert buffer contains payload information for the format strings
ezyang
left a comment
There was a problem hiding this comment.
This looks good. The biggest trouble is lack of docs, but if you like I can help with that. Looking forward to seeing this on master :)
|
Hi @andreaskoepf! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but we do not have a signature on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
2 similar comments
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
|
Out of curiosity, what happened to this? |
|
@t-vi As far as I remember after discussing in person with @ezyang we decided not to go on with graceful asserts, probably because it would require changes to critical core-infrastructure and the usage of the proposed macros would be cumbersome. There were a few points that made things complicated:
It was a proof of concept which showed that it might be possible to handle cuda-side asserts gracefully. I have not been working on cuda code for a while now, therefore I do not know if new ways to address the problem became available. |
|
Oh, cool, thank you for the detailed conclusions! And too bad... I would have loved something that allows to investigate after an assert. |
|
I'm still kind of interested to see if we can do a simplified version and still get this in, would be useful... |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
PR for Discussion of Concept
This is an experimental patch to evaluate if it was feasible to use a custom assert-mechanism in cuda kernels that does not result in
CUDA error: device-side assert triggerederrors.The following code block triggers an assert in
IndexKernel.cu. With the modifications in this PR cuda can be further used after the assert.This experiment is related to:
A couple of things need to be further analyzed: