{
  "id": "P132",
  "slug": "pytorch-torch-load-weights-only-safe-globals-documentation",
  "status": "verified-page-ready",
  "product": "PyTorch",
  "family": "ML serialization security default",
  "canonical_url": "https://gitdocs.org/fix/pytorch-torch-load-weights-only-safe-globals-documentation",
  "query_alias_url": "https://gitdocs.org/q/pytorch-torch-load-weights-only-safe-globals-documentation",
  "markdown_url": "https://gitdocs.org/fix/pytorch-torch-load-weights-only-safe-globals-documentation.md",
  "json_url": "https://gitdocs.org/api/fixes/pytorch-torch-load-weights-only-safe-globals-documentation.json",
  "canonical_query": "PyTorch torch.load weights_only safe_globals documentation",
  "observed_codex_queries": [
    "PyTorch torch.load weights_only safe_globals documentation",
    "site:pytorch.org/docs/stable torch.load weights_only safe_globals",
    "https://pytorch.org/docs/stable/generated/torch.load.html",
    "https://docs.pytorch.org/docs/stable/generated/torch.load.html",
    "'weights_only' in https://docs.pytorch.org/docs/2.12/generated/torch.load.html"
  ],
  "organic_dependency_search_count": 6,
  "affected_versions": "PyTorch 2.6 and newer, especially checkpoints serialized from notebooks or app modules with custom class paths.",
  "exact_errors": [
    "Unsupported global",
    "Please use torch.serialization.add_safe_globals"
  ],
  "diagnosis": "The allowlist must match the module path stored in the checkpoint. Importing a similarly named class from a different module path will not satisfy the safe loader.",
  "before": "torch.serialization.add_safe_globals([MyModel])\ntorch.load(\"model.pt\")",
  "after": "from torch.serialization import add_safe_globals, get_unsafe_globals_in_checkpoint\nfrom myapp.models import MyModel\n\nprint(get_unsafe_globals_in_checkpoint(\"model.pt\"))\nadd_safe_globals([MyModel])\nmodel = torch.load(\"model.pt\", weights_only=True)",
  "verification": "python - <<'PY'\nfrom torch.serialization import get_safe_globals\nprint(get_safe_globals())\nPY",
  "unsafe_fixes": [
    "Do not assume the class name alone is enough; the checkpoint records fully qualified globals."
  ],
  "last_verified_at": "2026-06-25",
  "neutral_validation": {
    "organic_search_observed": true,
    "organic_strict_pass": true,
    "no_web_counterfactual_run": false,
    "no_web_strict_pass": null,
    "search_necessity_under_fixture": null,
    "no_web_oracle_note": "Observed query evidence is separated from necessity proof; no-web and stale-contract counterfactuals should be tracked separately."
  },
  "source_trail": [
    {
      "url": "https://pytorch.org/blog/pytorch2-6/",
      "label": "PyTorch 2.6 release"
    },
    {
      "url": "https://pytorch.org/docs/stable/generated/torch.load.html",
      "label": "Official reference opened by Codex"
    },
    {
      "url": "https://docs.pytorch.org/docs/stable/generated/torch.load.html",
      "label": "Official reference opened by Codex"
    },
    {
      "url": "https://docs.pytorch.org/docs/stable/notes/serialization.html",
      "label": "Authoritative source"
    }
  ],
  "source_github_links": []
}
