Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing error using LanguageModel.trace() as a context manager #273

Open
chanind opened this issue Oct 12, 2024 · 1 comment
Open

Typing error using LanguageModel.trace() as a context manager #273

chanind opened this issue Oct 12, 2024 · 1 comment

Comments

@chanind
Copy link

chanind commented Oct 12, 2024

The following code works, but pyright/pylance gives a typing error:

from nnsight import LanguageModel

model = LanguageModel('google/gemma-2-2b')

with model.trace('hi'):
    acts = model.model.layers[0].output.save()

The errors are the following:

Object of type "InterventionProxy" cannot be used with "with" because it does not implement __enter__
  Attribute "__enter__" is unknown

Object of type "InterventionProxy" cannot be used with "with" because it does not implement __exit__

It looks like this because LanguageModel.__new__() is typed as returning either a LanguageModel or an Envoy. Envoy.__call__() returns an InterventionProxy, which is not a contextmanager. Since the type checker doesn't know if the model is a LanguageModel or an Envoy, it has to assume that model might be an Envoy and thus can't use with model.trace() as a contextmanager. It seems like __new__() should be typed to just return a LanguageModel, no?

@JadenFiotto-Kaufman
Copy link
Member

@chanind Would you mind trying this on the 0.4 branch and let me know if it still complains? pip install --upgrade git+https://github.com/ndif-team/[email protected] If so I'll take a closer look.

In general though, I am doing very weird things with typing... this is because I have a lot of objects (like NNsight, Envoy, and Proxies) that represent other types of objects (Huggingface models, torch modules, and tensors respectively) but aren't themselves those objects. So I would like you IDE to still be able to complete/hint at methods/attributes on their "wrapper" objects. If you have any thoughts on this I'd love to hear them!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants