-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patching module. Remove custom engine HF cache. alteration module tha…
…t has default alterations for certain model_types
- Loading branch information
1 parent
f273f9a
commit b717b76
Showing
35 changed files
with
255 additions
and
100,570 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,3 @@ | ||
from .gpt import GPT2Patcher | ||
|
||
MODEL_TYPE_TO_ALTERATION = {"gpt2": GPT2Patcher} |
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,90 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional, Tuple, Union | ||
|
||
import torch | ||
from transformers.models import gpt2 | ||
|
||
from .. import util | ||
from ..patching import Patch, Patcher | ||
|
||
|
||
class GPT2AttentionAltered(gpt2.modeling_gpt2.GPT2Attention): | ||
def __init__(self, config, is_cross_attention=False, layer_idx=None): | ||
super().__init__(config, is_cross_attention, layer_idx) | ||
|
||
self.query = util.WrapperModule() | ||
self.key = util.WrapperModule() | ||
self.value = util.WrapperModule() | ||
|
||
def forward( | ||
self, | ||
hidden_states: Optional[Tuple[torch.FloatTensor]], | ||
layer_past: Optional[Tuple[torch.Tensor]] = None, | ||
attention_mask: Optional[torch.FloatTensor] = None, | ||
head_mask: Optional[torch.FloatTensor] = None, | ||
encoder_hidden_states: Optional[torch.Tensor] = None, | ||
encoder_attention_mask: Optional[torch.FloatTensor] = None, | ||
use_cache: Optional[bool] = False, | ||
output_attentions: Optional[bool] = False, | ||
) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: | ||
if encoder_hidden_states is not None: | ||
if not hasattr(self, "q_attn"): | ||
raise ValueError( | ||
"If class is used as cross attention, the weights `q_attn` have to be defined. " | ||
"Please make sure to instantiate class with `GPT2Attention(..., is_cross_attention=True)`." | ||
) | ||
|
||
query = self.q_attn(hidden_states) | ||
key, value = self.c_attn(encoder_hidden_states).split( | ||
self.split_size, dim=2 | ||
) | ||
attention_mask = encoder_attention_mask | ||
else: | ||
query, key, value = self.c_attn(hidden_states).split(self.split_size, dim=2) | ||
|
||
query = self._split_heads(query, self.num_heads, self.head_dim) | ||
key = self._split_heads(key, self.num_heads, self.head_dim) | ||
value = self._split_heads(value, self.num_heads, self.head_dim) | ||
|
||
# Altered ------------- | ||
|
||
query = self.query(query) | ||
key = self.key(key) | ||
value = self.value(value) | ||
|
||
# --------------------- | ||
|
||
if layer_past is not None: | ||
past_key, past_value = layer_past | ||
key = torch.cat((past_key, key), dim=-2) | ||
value = torch.cat((past_value, value), dim=-2) | ||
|
||
if use_cache is True: | ||
present = (key, value) | ||
else: | ||
present = None | ||
|
||
if self.reorder_and_upcast_attn: | ||
attn_output, attn_weights = self._upcast_and_reordered_attn( | ||
query, key, value, attention_mask, head_mask | ||
) | ||
else: | ||
attn_output, attn_weights = self._attn( | ||
query, key, value, attention_mask, head_mask | ||
) | ||
|
||
attn_output = self._merge_heads(attn_output, self.num_heads, self.head_dim) | ||
attn_output = self.c_proj(attn_output) | ||
attn_output = self.resid_dropout(attn_output) | ||
|
||
outputs = (attn_output, present) | ||
if output_attentions: | ||
outputs += (attn_weights,) | ||
|
||
return outputs # a, present, (attentions) | ||
|
||
|
||
GPT2Patcher = Patcher([ | ||
Patch(gpt2.modeling_gpt2.GPT2Attention, GPT2AttentionAltered) | ||
]) |
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,4 +1,2 @@ | ||
API: | ||
HOST: nagoya.research.khoury.northeastern.edu:5000 | ||
APP: | ||
MODEL_CACHE_PATH: ./model_checkpoints | ||
HOST: localhost:5000 |
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 was deleted.
Oops, something went wrong.
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.
Empty file.
1 change: 0 additions & 1 deletion
1
engine/model_checkpoints/models--decapoda-research--llama-65b-hf/refs/main
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...oda-research--llama-65b-hf/snapshots/47d2b93e8c0a3d5d6582bdec13f233ca0527499a/config.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...h--llama-65b-hf/snapshots/47d2b93e8c0a3d5d6582bdec13f233ca0527499a/generation_config.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...ma-65b-hf/snapshots/47d2b93e8c0a3d5d6582bdec13f233ca0527499a/pytorch_model.bin.index.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...--llama-65b-hf/snapshots/47d2b93e8c0a3d5d6582bdec13f233ca0527499a/special_tokens_map.json
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...ch--llama-65b-hf/snapshots/47d2b93e8c0a3d5d6582bdec13f233ca0527499a/tokenizer_config.json
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.