Nightly model training
Every night production exports the photo examples that are new or corrected since the night before, rents a GPU on RunPod, trains the self-hosted vision model (Qwen3-VL-8B-Instruct with a LoRA adapter) a little further, and emails [email protected] what happened. The app does not use this model yet: every photo still goes to the model named by OPENROUTER_MODEL. The training builds up a model ready for the day it is switched on.
The code is infra/training/:
| File | Runs on | Does |
|---|---|---|
nightly.py | the production Forge server, hourly | export, rent the GPU, copy files, wait, read the result, delete the GPU, email |
pod/train_nightly.sh | the GPU pod | install the training tools, build tonight's set, train, score |
pod/build_corpus.py | the GPU pod | fold tonight's zips into the corpus, pick tonight's rows |
pod/finish.py | the GPU pod | compare scores, promote the adapter if it improved, write result.json |
pod/report_card.py | the GPU pod, weekly | write answers on the gold set and grade them beside the frontier model's |
Tests: backend/tests/test_nightly_model_training.py.
A night, step by step
- Export. The Scheduler runs
nightly.pyevery hour. The first tick at or after 8 PM New York time (the New York clock is checked in the script, so daylight saving needs no change) runsscripts/export_model_examples.py --pendinginside thebackendcontainer and copies the zip to/home/forge/training-exports/pending/model-examples-<New York date>.zip. The export stamps its rows, so the next night only gets what changed after it. No new rows: a one-line email and no GPU. - Rent a GPU. Any GPU with 48 GB or more on RunPod Secure Cloud (customer photos never go to Community Cloud), cheapest first: A40, RTX A6000, L40, L40S, RTX 6000 Ada, A100, RTX PRO 6000, H100, H200. A night is never skipped for capacity: if none is free, the job asks again every five minutes until one is, and emails once if the wait passes an hour. Only a low RunPod balance stops it, and that emails too.
- Train. The pod mounts the shared volume at
/data, unpacks the zip into the corpus, and trains for one pass over tonight's photo sets (repeated by rank: rank 2 four times, rank 1 twice, rank 0 once) plus a random sample of older photo sets, at least 200 rows, so the model does not forget what it knew. It continues from the official adapter. One pass, not two: the first training showed a second pass over the same photos makes the score worse. - Score and promote. The test is whole claims: one claim in ten is held back for good, and no photo from it is ever trained on, so the score is measured on houses, items and photos the model has never seen (a different angle of a trained item cannot sneak into the test). The answer key is customer-checked answers only: a test photo set counts when every item in it was edited by a customer or sent by one to ContentsEstimation. Most untouched answers are the frontier model's, and a model that learned past it would score worse against them. The test is scored before training (the official model, tonight) and after, as eval loss (lower is better). Tonight's adapter becomes the official one only when it scored at least 1% lower, so a bad night cannot make the model worse and a small test cannot swap models on noise. Until there are 20 customer-checked test photo sets, the whole test is the answer key, and the email says so.
- Weekly report card. About once a week (the first training night 7 days after the last one), after scoring, the official model writes real item lists for the gold set (the customer-checked test photo sets, up to 150) with
swift infer, andpod/report_card.pygrades them: items found, extra items, brand, quantity, damage. The frontier model's original answer for the same photos is kept on every example, so it is graded side by side at no extra cost. This is the number that says when the model is good enough to replace the frontier model.
The test split
Each exported example carries claim, a one-way code made from its claim id (claim_code in app/services/model_examples.py), so the export still names no claim. A claim is a test claim when a hash of its code is 0 modulo 10 (is_test_claim in pod/build_corpus.py), so the choice never changes and new claims fall on either side at the same rate. A photo set that touches any test claim is test.
Examples exported before codes existed get theirs from a claim map: every night, before training, Forge runs scripts/export_model_examples.py --claim-map (each example's claim code and the frontier model's original answer, nothing stamped) and sends it with the zips.
The gold set is not reviewed by hand: customers already checked those answers. It grows as customers edit or send items from test claims, and an answer a customer corrects again is updated in it the next night. One bias to know: when a customer sent an item without editing it, the frontier model's answer is the checked answer, so the frontier model starts with an edge on those. A night where more than 5% of the corpus still has no code is refused rather than trained on a broken split.
The first training (2026-11-19) split by photo set, not by claim, so its adapter has seen other photos of some test claims and its score of 0.2740 was flattered. models/best/score.json records the split an adapter was trained under (split). When it is not the current one (claims-1-in-10), the night is a full retrain: one pass over every training photo set, from the base model, with the first training's settings (LoRA rank 16, learning rate 1e-4). Its adapter replaces the official one whatever the score, and its score is the bar from then on. Expect it to be higher (worse) than 0.2740: the test is harder, not the model.
6. Clean up and email. Forge deletes the pod and confirms it is gone, then emails: examples pulled, photo sets trained, training time, the before and after scores, whether the official model changed, the GPU and its cost, and that the machine is deleted.
A run that fails keeps its zip in pending/ and tries again straight away, up to 3 failed runs in a row on the same exports and the same pod scripts (MAX_FAILED_ATTEMPTS, counted in failed-attempts.json). Then it stops retrying, because a failure that repeats is a bug and every retry is a full GPU run. The count starts over by itself when the next export arrives or a release changes the pod scripts, so a fixed bug picks the night back up within the hour. It emails on the first failure and when it gives up, not on each retry. Nothing is lost. Waiting for a free GPU is not a failure and never counts.
The shared storage refuses permission changes (chmod, utime), so everything that writes to /data copies file by file with shutil.copyfile, never copytree or copy2. On 2026-11-21 a copytree in the promotion step failed five full retrains in a row at the very end. A lock file (training.lock) keeps it to one run at a time; while one is training or waiting for a GPU, the hourly ticks still export on time and queue their zips behind it.
Where things live
Forge server, /home/forge/training-exports/:
pending/: zips not yet trained ontrained/: zips trained on, kept 30 daysnightly.log: one line per step, newest at the bottomlast-export-date: the New York date of the last export
RunPod shared volume contentsvision-lora (a global volume, id cmwng3kuj000007l2c7xxfdqq, mountable in any data center):
models/best/: the official adapter, withscore.json. When the app is switched to this model, it loads this one.models/history/<run>/: every adapter that was ever promotedcv-train/rows.jsonl: the corpus, one row per example id with its claim code;photos/cv-train/groups.jsonl: the first export's groups (2026-11-19), whichrows.jsonlwas built fromruns/<run>/: each night's plan, logs, scores andresult.jsonexports/: every zip that was trained oncheckpoint-400/,checkpoint-1000/: the first training's adapters. 400 scored best and was the first official adapter.
The volume is slow to write (a few MB a second), so each pod downloads the 17 GB base model from Hugging Face to its own disk instead (a few minutes). huggingface-cache/ and vllm-cache/ on the volume are left from the first training and are not used.
The pod that did the first training, contentsvision-qwen3vl-train, is stopped and no longer used.
Set up (once)
- RunPod key. Forge > production site > Environment: add
RUNPOD_API_KEY=<key>with read and write on Pods and read on storage.MAILGUN_*must already be there (it is for production). - SSH key for the pods. Forge > production site > Commands:
test -f ~/.ssh/cv_training || ssh-keygen -q -t ed25519 -N "" -f ~/.ssh/cv_training; echo OK
- Check. Commands:
python3 /home/forge/contentsvision.com/current/infra/training/nightly.py --check. It prints what is missing, or "everything is set up" and sends a test email. - Schedule. Forge > server > Scheduler: command
python3 /home/forge/contentsvision.com/current/infra/training/nightly.py, userforge, frequency Every Hour.
To run a night now instead of waiting for 8 PM, in Commands:
nohup python3 /home/forge/contentsvision.com/current/infra/training/nightly.py --train > /dev/null 2>&1 & echo STARTED
It exports (unless tonight's export already ran) and trains in the background; the email comes when it is done.
The older /home/forge/training-exports/export-nightly.sh (2026-11-20) is replaced by this. Do not schedule it: any zip it leaves in /home/forge/training-exports/ is moved into pending/ and trained on.
Test run
CV_TRAINING_POD_ENV="DRY_RUN=1 " makes the pod train and score as usual but change neither the official adapter nor the corpus, and delete its zips from the volume's inbox. The email subject starts with [TEST]. Add MAX_STEPS=15 to cap the training steps.