How do I create Torch Tensor without any wasted storage space/baggage?

tlim: <p>From testing experience, the first Tensor push to GPU will roughly take up to 700-800 MiB of the GPU VRAM.  You can then allocate more tensor to GPU without a shift in the VRAM until you have exceeded the pre-allocated space given from the first Tensor.</p>
<p>Example:</p>
<pre><code class="lang-auto">x = torch.tensor(1).cuda()
# ~770 MiB allocated
z = torch.zeros((1000)).cuda()
# VRAM not affected.
d = torch.zeros((1000000000)).cuda() 
# plus ~1000MiB (more accurate allocation according to bytes allocated for the datatype multiply by itemsize.
</code></pre>
<p>How do I create the first Tensor without 700+MiB worth of VRAM baggage? This will be especially useful for inference/deployment. Most Deep Learning applications have static inputs allocation; for example, a batch of images (ex: size (10,3,200,200) in float32) will just require 4.8MB of VRAM space. It does not make any sense in deployment to waste so much space for your model inputs.</p>
<p><strong>Are there a specific way to initialise PyTorch Tensor similar to how we do it in PyCUDA/CUDA with allocate and copy?</strong> It will awesome if there is a way to allow developers to specify the nbytes of their Tensor like CUDA.</p>
<p>Hopefully there is already a way in PyTorch or else only the implementation of CUDA code itself can ensure <strong>CORRECT</strong> memory allocation for our inputs. PyCUDA GpuArray does not transfer well to PyTorch Tensor (the other way around works though) which leads to the need to code every single function in PyCUDA from scratch.</p>

ptrblck: <aside class="quote no-group" data-username="tlim" data-post="1" data-topic="131134">
<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/tlim/48/32364_2.png" class="avatar"> tlim:</div>
<blockquote>
<p>From testing experience, the first Tensor push to GPU will roughly take up to 700-800 MiB of the GPU VRAM. You can then allocate more tensor to GPU without a shift in the VRAM until you have exceeded the pre-allocated space given from the first Tensor.</p>
</blockquote>
</aside>
<p>No, that’s not the case. PyTorch will create the CUDA context in the very first CUDA operation, which can use ~600-1000MB of GPU memory depending on the CUDA version as well as the used device.<br>
PyTorch itself will allocate the needed memory and will use an internal cache mechanism. You can read more about it <a href="https://pytorch.org/docs/stable/notes/cuda.html">here</a>.</p>
<aside class="quote no-group" data-username="tlim" data-post="1" data-topic="131134">
<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/tlim/48/32364_2.png" class="avatar"> tlim:</div>
<blockquote>
<p>How do I create the first Tensor without 700+MiB worth of VRAM baggage?</p>
</blockquote>
</aside>
<p>That’s not possible, since the CUDA context has to be created, which contains e.g. the device code.</p>

tlim: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> ,</p>
<p>Thank you for your prompt reply. I did some further digging with thanks to your information.</p>
<p>I think I have a few follow up questions if you do not mind:</p>
<ol>
<li>Is there a way in torch internals to lower the VRAM necessary for the CUDA context?</li>
<li>(link to 1) Is there a way to remove unnecessary internal caching if I am 100% sure that the only torch tensor that will be allocated is the input tensor? As expected, I always see a lot of memory reserved that is not in use.</li>
<li>If PyCUDA has already created context, must we still create a new context for PyTorch on the same running script?</li>
</ol>
<p>I have PyCUDA + TensorRT code in the same script with my PyTorch pre/post-processing code, the context has already been created:</p>
<pre><code class="lang-auto">import pycuda.driver as cuda
ctx = cuda.Device(0).make_context()
</code></pre>
<p>It seems that creating the context as shown above only takes up 200 MiB while PyTorch initialisation takes up to 770MiB. Tested on my GTX1080, CUDA11.1.</p>
<p>If there is no way to remove the memory caching/reserved in PyTorch Tensor then it seems that PyTorch can only be used for quick development to train and test models. Seems like the workflow has to be:</p>
<p>PyTorch Development → Get model weights → Convert to TensorRT → Convert all pre and post-processing to CUDA implementation (super time consuming)</p>

ptrblck: <ol>
<li>You could rebuild PyTorch and remove libraries shipping with device code, such as cuDNN. While this would yield a performance hit, no cuDNN kernels will be loaded and stored in the CUDA context.</li>
<li>Yes, you can use <code>PYTORCH_NO_CUDA_MEMORY_CACHING=1</code> to disable the cache.</li>
<li>PyTorch needs to load its own kernels as well as device code from other libs (cuDNN, cublas, NCCL, etc.), which might not be the case for PyCUDA. It might be possible to share some driver-related code in the context, but I don’t know how much memory savings would be expected and haven’t experimented with it.</li>
</ol>

tlim: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, once again, thank you for the prompt reply.</p>
<p>From your answers, I guess the only way is to figure out what library is the PyTorch functions specifically using (ex: for concat, stacking, NMS) and remove the rest during build (ex: cuDNN is not needed as DNN is managed by TensorRT).</p>
<p>Appreciate the help.</p>
<hr>
<p><strong>Potential future discussion for AI developers that stumbles upon this thread</strong>.</p>
<p>It seems that if we want to deploy using PyTorch functions (NMS, Stacking, etc) that made our lives so much easier, we have to deal with the huge memory allocated for its context.</p>
<p>This problem must be faced by many that want to deploy multiple modules, each leveraging PyTorch for ease of GPU programming. The context memory stacks up very quickly to fill up the entire VRAM of the GPU especially since deployment based GPU usually only has 8MiB-10MiB memory.</p>
<p>My personal workflow is that I use PyTorch for preprocessing and postprocessing as manipulating PyTorch Tensor is very easy.  The pointer of PyTorch processed Tensor (<a href="https://gist.github.com/szagoruyko/440c561f7fce5f1b20e6154d801e6033" class="inline-onebox" rel="noopener nofollow ugc">pycudatorch.py · GitHub</a>) can then be passed into TensorRT (optimised model), output from TensorRT will remain as a PyTorch Tensor allowing very easy postprocessing (PyTorch readily available functions) and you can also use CUDA kernels that you have written to leverage the GPU parallelism (PyCUDA) when you need more control over the implementations of individual threads that PyTorch does not provide. <strong>The obvious weakness of this workflow is the PyTorch context memory allocation, I currently cannot run more than 2 models in my GPU due to this.</strong></p>
<p>I guess the only way for developers that need to deploy multiple models/modules and are constrained by the GPU memory (usually the case) is to <strong>not use PyTorch at all for deployment</strong>.  Write functions you need in CUDA yourself and use TensorRT as usual.</p>

googlebot: <p>you can try recompiling pytorch with cuda kernels runtime compiled on demand, I think config line like TORCH_CUDA_ARCH_LIST=“6.0+PTX” would do that. First run will be very slow, then they’ll be loaded from disk cache. I’m not certain that this will have a big effect though.</p>
<p>I’ve also heard something about “static runtime” somewhere on github, not sure if anything materialized from that.</p>

ptrblck: <p>While building PyTorch for your compute capability would speed up the build process, the CUDA context should already only load the device kernels, so I would not assume to see a reduction in memory.</p>
<p><a class="mention" href="/u/tlim">@tlim</a> besides the loaded kernels coming from libraries, you would also need to check the native PyTorch kernels, which are often templated for various <code>dtypes</code> etc. and would also use memory on the device. <a class="mention" href="/u/eqy">@eqy</a> profiled the CUDA context size a while ago and could probably add more information.</p>

eqy: <p>If you aren’t using any linear algebra routines that would be provided by <code>MAGMA</code>, compiling with <code>USE_MAGMA=0</code> should also give you around 100MiB? or so of memory savings.</p>
<p>Of course, once you are willing to pay recompilation costs and are willing to specialize your build for certain use cases, the world is your oyster when it comes to deleting unused GPU kernels to save memory usage…<br>
The hard limit for this would probably be about 300MiB or the CUDA context size.</p>

googlebot: <aside class="quote no-group" data-username="ptrblck" data-post="7" data-topic="131134">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>the CUDA context should already only load the device kernels</p>
</blockquote>
</aside>
<p>And somehow you can get small (&lt;100mb) contexts in general, but big monoliths like pytorch have huge contexts from the start.</p>

ptrblck: <p>That’s interesting, as I haven’t seen such a small context in a while. Could you point me to an example, if possible?<br>
E.g using the <a href="https://github.com/NVIDIA/cuda-samples/tree/master/Samples/matrixMul">matrixMul example</a> on an A100 I see a memory usage of ~400MB for a matmul of <code>[1, 1] x [1, 1]</code>.<br>
Building for sm_80 or all architectures also doesn’t change the usage.</p>

googlebot: <p>Yeah, I quoted a number for ancient cards (or old sm, not sure), 200mb is a more reasonable baseline measured above. The point was that pytorch’s context is bigger than that baseline, before kernels are needed.</p>

tlim: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>,  regarding your mention of different GPU on CUDA version producing different context memory allocation, I tested a GTX1080 and a GTX2080 (on CUDA 11.1, PyTorch 1.8.1) and it seems that the GTX1080 allocates 771 MiB while the GTX2080 allocates 1177MiB.</p>
<p>I am wondering whether is there some sort of <strong>general guides on CUDA, PyTorch, Driver, GPU Versioning to memory needed for context allocation.</strong>  This could be a much easier thing to follow than trying out unique compilations.</p>
<p>At the end of the day, I think it seems that writing the processing code in CUDA is the only way to go about for deployment due to VRAM limitation.</p>

ptrblck: <aside class="quote no-group" data-username="tlim" data-post="12" data-topic="131134">
<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/tlim/48/32364_2.png" class="avatar"> tlim:</div>
<blockquote>
<p>I am wondering whether is there some sort of <strong>general guides on CUDA, PyTorch, Driver, GPU Versioning to memory needed for context allocation.</strong> This could be a much easier thing to follow than trying out unique compilations.</p>
</blockquote>
</aside>
<p>You could try to see some theoretical requirements which would depend on the actual hardware setup, but given that each PyTorch, cuDNN, cublas, CUDA, NCCL version might add/remove kernels for a specific compute capability, the easiest way it to profile the CUDA context directly.</p>

tlim: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  I guess that’s the only way to go about it. Thanks for the advice.</p>