-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for model parallelism (#110)
* Add support for Model Parallelism * Add support for Model Parallelism * Add support for Model Parallelism * Add support for Model Parallelism * Add support for Model Parallelism * Update * update * update * Update trainer.py * Update trainer.py * Update * Update * Add files via upload * Update convert_llama_from_megatron_checkpoint_to_pytorch_checkpoint.py * Update convert_llama_from_pytorch_checkpoint_to_megatron_checkpoint.py * Update trainer.py * Update convert_llama_from_megatron_checkpoint_to_pytorch_checkpoint.py * Update convert_llama_from_pytorch_checkpoint_to_megatron_checkpoint.py * update dataloader name * update comment --------- Co-authored-by: Cheng <[email protected]> Co-authored-by: “karots123” <“962”[email protected]> Co-authored-by: kaeli <[email protected]>
- Loading branch information
1 parent
669d46c
commit f7f18c8
Showing
29 changed files
with
2,894 additions
and
138 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ six>=1.12.0 | |
packaging | ||
numpy | ||
regex | ||
sentencepiece |
56 changes: 56 additions & 0 deletions
56
scripts/convert_llama_from_megatron_checkpoint_to_pytorch_checkpoint.py
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,56 @@ | ||
import argparse | ||
import os | ||
import torch | ||
|
||
|
||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument("--input_model_path", type=str, default="models/input_model", | ||
help=".") | ||
parser.add_argument("--output_model_path", type=str, default="models/output_model", | ||
help=".") | ||
parser.add_argument("--layers_num", type=int, default=32) | ||
parser.add_argument("--tensor_model_parallel_size", type=int, default=4) | ||
|
||
|
||
args = parser.parse_args() | ||
|
||
if not os.path.exists(args.output_model_path): | ||
os.mkdir(args.output_model_path) | ||
|
||
output_model=torch.load(os.path.join(args.input_model_path,'mp_rank_00_model_states.pt'),map_location='cpu')["module"] | ||
|
||
for n in range(1,args.tensor_model_parallel_size): | ||
index=str(n) if len(str(n))==2 else '0'+str(n) | ||
model_name=f"mp_rank_{index}_model_states.pt" | ||
model_piece = torch.load(os.path.join(args.input_model_path,model_name),map_location="cpu")["module"] | ||
output_model["embedding.word.embedding.weight"] = torch.cat((output_model["embedding.word.embedding.weight"],model_piece["embedding.word.embedding.weight"]),dim=-2) | ||
|
||
for i in range(args.layers_num): | ||
for j in range(3): | ||
tensor_a=output_model["encoder.transformer." + str(i) + ".self_attn.linear_layers."+ str(j) +".weight"] | ||
tensor_b=model_piece["encoder.transformer." + str(i) + ".self_attn.linear_layers."+ str(j) +".weight"] | ||
output_model["encoder.transformer." + str(i) + ".self_attn.linear_layers."+ str(j) +".weight"]=torch.cat((tensor_a,tensor_b),dim=-2) | ||
|
||
tensor_a=output_model["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"] | ||
tensor_b=model_piece["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"] | ||
|
||
output_model["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"]=torch.cat((tensor_a,tensor_b),dim=-1) | ||
|
||
tensor_a=output_model["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"] | ||
tensor_b=model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"] | ||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"]=torch.cat((tensor_a,tensor_b),dim=-2) | ||
|
||
tensor_a=output_model["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"] | ||
tensor_b=model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"] | ||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"]=torch.cat((tensor_a,tensor_b),dim=-2) | ||
|
||
tensor_a=output_model["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"] | ||
tensor_b=model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"] | ||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"]=torch.cat((tensor_a,tensor_b),dim=-1) | ||
|
||
tensor_a=output_model["target.lm.output_layer.weight"] | ||
tensor_b=model_piece["target.lm.output_layer.weight"] | ||
output_model["target.lm.output_layer.weight"]=torch.cat((tensor_a,tensor_b),dim=-2) | ||
|
||
torch.save(output_model,os.path.join(args.output_model_path,'merge_model.bin')) | ||
|
61 changes: 61 additions & 0 deletions
61
scripts/convert_llama_from_pytorch_checkpoint_to_megatron_checkpoint.py
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,61 @@ | ||
import argparse | ||
import collections | ||
import torch | ||
import os | ||
|
||
|
||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument("--input_model_path", type=str, default="models/input_model.bin", | ||
help=".") | ||
parser.add_argument("--output_model_path", type=str, default="models/output_model", | ||
help=".") | ||
parser.add_argument("--layers_num", type=int, default=32) | ||
parser.add_argument("--tensor_model_parallel_size", type=int, default=4) | ||
parser.add_argument("--hidden_size", type=int, default=4096) | ||
parser.add_argument("--feedforward_size", type=int, default=11008) | ||
|
||
args = parser.parse_args() | ||
|
||
input_model = torch.load(args.input_model_path) | ||
|
||
if not os.path.exists(args.output_model_path): | ||
os.mkdir(args.output_model_path) | ||
|
||
seg_feed_size=args.feedforward_size // args.tensor_model_parallel_size | ||
seg_hidden_size = args.hidden_size // args.tensor_model_parallel_size | ||
seg_word_size=input_model["embedding.word.embedding.weight"].size()[0] // args.tensor_model_parallel_size | ||
|
||
for n in range(args.tensor_model_parallel_size): | ||
model_piece=collections.OrderedDict() | ||
seg_dim=input_model["embedding.word.embedding.weight"].size()[0]//args.tensor_model_parallel_size | ||
model_piece["embedding.word.embedding.weight"] = input_model["embedding.word.embedding.weight"][n* seg_dim :(n+1) * seg_dim,:] | ||
|
||
for i in range(args.layers_num): | ||
for j in range(3): | ||
model_piece["encoder.transformer." + str(i) + ".self_attn.linear_layers."+str(j)+".weight"] = input_model["encoder.transformer." + str(i) + ".self_attn.linear_layers."+str(j)+".weight"][n * seg_hidden_size:(n+1) * seg_hidden_size,:] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"] = \ | ||
input_model["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"][:,n * seg_hidden_size:(n+1) * seg_hidden_size] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".layer_norm_1.weight"] = \ | ||
input_model["encoder.transformer." + str(i) + ".layer_norm_1.weight"] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"] = \ | ||
input_model["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"][n * seg_feed_size:(n+1) * seg_feed_size,:] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"]= \ | ||
input_model["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"][n * seg_feed_size:(n+1) * seg_feed_size,:] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"] = \ | ||
input_model["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"][:,n * seg_feed_size:(n+1) * seg_feed_size] | ||
|
||
model_piece["encoder.transformer." + str(i) + ".layer_norm_2.weight"] = \ | ||
input_model["encoder.transformer." + str(i) + ".layer_norm_2.weight"] | ||
|
||
model_piece["encoder.layer_norm.weight"] = input_model["encoder.layer_norm.weight"] | ||
|
||
model_piece["target.lm.output_layer.weight"]= input_model["target.lm.output_layer.weight"][n * seg_word_size:(n+1) * seg_word_size,:] | ||
|
||
name=str(n) if len(str(n))==2 else '0'+str(n) | ||
torch.save(model_piece, os.path.join(args.output_model_path,"mp_rank_"+str(name)+"_model_states.pt")) | ||
|
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.