Hugging Face, End to EndLesson 64 of 75

Module 7 · Training and adapting, with the libraries the Hub is built on

Loss is NaN, memory is gone, nothing is learning

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 8 of 9 in this chapter

This chapter’s goalDecide from evidence whether a task needs fine-tuning at all, run a LoRA or QLoRA adaptation with Trainer or SFTTrainer on a single free GPU, read a loss curve well enough to name what is wrong, and publish an adapter a stranger can apply to the base model.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:08Not available
Full audiobookNot availableNot available
The curve that tells you to stop0120700training stepsloss—— Training loss– – Validation lossTraining loss keeps falling and validation turns upward at about step 350. Everything after that pointis memorisation, and it is visible in the logged numbers long before it is visible in the output.load_best_model_at_end keeps the checkpoint from before the turn, which is not the last one and isusually better.
Training loss keeps falling and validation turns upward at about step 350. Everything after that point is memorisation, and it is visible in the logged numbers long before it is visible in the output. load_best_model_at_end keeps the checkpoint from before the turn, which is not the last one and is usually better.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

An ordered list, because guessing is expensive

Training failures are slower to diagnose than inference failures, because each attempt costs an hour. Work down this list rather than trying things.

Out of memory

The most common failure and the most mechanical to fix. In order, because each step costs progressively more:

  1. Reduce batch size, raise gradient accumulation. Free.
  2. gradient_checkpointing=True. Costs 20 to 30% speed, saves a lot.
  3. optim="adamw_bnb_8bit". Saves about three quarters of the optimiser state.
  4. Shorten max_length. Check your actual length distribution first: often the 95th percentile is far below what you set.
  5. LoRA, then QLoRA. The large wins.

One detail that catches people: out of memory during evaluation, not training. Evaluation batches default to the training batch size and generation builds a KV cache, so the peak can be higher than in training. Set per_device_eval_batch_size explicitly, lower.

And on a shared or notebook environment, memory from a previous failed run is often still held:

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Loss is NaN, memory is gone, nothing is learning’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/what-goes-wrong-in-training (Accessed: 29 November 2026).