Skip to content

Conversation

@BKHMSI
Copy link
Contributor

@BKHMSI BKHMSI commented Jul 6, 2024

Users can now perform functional localization as described in Brain-Like Language Processing via a Shallow Untrained Multihead Attention Network

Changes:

  • Localization stimuli saved in data/fedorenko2010_localization and can be loaded via the data_registry
  • Localization script can be found in model_helpers/localize that computes language mask according to the paper mentioned above
  • Language mask is cached in .brainio
  • HuggingfaceSubject class was adapted to extract activations from multiple layers at once and make use of the localization script if the use_localizer flag is set to True. This extracts only the language selective units from all the activations.

Usage:

  • Example script can be found in examples/score_localization
benchmark = load_benchmark('Pereira2018.243sentences-linear')

num_blocks = 12
layer_names = [f'transformer.h.{block}.{layer_type}' 
    for block in range(num_blocks) 
    for layer_type in ['ln_1', 'attn', 'ln_2', 'mlp']
]

model = HuggingfaceSubject(model_id='gpt2', 
    region_layer_mapping={ArtificialSubject.RecordingTarget.language_system: layer_names},
    use_localizer=True,
    localizer_kwargs={
        'hidden_dim': 768,
        'batch_size': 16,
        "top_k": 4096,
    }
)

model_score = benchmark(model)

Comment on lines 29 to 30
for stimuli_idx in range(3, 14):
data["sent"] += " " + data[f"stim{stimuli_idx}"].apply(str.lower)
Copy link
Member

Choose a reason for hiding this comment

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

what does this do? add comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

from brainscore_language import load_dataset

BRAINIO_CACHE = os.environ.get("BRAINIO", f"{Path.home()}/.brainio")
os.environ["TOKENIZERS_PARALLELISM"] = "False"
Copy link
Member

Choose a reason for hiding this comment

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

comment why this is necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added


class Fed10_langlocDataset(Dataset):
def __init__(self):
self.num_samples = 240
Copy link
Member

Choose a reason for hiding this comment

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

where is this being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

line #103 in the extract_representations function

Copy link
Member

Choose a reason for hiding this comment

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

ok I'm not actually sure what this does -- looks like it's just used to zero-fill layer_name (??)
Could this not also be derived from self.sentences?

   final_layer_representations = {
        "sentences": {layer_name: np.zeros((langloc_dataset.num_samples, hidden_dim)) for layer_name in layer_names},
        "non-words": {layer_name: np.zeros((langloc_dataset.num_samples, hidden_dim)) for layer_name in layer_names}
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced langloc_dataset.num_samples with len(langloc_dataset.sentences)

Copy link
Member

@mschrimpf mschrimpf left a comment

Choose a reason for hiding this comment

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

looks good; but please check comments

.gitignore Outdated
### project specific additions:

brainscore_language/data
# brainscore_language/data
Copy link
Member

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uncommented in the new commit

@@ -0,0 +1,13 @@
from brainscore_language import metric_registry
Copy link
Member

Choose a reason for hiding this comment

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

could we just import this metric from brain-score-vision?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that would require brain-score-vision to be a dependency for brain-score-language, not sure if that's a good idea?

@mschrimpf
Copy link
Member

@BKHMSI OK to merge?

@BKHMSI
Copy link
Contributor Author

BKHMSI commented Mar 12, 2025

Yes, it was tested using Python 3.11

@mschrimpf mschrimpf merged commit c505c70 into brain-score:main Mar 13, 2025
0 of 3 checks passed
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.

2 participants