Repository navigation
Conversation
Includes support for stream capture to a cuda graph of pinned host memory to the memory pool
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/161583
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 New FailuresAs of commit 6fd73e8 with merge base 68d395d ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
It's not good enough for host allocator - current events are to prevent reuse on the cpu while previous copy has not finished. Typical usecase: |
|
@ngimel well that's embarrassing for me. I got so obsessed with offloading activations to host for training that I didn't think about anything else. Thank you. I will need to think this over for a bit. |
guangyey
left a comment
There was a problem hiding this comment.
Thanks for your mention.
I noticed that in this PR, the host caching allocator has multiple implementations, and each implementation maps to a MempoolId_t. Why not introduce a PrivatePool concept similar to what DeviceCachingAllocator uses?
That way, the host caching allocator would only need a single implementation, and each implementation could internally manage multiple PrivatePools. This approach may not break the original design and would also allow us to reuse existing code.
IMO, aside from differences in runtime APIs and some subtle behavioral variations, the core logic for managing pools/blocks is largely consistent across accelerators. It may be worthwhile to consolidate this logic into shareable code, which would also provide other backends an opportunity to benefit.
In addition, several accelerators provide similar graph modes, such as XPUGraph (coming soon), NPUGraph, MLUGraph, and others. Their behavior is quite similar to CUDAGraph, so the allocator logic should remain the same and could be made shareable.
My point is that this was true when you made your initial work, but it isn't true today for the following reason:
It's possible that Natalia's comment above may invalidate my need to make a lot of big changes. I am still trying to evaluate fully and I am slow :( Again, I think that making the at::HostAllocator abstract interface is good. My problem is with prescribing how this interface is implemented via insisting upon usage of at::CachingHostAllocatorInterface and an Impl class that implements an implicit interface via this macro: pytorch/aten/src/ATen/core/CachingHostAllocator.h Lines 710 to 717 in 47ecd20 Following this suggestion makes it extremely hard to change the implementation in non-trivial ways because, as soon as you need to introduce a new abstract method to do something (e.g., I need to call getCurrentCUDAStream() at one point, so I would need to wrap this in a virtual method that would be added here: pytorch/aten/src/ATen/core/CachingHostAllocator.h Lines 602 to 621 in 2efcf9d
I originally considered doing that. But this approach effectively does the same thing as that, but in a fairly simple way. It also allows each pool to use its own mutexes in a way that will minimize cross-talk across threads. (You do need to use a mutex in CachingHostAllocatorInterface to handle selecting the proper impl, but you can do that via a reader-write lock, aka std::shared_mutex, fortunately.) Since we want pinned host memory allocations to be allocated to the same numa node as the current thread, which (should) be on a numa node next to its current GPU, I think this is a fairly good design that involves minimal changes to CachingHostAllocatorImpl, since it doesn't need to be aware of what a memory pool id is. Ultimately, I am not attached to one particular design, but I think this one is fairly straightforward to understand since it essentially separates out memory pools from the actual allocation logic. |
|
@ngimel Okay, thanks a lot for the pointer. I can write up a more mathematically precise document if desired, but I think my conclusion is as follows:
Some possible use cases come to mind:
The first case is niche, and the second case is hard to implement without blowing up the scope of this. (Note that we would need to expand the record_event() API to document whether the host memory is being read or written as well.) Nevertheless, the point still stands that being able to pin memory during stream capture is helpful for the sake of increasing memory capacity during training by offloading cached activations to host. I propose this simple path forward: Never recycle any host memory in a memory pool if it ever is used during stream capture. This would not hamper the main use case at all. This might make @guangyey happy because it means very little will change to CachingHostAllocatorImpl, such that I can use the original implementation without duplicating it. (But I still think the point stands that there is potential for a divergence to happen in the future.) |
|
Yeah I agree with a simple path forward if it enables important usecases, my comment was we should avoid breaking the world, which recycling memory had a full potential of doing. Lmk when this PR is ready for review. |
Yes, I agree strongly and am a bit disappointed I did not realize the counterexample you gave. Always appreciate your timely feedback. |
|
Okay, so I discussed a bit internally. If we allocate host memory that the host is banned from touching (we can mark the read and write bits to 0 in the page table on the host to guarantee this), we can actually recycle memory via graph search algorithms. This would allow interleaved pipeline parallelism to be able to reuse memory. This is possible as of CUDA 13.0, since the cuMemCreate and cuMemSetAccess APIs together allow us to allocate memory on the host (that is device readable and writable but not readable or writable by the host):
I'm not planning to do this anytime soon. We would need (1) a pluggable allocator API like @syed-ahmed made for CUDACachingAllocator and (2) to change the logic for detecting when memory blocks are reusable depending upon if a particular allocator is used. This is non-trivial and not needed by me or anyone internally right this instant. But I figure it was worth noting here. Honestly, @guangyey, I know I've been belaboring this point a few times, but it's going to get more and more difficult to keep the CachingHostAllocatorImpl and CachingHostAllocatorInterface classes "generic" as time goes on so I unfortunately will likely have to break away from using them for the CUDA implementation at some point. |
|
Currently, we see two possible approaches here:
Option 1 risks diverging the codebase, while Option 2 keeps the implementation generic and shareable across backends. In our view, we should always try to avoid divergence where possible, and evaluate on a case-by-case basis whether keeping the code generic is feasible. |
|
@guangyey Thank you for the summary message. I will go ahead and evaluate option 2 for this PR, in an effort to reduce the amount of new code, once I revamp it to no longer try to memory block recycling. I do understand that there is clear benefit to maintaining abstractions so that the implementations can be similar, especially when they are quite complicated (e.g., see all the locks here). I appreciate you bearing my long messages. |
|
@galv Thanks a lot for the thoughtful evaluation. Let me know if there’s anything I can help with during your design. |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
I needed to move MemPool from c10 to aten. Otherwise, c10 would depend upon CachingHostAllocator in aten, which is a circular dependency. An alternative is to move the CachingHostAllocator to c10, but that is more likely to break people. This also works with cudagraphs, but in a pessimizing way: If a block is ever allocated during stream capture, it will never be reused. There might be some remaining issues regarding host statistics. Follow up to pytorch#161583, which did not work because of pytorch#161583 (comment)
Both allocation to a cuda graph's private pool via stream capture and allocation to a memory pool in non-stream-captured code are supported. In the case of stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See pytorch/pytorch#161583 (comment) for elaboration. This is lacking test cases for pagedable host memory copies. We must make sure that record_event() does not fail in that case. ghstack-source-id: 7f22464 Pull-Request: pytorch/pytorch#167507
Both allocation to a cuda graph's private pool via stream capture and allocation to a memory pool in non-stream-captured code are supported. In the case of stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See #161583 (comment) for elaboration. This is lacking test cases for pagedable host memory copies. We must make sure that record_event() does not fail in that case. ghstack-source-id: a9db4f7 Pull-Request: #167507
Both allocation to a cuda graph's private pool via stream capture and allocation to a memory pool in non-stream-captured code are supported. In the case of stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See pytorch/pytorch#161583 (comment) for elaboration. This is lacking test cases for pagedable host memory copies. We must make sure that record_event() does not fail in that case. ghstack-source-id: 1f63f15 Pull-Request: pytorch/pytorch#167507
Both allocation to a cuda graph's private pool via stream capture and allocation to a memory pool in non-stream-captured code are supported. In the case of stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See #161583 (comment) for elaboration. This is lacking test cases for pagedable host memory copies. We must make sure that record_event() does not fail in that case. Pull Request resolved: #167507 Approved by: https://github.com/eqy, https://github.com/eee4017, https://github.com/ngimel
Both allocation to a cuda graph's private pool via stream capture and allocation to a memory pool in non-stream-captured code are supported. In the case of stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See pytorch#161583 (comment) for elaboration. This is lacking test cases for pagedable host memory copies. We must make sure that record_event() does not fail in that case. Pull Request resolved: pytorch#167507 Approved by: https://github.com/eqy, https://github.com/eee4017, https://github.com/ngimel
Only allocation to a cuda graph's private pool via stream capture is supported. Allocation to a memory pool in non-stream-captured code is not supported. There is no obvious usecase at this time for that. In stream capture, we refuse to reuse a host memory block as soon as record_event() is called on that block. This is to prevent a stream-captured CUDA kernel from reading different contents from a memory block than would be read if, counterfactually, this CUDA kernels were running eagerly on a cuda stream. See pytorch/pytorch#161583 (comment) for elaboration. ghstack-source-id: d1e30fc Pull-Request: pytorch/pytorch#167507
Includes basic support for stream capture to a cuda graph of pinned host memory to the memory pool.
We need a concept of memory pools in order to support a few things:
Note that (1) is most important right now, and therefore I have not added support for (2) at this time.
#146924 was my original attempt at this several months ago, where I thought that the major problem was how to implement a graph search algorithm inside the cuda graph to detect whether a given call to record_event() could be killed. In hindsight, this is not important in almost all use cases of interest today. In addition, this implementation also prevented me from recycling memory across seaprate cuda graphs sharing a memory pool (oops). Finally, it seems that there is preliminary evidence from #158352 (comment) that this graph search on the cuda graph can increase stream capture time substantially. I'm sure that is solvable, but I don't want to hold off on this. Instead, I use the current approach in CUDACachingAllocator.cpp: If a block is used on only one stream, and that stream is the stream that allocated it, then it can be reused. Otherwise, it can never be reused.
The initial use case of this is for offloading cached activations from the forward pass to the host, in order to reduce device memory pressure. Running these training workloads with cuda graphs allows them to run a few percentage points faster. Note that this use case does not ever recycle memory, since all allocations happen in the forward pass, and all frees happen in the backward pass.
@guangyey please give me your feedback on this. I had a choice between making breaking changes to CachingHostAllocatorImpl, which will break your XPU backend and potentially anyone else using your interface (is anyone?), or making a duplicate of it. I did the latter, since I needed to add a new field
was_allocated_during_stream_capture_to the Block template argument. This field needs to be inspected during the allocate() routine, in a way that cannot. Furthermore, I think that keeping implementation separate is good for when I eventually add support for reusing blocks during stream capture (see https://github.com/pytorch/pytorch/pull/146924/files#diff-5d3beb56bf9b6f380f91d5f6f063480ce2e14ca15c415d59d153436018089223R396-R404 for details on the algorithm), which will require two things: (1) usage of cuda graph APIs that exist in cuda but not sycl and (2) eagerly creating events when record_event() is called (which does not fit into the current implementation of CachingHostAllocatorImpl and its virtual methods at all).