DEV Community

Cover image for I kept copy-pasting the same checkpoint/resume code into every Hugging Face Trainer script. So we fixed it.
Durvesh
Durvesh

Posted on

I kept copy-pasting the same checkpoint/resume code into every Hugging Face Trainer script. So we fixed it.

If you've trained anything with transformers.Trainer on Colab or Kaggle, you know the drill: your session has a time limit, there's no guarantee you'll finish, and the second it dies you're stuck re-reading your own trainer_state.json trying to figure out where you actually left off — assuming you remembered to save one at all.

We'd already solved this for plain PyTorch training loops in Efficio (time-budgeted checkpointing, smaller checkpoint modes, resume across Colab ↔ Kaggle). But the moment someone used Trainer.train() or Lightning's Trainer.fit() instead of a raw loop, none of that applied — those frameworks manage their own checkpointing, so bolting Efficio on meant reimplementing budget tracking and resume logic by hand.

0.3.0 fixes that with two callbacks:

\`python
from efficio.adapters import HFTrainerCallback

efficio_cb = HFTrainerCallback(budget="2h", storage="hf://you/your-repo")
args = TrainingArguments(output_dir="out", fp16=True, save_strategy="no")
trainer = Trainer(model=model, args=args, train_dataset=ds, callbacks=[efficio_cb])
trainer.train(resume_from_checkpoint=efficio_cb.resume_from(trainer))
efficio_cb.report()
`\

Same idea for Lightning, as a Callback + a checkpoint_io plugin.

What you get, without writing any of it yourself

  • A time budget — training stops cleanly before your Colab/Kaggle session gets killed, checkpoint saved and uploaded first.
  • Resume through the framework's own mechanism — for HF Trainer, Efficio writes a real Trainer checkpoint folder, so weights, optimizer, scheduler and RNG all come back the way the Trainer expects. Lightning reads Efficio checkpoints straight through its own checkpoint_io plugin.
  • Cross-platform resume — start on Kaggle's dual T4s, finish on a single Colab T4.
  • Everything else from core Efficio — HF Hub or Google Drive checkpoint storage, smaller checkpoint modes (2–3x), all work here too.

Caveat

Single-process only for now. torchrun or strategy="ddp" raises rather than silently doing the wrong thing — multi-GPU support for the adapters is on the roadmap, right after we ship automatic OOM recovery with auto micro-batch sizing in the next release.

Tested on a real Colab T4: budget stop mid-epoch, then resume, with both frameworks.

\`bash
pip install "efficio[hf]"

or

pip install "efficio[lightning]"
`\

Repo: github.com/HuNTer8272/Efficio

Would love feedback, especially from anyone running longer HF Trainer or Lightning jobs on free-tier GPUs.

Top comments (0)