Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,11 @@ MAX_PROMPT_TOKENS = 60
|
|
| 21 |
MAX_NUM_LAYERS = 50
|
| 22 |
welcome_message = '**You are now running {model_name}!!** 🥳🥳🥳'
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@dataclass
|
| 25 |
class LocalState:
|
| 26 |
hidden_states: Optional[torch.Tensor] = None
|
|
@@ -96,8 +101,8 @@ def get_hidden_states(raw_original_prompt, force_hidden_states=False):
|
|
| 96 |
outputs = model(**model_inputs, output_hidden_states=True, return_dict=True)
|
| 97 |
hidden_states = torch.stack([h.squeeze(0).cpu().detach() for h in outputs.hidden_states], dim=0)
|
| 98 |
# TODO: document this!
|
| 99 |
-
hidden_scores = F.normalize(hidden_states, dim=-1).diff(dim=0).norm(dim=-1).cpu() # num_layers x num_tokens
|
| 100 |
-
important_tokens =
|
| 101 |
print(f'{important_tokens=}\t\t{hidden_states.shape=}')
|
| 102 |
global_state.local_state.hidden_states = hidden_states.cpu().detach()
|
| 103 |
|
|
@@ -162,7 +167,6 @@ def run_interpretation(raw_original_prompt, raw_interpretation_prompt, max_new_t
|
|
| 162 |
diff_score = ((diff_score1 - diff_score1.min()) / (diff_score1.max() - diff_score1.min())
|
| 163 |
+ (diff_score2 - diff_score2.min()) / (diff_score2.max() - diff_score2.min()))
|
| 164 |
|
| 165 |
-
avoid_first, avoid_last = 3, 3 # layers that are usually never important
|
| 166 |
assert avoid_first >= 1 # due to .diff() we will not be able to compute a score for the first layer
|
| 167 |
diff_score = diff_score[avoid_first-1:len(diff_score)-avoid_last]
|
| 168 |
important_idxs = avoid_first + diff_score.topk(k=int(np.ceil(0.3 * len(diff_score)))).indices.cpu().numpy() #
|
|
|
|
| 21 |
MAX_NUM_LAYERS = 50
|
| 22 |
welcome_message = '**You are now running {model_name}!!** 🥳🥳🥳'
|
| 23 |
|
| 24 |
+
# Used by the layer and token importance heuristic in this file.
|
| 25 |
+
# These layers are usually not important. We will ignore them when looking for important layers
|
| 26 |
+
avoid_first, avoid_last = 3, 2
|
| 27 |
+
|
| 28 |
+
|
| 29 |
@dataclass
|
| 30 |
class LocalState:
|
| 31 |
hidden_states: Optional[torch.Tensor] = None
|
|
|
|
| 101 |
outputs = model(**model_inputs, output_hidden_states=True, return_dict=True)
|
| 102 |
hidden_states = torch.stack([h.squeeze(0).cpu().detach() for h in outputs.hidden_states], dim=0)
|
| 103 |
# TODO: document this!
|
| 104 |
+
hidden_scores = F.normalize(hidden_states[avoid_first-1:len(hidden_states)-avoid_last], dim=-1).diff(dim=0).norm(dim=-1).cpu() # num_layers x num_tokens
|
| 105 |
+
important_tokens = np.unravel_index(hidden_scores.flatten().topk(k=5).indices.numpy(), hidden_scores.shape)[1]
|
| 106 |
print(f'{important_tokens=}\t\t{hidden_states.shape=}')
|
| 107 |
global_state.local_state.hidden_states = hidden_states.cpu().detach()
|
| 108 |
|
|
|
|
| 167 |
diff_score = ((diff_score1 - diff_score1.min()) / (diff_score1.max() - diff_score1.min())
|
| 168 |
+ (diff_score2 - diff_score2.min()) / (diff_score2.max() - diff_score2.min()))
|
| 169 |
|
|
|
|
| 170 |
assert avoid_first >= 1 # due to .diff() we will not be able to compute a score for the first layer
|
| 171 |
diff_score = diff_score[avoid_first-1:len(diff_score)-avoid_last]
|
| 172 |
important_idxs = avoid_first + diff_score.topk(k=int(np.ceil(0.3 * len(diff_score)))).indices.cpu().numpy() #
|