Skip to content
qgonDEV DOCSSDK 0.3.31
ENKR

Checkpoints and resume

In the appBuild › Experiments·Build › SchedulingExamples checked on SDK 0.3.29

If you keep the intermediate training state on the server, a run that failed or stopped can start again from that point. After reading this page you can add checkpoints to your training code and resume from any checkpoint in the UI. It also sets out when a run stopped by low memory or a GPU yield resumes on its own, and when someone has to click.

Add two things. At the start, check with qgon.resume_point() whether there is a checkpoint to pick up. During training, upload state with qgon.checkpoint().

import qgon
run = qgon.init(project="vision", name="resnet18")
cfg = run.params({"lr": 1e-3, "epochs": 20})
# model, opt and server setup omitted
start = 0
rp = qgon.resume_point() # a checkpoint if this run was started by Resume training, else None
if rp:
ck = rp.load(map_location=server) # loads the file and restores random number state
model.load_state_dict(ck["model"])
opt.load_state_dict(ck["optimizer"])
start = rp.epoch or 0
for ep in range(start, cfg["epochs"]):
train_one_epoch(model, opt)
vl, acc = evaluate(model)
run.log_metrics({"loss": vl, "acc": acc}, step=ep, prefix="val")
qgon.checkpoint({"model": model.state_dict(), "optimizer": opt.state_dict()},
epoch=ep + 1, metrics={"val/acc": acc})

Set epoch to the number of epochs finished so far. When you resume, this value comes back as rp.epoch. step works the same way and comes back as rp.step. Values in metrics (up to 20) show in the checkpoint list in the UI.

Each checkpoint is uploaded as one model in the model store. Every call adds one, so call it only every few epochs for large models. If there is more state to carry on, such as a learning-rate scheduler, put it in the same dict.

By default qgon.checkpoint() waits until the upload finishes. With wait=False, uploads go one by one in the background and training continues right away. To wait for them midway, call qgon.wait_uploads(). The program waits for them on its own when it ends. Only the latest local copy is kept inside the job container (keep_local).

An upload is attempted up to three times. If it still fails, you get a warning and training continues. The returned value then has ok set to False. The UI list shows the checkpoint as not fully uploaded, and it cannot be used to resume. To treat an upload failure as an error and stop training, pass strict=True.

Before qgon.init() is called, it saves locally only.

rp.load() uses safe loading by default. It reads only tensors, dicts, lists, numbers and strings. If you saved objects of your own classes, loading fails with a message. Open the file with rp.load(weights_only=False) only when you trust it.

For frameworks other than PyTorch, save the file yourself and upload that file. When resuming, read the file path from rp.path. Only one file is uploaded, so bundle several files into one.

save_my_model("ckpt.bin")
qgon.checkpoint(path="ckpt.bin", epoch=ep + 1)

To resume from a local file when running directly without a queue, pass it as qgon.resume_point(path="ckpt.pt"). If the file does not exist, you get an error, not None.

Code that does not follow this pattern still gets environment variables as long as it calls qgon.init(). In a resumed run, QGON_RESUME_PATH (the downloaded file path), QGON_RESUME_EPOCH, QGON_RESUME_STEP and QGON_RESUME_CHECKPOINT are set. If the download failed, QGON_RESUME_ERROR is set. If the code reads none of these, it trains from scratch. Restoring the optimizer, epoch and random number state is up to the code.

  1. Open the run in Build › Experiments and go to the Checkpoints tab. The list shows epoch, step, metric, size and save time.

  2. Click Resume on the row of the checkpoint to continue from. Resume training in the header picks the latest checkpoint. These buttons are shown only to the run's owner.

  3. Choose the queue and the new run name. If you want to change values, enter them under Change settings. Values you leave out keep their original value.

  4. Click Start. A new run with the same code, environment and settings goes on the queue.

In lineage, the new run is linked to the original run as resumed from it. The chosen checkpoint becomes the new run's input model. To train a finished run further, raise the number of epochs under Change settings (for example, set epochs to 20). If the number of epochs is not larger than the checkpoint's epoch, the run does not start.

You cannot resume in these cases. The UI shows the reason.

  • The run is still waiting or running.
  • The run was not created from the New run screen or with qgon run --queue. A code bundle is needed to run the same code again. LLM fine-tuning runs are resumed from the fine-tuning screen.
  • The checkpoint upload did not finish, or the checkpoint is in the trash.
  • It is someone else's run. Even admins cannot resume other people's runs.
  • You do not have permission to use the queue or the run environment.

You can resume even while the workspace is stopped. If you clone a resumed run again or use it as the base of a hyperparameter search, the clone starts from scratch.

In the Checkpoints tab, each row has these actions. Only the run's owner and admins can change them. Others can only view.

Action Result
Keep Excluded from deletion and from admin cleanup. Undo with Unkeep
Publish Locks edits and deletion so the checkpoint stays as it is. It cannot be undone in the UI
Download Downloads the file
Move to trash Moves the selected checkpoints to the trash. Files are deleted after 7 days. Until then, Restore selected brings them back

Published or kept checkpoints, and checkpoints that another experiment uses as input, are not moved to the trash. The server never deletes checkpoints on its own.

The top of the tab shows how much storage this run uses, split into checkpoints, models, result files and other. 'My storage' on Overview shows your total and usage per project. Storage is recounted every few hours, so something you just uploaded shows up after the next count.

Admins see usage per user and project in Settings › Storage. They can preview old checkpoints and move them to the trash, and set storage warning limits. The limits only warn and do not block saving.

When you record progress with qgon.progress(), the Scheduling Running tab and the Experiments page show percent done and time left.

total = cfg["epochs"] * len(loader)
for ep in range(start, cfg["epochs"]):
for i, batch in enumerate(loader):
...
qgon.progress(ep * len(loader) + i + 1, total, epoch=ep)

step is the number of steps finished so far and total is the total number of steps. If you do not know the total, leave out total. A value given once is kept even if later calls leave it out. You can call it on every step. It writes to the server only once every 10 seconds, in the background. A failed write does not block training. Time left is not shown while progress is under 5% or the total is unknown. Before qgon.init() it does nothing, so the same code runs unchanged in a notebook.

Hugging Face Trainer and PyTorch Lightning

Section titled “Hugging Face Trainer and PyTorch Lightning”

Both frameworks have a callback that handles progress and GPU yield. Importing it does not fail even if the library is not installed.

from qgon.integrations.hf import QgonProgressCallback, resume_dir
trainer = Trainer(model=model, args=args, train_dataset=ds,
callbacks=[QgonProgressCallback()])
trainer.train(resume_from_checkpoint=resume_dir())

If you ran from transformers import Trainer before qgon.init(), the callback is attached on its own. To turn this off, set the environment variable QGON_AUTO_PROGRESS=0. resume_dir() returns the checkpoint folder path in a resumed run, and None otherwise. With None, training starts from scratch.

The callbacks upload a checkpoint to the server only when a yield request arrives. Hugging Face bundles the checkpoint-N folder into one file at the end of the next step and uploads it. Lightning saves and uploads at the end of the next batch. Checkpoints saved the rest of the time are not uploaded. To also resume runs stopped by low memory, upload checkpoint files yourself with qgon.checkpoint(path=…).

When an admin asks a run to make room on the GPU for an urgent run, or the owner yields on their own, the running job saves a checkpoint, ends, and lines up again. Sending requests is covered in Share GPUs and the queue. This section covers how code responds.

The SDK checks for requests every 15 seconds in the background, so it does not block the training loop. When a request arrives, the code responds in one of three ways.

  1. Code that calls qgon.checkpoint(): it ends with exit code 75 right after the next checkpoint is uploaded. Nothing else to do.
  2. qgon.on_yield(fn): when a request arrives, fn is called at the next qgon.progress(). Use it when checkpoints are further apart than the yield deadline. If fn calls qgon.checkpoint(), the run ends with 75 after the upload. If fn just returns, the run still ends with 75 and resumes from the last checkpoint.
  3. qgon.yield_requested(): returns a bool telling whether a request arrived. Check it in your loop. If you then call qgon.checkpoint(), the run ends as in 1.
state = {"ep": start}
@qgon.on_yield
def save_now():
qgon.checkpoint({"model": model.state_dict(), "optimizer": opt.state_dict()},
epoch=state["ep"])
for ep in range(start, cfg["epochs"]):
state["ep"] = ep
for i, batch in enumerate(loader):
...
qgon.progress(ep * len(loader) + i + 1, total) # save_now is called here if a request is pending

The first call to qgon.checkpoint(), registering on_yield, or one of the callbacks starting training marks the run as yieldable. Runs without this mark can only be force-stopped and requeued by an admin. Training that runs as several processes (WORLD_SIZE of 2 or more) gets neither the mark nor automatic yield. To turn off the yield check entirely, set the environment variable QGON_YIELD=0.

The yield deadline is a server setting, 5 minutes by default. If the run does not end in time, the server stops it by force. If uploading a large checkpoint takes long, raise the run setting qgon/yield_wait_s (seconds). It goes up to the maximum the admin set.

run.params({"yield_wait_s": 900}, name="qgon") # this run gets a 15-minute yield deadline

A run that ended by yielding shows as failed with exit code 75 and a mark that it yielded. The new run that picks it up joins the queue with a name ending in (이어서·양보 N).

Putting a stopped run back in line is called requeue. It happens in two cases: when the memory guard stops a run, and when a run ends by yielding the GPU. In either case, the run resumes from its checkpoint on its own only if all of the following hold.

  1. Auto-assign is on for the server. Check it on the Scheduling page or on the first line of qgon queue status (배차: 켜짐 means on). A server with it off does not requeue.
  2. The run was sent to a queue (qgon run --queue, the New run screen, LLM fine-tuning). Training run directly in a workspace terminal or notebook only gets a notification.
  3. It is not a hyperparameter search trial, a pipeline step, or their controller. Their controllers handle those failures.
  4. There is a checkpoint whose upload finished.
  5. The server can create a new run in the owner's name. The owner must have set up access in a workspace (workspace key), or the run must have been sent from LLM fine-tuning with automatic requeue turned on.
  6. The training code reads the checkpoint with qgon.resume_point() (or the callbacks or environment variables above). Code that does not read it starts from scratch even after a requeue.

Runs stopped by the memory guard have more limits. The same run family is requeued at most 3 times, and only within one day of the stop. If another run of the same family is already waiting or running, it is not requeued, because the user is taken to have re-run it. The new run's name ends with (이어서·메모리 N).

Runs that ended by yielding do not count toward the 3-time limit and line up right behind the urgent run. If that run has no checkpoint, it resumes from the most recent checkpoint in the same family. Runs an admin stopped by force are requeued from scratch even without a checkpoint.

If the conditions are not met, the owner gets a notification to open the experiment and use Resume or Re-run. For a run stopped by low memory, a notice appears on the Experiments page. Within 24 hours of the stop, click Requeue there to put it back in line at once. Without a checkpoint it trains from scratch.

To have a run start over from scratch on its own even without a checkpoint, turn on qgon/auto_requeue for the run.

run.params({"auto_requeue": 1}, name="qgon") # requeue from scratch automatically if there is no checkpoint