Sequential inference of multiple models in a memory limited environment

veritas: <p>Dear fellow PyTorch users, I have a question concerning deployment in memory-constrained environments.</p>
<p>I wish to deploy a large number (more than 20) of models on the same data. For example, one model would detect the cars in the image and a separate network would segment the streets, another would apply domain transfer to improve the model performance of the other networks if the given scene has unusual characteristics (rain, low lighting, etc.).</p>
<p>Though it may be better to have a single model perform these tasks, I have found that using separate models gives much better performance. Also, our clients (and I myself) value the modularity that using separate modules provides.</p>
<p>Currently, using Tensorflow in graph mode provides automatic memory optimizations in GPU and CPU RAM memory. Even if there are 20 models, with 1GB of parameters in each model, the GPU memory allocated is not 20GB because Tensorflow automatically loads only the necessary models from disk to RAM and GPU DRAM. This saves on both GPU memory and RAM, both of which are valuable resources in deployment.</p>
<p>Is there any way to do something similar in PyTorch? I would like to run multiple, potentially very heavy, models in sequence on the same data without exhausting my GPU and RAM memory limitations. I have considered manually loading and sending each model in sequence. However, this would lead to inefficiencies if disk IO or CPU/GPU transfer were a bottleneck. Also, TorchScript models seem to discourage transferring the model after scripting is complete.</p>
<p>I have considered converting to ONNX. However, ONNX supports a relatively limited set of PyTorch operators and has numerous implementation differences that may lead to different outputs. I would like to ask if there is a PyTorch native solution to this problem.</p>
<p>Many thanks in advance for any replies.</p>

Chris_77: <p>Good day vertias,</p>
<p>without knowing details about the exact memory footprint, architecture and payload of inferences, it is not easy to give detailed help, but:</p>
<ul>
<li>Have you taken a look at the <a href="https://pytorch.org/blog/pytorch-1.9-released/#beta-mobile-interpreter" rel="noopener nofollow ugc">mobile Interpreter</a>? This may reduce the memory footprint by up to 75%.</li>
<li>Consider spending more time on producing an end-to-end architecture, since this will save all your problems in one go. A good segmentation architecture should in general be capable of segmenting streets AND cars.</li>
<li>Reduce the architecture of your model by applying <a href="https://en.wikipedia.org/wiki/Knowledge_distillation" rel="noopener nofollow ugc">Knowledge Distillation</a>.</li>
</ul>
<p>Cheers.</p>

veritas: <p><a class="mention" href="/u/chris_77">@Chris_77</a> Thank you for your reply.</p>
<p>As you have mentioned, using smaller models would be one possible solution.</p>
<p>However, the problem that I face is applying multiple models on the same data <strong><em>in sequence</em></strong>. This setting is not as uncommon as one may imagine.</p>
<p>In this circumstance, I think that it is inefficient to use scarce memory to hold models that do not have to be used immediately.</p>