Weights from the Hub¶
A lens, a sparse autoencoder or a probe can be gigabytes of weights. Download the file inside
the session, with hf_hub_download. It goes from the Hub to NDIF, and your request carries
only the repository and file names.
The pattern¶
Neuronpedia publishes a Jacobian lens for
Qwen/Qwen3-8B: one [4096, 4096] matrix per layer, in a 1.2 GB file. This session loads the
file on the server and reads layer 20 through the lens, at every token.
import torch
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
tok = model.tokenizer
REPO = "neuronpedia/jacobian-lens"
FILE = "qwen3-8b/jlens/Salesforce-wikitext/Qwen3-8B_jacobian_lens.pt"
prompt = "The Eiffel Tower is located in the city of"
with model.session(remote=True):
from huggingface_hub import hf_hub_download
lens = torch.load(hf_hub_download(REPO, FILE), map_location="cpu")
with model.trace(prompt):
hidden = model.model.layers[20].output[0] # [tokens, 4096]
J = lens["J"][20].to(hidden.device, hidden.dtype) # [4096, 4096]
with_lens = model.lm_head(model.model.norm(hidden @ J.T)).argmax(-1).save()
without = model.lm_head(model.model.norm(hidden)).argmax(-1).save()
print("tokens :", [tok.decode(t) for t in tok(prompt)["input_ids"]])
print("with lens:", [tok.decode(t) for t in with_lens])
print("without :", [tok.decode(t) for t in without])
# tokens : ['The', ' E', 'iff', 'el', ' Tower', ' is', ' located', ' in', ' the', ' city', ' of']
# with lens: ['/Internal', ' Cube', 'itecture', '巴黎', ' French', '____', '____', '____', ' city', ' city', '____']
# without : ['玿', '‐', 'ian', ' Tower', ' famously', ' famously', ' ____', ' ______', ' center', ' _____', ' _____']
At layer 20 the lens already reads 巴黎 (Paris) at the end of the tower's name. The two rows
of token ids are all that is downloaded.
What each part does¶
hf_hub_downloadinside the session. The file is fetched by the server. Outside the session, the same call would download it to your machine, and usinglensin the block would then upload it with the request.lensis not saved. It is used on the server and dropped when the job ends..to(hidden.device, hidden.dtype). The file loads on the CPU in float32. The model's activations are on a GPU in half precision.- One layer's matrix is moved to the GPU, not the whole file. GPU memory for a job is limited to what is left beside the model's weights (Limits and errors).
Other files¶
The same three lines work for any file in a public Hub repository.
| You have | Load it with |
|---|---|
A .pt file |
torch.load(hf_hub_download(repo, file), map_location="cpu") |
A .safetensors file |
safetensors.torch.load_file(hf_hub_download(repo, file)) |
| A LoRA adapter in PEFT format | peft= in the constructor (Adapter from the Hub) |
For weights of your own, upload them to a public repository first. A block cannot read a private or gated one (Code on the server).