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

chore: setting up pyright type checking and fixing typing errors #18

Merged
merged 2 commits into from
Mar 1, 2024

Conversation

chanind
Copy link
Collaborator

@chanind chanind commented Feb 28, 2024

This PR sets up Pyright for type-checking, and attempts to fix all type errors in the codebase.

Some things to note:

  • I struggled a lot with the types of the various configs expected throughout the code, I might have made a mistake somewhere. I switched to just typing the configs as Any in later parts of the code.
  • I was pretty liberal about casting to Any when I didn't know what the correct type of things was
  • I was pretty liberal about adding assertions to help the type checker understand what's going on
  • This PR does have some minor changes to the codebase itself where necessary for the type checking to pass. Likely many of these are actual bugs in the code, but it's also possible I've messed something up and potentially broken something.
  • I left TODO notes where there was some weird typing thing I didn't know how to handle properly.
  • The geom_median dir is ignored by the type checking, since that's sort of independent of this codebase.

closes #15

@@ -462,11 +465,7 @@ def get_all_html(self, debug: bool = False, split_scripts: bool = False) -> str:
if debug:
display(HTML(html_string))

if split_scripts:
scripts, html_string = extract_and_remove_scripts(html_string)
return scripts, html_string
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This split_scripts option causes the return type to be a tuple instead of a string, which breaks typing. It looks like this option is never used anyway throughout the code so I just deleted it here. If this is not OK I can try going back and casting everything to str everywhere this function is called, but IMO it's probably cleaner to have functions have only a single return type in general.

@chanind
Copy link
Collaborator Author

chanind commented Feb 28, 2024

Looks like tests are failing due to the huggingface hub being down 🤦‍♂️. https://twitter.com/huggingface/status/1762954032312639702

@jbloomAus
Copy link
Owner

Looks like tests are failing due to the huggingface hub being down 🤦‍♂️. https://twitter.com/huggingface/status/1762954032312639702

No worries! Can wait.

@jbloomAus
Copy link
Owner

@chanind Amazing work! I think we're likely to delete a lot of the SAE visualizer stuff (sorry for not telling you earlier) as the new version of SAE vis (https://github.com/callummcdougall/sae_vis) should more/less work with our code. I'll make an issue for making a script to show how to use this with those SAEs.

Also #17 might cause some further type checking issues. Idk if it make more sense to merge this PR or that PR first, do you have thoughts? Many thanks!

@chanind
Copy link
Collaborator Author

chanind commented Feb 29, 2024

No worries! Go ahead and merge #17 first and I'll fix up any typing issues in this PR. That's likely easier than you having to figure out the typing stuff just to get the PR merged. It shouldn't be too much to fix up.

@jbloomAus
Copy link
Owner

@chanind other PR has been merged if you want to rebase :)


assert sparse_autoencoder.cfg.d_sae is not None # keep pyright happy
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is d_sae really optional? It seems like throughout the code it's assumed to be present

self.activation_store,
) = LMSparseAutoencoderSessionloader.load_session_from_pretrained(self.sae_path)
self.sparse_autoencoder = sae_group.autoencoders[0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to pull out just the first autoencoder from the group here, since it looks like this file is expecting a single autoencoder to come out of this function rather than a group. IIRC this file will be removed in the future anyway, so probably fine?

self.activation_store,
) = LMSparseAutoencoderSessionloader.load_session_from_pretrained(self.sae_path)
# TODO: handle multiple autoencoders
self.sparse_autoencoder = sae_group.autoencoders[0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to pull out just the first autoencoder from the group here as well. Is this file also going to be removed in the future?

@chanind
Copy link
Collaborator Author

chanind commented Feb 29, 2024

👍 should be updated for the new changes.

@jbloomAus jbloomAus merged commit bd5fc43 into jbloomAus:main Mar 1, 2024
2 checks passed
@jbloomAus
Copy link
Owner

thanks @chanind

@chanind chanind deleted the type-checking branch March 1, 2024 16:23
tom-pollak pushed a commit to tom-pollak/SAELens that referenced this pull request Oct 22, 2024
chore: setting up pyright type checking and fixing typing errors
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.

Type-checking is not enforced
2 participants