Measuring peak memory usage: tracemalloc for pytorch?

stas: <p>I’ve been working on tools for memory usage diagnostics and management (<a href="https://github.com/stas00/ipyexperiments/" rel="nofollow noopener">ipyexperiments </a>) to help to get more out of the limited GPU RAM. The features include tracking real used and peaked used memory (GPU and general RAM). The peak memory usage is crucial for being able to fit into the available RAM.</p>
<p>Initially, I was spinning off a thread that recorded peak memory usage while the normal process runs.</p>
<p>Then I discovered that I can use python’s <a href="https://docs.python.org/3/library/tracemalloc.html" rel="nofollow noopener">tracemalloc</a> to measure the allocated general RAM, but more importantly peak memory usage:</p>
<pre><code class="lang-auto">import tracemalloc
tracemalloc.start()
do_someting_that_consumes_ram_and releases_some()
# show how much RAM the above code allocated and the peak usage
current, peak =  tracemalloc.get_traced_memory()
print(f"{current:0.2f}, {peak:0.2f}")
tracemalloc.stop()
</code></pre>
<p>So I no longer need the monitor thread for tracking general RAM but still need it for GPU RAM. Perhaps there is a way to have a similar to python’s tracemalloc functionality in pytorch? If not, and it’s something feasible, perhaps this can be a feature request? I’m not asking for all of tracemalloc’s features, but just the limited capacity to measure allocated/free gpu ram, as demonstrated in the example above.</p>
<p>Thank you.</p>

SimonW: <p>See <a href="https://pytorch.org/docs/master/notes/cuda.html#memory-management" rel="nofollow noopener">https://pytorch.org/docs/master/notes/cuda.html#memory-management</a></p>

stas: <p>I fail to see how your link answers my question, <a class="mention" href="/u/simonw">@SimonW</a>. I wasn’t asking about pytorch memory management, I’m asking about tracemalloc-like functionality.</p>
<p>Currently to get the peak GPU RAM used by pytorch, I need to:</p>
<ol>
<li>Start a thread that monitors gpu used memory every few msecs</li>
<li>Run the real code in the main process</li>
<li>Stop the thread and collect the results.</li>
</ol>
<p>It works, but it’s awkward and imprecise. And, of course, I have to clear cache to get the actual memory usage.</p>

SimonW: <p>From the link:</p>
<blockquote>
<p>You can use <a href="https://pytorch.org/docs/master/cuda.html#torch.cuda.memory_allocated"> <code>memory_allocated()</code> </a> and <a href="https://pytorch.org/docs/master/cuda.html#torch.cuda.max_memory_allocated"> <code>max_memory_allocated()</code> </a> to monitor memory occupied by tensors, and use <a href="https://pytorch.org/docs/master/cuda.html#torch.cuda.memory_cached"> <code>memory_cached()</code> </a> and<a href="https://pytorch.org/docs/master/cuda.html#torch.cuda.max_memory_cached"> <code>max_memory_cached()</code> </a> to monitor memory managed by the caching allocator.</p>
</blockquote>

stas: <p>I wish it were so.</p>
<p>I’m using nvml to get the memory readings and there is no match with torch memory functions reports.</p>
<hr>
<p>First, the cache: If I run torch.cuda.empty_cache(), shouldn’t these 2 returns 0’s?<br>
torch.cuda.memory_cached(), torch.cuda.max_memory_cached()<br>
they don’t. Why?</p>
<blockquote>
<p>Calling <a href="https://pytorch.org/docs/master/cuda.html#torch.cuda.empty_cache" rel="noopener nofollow ugc"> <code>empty_cache()</code> </a> can release all <strong>unused</strong> cached memory from PyTorch so that those can be used by other GPU applications.</p>
</blockquote>
<p>“can” == “will”? that’s very ambiguous if it can’t state exactly what it does.</p>
<p>I understand that probably torch.cuda.max_memory_cached() measures the highest cache ever allocated during a given process and it never goes down. It’s of a low utility, but fair enough.</p>
<p>Why doesn’t then torch.cuda.memory_cached() report 0 after the cache was cleared?</p>
<hr>
<p>Then comes torch.cuda.max_memory_allocated(), which again is of a very little utility, since it appears to be showing the max ever memory allocation, so once you hit some peak, you no longer can measure any peaks below the highest peak.</p>
<p>Let’s compare the torch memory reports with nvml reports in MBs (I call torch.cuda.empty_cache() before each measurement to kill the caching effects - and hoping that it actually does what it advertises to do).</p>
<p>Report columns:<br>
A torch.cuda.memory_allocated() (delta)<br>
B torch.cuda.max_memory_allocated() (delta)<br>
C nvml’s used memory (delta)<br>
D nvml’s peak memory measure via a peak monitor thread (delta)</p>
<pre><code class="lang-auto">          A    B    C     D
epoch 1  25 6050    80	6220
epoch 2   0    0     0	 924
epoch 3   0    0     0	 924
</code></pre>
<p>Note how column B is stuck at the same number, whereas column D shows that the peak memory used is different between the first epoch and the rest. I double checked that by watching the live nvidia-smi outputs. The GPU RAM consumption peaks at around 7GB during the first run and only at 1.6GB during consequent runs.</p>
<p>What pytorch needs is to have a stop/start method to reset the max measurements, like tracemalloc does. Otherwise, it’s like a depth gauge, where it only indicates the deepest depth one has gone to, but gives zero indication on the actual depth profile.</p>
<p>You can also see from the little table, that the actual GPU memory usage is larger than what pytorch reports. That means that pytorch isn’t reporting the extras the gpu card allocating beyond what pytorch requested. Albeit the difference is not huge.</p>
<p>Also, to clarify, I could easily measure peak memory usage if I were to deal with low-level code. Just take a snapshot of used memory before and after. What I’m trying to profile is top-level code that does internally a whole bunch of various allocations and deallocations.</p>
<p>So at the moment using a peak memory monitoring thread seems to be the only viable solution for measuring intermediate steps. And staying away from pytorch’s memory reporting functions seems to help as well, going directly for nvml reports, getting it directly from horse’s mouth, because that really is the only thing that counts when it comes to OOM problems.</p>
<p>Please don’t get me wrong, I’m not saying that pytorch’s memory reporting functions are incorrect. I’m just saying that they show only part of the picture and more detailed views are needed to be able to tune up one’s code more efficiently.</p>
<p>We are talking about a single process using single GPU setup here, so there is no interference from other processes.</p>
<p>I just hope that torch.cuda.empty_cache really works by releasing all of its cache. Since if it doesn’t then the measurements are incorrect.</p>
<p><strong>edit</strong>: I changed the numbers in the table to deltas (before and after), so it’s easier to make sense out of it.</p>

stas: <p>And for some of you code and hard numbers might speak better than words, so here is a simplified typical situation where some RAM gets allocated and some gets released:</p>
<pre><code class="lang-auto">def consume_gpu_ram(n): return torch.ones((n, n)).cuda()
def consume_gpu_ram_256mb(): return consume_gpu_ram(2**13)

# should be: 256 used, 512 peaked
c1 = consume_gpu_ram_256mb()
c2 = consume_gpu_ram_256mb()
del c1
</code></pre>
<p>I want to have a tool that will tell me that when this program has run, the memory consumption peaked at 512MB and then finally stabilized at 256MB (we are talking deltas here). I hope it’s clear to see:</p>
<pre><code class="lang-auto">start:                 -&gt; using   0MB
line 1: allocate 256MB -&gt; using 256MB
line 2: allocate 256MB -&gt; using 512MB
line 3: free 256MB     -&gt; using 256MB
</code></pre>
<p>There is no way current pytorch tools can tell me that there was a peak of 512MB there, unless it’s the very first code that was run and or no other previously run code ever consumed more RAM than this program requires. So here is a better example:</p>
<pre><code class="lang-auto"># part 1: render torch.cuda.max_memory_allocated() useless for future peak computations 
# in lower ranges by allocating a much bigger chunk of RAM and then freeing it.
z = [consume_gpu_ram_256mb() for i in range(4)] # 1GB
del z

# part 2: now measure:
# should be: 256 used, 512 peaked
c1 = consume_gpu_ram_256mb()
c2 = consume_gpu_ram_256mb()
del c1
</code></pre>
<p>torch.cuda.max_memory_allocated() will now report 1024, when part 2 consumed only 512MB at its peak.</p>
<p>And here is a simple program that shows how cuda tools are lacking, and how the workaround with peak measuring thread does measure the correct things (with a possible small error due to the thread’s unpredictable timing).</p>
<p>You will need pynvml installed: <code>pip/conda install nvidia-ml-py3</code></p>
<pre><code class="lang-auto">import threading, torch, time, pynvml

def preload_pytorch():
    torch.ones((1, 1)).cuda()

def gpu_mem_used(id):
    handle = pynvml.nvmlDeviceGetHandleByIndex(id)
    info = pynvml.nvmlDeviceGetMemoryInfo(handle)
    return int(info.used/2**20)

def gpu_mem_used_no_cache(id):
    torch.cuda.empty_cache()
    return gpu_mem_used(id)

def peak_monitor_start():
    global peak_monitoring
    peak_monitoring = True

    # this thread samples RAM usage as long as the current epoch of the fit loop is running
    peak_monitor_thread = threading.Thread(target=peak_monitor_func)
    peak_monitor_thread.daemon = True
    peak_monitor_thread.start()

def peak_monitor_stop():
    global peak_monitoring
    peak_monitoring = False

def peak_monitor_func():
    global nvml_peak, peak_monitoring
    nvml_peak = 0
    id = torch.cuda.current_device()

    while True:
        nvml_peak = max(gpu_mem_used(id), nvml_peak)
        if not peak_monitoring: break
        time.sleep(0.001) # 1msec

def consume_gpu_ram(n): return torch.ones((n, n)).cuda()
def consume_gpu_ram_256mb(): return consume_gpu_ram(2**13)

peak_monitoring = False
nvml_peak = 0
preload_pytorch()
pynvml.nvmlInit()
id = torch.cuda.current_device()

# push the pytorch's peak gauge high up and then release the memory
z = [consume_gpu_ram_256mb() for i in range(4)] # 1GB
del z

peak_monitor_start()
nvml_before = gpu_mem_used_no_cache(id)
cuda_before = int(torch.cuda.memory_allocated()/2**20)

# should be: 256 used, 512 peaked
c1 = consume_gpu_ram_256mb()
c2 = consume_gpu_ram_256mb()
del c1

# code finished
peak_monitor_stop()
nvml_after = gpu_mem_used_no_cache(id)
cuda_after = int(torch.cuda.memory_allocated()/2**20)
cuda_peak  = int(torch.cuda.max_memory_allocated()/2**20)
print("nvml:", nvml_after-nvml_before, nvml_peak-nvml_before)
print("cuda:", cuda_after-cuda_before, cuda_peak-cuda_before)

</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">nvml: 256 512
cuda: 256 1024
</code></pre>
<p>cuda tools can’t give me the right answer of 512MB here.</p>
<p>Now look at the simplicity of tracemalloc doing the same thing:</p>
<pre><code class="lang-auto">import tracemalloc, numpy as np

def consume_cpu_ram(n): return np.ones((n, n))
def consume_cpu_ram_128mb(): return consume_cpu_ram(2**12)

# push the process' peak gauge high up and then release the memory
z = [consume_cpu_ram_128mb() for i in range(8)] # 1GB
del z

tracemalloc.start()

# expecting peak requirements of 256MB, and final 128MB
a1 = consume_cpu_ram_128mb()
a2 = consume_cpu_ram_128mb()
del a1

cpu_current, cpu_peak = list(map(lambda x: int(x/2**20), tracemalloc.get_traced_memory()))
tracemalloc.stop()

print(cpu_current, cpu_peak)
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">128 256
</code></pre>
<p>pytorch could do exactly the same, here is some pseudo-code:</p>
<pre><code class="lang-auto">class max_memory_allocated_local():
    def start(self):
        self.begin = memory_allocated()
        max_memory_allocated_local_reset() # put the peak gauge to zero
    def stop(self):
        self.end  = memory_allocated()
        self.peak = max_memory_allocated_local()
    def get_traced_memory(self):    
        return self.end-self.begin, self.peak-self.begin
</code></pre>
<p>it introduces a local max_memory_allocated_local, which can be reset by the user, but otherwise working the same as max_memory_allocated().</p>

SimonW: <p>The section is just a faq, if you click on the function names it leads you to the doc pages, which are very clear and what the functions do. There is no need to guess.</p>
<p>These numbers naturally don’t match exactly as nvidia-smi because of cuda context.</p>
<p>As said in the page, empty_cache only frees <strong>unused</strong> cached memory. If a memory block in the caching allocator backs any tensor, it won’t get freed. So memory_cached is not 0 after you call empty_cache.</p>
<p>I agree that resetting methods will be useful. Please submit a feature request.</p>

stas: <blockquote>
<p>These numbers naturally don’t match exactly as nvidia-smi because of cuda context.</p>
</blockquote>
<p>Which renders the pytorch method <code>memory_allocated()</code> not so useful, because it doesn’t help to know how much memory was really allocated, if one is trying to calculate their hyperparameters based on available free-memory. But I suppose there is no way pytorch could even try to estimate the extras that it can’t account for. It sounds like something is wrong in that logic, but unfortunately I don’t know anything about CUDA internals to tell why it can’t communicate the client how much memory it allocated so that that memory_allocated could match the real thing. So I guess direct access to the the gpu via nmvl is really the only method that I know of that can be used for precise measurements, and pytorch’s <code>memory_allocated</code> is more of an estimate.</p>
<blockquote>
<p>I agree that resetting methods will be useful. Please submit a feature request.</p>
</blockquote>
<p>Done: <a href="https://github.com/pytorch/pytorch/issues/15968" class="inline-onebox" rel="noopener nofollow ugc">user-resettable torch.cuda.max_memory_allocated() · Issue #15968 · pytorch/pytorch · GitHub</a></p>
<p>Thank you for following up on this thread, <a class="mention" href="/u/simonw">@SimonW</a></p>

SimonW: <aside class="quote no-group" data-username="stas" data-post="8" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stas/48/8409_2.png" class="avatar"> stas:</div>
<blockquote>
<p>But I suppose there is no way pytorch could even try to estimate the extras that it can’t account for.</p>
</blockquote>
</aside>
<p>You are right. Unfortunately we can’t know the exact size of the cuda context. The <code>memory_allocated/cached</code> methods report all memory allocated by pytorch. But if cuda context is out of our control. But, I think you can safely assume that cuda context is 100~250MB.</p>
<aside class="quote no-group" data-username="stas" data-post="8" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stas/48/8409_2.png" class="avatar"> stas:</div>
<blockquote>
<p>Done: <a href="https://github.com/pytorch/pytorch/issues/15968">https://github.com/pytorch/pytorch/issues/15968 </a></p>
</blockquote>
</aside>
<p>I submitted a patch to add those methods. Could you check the PR (linked in the issue) and let me know if they satisfy your needs?</p>

stas: <aside class="quote no-group quote-modified" data-username="SimonW" data-post="9" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/48/1702_2.png" class="avatar"> SimonW:</div>
<blockquote>
<blockquote>
<p>But I suppose there is no way pytorch could even try to estimate the extras that it can’t account for.</p>
</blockquote>
<p>You are right. Unfortunately we can’t know the exact size of the cuda context. The <code>memory_allocated/cached</code> methods report all memory allocated by pytorch. But if cuda context is out of our control. But, I think you can safely assume that cuda context is 100~250MB.</p>
</blockquote>
</aside>
<p>Are you talking about the context of loading cuda, the first time it’s used? I get a pretty consistent 0.5GB per process with cuda100.</p>
<p>If you’re talking about cuda context allocated during the actual use of cuda, how can it be a relatively fixed number, regardless of memory usage? clearly, I know very little about it cuda context, and can only talk about the actual usage numbers I see. Do you have a recommendation for a concise description/guide to read to understand cuda context w/o a need to study something huge?</p>
<p>Thank you.</p>

stas: <aside class="quote no-group quote-modified" data-username="SimonW" data-post="9" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/48/1702_2.png" class="avatar"> SimonW:</div>
<blockquote>
<blockquote>
<p>Done: <a href="https://github.com/pytorch/pytorch/issues/15968" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/issues/15968 </a></p>
</blockquote>
<p>I submitted a patch to add those methods. Could you check the PR (linked in the issue) and let me know if they satisfy your needs?</p>
</blockquote>
</aside>
<p>It solves the problem. Thank you very much.<br>
Here is a use case that others can use and feel free to add it to the test suite:</p>
<pre><code class="lang-auto">import torch
def consume_gpu_ram(n): return torch.ones((n, n)).cuda()
def consume_gpu_ram_256mb(): return consume_gpu_ram(2**13)

def b2mb(x): return int(x/2**20)
class TorchTracemalloc():

    def __enter__(self):
        self.begin = torch.cuda.memory_allocated()
        torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero
        return self

    def __exit__(self, *exc):
        self.end  = torch.cuda.memory_allocated()
        self.peak = torch.cuda.max_memory_allocated()
        self.used   = b2mb(self.end-self.begin)
        self.peaked = b2mb(self.peak-self.begin)
        print(f"delta used/peak {self.used:4d}/{self.peaked:4d}")

# push the process' peak gauge high up and then release all the memory
# expecting 0 used / 1024 peaked
with TorchTracemalloc() as tt:
    z = [consume_gpu_ram_256mb() for i in range(4)] # 1GB
    del z
assert tt.used == 0 and tt.peaked == 1024

# allocate, allocate, release half
# expecting 256 used / 512 peaked
with TorchTracemalloc() as tt:
    # should be: 256 used, 512 peaked
    c1 = consume_gpu_ram_256mb()
    c2 = consume_gpu_ram_256mb()
    del c1
assert tt.used == 256 and tt.peaked == 512
del c2 # reset for next test

# allocate, allocate, release all
# expecting 0 used / 512 peaked
with TorchTracemalloc() as tt:
    # should be: 0 used, 512 peaked
    c1 = consume_gpu_ram_256mb()
    c2 = consume_gpu_ram_256mb()
    del c1, c2
assert tt.used == 0 and tt.peaked == 512

# allocate, don't release
# expecting 1536 used / 1536 peaked
with TorchTracemalloc() as tt:
    z = [consume_gpu_ram_256mb() for i in range(6)]
assert tt.used == 1536 and tt.peaked == 1536
del z # reset for next test
</code></pre>
<p>asserts already do the checks, but there is a visual check as well, output:</p>
<pre><code class="lang-auto">delta used/peak    0/1024
delta used/peak  256/ 512
delta used/peak    0/ 512
delta used/peak 1536/1536
</code></pre>

SimonW: <aside class="quote no-group" data-username="stas" data-post="10" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stas/48/8409_2.png" class="avatar"> stas:</div>
<blockquote>
<p>Are you talking about the context of loading cuda, the first time it’s used? I get a pretty consistent 0.5GB per process with cuda100.</p>
</blockquote>
</aside>
<p>Yes. Each cuda context is fixed size. For PyTorch, we use something called primary ctx, which is unique per process.<br>
<del>The rest of 0.5GB comes from the caching allocator. Basically when you first allocates cuda memory, if it is smaller than what we call a “block” (which I think is 256MB), we allocate a whole block of memory, and cache the rest. Also, using random sampling methods for the 1st time also allocates additional cuda rng state, but that is also one time and fixed size.</del></p>
<p>Edit: The above info is not accurate. See follow up below.</p>

stas: <blockquote>
<p>Basically when you first allocates cuda memory, if it is smaller than what we call a “block” (which I think is 256MB), we allocate a whole block of memory, and cache the rest.</p>
</blockquote>
<p>I’m trying to see what you’re saying by measuring it and it doesn’t match up:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch 
import pynvml
pynvml = pynvml
pynvml.nvmlInit()
preload = 0
prev = 0
def nvml_used():
    handle = pynvml.nvmlDeviceGetHandleByIndex(torch.cuda.current_device())
    info   = pynvml.nvmlDeviceGetMemoryInfo(handle)
    return b2mb(info.used)
def b2mb(x): return int(x/2**20)
def consume_gpu_ram(n): return torch.ones((n, n)).cuda()
def consume_gpu_ram_64mb():  return consume_gpu_ram(2**12)
def consume_gpu_ram_256mb(): return consume_gpu_ram(2**13)
def mem(): 
    global preload, prev
    this = nvml_used()
    delta_cached = this - preload
    delta_used = this - prev
    prev = this
    print(f"   nvml used: {delta_used:4d}, allocated: {delta_cached:4d}")
    print(f"pytorch used: {b2mb(torch.cuda.memory_allocated()):4d}, allocated: {b2mb(torch.cuda.memory_cached()):4d}\n")

print("preloading:")
mem()
_ = torch.ones((1, 1)).cuda()
mem()
preload = nvml_used()

print("\nrunning:")
x1 = consume_gpu_ram_64mb()
mem()
x2 = consume_gpu_ram_256mb()
mem()
del x2
mem()
x3 = consume_gpu_ram_64mb()
mem()
</code></pre>
<p>So we have two parts to the program, the first one forces the loading of all that’s needed to run cuda, which allocates 0.5GB on the card and then it does a few allocations. And we measure torch.cuda’s allocated and cached and compare it to nvml numbers of allocated - I rigged the nvml_used() function to match torch.cuda.memory_cached()'s behavior by subtracting the memory allocated right after preloading happened, hence the different behavior during and after preloading.</p>
<p>nvml used reports don’t report the right thing once memory gets reported and caching kicks in. <code>empty_cache</code> needs to be called to get it right, but then it’d mess up with memory_cached repors which this code is exploring, so you can ignore nvml_used numbers.</p>
<p>Running it:</p>
<pre><code class="lang-auto">preloading:
   nvml used:   10, allocated:   10
pytorch used:    0, allocated:    0

   nvml used:  495, allocated:  505
pytorch used:    0, allocated:    1


running:
   nvml used:   64, allocated:   64
pytorch used:   64, allocated:   65

   nvml used:  256, allocated:  320
pytorch used:  320, allocated:  321

   nvml used:    0, allocated:  320
pytorch used:   64, allocated:  321

   nvml used:    0, allocated:  320
pytorch used:  128, allocated:  321
</code></pre>
<p>First, where is that block of cache you were referring to? When 0.5 GB is consumed pytorch reports 1MB of cache as compared to you saying that it should have allocated a block of about 256MB in cache.</p>
<p>Second, any reason why <code>memory_allocated</code> isn’t including those 0.5GB? Surely, for a user who isn’t going to dig into details this would appear very confusing. If she starts with a card not used by any process and allocates a tensor of 100MB, they would see it consuming 600MB, but reported only 100MB by pytorch.</p>
<p>Finally, from this report I understand that I didn’t original get what <code>memory_cached()</code> really is. <code>memory_cached</code> isn’t showing how much memory pytorch has cached that’s not currently used. It just says how much memory pytorch pre-allocated, but only some of it it may be free. So really to see how much available to use memory in cache is  <code>memory_cached()-memory_allocated()</code>.</p>
<p>So my only remaining puzzle to understand is whether any of the 0.5GB allocated on the first cuda call is ever available to the user for allocating tensors, because currently it appears that when I use pytorch my card’s RAM is really TOTALRAM-0.5GB*num_of_processes. So If 2 processes use the card, I lose 1GB (!) of RAM.</p>
<p>Thank you.</p>
<p>edit: in case you decide to study the code do note that it has an issue of reporting delta used for nvml, but absolute used (allocated) for pytorch.cuda. So the first column (used) is somewhat bogus. while it’s easy to fix <code>pytorch used</code> to report delta, nvml can’t report the correct numbers because of caching here, so really just ignore the first column.</p>

SimonW: <p>Apologies. I went ahead and check the code. The smallest block size is 1MB. So the memory usage you see is not related to that.</p>
<p>However, it is still very possible that most of 0.5GB is CUDA ctx. The actual size depends on the CUDA version &amp; card type (<a href="https://devtalk.nvidia.com/default/topic/1044191/cuda-programming-and-performance/determine-memory-cuda-context-memory-usage/post/5304742/#5304742" rel="nofollow noopener">reference</a>). I checked with a 1080Ti &amp; CUDA 8 I have access to, it takes 343MB. And from my memory on a Titan Xp and CUDA 9 it took ~200MB.</p>
<p>Google search shows other people reporting a ctx as large as 500MB: <a href="https://stackoverflow.com/questions/12109985/cudasetdevice-allocates-more-than-580-mb-of-global-memory/14101969" rel="nofollow noopener">https://stackoverflow.com/questions/12109985/cudasetdevice-allocates-more-than-580-mb-of-global-memory/14101969</a>.</p>
<p>You can check by running another CUDA program and see if it also allocates ~500MB on the first CUDA call.</p>
<p>Btw, in your code, do you mean to use <code>memory_cached</code> rather than <code>max_memory_cached</code>?</p>

stas: <aside class="quote no-group" data-username="SimonW" data-post="14" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/48/1702_2.png" class="avatar"> SimonW:</div>
<blockquote>
<p>Apologies. I went ahead and check the code. The smallest block size is 1MB. So the memory usage you see is not related to that.</p>
</blockquote>
</aside>
<p>OK, so 1MB it is. We are on the same page now.</p>
<blockquote>
<p>However, it is still very possible that most of 0.5GB is CUDA ctx. The actual size depends on the CUDA version &amp; card type (<a href="https://devtalk.nvidia.com/default/topic/1044191/cuda-programming-and-performance/determine-memory-cuda-context-memory-usage/post/5304742/#5304742" rel="noopener nofollow ugc">reference</a>). I checked with a 1080Ti &amp; CUDA 8 I have access to, it takes 343MB. And from my memory on a Titan Xp and CUDA 9 it took ~200MB.</p>
</blockquote>
<p>Fascinating - the larger the card the less overhead! Mine is 8GB and is taking 0.5GB.</p>
<p>Now I know it’s card-dependent. thank you!</p>
<blockquote>
<p>Btw, in your code, do you mean to use <code>memory_cached</code> rather than <code>max_memory_cached</code> ?</p>
</blockquote>
<p>Yes, you’re correct. fixed. Thank you.</p>

stas: <p>Going back to the initial question - would it be difficult to add another function that measures the allocated memory but is resettable too?</p>
<p>Here is the <code>tracemalloc.get_traced_memory()</code> sample again:</p>
<pre><code class="lang-auto">import tracemalloc
tracemalloc.start()
do_someting_that_consumes_ram_and releases_some()
# show how much RAM the above code allocated and the peak usage
current, peak =  tracemalloc.get_traced_memory()
print(f"{current:0.2f}, {peak:0.2f}")
tracemalloc.stop()
</code></pre>
<p>So with your patch we now have <code>peak</code> covered, by resetting <code>torch.cuda.reset_max_memory_allocated()</code> before a code to be profiled is run. But the only way to measure the consumed memory by the same code currently is to force <code>clear_cache</code> before and after the code is run. Note, consumed measures how much allocated minus freed a given piece of code generated, disregarding the caching mechanism.</p>
<p>Except, of course, it must leave <code>torch.cuda.memory_allocated()</code> alone, since we don’t want to break that functionality. so it’ll require 2 functions, one that measures and one that that resets it.</p>

SimonW: <aside class="quote no-group" data-username="stas" data-post="15" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stas/48/8409_2.png" class="avatar"> stas:</div>
<blockquote>
<p>Fascinating - the larger the card the less overhead!</p>
</blockquote>
</aside>
<p>I don’t have access to the Xp now so I could remembered wrong :).</p>
<aside class="quote no-group" data-username="stas" data-post="16" data-topic="34067">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stas/48/8409_2.png" class="avatar"> stas:</div>
<blockquote>
<p>But the only way to measure the consumed memory by the same code</p>
</blockquote>
</aside>
<p>What do you mean by “measure the consumed memory by code”? If you are looking for how large are the new tensors created by running this segment of code, can’t you just look at the difference of <code>memory_allocated</code>?</p>

stas: <blockquote>
<p>If you are looking for how large are the new tensors created by running this segment of code, can’t you just look at the difference of <code>memory_allocated</code> ?</p>
</blockquote>
<p>You’re correct, <a class="mention" href="/u/simonw">@SimonW</a>. It works just fine. For some reason I thought caching would affect the numbers, but it doesn’t. I verified it with some more tests.</p>
<p>So I am all set, just need to wait for pytorch-1.0.1 to be out.</p>
<p>Thank you, again.</p>

stas: <p><a class="mention" href="/u/simonw">@SimonW</a>, I have been thinking about the solution you implemented and there is a need for scoped-memory measurements, where scopes can overlap or be nested.</p>
<p>Scenario 1: an application relying on the normal functioning (pre-reset implementation) <code>max_memory_allocated</code> or <code>max_memory_cached</code> could now malfunction if some other application resets either or both (action a a distance).</p>
<p>Scenario 2: two profilers measuring different scopes. Say one measuring at a function level, another at a wider or narrower scope. Since there is only one counter they will be resetting each other’s measurements. python’s <code>tracemalloc</code> has the same issue, since it doesn’t create an object instance for the counter.</p>
<p>The 2nd scenario is not hypothetical, it’s actually a need I have right now as I have different profilers measuring different scopes. There are in different applications so they can’t really communicate with each other to keep each other in sync with reset calls. e.g. I have one profiler running on the train loop epoch-level, another on the jupyter cell-level, yet another on larger parts of the notebook. And unfortunately, my current measuring thread approach is clearly failing to catch all peaks <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=6" title=":frowning:" class="emoji" alt=":frowning:"> so it’d be extremely helpful to be able to switch to use <code>max_memory_allocated</code> yet different instances of it in different scopes.</p>
<p>So I need to be able to do something like:</p>
<pre><code class="lang-auto">max_obj1 = MaxMemoryAllocated()
# run some code 1
for epoch in epochs:
    max_obj2 = MaxMemoryAllocated()
    # run epoch code
    peak_epoch = max_obj2.peak()
# run some code ...
peak = max_obj1.peak()
del max_obj1
</code></pre>
<p>Of course, those would be unrelated applications, this code sample is just demonstrating how their execution will overlap and why the current implementation is insufficient.</p>

stas: <p>and also I’m missing <code>torch.cuda.free_memory</code> or <code>total_memory</code>? How can I know from torch.cuda how much memory do I have available (or total, from which I could derived free)?</p>
<p>Thank you.</p>