Skip to content

NNsight and NDIF: Democratizing Access to Open-Weight Foundation Model Internals

optionalextendedfrontiertoolrubric 8/122024 · ~40 min

Jaden Fiotto-Kaufman, Alexander R. Loftus, Eric Todd, … , David Bau

Status

TL;DR. nnsight wraps any PyTorch model, including HuggingFace transformers, diffusers and vLLM, so that you can describe an experiment as Python inside with model.trace(...). The 2024 paper describes compiling this block into an intervention graph. The current release (0.8) instead captures and compiles the block’s Python source and runs it interleaved with the forward pass. The same experiment can run locally or, with remote=True, on NDIF, a shared service with large models already loaded. Install with pip install "nnsight>=0.8"; examples here follow the 0.8 API, and older tutorials may use the earlier execution model.

Execution model. The paper describes an earlier intervention-graph architecture. In nnsight 0.8, a trace captures and compiles the block’s Python source, then interleaves its execution with the model. Reading model.transformer.h[6].output waits for the actual tensor at that module. Assigning to it replaces the value before downstream computation. .save() (bound to a name) retains a result after the block exits. Remote execution sends the trace to NDIF, where the model is already loaded, so researchers never have to load 70B+ weights themselves. The paper’s argument that a constrained graph lets NDIF safely co-host many users’ experiments (“co-tenancy”) was made for the graph design; how the current service isolates arbitrary Python is a property of the NDIF deployment, not of the tracing model.

from nnsight import TransformersModel
model = TransformersModel("openai-community/gpt2", dispatch=True)
with model.trace(clean_prompt):
clean_h = model.transformer.h[5].output.save()
with model.trace(corrupted_prompt):
model.transformer.h[5].output[:, pos] = clean_h[:, pos] # denoising patch
logits = model.output.logits.save()

Why it matters. Nanda’s guide recommends nnsight for larger models, since it is a thin wrapper over standard libraries, and points to NDIF hosting models up to Llama 3 405B. ARENA 1.3.2 (function vectors and steering) is taught in nnsight. For an IR lab without large GPUs, NDIF is the most practical way to run patching or steering on frontier-scale open LLM rerankers. Module paths follow the HuggingFace model (e.g. model.model.layers for Llama), not TransformerLens’s uniform names.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Mechanism

    Inside a with model.trace(...) block nnsight (0.8) does not simply run your code line by line before the model. Explain what happens to the block, how reading and assigning .output work, and why .save() is needed. How does this differ from the intervention graph described in the 2024 paper?

    What a strong answer covers
    • In 0.8 the trace captures the block's Python source (via its AST), compiles it, and on exit runs it interleaved with the model's forward pass as ordinary Python.
    • Reading module.output waits until execution reaches that module and returns the actual tensor; assigning to it (or editing in place) replaces the value before downstream computation uses it.
    • Only values marked with .save() and bound to a name are kept after the block exits; everything else is discarded.
    • With remote=True the trace (its source plus the values it references) is sent to NDIF, which runs it against an already-loaded model and returns the saved values.
    • The paper describes an earlier architecture in which the block built a graph of proxy operations; that intervention-graph account is historical context for current versions.
  2. Design

    Write the nnsight version of denoising activation patching for one layer and position of GPT-2, then say what changes to run it on a 70B model via NDIF.

    What a strong answer covers
    • Trace 1 on the clean prompt: save model.transformer.h[L].output.
    • Trace 2 on the corrupted prompt: assign the clean value at position p into h[L].output and save the logits or a logit difference.
    • Alternatively, use two tracer.invoke blocks in one trace for batching; each invoke sees only its own rows.
    • For a large model: load it by name without local weights and pass remote=True with an NDIF API key; module paths change (e.g. model.model.layers) and only saved values are returned.
  3. Critique

    The paper finds 60.6% of surveyed papers since Feb 2023 study models below 40% MMLU, and argues that remote access fixes this. Assess the evidence and what NDIF does and does not solve.

    What a strong answer covers
    • Survey: 184 papers from the citations of an interpretability survey; the share measures model capability, not a direct measure of compute limits.
    • Benchmarks: on four H100s nnsight is comparable to TransformerLens, pyvene and baukit, except TransformerLens loads weights about three times slower.
    • On OPT models (125M–66B), HPC load time grows nearly linearly while NDIF load time is negligible; remote patching adds a roughly constant overhead and wins from about 3B parameters.
    • NDIF significantly outperforms Petals for remote interventions but matches it for plain inference.
    • Limits: you can only use models NDIF hosts, you share a queue with other users, and capability is only one of the barriers (data, expertise and time are others).

Done with this page?

Status