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

fix: Test JumpReLU/Gated SAE and fix sae forward with error term #328

Merged
merged 8 commits into from
Oct 23, 2024

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 diverging and causing bugs.

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

Fixes #323
Fixes #326

Note: after investigating #326, it looks like this is caused by a bad copy/paste in the duplicated code for jumprelu forward, where we're accidentally including the hooks: https://github.com/jbloomAus/SAELens/blob/main/sae_lens/sae.py#L479.

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.

Copy link
Owner

Choose a reason for hiding this comment

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

yep.

@ 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
Owner

Choose a reason for hiding this comment

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

yes. Disabling hooks is a good solution, better than duplicating code.

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 64.92%. Comparing base (36e1d86) to head (bc1c798).
Report is 49 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   63.97%   64.92%   +0.94%     
==========================================
  Files          25       25              
  Lines        3223     3190      -33     
  Branches      408      407       -1     
==========================================
+ Hits         2062     2071       +9     
+ Misses       1052     1013      -39     
+ Partials      109      106       -3     

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

@chanind chanind force-pushed the test-and-refactor-sae-forward branch from ff865b3 to 5314f2a Compare October 17, 2024 11:16
@chanind chanind changed the title chore: Test JumpReLU/Gated SAE and refactor sae forward fix: Test JumpReLU/Gated SAE and refactor sae forward Oct 17, 2024
@chanind chanind changed the title fix: Test JumpReLU/Gated SAE and refactor sae forward fix: Test JumpReLU/Gated SAE and fix sae forward with error term Oct 17, 2024

# "... d_in, d_in d_sae -> ... d_sae",
hidden_pre = sae_in @ self.W_enc + self.b_enc
feature_acts = self.hook_sae_acts_post(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

calling the hook here is a bug and is responsible for #326

Copy link
Owner

Choose a reason for hiding this comment

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

correct, thanks for catching this!

Copy link
Owner

@jbloomAus jbloomAus left a comment

Choose a reason for hiding this comment

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

Great PR. Sorry for all the BS. Really like the context for disabling hooks.

@ self.W_dec
+ self.b_dec,
d_head=self.d_head,
)
Copy link
Owner

Choose a reason for hiding this comment

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

yes. Disabling hooks is a good solution, better than duplicating code.


# "... d_in, d_in d_sae -> ... d_sae",
hidden_pre = sae_in @ self.W_enc + self.b_enc
feature_acts = self.hook_sae_acts_post(
Copy link
Owner

Choose a reason for hiding this comment

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

correct, thanks for catching this!

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
Owner

Choose a reason for hiding this comment

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

yep.

@jbloomAus jbloomAus merged commit ae345b6 into main Oct 23, 2024
7 checks passed
@chanind chanind deleted the test-and-refactor-sae-forward branch October 23, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants