Dataset in a session¶
To run a model over a dataset, load the dataset on NDIF. Import datasets and call
load_dataset inside the session. The download happens on the server, and your request stays
small.
The pattern¶
Here the model labels 40 movie reviews as positive or negative by comparing two logits. Only the tally comes back.
import torch
import nnsight
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
pos = model.tokenizer.encode(" positive")[0]
neg = model.tokenizer.encode(" negative")[0]
with model.session(remote=True):
from datasets import load_dataset
rows = load_dataset("nyu-mll/glue", "sst2", split="validation[:40]")
tally = nnsight.save({"n": 0, "correct": 0})
for start in range(0, len(rows), 10):
batch = rows[start : start + 10]
prompts = [f"Review: {s.strip()}\nSentiment:" for s in batch["sentence"]]
with model.trace(prompts):
logits = model.lm_head.output[:, -1]
pred = (logits[:, pos] > logits[:, neg]).long().cpu()
tally["n"] += len(pred)
tally["correct"] += int((pred == torch.tensor(batch["label"])).sum())
print(tally)
# {'n': 40, 'correct': 40}
What each part does¶
from datasets import load_datasetinside the session. The session body runs on the server, so the import and the download happen there.split="validation[:40]". The slice is part of the request to the Hub. The server reads 40 rows, not the whole split.- A list of strings per trace. Ten prompts are one forward pass. Batches are padded on the
left, so
[:, -1]is the last token of every row. nnsight.save({...})inside the session.tallylives on the server, each trace adds to it, and the final dict is what you get back.
The first time the server downloads a dataset, its progress bars appear as LOG lines beside
your status line.
A dataset loaded on your machine does not work¶
A Dataset object is a pointer to files in your local cache. Sent in a request, it points at
files the server does not have.
from datasets import load_dataset
rows = load_dataset("nyu-mll/glue", "sst2", split="validation[:40]") # on your machine
with model.session(remote=True):
n = nnsight.save(len(rows))
# RemoteError: Your request payload could not be read (FileNotFoundError: [Errno 2] Failed to open local file
# '/home/you/.cache/huggingface/datasets/nyu-mll___glue/sst2/0.0.0/bcdcba79d07bc864c1c254ccfcedcce55bcc9a8c/glue-validation.arrow'. ...
If your data is a short list of strings, sending it is fine: build a plain Python list and use it in the block. For anything larger, put it in a public dataset repository on the Hub and load it as above. A block cannot read a private or gated dataset (Code on the server).