-
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 pull request #93 from stanfordnlp/zen/stringaccess
[Major] Update with string access and code refactory (#83)
- Loading branch information
Showing
18 changed files
with
433 additions
and
1,203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
tutorials/* linguist-vendored | ||
tutorials/basic_tutorials/* linguist-vendored | ||
tutorials/advanced_tutorials/* linguist-vendored | ||
pyvene_101.ipynb | ||
tests/qa_runbook.ipynb linguist-vendored |
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 |
---|---|---|
@@ -1,88 +1,17 @@ | ||
CONST_VALID_INTERVENABLE_UNIT = ["pos", "h", "h.pos", "t"] | ||
|
||
import torch | ||
|
||
CONST_INPUT_HOOK = "register_forward_pre_hook" | ||
CONST_OUTPUT_HOOK = "register_forward_hook" | ||
CONST_GRAD_HOOK = "register_hook" | ||
|
||
|
||
CONST_TRANSFORMER_TOPOLOGICAL_ORDER = [ | ||
"block_input", | ||
"query_output", | ||
"head_query_output", | ||
"key_output", | ||
"head_key_output", | ||
"value_output", | ||
"head_value_output", | ||
"attention_input", | ||
"attention_weight", | ||
"head_attention_value_output", | ||
"attention_value_output", | ||
"attention_output", | ||
"cross_attention_input", | ||
"head_cross_attention_value_output", | ||
"cross_attention_value_output", | ||
"cross_attention_output", | ||
"mlp_input", | ||
"mlp_activation", | ||
"mlp_output", | ||
"block_output", | ||
# special keys for backpack model | ||
"sense_block_output", | ||
"sense_mlp_input", | ||
"sense_mlp_activation", | ||
"sense_mlp_output", | ||
"sense_output", | ||
] | ||
|
||
|
||
CONST_MLP_TOPOLOGICAL_ORDER = [ | ||
"block_input", | ||
"mlp_activation", | ||
"block_output", | ||
] | ||
|
||
|
||
CONST_GRU_TOPOLOGICAL_ORDER = [ | ||
"cell_input", | ||
"x2h_output", | ||
"h2h_output", | ||
"reset_x2h_output", | ||
"update_x2h_output", | ||
"new_x2h_output", | ||
"reset_h2h_output", | ||
"update_h2h_output", | ||
"new_h2h_output", | ||
"reset_gate_input", | ||
"update_gate_input", | ||
"new_gate_input", | ||
"reset_gate_output", | ||
"update_gate_output", | ||
"new_gate_output", | ||
"cell_output", | ||
] | ||
|
||
|
||
CONST_QKV_INDICES = { | ||
"query_output": 0, | ||
"key_output": 1, | ||
"value_output": 2, | ||
"head_query_output": 0, | ||
"head_key_output": 1, | ||
"head_value_output": 2, | ||
"reset_x2h_output": 0, | ||
"update_x2h_output": 1, | ||
"new_x2h_output": 2, | ||
"reset_h2h_output": 0, | ||
"update_h2h_output": 1, | ||
"new_h2h_output": 2, | ||
} | ||
split_and_select = lambda x, num_slice, selct_index: torch.chunk(x, num_slice, dim=-1)[selct_index] | ||
def split_heads(tensor, num_heads, attn_head_size): | ||
"""Splits hidden_size dim into attn_head_size and num_heads.""" | ||
new_shape = tensor.size()[:-1] + (num_heads, attn_head_size) | ||
tensor = tensor.view(new_shape) | ||
return tensor.permute(0, 2, 1, 3) # (batch, head, seq_length, head_features) | ||
|
||
CONST_RUN_INDICES = { | ||
"reset_x2h_output": 0, | ||
"update_x2h_output": 1, | ||
"new_x2h_output": 2, | ||
"reset_h2h_output": 0, | ||
"update_h2h_output": 1, | ||
"new_h2h_output": 2, | ||
} | ||
split_half = lambda x, selct_index: torch.chunk(x, 2, dim=-1)[selct_index] | ||
split_three = lambda x, selct_index: torch.chunk(x, 3, dim=-1)[selct_index] | ||
split_head_and_permute = lambda x, num_head: split_heads(x, num_head, x.shape[-1]//num_head) |
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
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
Oops, something went wrong.