Training issue the first Epoch took tooo long

Beginwithtorch: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/0/3093aa588652a325bd4bf6b2df868c40239120b1.png" data-download-href="https://discuss.pytorch.org/uploads/default/3093aa588652a325bd4bf6b2df868c40239120b1" title="grafik"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/0/3093aa588652a325bd4bf6b2df868c40239120b1.png" alt="grafik" data-base62-sha1="6VJhZNzc0faehrutsyENU5FxwZ3" width="690" height="353" data-dominant-color="3D3D3D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">grafik</span><span class="informations">918×470 9.91 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
Ass you can see at the image, i have problems with the training of the model. The first epoch took while to long.<br>
And my second Problem is, that i just use 3.7 GB of possible 40 GB GPU. #<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca92558a169392e336170539fc304f3f64cbc518.png" alt="grafik" data-base62-sha1="sU1Y2OYeVcDnuo1oV7cIEEpCQRi" width="131" height="130"></p>
<p>I try to take more and less worker and set a higher and lower batch size but it dont help to use more gpu power.</p>

Essoz: <p>Larger batch size should be able to increase GPU memory usage, so it is weird that changing it does not affect GPU.</p>
<p>Can you share more information, such as code snippet, data set and your configuration parameter, so that we can help you?</p>

Beginwithtorch: <p>The dataset which I use is the offen source dataset cifar 10. I use the ResNet18 architecture for train.</p>
<p>Which code snippets you need ?</p>

Essoz: <p>Your pipeline for reading data and training the model <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=12" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>

Beginwithtorch: <pre><code class="lang-auto">def get_loader(args, train_dataset, test_dataset):
    if args.dataset == "imagenet100":
        train_loader = torch.utils.data.DataLoader(
            train_dataset, batch_size=args.bs, shuffle=True, num_workers=4, drop_last=True
        )
        test_loader = torch.utils.data.DataLoader(
            test_dataset, batch_size=args.bs, shuffle=True, num_workers=4
        )
    else:
        train_loader = torch.utils.data.DataLoader(
            train_dataset, batch_size=args.bs, shuffle=False, num_workers=4, drop_last=True
        )
        test_loader = torch.utils.data.DataLoader(
            test_dataset, batch_size=args.bs, shuffle=False, num_workers=4
        )
    return train_loader, test_loader


def loss_mix(y, logits):
    criterion = F.cross_entropy
    loss_a = criterion(logits, y[:, 0].long(), reduction="none")
    loss_b = criterion(logits, y[:, 1].long(), reduction="none")
    return ((1 - y[:, 2]) * loss_a + y[:, 2] * loss_b).mean()


def acc_mix(y, logits):
    pred = torch.argmax(logits, dim=1).to(y.device)
    return (1 - y[:, 2]) * pred.eq(y[:, 0]).float() + y[:, 2] * pred.eq(y[:, 1]).float()
</code></pre>

J_Johnson: <p>The reason for the first epoch taking so long is probably that you are redownloading the dataset each time. Is this training session on your own computer or cloud hosted?</p>
<p>If you want to see the memory footprint increase, you’ll need a much larger batch size, something like 2k+ images per batch. You might even be able to fit the entire dataset on the GPU and still have room to spare. CIFAR images are only 32x32 with 3 channels, which is around 3kB per image.</p>

Beginwithtorch: <p>it is cloud hosted i am using google colab and it has consumed too many resources so i need to optimize the code so that it can train fast. I even have the premium version and it takes between 4-5 hours to train a model. It only uses 3-4 GB of the GPU, but I haven’t found a way to optimize it yet… <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>

Beginwithtorch: <aside class="quote no-group" data-username="J_Johnson" data-post="6" data-topic="193668" data-full="true">
<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/j_johnson/48/55494_2.png" class="avatar"> J_Johnson:</div>
<blockquote>
<p>The reason for the first epoch taking so long is probably that you are redownloading the dataset each time. Is this training session on your own computer or cloud hosted?</p>
<p>If you want to see the memory footprint increase, you’ll need a much larger batch size, something like 2k+ images per batch. You might even be able to fit the entire dataset on the GPU and still have room to spare. CIFAR images are only 32x32 with 3 channels, which is around 3kB per image.</p>
</blockquote>
</aside>
<p>can you reccomend me a batchsize?</p>

J_Johnson: <aside class="quote no-group" data-username="Beginwithtorch" data-post="7" data-topic="193668">
<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/b/97f17d/48.png" class="avatar"> Beginwithtorch:</div>
<blockquote>
<p>even have the premium version and it takes between 4-5 hours to train a model.</p>
</blockquote>
</aside>
<p>How many epochs are you letting it run? Your picture earlier shows ~21 seconds per epoch.</p>
<p>Regarding the batch size, you’d have to experiment to see what the max is given the model, optimizer, data size, etc. But that’s not necessarily going to speed anything up for the following reasons:</p>
<ol>
<li>You may have a bottleneck elsewhere, such as the num_workers of the dataloader;</li>
<li>A GPU has processing cores and RAM. The number of cores often determine how quickly the calculations are performed. You can think of all the calculations on a given batch like a pile of dirt. And the number of cores is the size of your shovel. Increasing the pile of dirt size doesn’t mean you’re shoveling any faster. For that, you need a bigger shovel.</li>
</ol>
<p>One other way you might speed things up is to put your data and model into bfloat16. That will cut the size in half and speed up the calculations at a very insignificant loss in precision(which isn’t important for ML, anyways).</p>

Beginwithtorch: <aside class="quote no-group" data-username="J_Johnson" data-post="9" data-topic="193668">
<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/j_johnson/48/55494_2.png" class="avatar"> J_Johnson:</div>
<blockquote>
<p>How many epochs are you letting it run? Your picture earlier shows ~21 seconds per epoch.</p>
<p>Regarding the batch size, you’d have to experiment to see what the max is given the model, optimizer, data size, etc. But that’s not necessarily going to speed anything up for the following reasons:</p>
<ol>
<li>You may have a bottleneck elsewhere, such as the num_workers of the dataloader;</li>
<li>A GPU has processing cores and RAM. The number of cores often determine how quickly the calculations are performed. You can think of all the calculations on a given batch like a pile of dirt. And the number of cores is the size of your shovel. Increasing the pile of dirt size doesn’t mean you’re shoveling any faster. For that, you need a bigger shovel.</li>
</ol>
<p>One other way you might speed things up is to put your data and model into bfloat16. That will cut the size in half and speed up the calculations at a very insignificant loss in precision(which isn’t important for ML, anyways).</p>
</blockquote>
</aside>
<p>I let it run for 200 Epochs. The duration of one epoch depends on the Attack which i use. Short info its a big Project where i compere some ML Attacks and Defenses on a CIFAR-10 Dataset. Its a existing git Repo.  The problem is i need it for my project in university. Next to me 4 other people try to optimize it but unsuccessful.<br>
I really dont know how i can optimize it. so that’s the reason why i start this threat. I just startet with pythorch sice 1,5 months</p>

minhnh: <p>If you have colab pro, I would recommend running <code>nvidia-smi</code> in console while letting the code run in notebook. Then you can see if your model utilized all GPU computational capacity to decide where the bottleneck is.</p>