Long run with progress¶
A sweep over thousands of rows has three problems a short job does not: you cannot see how far it is, one job has a time limit, and a failure near the end loses everything. Print progress from the server, split the work into chunks, and keep each chunk's result as it arrives.
Print progress¶
print inside a session runs on the server, and each line appears on your screen as a LOG
entry while the job runs.
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) as session:
from datasets import load_dataset
rows = load_dataset("nyu-mll/glue", "sst2", split="validation[:100]")
margins = nnsight.save([])
for start in range(0, len(rows), 25):
prompts = [f"Review: {s.strip()}\nSentiment:" for s in rows[start : start + 25]["sentence"]]
with model.trace(prompts):
logits = model.lm_head.output[:, -1]
margins.extend((logits[:, pos] - logits[:, neg]).float().tolist())
print(f"{start + 25} of {len(rows)} rows")
# ℹ [e18dcb15ecc94e3fbf5782026beee873] LOG 25 of 100 rows
# ℹ [e18dcb15ecc94e3fbf5782026beee873] LOG 50 of 100 rows
# ℹ [e18dcb15ecc94e3fbf5782026beee873] LOG 75 of 100 rows
# ℹ [e18dcb15ecc94e3fbf5782026beee873] LOG 100 of 100 rows
Read what the job cost¶
After the session, session.backend.meta reports how long it ran on the server and how much
GPU memory it used. Use the first chunk to estimate the rest.
print(session.backend.meta["runtime"], session.backend.meta["max_mem_pct_by_gpu"])
# 3.4279 {'3': 9.84}
runtime is seconds: 100 rows took 3.4, so 10,000 rows would take about 6 minutes.
max_mem_pct_by_gpu is the share of the job's GPU memory it used. At 10 percent the batch
could be several times larger. Near 100 it is as large as it can be.
Split into chunks¶
Give each chunk its own session. A chunk that fails can be sent again without repeating the others, and no single job comes near the time limit.
import json
def run_chunk(start, stop):
with model.session(remote=True):
from datasets import load_dataset
rows = load_dataset("nyu-mll/glue", "sst2", split=f"validation[{start}:{stop}]")
margins = nnsight.save([])
for i in range(0, len(rows), 25):
prompts = [f"Review: {s.strip()}\nSentiment:" for s in rows[i : i + 25]["sentence"]]
with model.trace(prompts):
logits = model.lm_head.output[:, -1]
margins.extend((logits[:, pos] - logits[:, neg]).float().tolist())
return margins
for start in range(0, 400, 200):
margins = run_chunk(start, start + 200)
json.dump(margins, open(f"margins-{start}.json", "w")) # keep each chunk as it arrives
print(start, len(margins))
# 0 200
# 200 200
To run the chunks without waiting for each, send them with blocking=False
(Many jobs at once).
Choosing the chunk size¶
- Large enough that queue waits are a small part of the total.
- Small enough that one chunk's runtime is well under the model's time limit.
- Small enough that losing one chunk costs little.