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

perf: support early-stopping in HF models #430

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chanind
Copy link
Collaborator

@chanind chanind commented Feb 17, 2025

Description

This PR supports early stopping in the huggingface wrapper. This is a perf improvement, so we don't need to run the whole model when all we want to do is extract some activations from an intermediate layer.

Fixes #429

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
  • performance improvement

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 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 tests

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

stop_hook = None
if stop_at_layer is not None:
if return_type != "logits":
raise NotImplementedError(

Choose a reason for hiding this comment

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

The fact that it's a NotImplementedError instead of eg ValueError or RuntimeError implies that it can be supported in the future -- is that true?

If it's true what is it -- returning (None, None) instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah there's nothing keeping this from being implemented in the future - it's just that SAELens currently doesn't use more of TLens API, so there's no point in implementing support for more options here beyond what SAELens actually uses. This class is just a wrapper around Huggingface models to make them look like a TLens model for SAELens to work wtih.

Copy link

codecov bot commented Feb 17, 2025

Codecov Report

Attention: Patch coverage is 80.95238% with 8 lines in your changes missing coverage. Please review.

Project coverage is 74.55%. Comparing base (8760f9e) to head (c29576d).

Files with missing lines Patch % Lines
sae_lens/load_model.py 80.95% 3 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #430      +/-   ##
==========================================
+ Coverage   74.44%   74.55%   +0.10%     
==========================================
  Files          19       19              
  Lines        3146     3183      +37     
  Branches      456      464       +8     
==========================================
+ Hits         2342     2373      +31     
- Misses        648      650       +2     
- Partials      156      160       +4     

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

@@ -72,6 +75,7 @@ def __init__(self, model: torch.nn.Module, tokenizer: PreTrainedTokenizerBase):
super().__init__()
self.model = model
self.tokenizer = tokenizer
self.decoder_block_matcher = guess_decoder_block_matcher(model)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is effectively getting the layer names from _guess_block_matcher_from_layers() the best we can do? I'm afraid it's error-prone and implicit.

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] Support early stopping in huggingface LLM wrapper
3 participants