Skip to content
mentoor

Topic 5 of 5 · 5 min read

Checking model files before loading them

Goal: Spot the risky files and loader flags in a model repository before loading it.

Downloading a model is downloading files, and some file formats and loader flags run code when you open them, so inspect the repo before you load it.

Check yourself · 1 of 3

Which file type can run code on your machine when a framework loads it?

Choose one answer

Why it matters

Loading a model looks like loading data. Two common paths run the repository's code instead. A pickle checkpoint (.bin, .pt, .pkl, .ckpt) can execute arbitrary Python while it unpickles. And trust_remote_code=True tells transformers to import and run Python files from the repo. Either one on a laptop with cloud keys or a company VPN is a real breach path.

How to check

  1. List the files first without downloading weights.
  2. Prefer .safetensors. The format stores only tensors and a header, so loading it does not execute code. GGUF files, used by llama.cpp, are also plain data formats.
  3. Treat pickle as untrusted. If a pickle is your only option, load it with torch.load(path, weights_only=True) inside a throwaway container or VM with no credentials. The Hub's pickle scanner flags known dangerous imports, but it is a filter, not a guarantee.
  4. Keep trust_remote_code off unless a model needs custom architecture code. If it does, read the .py files at the pinned commit, and pin that commit so they cannot change later.
  5. Judge the uploader. Check the organisation, its history and other models. Community re-uploads and quantized copies (GGUF, AWQ, GPTQ) come from individuals. They are convenient, but the weights are only as trustworthy as the person who converted them. Prefer the official repo or a well-known converter, and compare the file list and sizes with the original.

Before and after

# Before: unknown repo, code execution allowed, latest revision
model = AutoModelForCausalLM.from_pretrained(
    "fast-quants/Orbit-8B-turbo", trust_remote_code=True)
from huggingface_hub import list_repo_files

files = list_repo_files("acme-labs/Orbit-8B-Instruct",
                        revision="3f9c2a1e8b7d4c5a96e0b1d2c3f4a5b6c7d8e9f0")
risky = [f for f in files if f.endswith((".bin", ".pt", ".pkl", ".ckpt", ".py"))]
print(risky)  # review these before any load; expect only safetensors

What goes wrong

  • Flag copied from a tutorial. trust_remote_code=True appears in many snippets; pasting it runs unreviewed code.
  • Believing a scan means safe. "No threats found" is weaker than "uses a format that cannot run code".
  • Trusting a lookalike name. A repo that mimics a famous model name is not that model.
  • Loading on the production host. Test new repos in a sandbox, not where secrets live.

Examples

A pickle-only repo

A research repo ships only pytorch_model.bin. Options: find a safetensors conversion from the original owner, convert it yourself inside a container with no credentials, or skip the model. Do not load it on a machine that holds cloud keys.

The custom-code prompt

transformers raises an error asking you to set trust_remote_code=True. Before you do, open the repo's modeling_*.py at the pinned commit, check for network calls, subprocess, os.system and eval, and pin that commit with revision= so the code cannot change under you.

Key points

  • Safetensors and GGUF files hold data only; pickle checkpoints can run code when loaded.
  • Set trust_remote_code only after reading the repository's Python files at a pinned commit.
  • List a repo's files before downloading and flag .bin, .pt, .pkl, .ckpt and .py entries.
  • A Hub pickle scan reduces risk but does not prove a pickle file is safe.
  • Community re-uploads and quants depend on the converter's trustworthiness, so prefer official or well-known sources and compare file lists with the original.

Common mistakes

  • Setting trust_remote_code=True because a snippet or an error message suggested it.
  • Loading an unknown pickle file on a machine that holds credentials.
  • Reading a clean scan result as a guarantee that a file cannot run code.
  • Downloading a quantized copy from an unknown uploader when the original owner offers one.

Check yourself · 2 of 3

An error tells you to set trust_remote_code=True to load a model. What is the best next step?

Choose one answer

Check yourself · 3 of 3

A third-party account offers a 4-bit copy of a popular model, and the original owner has a repo too. Which choice is safest?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic