-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add single token feature detection and logging #91
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,40 @@ def pool_max_activation_windows( | |
| return token_windows, activation_windows | ||
|
|
||
|
|
||
| def is_single_token_feature( | ||
| activations: Float[Tensor, "examples ctx_len"], | ||
| quantile_threshold: float = 0.5, | ||
| activation_ratio_threshold: float = 0.8, | ||
| ) -> bool: | ||
| """ | ||
| Determine if a feature is primarily activated by single tokens. | ||
|
|
||
| Args: | ||
| activations: Activation values across context windows | ||
| quantile_threshold: Threshold for considering top activations (0.5 means top 50%) | ||
| activation_ratio_threshold: Ratio of single-token activations needed (0.8 means 80%) | ||
|
|
||
| Returns: | ||
| bool: True if the feature is primarily single-token activated | ||
| """ | ||
| # For each example, check if activation is concentrated in a single position | ||
| max_activations = activations.max(dim=1).values | ||
| top_k = int(len(max_activations) * quantile_threshold) | ||
| top_indices = max_activations.topk(top_k).indices | ||
|
|
||
| # For top activating examples, check if activation is concentrated in single token | ||
| top_examples = activations[top_indices] | ||
|
|
||
| # Count positions where activation is significant | ||
| threshold = top_examples.max(dim=1).values.unsqueeze(1) * 0.5 | ||
| significant_activations = (top_examples > threshold).sum(dim=1) | ||
|
|
||
| # Calculate ratio of single token activations | ||
| single_token_ratio = (significant_activations == 1).float().mean().item() | ||
|
|
||
| return single_token_ratio >= activation_ratio_threshold | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it doesn't look like this method actually checks whether the activations are single token 🤔 maybe I'm just confused but I can't see it |
||
|
|
||
|
|
||
| def constructor( | ||
| record: LatentRecord, | ||
| activation_data: ActivationData, | ||
|
|
@@ -125,6 +159,17 @@ def constructor( | |
| max_examples = constructor_cfg.max_examples | ||
| min_examples = constructor_cfg.min_examples | ||
|
|
||
| token_windows, act_windows = pool_max_activation_windows( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this call has been duplicated by mistake. I don't believe this code will run. I have added documentation on how to run the tests to the README.md.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @meghana-0211 try |
||
| activations=activations, | ||
| tokens=reshaped_tokens, | ||
| ctx_indices=ctx_indices, | ||
| index_within_ctx=index_within_ctx, | ||
| ctx_len=example_ctx_len, | ||
| max_examples=max_examples, | ||
| ) | ||
|
|
||
| record.is_single_token = is_single_token_feature(act_windows) | ||
|
|
||
| # Get all positions where the latent is active | ||
| flat_indices = ( | ||
| activation_data.locations[:, 0] * cache_ctx_len | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the activations are already in top k order (tbh they should probably be renamed to ordered_example_acts or similar to reflect this, starting in
_top_k_pools). So we should be able to directly slice the firstlen(activations) * quantile_thresholdactivations.