-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
52 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
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,41 +1,36 @@ | ||
import torch | ||
from internlm.initialize.initialize_tensor import normal_, scaled_init_method_normal | ||
|
||
from .configuration_internlm2 import InternLM2Config | ||
from .modeling_internlm2 import InternLM2ForCausalLM | ||
|
||
from internlm.initialize.initialize_tensor import ( | ||
normal_, | ||
scaled_init_method_normal, | ||
) | ||
|
||
import torch | ||
|
||
def reset_attn_parameters(layer_idx, layer, use_scaled_init=True): | ||
def reset_attn_parameters(layer_idx, layer, use_scaled_init=True, std=0.02): | ||
for name, param in layer.attention.named_parameters(): | ||
if param.ndim == 1: | ||
if param.ndim == 1: # bias | ||
param.data.zero_() | ||
elif "wq" in name or "wk" in name or "wv" in name: | ||
normal_(std=0.02)(param.data) | ||
normal_(std=std)(param.data) | ||
elif use_scaled_init: # wo | ||
scaled_init_method_normal(sigma=0.02, num_layers=layer_idx + 1)(param.data) | ||
else: | ||
normal_(std=0.02)(param.data) | ||
scaled_init_method_normal(sigma=std, num_layers=layer_idx + 1)(param.data) | ||
else: # wo | ||
normal_(std=std)(param.data) | ||
|
||
for name, param in layer.feed_forward.named_parameters(): | ||
if use_scaled_init: | ||
scaled_init_method_normal(sigma=0.02, num_layers=layer_idx + 1)(param.data) | ||
scaled_init_method_normal(sigma=std, num_layers=layer_idx + 1)(param.data) | ||
else: | ||
normal_(std=0.02)(param.data) | ||
normal_(std=std)(param.data) | ||
|
||
|
||
def reset_parameters(model): | ||
def reset_parameters(model, std=0.02): | ||
with torch.no_grad(): | ||
for _, param in model.model.tok_embeddings.named_parameters(): | ||
normal_(std=0.02)(param) | ||
normal_(std=std)(param) | ||
for layer_idx, layer in enumerate(model.model.layers): | ||
reset_attn_parameters(layer_idx, layer) | ||
for _, param in model.output.named_parameters(): | ||
normal_(std=0.02)(param) | ||
normal_(std=std)(param) | ||
|
||
|
||
__all__ = [ | ||
"InternLM2Config", | ||
"InternLM2ForCausalLM", | ||
"reset_parameters" | ||
] | ||
__all__ = ["InternLM2Config", "InternLM2ForCausalLM", "reset_parameters"] |
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,7 +1,40 @@ | ||
import torch | ||
from internlm.initialize.initialize_tensor import normal_, scaled_init_method_normal | ||
|
||
from .configuration_internlm import InternLMConfig | ||
from .modeling_internlm import InternLMForCausalLM | ||
|
||
|
||
def reset_attn_parameters(layer_idx, layer, use_scaled_init=True, std=0.02): | ||
for name, param in layer.self_attn.named_parameters(): | ||
if param.ndim == 1: # bias | ||
param.data.zero_() | ||
elif "q_proj" in name or "k_proj" in name or "v_proj" in name: | ||
normal_(std=std)(param.data) | ||
elif use_scaled_init: # wo | ||
scaled_init_method_normal(sigma=std, num_layers=layer_idx + 1)(param.data) | ||
else: # wo | ||
normal_(std=std)(param.data) | ||
|
||
for name, param in layer.mlp.named_parameters(): | ||
if use_scaled_init: | ||
scaled_init_method_normal(sigma=std, num_layers=layer_idx + 1)(param.data) | ||
else: | ||
normal_(std=std)(param.data) | ||
|
||
|
||
def reset_parameters(model, std=0.02): | ||
with torch.no_grad(): | ||
for _, param in model.model.embed_tokens.named_parameters(): | ||
normal_(std=std)(param) | ||
for layer_idx, layer in enumerate(model.model.layers): | ||
reset_attn_parameters(layer_idx, layer) | ||
for _, param in model.lm_head.named_parameters(): | ||
normal_(std=std)(param) | ||
|
||
|
||
__all__ = [ | ||
"InternLMConfig", | ||
"InternLMForCausalLM", | ||
"reset_parameters" | ||
] |