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

(v2) Draft PR: add Pyright static typing to hook_points.py #590

Closed
wants to merge 26 commits into from

Conversation

starship006
Copy link
Contributor

@starship006 starship006 commented May 11, 2024

Description

This is a copy of PR #464 , but just remade so that @bryce13950 can close it.


This draft PR adds Pyright static typing to hook_points.py. See Alan's message on the slack for motivation/background. Specifically, this PR:

  • modifies the HookPoint and HookedRootModule to support a baseline amount of pyright typing
  • adds a [tool.pyright] configuration to pyproject.toml to configure the typing for this project

It's not entirely complete, but is in a state to where it would benefit from some review/assistance. Before being PR ready, we should:

  • Address some TODO's I've marked on lines of code that I'm ucnertain about
  • Clean up remaining type warnings on hook.py
  • If possible, adjust the pyproject.toml file so the type warnings quit applying to all files

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@starship006
Copy link
Contributor Author

(fixing the extra bugs right now - should be done soon)

@starship006
Copy link
Contributor Author

starship006 commented May 11, 2024

I've changed _HookFunctionProtocol for the tensor parameter to take in Union[torch.Tensor, Tuple[torch.Tensor, ...]], because some new code was passing in tuples of tensors into full_hook (i think as part of hooked SAE)? not sure if this is bad or not

added assertion statements to deal with some of these issues - though, I'm recalling that alan sent a message here about using if statements instead of assertions. is that important for our purpose (and should I go through and change all instances?)

however, I'm kind of confused on how to deal with this recurring issues. we're running into some error on calls to isinstance(names_filter, Callable). i cannot figure out what is going on here, and some google searches indicate this might be a bug with the library? need some help here

^ Edit: nevermind, i think i figured it out, I've replaced those with callable(names_filter)

@bryce13950 bryce13950 changed the base branch from main to dev May 13, 2024 23:04
@bryce13950
Copy link
Collaborator

bryce13950 commented May 13, 2024

I saw that you merged main into your branch. We are actually going to be targeting dev as this will either be worked into our 2.0 release, or it will come quickly after that. I don't think there will be any new issues caused by the branch difference, but if you may want to update your branch to dev, that would probably be a good idea.
Also, for the future, if the branch you submit is named something other than main or dev, I can actually merge the most recent origin branch into your PR without bothering you. Not a big deal by any means, but it might just save you sometime in the future.

For the asserts, from a personal preference, I do prefer having errors being checked with an if, and then thrown. Assert is for testing. It really should only be used in testing. It is pretty common to use it in the research world, but throwing errors is typically the preferred way to handle things. It allows you to be specific with your errors, and it's a lot easier to read when glancing at the code. You have to be fairly vigilant to see an assert on what is most of the time a single line as opposed to an if where there's a pretty deliberate message visible in an error.
That being said, this code base is filled with places where people have asserted various bits of logic. Someone needs to go through the whole code base, and replace all of these asserts with ifs at some point. That in itself will be a project. If you have the time to change the ones you have added, then I would very much appreciate it. However, if we close out this PR, and we add a handful more asserts to the codebase, I don't really see that as a big deal. Maybe we are adding 2-3% more work to the eventual project of changing them everywhere.
The way I see it is that you have been waiting to get this change into the code base for over 5 months. If you have the time to change them, then that's great, but I am not going to require that you spend the time to do that after all of this time when you are basically following the existing style.

@bryce13950
Copy link
Collaborator

For Hooked SAE stuff, don't worry about that. We are removing that in 2.0 in favor of sending people over to SAELens. If you want to wait until tomorrow, I can have it removed, so you can merge agains the pending 2.0 with it about 98% ready for release.

cmathw and others added 2 commits May 15, 2024 21:52
)

* update activation function to tanh approximation

* keep RMSNorm calcs in float32 and match cfg dtype for embedding scaling

* formatting

* keep mypy happy

* formatting
@starship006
Copy link
Contributor Author

starship006 commented May 16, 2024

Okay sounds good, I can wait until the HookedSAE gets removed and then I'll make these changes

@bryce13950
Copy link
Collaborator

bryce13950 commented May 16, 2024

Everything pending for 2.0 is in the dev branch. If you want to just update to that branch, you should be ready to wrap this up. We are at the moment just waiting for something to be added to another project, but the actual 2.0 is going to change very little before release.

@bryce13950
Copy link
Collaborator

I'll have the hooked SAE stuff removed tomorrow, but you can just ignore it if it's still there.

@starship006
Copy link
Contributor Author

I reverted the change to _HookFunctionProtocol and moved up the if statement.

I think I'm being a git noob herem but I can't figure out how to merge the dev branch into this, all I can do is merge main into it

@starship006
Copy link
Contributor Author

Someone needs to go through the whole code base, and replace all of these asserts with ifs at some point. That in itself will be a project. If you have the time to change the ones you have added, then I would very much appreciate it. However, if we close out this PR, and we add a handful more asserts to the codebase, I don't really see that as a big deal. Maybe we are adding 2-3% more work to the eventual project of changing them everywhere.

I left the other ones alone for now, I want to get this PR done first. I might be able to do a second pass-through and clean them all up down the line.

@bryce13950
Copy link
Collaborator

If you can rename your pr branch, I can get dev merged into your branch, and wrap up the last minute details on this.

@starship006
Copy link
Contributor Author

did that, thanks

bryce13950 added a commit that referenced this pull request May 24, 2024
* Initial Commit (add pyright + test by adding few annotations)

* Slightly more typing added

* more typing

* Additional typing

* Completed typing for hook_points.py file

* todo clarifications

* formatting changes to hook_points.py

* Apply some suggestions from code review

Co-authored-by: Alan <41682961+alan-cooney@users.noreply.github.com>

* Added typing for Literals and changed some assertions to if statements

* formatting

* update to accout for merged code

* small typing issue

* changing hookfunction protocol + more assertions

* change the slice input

* change from isinstance to callable checks

* fix: Update Gemma to reflect upstream HF changes (#596)

* update activation function to tanh approximation

* keep RMSNorm calcs in float32 and match cfg dtype for embedding scaling

* formatting

* keep mypy happy

* formatting

* allow user to force trust_remote_code=true via from_pretrained kwargs (#597)

* change + revert HookFunctionProtocol

* format

* module_output is now just a tensor

* set module ouput to be any type

---------

Co-authored-by: Alan <41682961+alan-cooney@users.noreply.github.com>
Co-authored-by: Bryce Meyer <bryce13950@gmail.com>
Co-authored-by: cmathw <108584265+cmathw@users.noreply.github.com>
Co-authored-by: Clement Dumas <clement.dumas@ens-paris-saclay.fr>
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

Successfully merging this pull request may close these issues.

4 participants