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

> Source: <https://dev.to/durveshhhhh/i-kept-copy-pasting-the-same-checkpointresume-code-into-every-hugging-face-trainer-script-so-we-ic9>
> Published: 2026-12-06 07:35:21+00:00

**Key takeaway:** Efficio 0.3.0's HFTrainerCallback and Lightning callback add time-budgeted checkpointing and resume to Trainer loops, but only in single-process runs—torchrun or DDP raises instead of silently misbehaving.

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](https://github.com/HuNTer8272/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.

`checkpoint_io` plugin.
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]"

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.
