Loss goes up after loading checkpoint

Vinc: <p>I am training a Transformer encoder-decoder model for machine translation in pytorch lightning. My dataset is too large to train the model in one go, so I decided to split the training data into 10 subsets. Entire training process consists of iterating once through every subset (every batch is seen only once). After training model on first subset and loading second one, loss goes up.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/4/443b34a83708eb12c115255b012a3ec3e403058d.png" alt="loss spike" data-base62-sha1="9JBjzalgpsdhF46HwwZEoLZlsEB" width="658" height="485"></p>
<p>This is unexpected since subsets were created by shuffling entire training data, so they do not differ much from one another. Problem does not occur when I load the same subset for the second time. (unfortunately new users can not put multiple images in one post so I can’t post it here…) Also this does not happen when I merge two subsets into one and train model on merged subset. This suggets that the issue is with checkpointing.</p>
<p>My first guess was that optimizer states or learning rate was reset, but after further investigation this is not the case.</p>
<p>I am using lightning checkpoining and following versions of packages:<br>
torch==2.0.1 torchvision torchaudio torchtext<br>
pytorch-lightning==2.0.1<br>
xformers==0.0.20<br>
triton==2.0.0.dev20221105 --no-deps</p>

HELLORPG: <p>So you are sure that you already load the ‘state_dict’ for the optimizer?</p>

HELLORPG: <p>Can you load the checkpoint, freeze all parameters, and perform inference-like training to see the resulting loss?</p>

Vinc: <p>According to documentation when I pass <code>ckpt_path</code> argument to <code>trainer.fit</code> it will load model, epoch, step, LR schedulers, apex, etc. If optimizer state was erased then loss should increase no matter which subset is loaded, am I right? But when I load the same subset for the second time loss goes down smoothly.</p>

HELLORPG: <p>It seems right.</p><aside class="quote" data-post="3" data-topic="190167">
  <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/hellorpg/48/53892_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/loss-goes-up-after-loading-checkpoint/190167/3">Loss goes up after loading checkpoint</a> <a class="badge-category__wrapper " href="/c/nlp/8"><span data-category-id="8" data-drop-close="true" class="badge-category " title="Topics related to Natural Language Processing"><span class="badge-category__name">nlp</span></span></a>
  </div>
  <blockquote>
    Can you load the checkpoint, freeze all parameters, and perform inference-like training to see the resulting loss?
  </blockquote>
</aside>
<p>
So have you try this? If you still get a higher loss on new subset but the same loss on old subset, I think everything goes well for the checkpointing.</p>

Vinc: <p>Okay, so I did what you suggested and it turns out everything is fine with checkpointing.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/9/0963a414f76808558549e155ec5494bf6bb68ba7.png" data-download-href="https://discuss.pytorch.org/uploads/default/0963a414f76808558549e155ec5494bf6bb68ba7" title="hmmm"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/0963a414f76808558549e155ec5494bf6bb68ba7_2_690x476.png" alt="hmmm" data-base62-sha1="1l3MaGEAkT7z7rzGz2TrW74IB8z" width="690" height="476" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/0963a414f76808558549e155ec5494bf6bb68ba7_2_690x476.png, https://discuss.pytorch.org/uploads/default/original/3X/0/9/0963a414f76808558549e155ec5494bf6bb68ba7.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/9/0963a414f76808558549e155ec5494bf6bb68ba7.png 2x" data-dominant-color="D9DADA"><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">hmmm</span><span class="informations">804×555 55.2 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></p>
<p>However, I do not understand why those two subsets differ from one another. I shuffled them and later divided into subsets.</p>

HELLORPG: <p>Yes, it seems like the checkpoint goes well.</p>
<p>I do not understand why this happens either <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20">. But usually, it is related to your task setting or data sampling.</p>

HELLORPG: <p>I’m not very familiar with NLP tasks. Is there any data sample in your dataset using the same input but different labels?</p>
<p>For example, in the Visual Grounding task, if I have an image, the dataset may have some different sentences to describe something in this image. These sentences have the same inputs (image) but different labels (bounding box). During training an epoch, I may only see the specific sentence only once, but the same image more than once. It may lead the loss being lower on the train set (old subset) than a new set (new subset).</p>

Vinc: <p>I do not think so. I filtered and de-duplicated training set it does not contain same/similar sentences. Weird behaviour probably has to do with bad sampling. I will investiage it further and give response asap!</p>

HELLORPG: <p>Hope you figure it out soon~</p>

nickums: <p>perfectly normal behaviour , the model optimises on the first batch, but not for the next batch, and so on.</p>

spico197: <p>Hi there, did you record the learning rate curve? Are there additional warmup steps after loading the checkpoint?</p>

Vinc: <p>No, it is not normal. Statistics of each subset are almost the same and manual investigation did not show anything peculiar.</p>

Vinc: <p>Yes, I have recored learning rate curve which looks as it should. There are no additional warmup steps after loading checkpoint.</p>

MostHumble: <p>How does the loss of training for 2 epochs on the same dataset, vs the loss of 1 epoch on the  concat of the same dataset? (without shuffling)<br>
Also curious why you think this is normal, it seems like a significant bump ( especially when you see that it’s happening in the first steps)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/7/0789c1ffe2ee5b223800d1820e75f467a63e296b.png" data-download-href="https://discuss.pytorch.org/uploads/default/0789c1ffe2ee5b223800d1820e75f467a63e296b" title="loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/7/0789c1ffe2ee5b223800d1820e75f467a63e296b_2_690x476.png" alt="loss" data-base62-sha1="14GucbD8wB1107CgQtnQOLXR62L" width="690" height="476" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/7/0789c1ffe2ee5b223800d1820e75f467a63e296b_2_690x476.png, https://discuss.pytorch.org/uploads/default/original/3X/0/7/0789c1ffe2ee5b223800d1820e75f467a63e296b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/7/0789c1ffe2ee5b223800d1820e75f467a63e296b.png 2x" data-dominant-color="D9DAD9"><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">loss</span><span class="informations">804×555 55.2 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></p>

J_Johnson: <p>If you’re training the model on different subsets of the dataset, one can expect the loss to increase initially for each change in subset.</p>
<p>Based on what you’ve said, let’s establish that:</p>
<ol>
<li>The subsets are not identical to each other,</li>
<li>Training a model on a given subset reduces loss on that subset only.</li>
</ol>
<p>Suppose they are 10% different from each other. Then 90% of the training on the first subset will generalize to the other subsets. But 10%, which represents the difference, will not generalize to the other subsets. Hence the bump in loss.</p>