ConceptStablesince v0.4.0

Model

How NeuronScope observes a model without owning it — wrapping, layer selection, hooks, and the removal contract.

Audience: All usersRead: 6 minEdit on GitHub

NeuronScope never owns your model. It wraps a reference and installs hooks with an explicit lifecycle. When the recording ends, every hook is removed and the model is left in the exact state it started in.

Wrapping a model

wrap.py·
·python
import neuronscope as ns
from transformers import AutoModel

model = AutoModel.from_pretrained("google-bert/bert-base-uncased")
wrapped = ns.Model(model, backend="pytorch")

print(wrapped.fingerprint())

Selecting layers

Layer selectors are strings. They resolve against the backend's canonical module path (parameter-name order, not traversal order — see fingerprinting).

  • "encoder.layer.*" — every encoder layer.
  • "encoder.layer.[0,5,11]" — a fixed subset.
  • "**.attention.self" — every self-attention module, anywhere in the tree.

The hook contract

  • Hooks run inside the framework's autograd context, so gradients still flow through them.
  • Hook output is copied into the ring buffer synchronously; the tensor is safe to release.
  • A hook that raises is caught, logged, and reported once at recorder close — it never crashes the run.

Removal

Recorder.close() is the only path that removes hooks. It runs in a finally block of the recording context manager, so an exception inside the loop still restores the model.

Related