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: Test JumpReLU/Gated SAE and refactor sae forward #328

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

chanind
Copy link
Collaborator

@chanind chanind commented Oct 10, 2024

Description

This PR adds test coverage to the JumpReLU and GatedSAE encode methods.

In doing this, I realized there's a lot of duplication between all the encode variants and cleaned that up as well. I think there were some potential minor bugs in this duplication, for instance in forward(), when adding error term, we called run_time_activation_norm_fn_out() after reshape_fn_out(), but we do the opposite in decode().

It looks like most of the duplication between the error section of forward() and the normal encode() / decode() was there just to avoid triggering hooks, so I added a contextmanager _disable_hooks() which is used to disable hooks in this branch of the code while reusing our existing encode() / decode() methods. This should mean we don't need to worry about these duplicated codepaths being slightly different.

If the refactor is out of scope, I can revert the changes to sae.py and just leave the test coverage.

Fixes #323

Type of change

Please delete options that are not relevant.

  • refactor
  • 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

You have tested formatting, typing and unit tests (acceptance tests not currently in use)

  • I have run make check-ci to check format and linting. (you can run make format to format code if needed.)

x = self.run_time_activation_norm_fn_in(x)

# apply b_dec_to_input if using that method.
sae_in = self.hook_sae_input(x - (self.b_dec * self.cfg.apply_b_dec_to_input))
Copy link
Collaborator Author

@chanind chanind Oct 10, 2024

Choose a reason for hiding this comment

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

we were calling hook_sae_input() here after subtracting b_dec, but in other encode() variants we call hook_sae_input() after reshape_fn_in() instead. I assumed this difference was a bug and not intentional.

@ self.W_dec
+ self.b_dec,
d_head=self.d_head,
)
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 assumed the code in these codepaths is trying to exactly recreate all the encode_x() variants followed by decode(), but is duplicating code just to avoid triggering hooks.

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.

Project coverage is 63.38%. Comparing base (432df87) to head (9b2f417).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
sae_lens/sae.py 96.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #328      +/-   ##
==========================================
+ Coverage   62.43%   63.38%   +0.95%     
==========================================
  Files          25       25              
  Lines        3144     3111      -33     
  Branches      550      398     -152     
==========================================
+ Hits         1963     1972       +9     
+ Misses       1077     1038      -39     
+ Partials      104      101       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

[Proposal] Add test coverage to GatedSAE and JumpReLU implementations
1 participant