rohitg00/ai-engineering-from-scratch · error · FileNotFoundError

safetensors file not found

Error message

safetensors file not found: {path}

What it means

load_safetensors is a missing-file guard: the safetensors checkpoint path given to the loader does not exist on disk, typically a wrong filename, wrong directory, or a download step that was skipped.

Solutions

  1. Verify the path with Path.exists() before loading
  2. Download or copy the checkpoint into the expected location
  3. Resolve the path relative to the repo root or an explicit checkpoints directory instead of the CWD
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py:215 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/2bf798838826f1e2. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py:215

        mapping[f"{prefix_src}.attn.c_attn.weight"] = f"{prefix_dst}.attn.qkv.weight"
        mapping[f"{prefix_src}.attn.c_attn.bias"] = f"{prefix_dst}.attn.qkv.bias"
        mapping[f"{prefix_src}.attn.c_proj.weight"] = f"{prefix_dst}.attn.out_proj.weight"
        mapping[f"{prefix_src}.attn.c_proj.bias"] = f"{prefix_dst}.attn.out_proj.bias"
        mapping[f"{prefix_src}.mlp.c_fc.weight"] = f"{prefix_dst}.mlp.fc1.weight"
        mapping[f"{prefix_src}.mlp.c_fc.bias"] = f"{prefix_dst}.mlp.fc1.bias"
        mapping[f"{prefix_src}.mlp.c_proj.weight"] = f"{prefix_dst}.mlp.fc2.weight"
        mapping[f"{prefix_src}.mlp.c_proj.bias"] = f"{prefix_dst}.mlp.fc2.bias"
    return mapping


def _needs_transpose(pretrained_name: str) -> bool:
    return any(pretrained_name.endswith(suffix) for suffix in CONV1D_SUFFIXES)


def load_safetensors(model: GPTModel, path: Path, verbose: bool = True) -> LoadReport:
    """Load weights into model. Refuse to assign on shape mismatch. Returns a report."""
    if not path.exists():
        raise FileNotFoundError(f"safetensors file not found: {path}")

    mapping = make_pretrained_to_local(model.cfg.num_layers)
    local_params = dict(model.named_parameters())
    report = LoadReport()

    seen_local: set[str] = set()
    pending: list[tuple[str, str, torch.Tensor]] = []
    with safe_open(str(path), framework="pt") as reader:
        pretrained_names = list(reader.keys())
        for src_name in pretrained_names:
            local_name = mapping.get(src_name)
            if local_name is None:
                report.unexpected.append(src_name)
                if verbose:
                    print(f"  [skip] {src_name} (no mapping)")
                continue
            if local_name not in local_params:
                report.unexpected.append(src_name)

View on GitHub (pinned to 39ea8a1c6d)