Writing a block¶
A remote block is written like a local one. This page covers what changes when it runs on NDIF: what comes back, what is sent, and where values live.
Save what you want back¶
A value comes back only if you .save() it. It returns to the variable you assigned it to, as
a tensor on the CPU.
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
with model.trace("The Eiffel Tower is in the city of", remote=True):
hidden = model.model.layers[10].output # not saved
token = model.lm_head.output[0, -1].argmax(-1).save() # saved
print(model.tokenizer.decode(token), token.device)
# Paris cpu
print("hidden" in dir())
# False
Everything you save is downloaded, so save the smallest value that answers your question: a token id instead of the logits, one position instead of the sequence. Sweep and summarize shows this over a dataset.
What is sent¶
nnsight sends the lines inside the with and every outside value those lines use. A tensor you
made locally arrives on the CPU, in the dtype you made it in. The server holds the model in
half precision and may spread it over several GPUs, so move the tensor where you use it and
take the device and dtype from the activation.
import torch
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
torch.manual_seed(0)
direction = torch.randn(4096)
direction = direction / direction.norm()
with model.trace("The Eiffel Tower is in the city of", remote=True):
hidden = model.model.layers[10].output
model.model.layers[10].output = hidden + 200.0 * direction.to(hidden.device, hidden.dtype)
token = model.lm_head.output[0, -1].argmax(-1).save()
print(model.tokenizer.decode(token))
# Hospital
A large tensor in the request is a large upload. To use big weights or a dataset, load them on the server instead (Dataset in a session, Weights from the Hub).
Helper functions and classes¶
A function or class defined in your script or notebook is sent with the block.
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
def last_token(logits):
return logits[0, -1].argmax(-1)
with model.trace("The Eiffel Tower is in the city of", remote=True):
token = last_token(model.lm_head.output).save()
print(model.tokenizer.decode(token))
# Paris
Code in a .py file beside your script is sent the same way. A library you installed with pip
is not: register it.
Two rules for code you send:
- A class calls
super(MyClass, self).__init__(), with the class named. A baresuper()fails on the server. - A package your code imports has to be installed on the server (Code on the server).
Print¶
print inside the block runs on the server. Each line shows up beside the status line as a
LOG entry while the job runs.
with model.trace("The Eiffel Tower is in the city of", remote=True):
hidden = model.model.layers[10].output
print("layer 10 norm:", hidden[0, -1].norm().item())
token = model.lm_head.output[0, -1].argmax(-1).save()
# ℹ [1ae6b40a7b2a4169894d09c08878de31] LOG layer 10 norm: 57.60318374633789
Print summaries, not tensors.
Generation¶
model.generate(..., remote=True) generates on the server. tracer.result is the token ids.
with model.generate("The Eiffel Tower is in the city of", max_new_tokens=5, do_sample=False, remote=True) as tracer:
out = tracer.result.save()
print(model.tokenizer.decode(out[0]))
# The Eiffel Tower is in the city of Paris, France. The
To read or change a value at each generated token, loop with tracer.iter, as in
Multiple Token Generation.
Gradients¶
The server's weights do not track gradients. Turn tracking on at an activation, then run the
backward pass and read .grad inside it.
with model.trace("The Eiffel Tower is in the city of", remote=True):
hidden = model.model.layers[10].output
hidden.requires_grad_(True)
logits = model.lm_head.output
with logits[0, -1].float().max().backward():
grad = hidden.grad.save()
print(grad.shape)
# torch.Size([1, 10, 4096])
torch.autograd.grad and gradients of the model's own weights need
trusted access.
Attention weights¶
A model returns attention weights only with the eager attention implementation. Ask for it in the constructor. nnsight sends the choice with each request.
from nnsight import TransformersModel
eager = TransformersModel("Qwen/Qwen3-8B", attn_implementation="eager")
with eager.trace("The Eiffel Tower is in the city of", remote=True):
weights = eager.model.layers[0].self_attn.output[1].save()
print(weights.shape) # [batch, heads, query, key]
# torch.Size([1, 32, 10, 10])