-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:stanfordnlp/pyvene into main
- Loading branch information
Showing
18 changed files
with
257 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import seaborn | ||
import torch | ||
|
||
def rotation_token_heatmap(rotate_layer, | ||
tokens, | ||
token_size, | ||
variables, | ||
intervention_size): | ||
|
||
W = rotate_layer.weight.data | ||
in_dim, out_dim = W.shape | ||
|
||
assert in_dim % token_size == 0 | ||
assert in_dim / token_size >= len(tokens) | ||
|
||
assert out_dim % intervention_size == 0 | ||
assert out_dim / intervention_size >= len(variables) | ||
|
||
heatmap = [] | ||
for j in range(len(variables)): | ||
row = [] | ||
for i in range(len(tokens)): | ||
row.append(torch.norm(W[i*token_size:(i+1)*token_size, j*intervention_size:(j+1)*intervention_size])) | ||
mean = sum(row) | ||
heatmap.append([x/mean for x in row]) | ||
return seaborn.heatmap(heatmap, | ||
xticklabels=tokens, | ||
yticklabels=variables) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
Each modeling file in this library is a mapping between | ||
abstract naming of intervention anchor points and actual | ||
model module defined in the huggingface library. | ||
We also want to let the intervention library know how to | ||
config the dimensions of intervention based on model config | ||
defined in the huggingface library. | ||
""" | ||
|
||
|
||
import torch | ||
from ..constants import * | ||
|
||
|
||
gemma_type_to_module_mapping = { | ||
"block_input": ("layers[%s]", CONST_INPUT_HOOK), | ||
"block_output": ("layers[%s]", CONST_OUTPUT_HOOK), | ||
"mlp_activation": ("layers[%s].mlp.act_fn", CONST_OUTPUT_HOOK), | ||
"mlp_output": ("layers[%s].mlp", CONST_OUTPUT_HOOK), | ||
"mlp_input": ("layers[%s].mlp", CONST_INPUT_HOOK), | ||
"attention_value_output": ("layers[%s].self_attn.o_proj", CONST_INPUT_HOOK), | ||
"head_attention_value_output": ("layers[%s].self_attn.o_proj", CONST_INPUT_HOOK), | ||
"attention_output": ("layers[%s].self_attn", CONST_OUTPUT_HOOK), | ||
"attention_input": ("layers[%s].self_attn", CONST_INPUT_HOOK), | ||
"query_output": ("layers[%s].self_attn.q_proj", CONST_OUTPUT_HOOK), | ||
"key_output": ("layers[%s].self_attn.k_proj", CONST_OUTPUT_HOOK), | ||
"value_output": ("layers[%s].self_attn.v_proj", CONST_OUTPUT_HOOK), | ||
"head_query_output": ("layers[%s].self_attn.q_proj", CONST_OUTPUT_HOOK), | ||
"head_key_output": ("layers[%s].self_attn.k_proj", CONST_OUTPUT_HOOK), | ||
"head_value_output": ("layers[%s].self_attn.v_proj", CONST_OUTPUT_HOOK), | ||
} | ||
|
||
|
||
gemma_type_to_dimension_mapping = { | ||
"block_input": ("hidden_size",), | ||
"block_output": ("hidden_size",), | ||
"mlp_activation": ("intermediate_size",), | ||
"mlp_output": ("hidden_size",), | ||
"mlp_input": ("hidden_size",), | ||
"attention_value_output": ("hidden_size",), | ||
"head_attention_value_output": ("hidden_size/num_attention_heads",), | ||
"attention_output": ("hidden_size",), | ||
"attention_input": ("hidden_size",), | ||
"query_output": ("hidden_size",), | ||
"key_output": ("hidden_size",), | ||
"value_output": ("hidden_size",), | ||
"head_query_output": ("hidden_size/num_attention_heads",), | ||
"head_key_output": ("hidden_size/num_attention_heads",), | ||
"head_value_output": ("hidden_size/num_attention_heads",), | ||
} | ||
|
||
|
||
"""gemma model with LM head""" | ||
gemma_lm_type_to_module_mapping = {} | ||
for k, v in gemma_type_to_module_mapping.items(): | ||
gemma_lm_type_to_module_mapping[k] = (f"model.{v[0]}", v[1]) | ||
|
||
|
||
gemma_lm_type_to_dimension_mapping = gemma_type_to_dimension_mapping | ||
|
||
|
||
"""gemma model with classifier head""" | ||
gemma_classifier_type_to_module_mapping = {} | ||
for k, v in gemma_type_to_module_mapping.items(): | ||
gemma_classifier_type_to_module_mapping[k] = (f"model.{v[0]}", v[1]) | ||
|
||
|
||
gemma_classifier_type_to_dimension_mapping = gemma_type_to_dimension_mapping | ||
|
||
|
||
def create_gemma( | ||
name="google/gemma-2b-it", cache_dir=None, dtype=torch.bfloat16 | ||
): | ||
"""Creates a Gemma Causal LM model, config, and tokenizer from the given name and revision""" | ||
from transformers import GemmaForCausalLM, GemmaTokenizer, GemmaConfig | ||
|
||
config = GemmaConfig.from_pretrained(name, cache_dir=cache_dir) | ||
tokenizer = GemmaTokenizer.from_pretrained(name, cache_dir=cache_dir) | ||
gemma = GemmaForCausalLM.from_pretrained( | ||
name, | ||
config=config, | ||
cache_dir=cache_dir, | ||
torch_dtype=dtype, # save memory | ||
) | ||
print("loaded model") | ||
return config, tokenizer, gemma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.