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

Support for functional localization #240

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

Conversation

BKHMSI
Copy link

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

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