ndif-team / nnsight

The nnsight package enables interpreting and manipulating the internals of deep learned models.
https://nnsight.net/
MIT License
400 stars 37 forks source link

Modularization and Edits #82

Closed cadentj closed 7 months ago

cadentj commented 8 months ago

Module Naming Standards and Module Edits

See this notebook for basic usage of the Edit module.

Standardization

The Alterations module contains simple bespoke name maps for different models.

You can pass unified=True to any model that inherits from NNsight model. This will load a dictionary attr_map into the model which is passed into ._envoy.

The Envoy maps standardized names onto bespoke model attributes, and represents custom names in a defined __repr__ and __str__ method.

model = LanguageModel("openai-community/gpt2", device_map="cuda:0", unified=True)

Module Edits

The Edit module is designed for dynamically editing models during runtime. Under the hood, it uses TorchDynamo to recompile PyTorch code into Torch FX graphs.

Edit Module

The Edit module contains three classes: Edit, Compiler, and Editor.

Edit The Edit object defines some edit a user wishes to make on the compiled FX graph.

Compiler The compiler is responsible for grouping and optimizing edits. A couple important methods:

Editor The Editor is a simple context manager which is entered if edits are passed into a LanguageModel.

Utils and Relevant Changes

A fold method was added to envoy which folds in _orig_mod for easier attribute access.

The Edit module contains various utils for printing and inspecting TorchDynamo compiled modules.

TODO