Memory management, memory not returned after CPU -> GPU
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.5k
- PR merge metrics
- PR metrics pending
Description
I have a simple C++ libtorch (2.10.0) program:
```
void PrintMemory(const char* label)
{
PROCESS_MEMORY_COUNTERS_EX info{};
GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&info), sizeof(info));
printf("PrivateUsage (committed pages): %.3f GB\n", (float)info.PrivateUsage / 1024 / 1024 / 1024);
}
```
```
PrintMemory("Before");
auto tensor = torch::zeros({ 16384, 16384, 4 }); // also 4GB float32
PrintMemory("After Init");
tensor = tensor.to(torch::kCUDA);
PrintMemory("After Cuda");
c10::cuda::CUDACachingAllocator::emptyCache();
PrintMemory("After emptyCache");
```
```
[Before]
PrivateUsage (committed pages): 0.700 GB
[After Init]
PrivateUsage (committed pages): 4.716 GB
[After Cuda]
PrivateUsage (committed pages): 4.952 GB
[After emptyCache]
PrivateUsage (committed pages): 4.952 GB
```
But CPU memory is nor returned.
However, when I do this:
```
PrintMemory("Before");
auto tensor = torch::zeros({ 16384, 16384, 4 }); // also 4GB float32
PrintMemory("After Init");
tensor.reset();
PrintMemory("After reset");
```
```
[Before]
PrivateUsage (committed pages): 0.701 GB
[After Init]
PrivateUsage (committed pages): 4.716 GB
[After Reset]
PrivateUsage (committed pages): 0.701 GB
```
memory is returned.
Why in case of CUDA transfer, memory stays allocated (or seems to be allocated)? When I use the to CUDA transfer for large models (not a single Tensor like in this case), the RAM stays filled (or seems) and I cannot allocate more. GPU is also correctly allocated.
cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @iremyux @Blackhex @jbschlosser @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia
Contributor guide
Assessment
This issue has not been assessed yet.