Anti-patterns¶
These mistakes are easy to make in remote code. Each entry shows the code, what happens, and the fix.
A loop of remote traces¶
answers = []
for prompt in prompts:
with model.trace(prompt, remote=True):
token = model.lm_head.output[0, -1].argmax(-1).save()
answers.append(token)
What happens. Every pass is its own job: its own upload, its own wait in the queue, its own download. Other users' jobs run between yours.
Fix. One session with the loop inside it. The loop runs on the server.
with model.session(remote=True):
answers = nnsight.save([])
for prompt in prompts:
with model.trace(prompt):
answers.append(model.lm_head.output[0, -1].argmax(-1).item())
See Sessions.
remote=True on the traces inside a session¶
with model.session(remote=True):
with model.trace(prompt, remote=True):
token = model.lm_head.output[0, -1].argmax(-1).save()
What happens. The trace inside tries to send a second job to NDIF from NDIF's own machine.
The connection is refused, and the session fails after a wait:
WebSocketProxyException: failed CONNECT via proxy status: 403.
Fix. remote=True goes on the session only. The traces inside it already run on the
server.
Saving whole activations or logits¶
with model.session(remote=True):
saved = nnsight.save([])
for batch in batches:
with model.trace(batch):
saved.append(model.lm_head.output)
What happens. The logits for every token of every prompt are downloaded: [batch, tokens,
151936] per trace. The download can take longer than the job.
Fix. Compute the number you want on the server and save that.
See Sweep and summarize.
Large local data in the block¶
lens = torch.load("Qwen3-8B_jacobian_lens.pt") # 1.2 GB, on your machine
rows = load_dataset("nyu-mll/glue", "sst2", split="validation")
with model.trace(prompt, remote=True):
J = lens["J"][20]
...
What happens. Every outside value the block uses is uploaded with the request, so the
whole lens dict is sent. A Dataset cannot be sent at all: it points at files on your
machine, and the job fails with FileNotFoundError.
Fix. Load the data on the server, inside a session.
See Weights from the Hub and Dataset in a session.
Filling a list made outside the block¶
results = []
with model.session(remote=True):
for prompt in prompts:
with model.trace(prompt):
results.append(model.lm_head.output[0, -1].argmax(-1).item())
print(results)
# []
What happens. The list is sent to the server and the server's copy is filled. Your list is unchanged.
Fix. Create the list inside the session with nnsight.save.
A training loop on your machine¶
vector = torch.nn.Parameter(torch.zeros(4096))
optimizer = torch.optim.Adam([vector], lr=0.1)
for batch in batches:
with model.trace(batch, remote=True):
hidden = layer.output
layer.output = hidden + vector.to(hidden.device, hidden.dtype)
loss = loss_of(model.lm_head.output).save()
loss.backward()
optimizer.step()
optimizer.zero_grad()
What happens. Each request carries a copy of vector. The gradient is computed for the
server's copy and discarded with it. vector.grad on your machine stays None,
optimizer.step() changes nothing, and the loss does not go down.
Fix. Create the parameter and the optimizer inside one session, with the loop.
See Train in one session.
Asking for a result in a tight loop¶
What happens. Every job() is a request to NDIF. The loop sends thousands of them per
minute and the job finishes no sooner.
Fix. Sleep between calls.
See Running jobs.