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 · 0 comments
Open

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

chanind opened this issue Oct 12, 2024 · 0 comments

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?

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

1 participant