karpathy/nanochat · error · FileNotFoundError

No checkpoints found in {checkpoints_dir}

Error message

No checkpoints found in {checkpoints_dir}

What it means

`find_largest_model(checkpoints_dir)` lists the subdirectories of the checkpoints directory (each expected to be a model tag like 'd12', 'd24'). If the directory exists but contains no subdirectories, `model_tags` is empty and FileNotFoundError is raised. This function auto-guesses which model to load when no explicit model_tag is given.

Source

Thrown at nanochat/checkpoint_manager.py:121

    model.init_weights() # note: this is dumb, but we need to init the rotary embeddings. TODO: fix model re-init
    model.load_state_dict(model_data, strict=True, assign=True)
    # Put the model in the right training phase / mode
    if phase == "eval":
        model.eval()
    else:
        model.train()
    # Load the Tokenizer
    tokenizer = get_tokenizer()
    # Sanity check: compatibility between model and tokenizer
    assert tokenizer.get_vocab_size() == model_config_kwargs["vocab_size"], f"Tokenizer vocab size {tokenizer.get_vocab_size()} does not match model config vocab size {model_config_kwargs['vocab_size']}"
    return model, tokenizer, meta_data


def find_largest_model(checkpoints_dir):
    # attempt to guess the model tag: take the biggest model available
    model_tags = [f for f in os.listdir(checkpoints_dir) if os.path.isdir(os.path.join(checkpoints_dir, f))]
    if not model_tags:
        raise FileNotFoundError(f"No checkpoints found in {checkpoints_dir}")
    # 1) normally all model tags are of the form d<number>, try that first:
    candidates = []
    for model_tag in model_tags:
        match = re.match(r"d(\d+)", model_tag)
        if match:
            model_depth = int(match.group(1))
            candidates.append((model_depth, model_tag))
    if candidates:
        candidates.sort(key=lambda x: x[0], reverse=True)
        return candidates[0][1]
    # 2) if that failed, take the most recently updated model:
    model_tags.sort(key=lambda x: os.path.getmtime(os.path.join(checkpoints_dir, x)), reverse=True)
    return model_tags[0]


def find_last_step(checkpoint_dir):
    # Look into checkpoint_dir and find model_<step>.pt with the highest step
    checkpoint_files = [f for f in os.listdir(checkpoint_dir) if re.search(r'model_(\d+)\.pt$', f)]

View on GitHub (pinned to 92d63d4e8b)

Solutions

  1. Verify the checkpoints directory actually contains model tag subdirectories (e.g. assets/checkpoints/d24/) — list it with `ls <checkpoints_dir>`.
  2. Pass an explicit `model_tag=` to load_model_fromdir if auto-discovery is not wanted.
  3. If checkpoints are missing, retrain or copy/sync the checkpoint assets to the expected location.
  4. Check the script's CLI argument for the checkpoints path (e.g. -i sft|rl selects which subdir is used) and correct it.

Example fix

# before
model, tokenizer, meta = load_model_from_dir("assets/checkpoints", device, "sft")  # dir empty

# after
# ensure a model tag dir exists, or name one explicitly
model, tokenizer, meta = load_model_from_dir("assets/checkpoints", device, "sft", model_tag="d24")
Defensive patterns

Strategy: validation

Validate before calling

import os
model_tags = [f for f in os.listdir(checkpoints_dir) if os.path.isdir(os.path.join(checkpoints_dir, f))]
if not model_tags:
    raise SystemExit(f"{checkpoints_dir} has no model tag subdirectories; train first or sync assets.")

Try / catch

try:
    model_tag = find_largest_model(checkpoints_dir)
except FileNotFoundError:
    print(f"No checkpoints under {checkpoints_dir}; falling back to prompting for an explicit path")
    raise

Prevention

When it happens

Trigger: Calling `load_model_fromdir(...)` (or `find_largest_model` directly) with a checkpoints_dir that is empty, contains only files (no subdirectories), or pointing at the wrong path (e.g. the log dir instead of the checkpoints dir).

Common situations: Running an eval/inference script before any training checkpoints were written; wrong --source or checkpoints path; checkpoints deleted or moved after training; a fresh clone where 'assets/' checkpoints were never downloaded/synced.

Related errors


AI-assisted analysis of karpathy/nanochat@92d63d4e8b (2026-08-15). Data as JSON: /api/errors/05ea57f479a730af. Report an issue: GitHub.