Tensor parallelism in image models like Unet

cporrasn: <p>Hello!!!<br>
I describe my problem.</p>
<p>I have a PC with 2 Tesla P40 GPUs. I have a Unet model that segments an image for me. Given a resizable image, I get the binary mask.</p>
<p>However, I have 4k images, which a single GPU does not fit, and I get a memory error simply with the processing of the 1st layer.</p>
<p>I have tried the FSDP module of pytorch, however, when I put 2 processes in it, it does the segmentation 2 times, and gives me a memory error for the 4k images. Therefore I assume that model parallelism alone is not enough, since a single layer (the first) does not fit on a single GPU.</p>
<p>I was thinking about tensor parallelism, with references like:<br>
1- <a href="https://github.com/NVIDIA/Megatron-LM" class="inline-onebox" rel="noopener nofollow ugc">GitHub - NVIDIA/Megatron-LM: Ongoing research training transformer models at scale</a> (but I interpret that they only focus on LLM text and not images)<br>
2- <a href="https://github.com/tunib-ai/parallelformers" class="inline-onebox" rel="noopener nofollow ugc">GitHub - tunib-ai/parallelformers: Parallelformers: An Efficient Model Parallelization Toolkit for Deployment</a> (which I also have the impression that they only cover NLP models)<br>
3- <a href="https://github.com/garg-aayush/model-parallelism/blob/main/B_unet_model_sharding.py" class="inline-onebox" rel="noopener nofollow ugc">model-parallelism/B_unet_model_sharding.py at main · garg-aayush/model-parallelism · GitHub</a> (I see that they apply model parallelism here but I don’t see that they use tensor parallelism in images.)</p>
<p>I have also tried HuggingFace’s accelerae library, but it gives me incorrect output (see issue: <a href="https://github.com/huggingface/accelerate/issues/2849" class="inline-onebox" rel="noopener nofollow ugc">Incorrect output when using accelerate in a pytorch Unet model · Issue #2849 · huggingface/accelerate · GitHub</a>).</p>
<p>Do you know if there is any solution already implemented for tensor parallelism in image models? I also want to know if a model already trained on a single GPU needs to be retrained to apply this parallelism, how would that be done?</p>

KFrank: <p>Hi Cyn!</p>
<p>I don’t have anything to say about “tensor parallelism.”  However …</p>
<aside class="quote no-group" data-username="cporrasn" data-post="1" data-topic="204494" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/a88e4f/48.png" class="avatar"> cporrasn:</div>
<blockquote>
<p>I have a Unet model that segments an image for me. Given a resizable image, I get the binary mask.</p>
<p>However, I have 4k images, which a single GPU does not fit,</p>
</blockquote>
</aside>
<p>If you hew to the careful design detailed in <a href="https://arxiv.org/pdf/1505.04597" rel="noopener nofollow ugc">the original U-Net paper,</a> you<br>
can use the “tiling strategy” described in that paper to process images of<br>
arbitrary size.</p>
<p>You could presumably distribute the tiles across your two gpus, but<br>
following the basic use case of pytorch’s <code>DistributedDataParallel</code>,<br>
you could distribute batch elements across your gpus (with the tiling<br>
for any given image occurring on a single gpu).</p>
<p>Best.</p>
<p>K. Frank</p>

Soumya_Kundu: <p>Maybe reduce the batch size?</p>

tianyu: <p>Thanks for the question!</p>
<p>To make sure I understand your question let me rephrase: You have a trained image model, and would like to do inference on 4,000 images using the model. And feeding all the 4,000 images together into GPU would cause out-of-memory error.</p>
<p>If that’s the case, there might be no need to do FSDP/TP on the model because the model can fit in GPU memory. Can you simply separate the 4,000 images in to groups and only feed a subset of the image into the model at one time? E.g. can you feed 1 image into the model and obtain correct result? If so you can binary search the largest group size that fit into memory. You can still parallelize inference by running on two GPUs simultaneously, each on 2,000 images.</p>