{"record":{"id":"2bf798838826f1e2","repo":"rohitg00/ai-engineering-from-scratch","slug":"safetensors-file-not-found-path","errorCode":null,"errorMessage":"safetensors file not found: {path}","messagePattern":"safetensors file not found: (.+?)","errorType":"validation","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py","lineNumber":215,"sourceCode":"        mapping[f\"{prefix_src}.attn.c_attn.weight\"] = f\"{prefix_dst}.attn.qkv.weight\"\n        mapping[f\"{prefix_src}.attn.c_attn.bias\"] = f\"{prefix_dst}.attn.qkv.bias\"\n        mapping[f\"{prefix_src}.attn.c_proj.weight\"] = f\"{prefix_dst}.attn.out_proj.weight\"\n        mapping[f\"{prefix_src}.attn.c_proj.bias\"] = f\"{prefix_dst}.attn.out_proj.bias\"\n        mapping[f\"{prefix_src}.mlp.c_fc.weight\"] = f\"{prefix_dst}.mlp.fc1.weight\"\n        mapping[f\"{prefix_src}.mlp.c_fc.bias\"] = f\"{prefix_dst}.mlp.fc1.bias\"\n        mapping[f\"{prefix_src}.mlp.c_proj.weight\"] = f\"{prefix_dst}.mlp.fc2.weight\"\n        mapping[f\"{prefix_src}.mlp.c_proj.bias\"] = f\"{prefix_dst}.mlp.fc2.bias\"\n    return mapping\n\n\ndef _needs_transpose(pretrained_name: str) -> bool:\n    return any(pretrained_name.endswith(suffix) for suffix in CONV1D_SUFFIXES)\n\n\ndef load_safetensors(model: GPTModel, path: Path, verbose: bool = True) -> LoadReport:\n    \"\"\"Load weights into model. Refuse to assign on shape mismatch. Returns a report.\"\"\"\n    if not path.exists():\n        raise FileNotFoundError(f\"safetensors file not found: {path}\")\n\n    mapping = make_pretrained_to_local(model.cfg.num_layers)\n    local_params = dict(model.named_parameters())\n    report = LoadReport()\n\n    seen_local: set[str] = set()\n    pending: list[tuple[str, str, torch.Tensor]] = []\n    with safe_open(str(path), framework=\"pt\") as reader:\n        pretrained_names = list(reader.keys())\n        for src_name in pretrained_names:\n            local_name = mapping.get(src_name)\n            if local_name is None:\n                report.unexpected.append(src_name)\n                if verbose:\n                    print(f\"  [skip] {src_name} (no mapping)\")\n                continue\n            if local_name not in local_params:\n                report.unexpected.append(src_name)","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py#L197-L233","documentation":"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.","triggerScenarios":"Thrown at phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py:215 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the path with Path.exists() before loading","Download or copy the checkpoint into the expected location","Resolve the path relative to the repo root or an explicit checkpoints directory instead of the CWD"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}