Sessions¶
A session runs several traces as one job. The job waits in the queue once, uploads once and downloads once. Use one whenever an experiment has more than one trace.
Several traces, one job¶
Put remote=True on the session and leave it off the traces inside. A value from one trace
can be used in a later one without saving it, because both run on the server.
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
layer = model.model.layers[28]
with model.session(remote=True):
with model.trace("The Eiffel Tower is in the city of"):
paris = layer.output[:, -1] # stays on the server
with model.trace("The Colosseum is in the city of"):
clean = model.lm_head.output[0, -1].argmax(-1).save()
with model.trace("The Colosseum is in the city of"):
layer.output[:, -1] = paris
patched = model.lm_head.output[0, -1].argmax(-1).save()
print(model.tokenizer.decode(clean), model.tokenizer.decode(patched))
# Rome Paris
Only clean and patched are downloaded. paris, a [1, 4096] activation, never leaves
the server.
Loops and imports run on the server¶
The whole body of the session is sent, not only the traces. A for loop, an import or a
call between two traces runs on NDIF.
import nnsight
prompts = ["The capital of France is", "The capital of Japan is", "The capital of Italy is"]
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())
print([model.tokenizer.decode(a) for a in answers])
# [' Paris', ' Tokyo', ' Rome']
This is what makes it possible to load a dataset or a file of weights on the server (Dataset in a session, Weights from the Hub) and to run a training loop there (Train in one session).
Collect results across traces¶
To build one result over many traces, create the container inside the session with
nnsight.save(...) and add to it, as answers does above. The server's copy, with everything
the traces added, is what comes back.
One failure ends the session¶
If any trace in a session raises, the job ends there and nothing is returned. For work where one failure should not lose the rest, send separate jobs (Many jobs at once).
What a session does not change¶
A session saves queue waits and network round trips. The model does the same amount of work, and the whole session counts as one job against the time limit.