V10a
Browse files
improve_gainlora/src/t5_gainlora_inflora.py
CHANGED
|
@@ -638,24 +638,17 @@ class T5Attention(nn.Module):
|
|
| 638 |
|
| 639 |
def agg_lora_states(hidden_states, lora_layer, pre_lora_layer, key_attention_weights):
|
| 640 |
if pre_lora_layer is not None:
|
| 641 |
-
|
| 642 |
-
|
|
|
|
|
|
|
|
|
|
| 643 |
|
| 644 |
-
# 1. Current expert
|
| 645 |
-
weights = key_attention_weights.squeeze(-1) # [batch_size, n_tasks]
|
| 646 |
-
agg_lora_states = lora_layer(hidden_states) * weights[:, 0].view(-1, 1, 1)
|
| 647 |
-
|
| 648 |
-
# 2. Previous experts
|
| 649 |
-
for i, pre_lora in enumerate(pre_lora_layer):
|
| 650 |
-
with torch.no_grad():
|
| 651 |
-
agg_lora_states += pre_lora(hidden_states) * weights[:, i + 1].view(-1, 1, 1)
|
| 652 |
-
|
| 653 |
else:
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
agg_lora_states = lora_layer(hidden_states) * weights[:, 0].view(-1, 1, 1)
|
| 657 |
|
| 658 |
-
return agg_lora_states
|
| 659 |
|
| 660 |
# modified
|
| 661 |
if self.get_feature:
|
|
|
|
| 638 |
|
| 639 |
def agg_lora_states(hidden_states, lora_layer, pre_lora_layer, key_attention_weights):
|
| 640 |
if pre_lora_layer is not None:
|
| 641 |
+
cur_lora_states = lora_layer(hidden_states).unsqueeze(0)
|
| 642 |
+
with torch.no_grad():
|
| 643 |
+
pre_lora_states = torch.cat([pre_lora(hidden_states).unsqueeze(0) for pre_lora in pre_lora_layer], dim=0)
|
| 644 |
+
concat_q = torch.cat([cur_lora_states, pre_lora_states], dim=0).transpose(0, 1).reshape(batch_size, -1, hidden_states.shape[1]*self.inner_dim)
|
| 645 |
+
agg_lora_states = torch.matmul(key_attention_weights.transpose(1, 2), concat_q).squeeze()
|
| 646 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
else:
|
| 648 |
+
cur_lora_states = lora_layer(hidden_states).unsqueeze(0).transpose(0, 1).reshape(batch_size, -1, hidden_states.shape[1]*self.inner_dim)
|
| 649 |
+
agg_lora_states = torch.matmul(key_attention_weights.transpose(1, 2), cur_lora_states).squeeze()
|
|
|
|
| 650 |
|
| 651 |
+
return agg_lora_states.reshape(batch_size, -1, self.inner_dim)
|
| 652 |
|
| 653 |
# modified
|
| 654 |
if self.get_feature:
|
improve_gainlora/src/t5_specroute.py
CHANGED
|
@@ -893,7 +893,7 @@ class T5ForConditionalGeneration(T5PreTrainedModel):
|
|
| 893 |
|
| 894 |
if 'lm_head.weight' in state_dict:
|
| 895 |
model.lm_head.weight.data.copy_(state_dict['lm_head.weight'])
|
| 896 |
-
|
| 897 |
# 5. Extract and load encoder/decoder stacks after stripping prefixes
|
| 898 |
# Standard T5 has 'encoder.block.0...' but we need 'block.0...'
|
| 899 |
encoder_state = {k[len('encoder.'):]: v for k, v in state_dict.items() if k.startswith('encoder.')}
|
|
|
|
| 893 |
|
| 894 |
if 'lm_head.weight' in state_dict:
|
| 895 |
model.lm_head.weight.data.copy_(state_dict['lm_head.weight'])
|
| 896 |
+
|
| 897 |
# 5. Extract and load encoder/decoder stacks after stripping prefixes
|
| 898 |
# Standard T5 has 'encoder.block.0...' but we need 'block.0...'
|
| 899 |
encoder_state = {k[len('encoder.'):]: v for k, v in state_dict.items() if k.startswith('encoder.')}
|