Initializing Techhub.cafe

How do you use managed Spot training and what must your script handle?

Updated Aug 1, 2026

Short answer

Managed Spot uses spare EC2 capacity for up to about 90% savings; you enable it with a max wait time and an S3 checkpoint path, and your script must save and resume from checkpoints so an interruption resumes rather than restarts.

Deep explanation

Spot capacity can be reclaimed with two minutes' notice. SageMaker handles the mechanics — it detects the interruption, waits for capacity, restarts the job — but it cannot recover your in-memory training state. That is entirely on your script.

The contract: SageMaker syncs checkpoint_local_path to checkpoint_s3_uri during the run, and restores it into the container before a resumed job starts. So the pattern is always look for a checkpoint first.

Python
estimator = PyTorch(
entry_point="train.py", role=role,
instance_type="ml.g5.12xlarge", instance_count=4,
use_spot_instances=True,
max_run=14400, # max training seconds
max_wait=28800, # must exceed max_run: training + waiting for capacity
checkpoint_s3_uri="s3://my-bucket/checkpoints/exp-42/",
checkpoint_local_path="/opt/ml/checkpoints",
)
Python
# train.py — resumable by construction
import os, glob, torch
CKPT_DIR = "/opt/ml/checkpoints"
os.makedirs(CKPT_DIR, exist_ok=True)
start_epoch = 0
existing = sorted(glob.glob(f"{CKPT_DIR}/ckpt_*.pt"))
if existing: # a resumed spot job lands here
state = torch.load(existing[-1], map_location="cpu")
model.load_state_dict(state["model"])
optimizer.load_state_dict(state["optimizer"])
scheduler.load_state_dict(state["scheduler"])
start_epoch = state["epoch"] + 1
print(f"Resumed from epoch {start_epoch}")
for epoch in range(start_epoch, args.epochs):
train_one_epoch(...)
# Atomic write: never leave a half-written checkpoint to be restored
tmp = f"{CKPT_DIR}/.tmp_{epoch}.pt"
torch.save({"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"epoch": epoch}, tmp)
os.replace(tmp, f"{CKPT_DIR}/ckpt_{epoch:04d}.pt")

Three details that separate a working answer from a theoretical one:

  1. Save the optimizer and scheduler, not just weights. Restoring weights alone resets momentum and learning-rate schedule, and the loss curve visibly suffers.
  2. Write atomically. An interruption mid-torch.save leaves a corrupt file that breaks the resume.
  3. `max_wait` must exceed `max_run` — it bounds training plus waiting for capacity, and the API rejects the job otherwise.

Judgement about where Spot fits: excellent for tuning jobs, experiments and long pre-training with checkpoints; poor for a job that must finish by a deadline, since capacity waits are unpredictable.

Real-world example

A team pre-training a domain language model runs 60-hour jobs on ml.p4d.24xlarge Spot with checkpoints every 20 minutes. They see two or three interruptions per run; each costs about 20 minutes of recomputation. Cost fell roughly 70% versus on-demand. Their final fine-tune before a release deadline runs on-demand — the savings are not worth missing a launch.

Common mistakes

  • - Enabling Spot without checkpointing, so an interruption restarts from zero and repeated interruptions can make a job never finish.
  • - Saving only model weights, losing optimizer and scheduler state and degrading convergence after resume.
  • - Setting `max_wait` equal to or below `max_run`, which fails validation.
  • - Non-atomic checkpoint writes producing corrupt files.
  • - Using Spot for deadline-bound work, where capacity waits are unbounded in practice.

Follow-up questions

  • How does the checkpoint sync actually work?
  • What savings should you expect, and how do you see them?
  • How do you reduce interruption impact besides frequent checkpoints?

More AWS Machine Learning interview questions

View all →