-
Notifications
You must be signed in to change notification settings - Fork 211
/
change_mp.py
142 lines (131 loc) · 5.61 KB
/
change_mp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
import os
import torch
import copy
checkpoint = sys.argv[1]
target_mp = int(sys.argv[2])
assert os.path.isdir(checkpoint)
with open(os.path.join(checkpoint, 'latest_checkpointed_iteration.txt')) as fin:
iteration = int(fin.read().strip())
checkpoint = os.path.join(checkpoint, str(iteration))
filenames = os.listdir(checkpoint)
filenames = sorted(filenames,
key=lambda x: int(x.split('_')[2]))
filenames = [os.path.join(checkpoint, x) for x in filenames]
if target_mp == len(filenames):
print("MP size keeps the same.")
exit(0)
if sys.argv[1][-1] == '/':
new_checkpoint = sys.argv[1][:-1] + '_MP' + sys.argv[2]
else:
new_checkpoint = sys.argv[1] + '_MP' + sys.argv[2]
if not os.path.exists(new_checkpoint):
os.mkdir(new_checkpoint)
with open(os.path.join(new_checkpoint, 'latest_checkpointed_iteration.txt'), 'w') as fout:
fout.write("{}\n".format(iteration))
new_checkpoint = os.path.join(new_checkpoint, str(iteration))
if not os.path.exists(new_checkpoint):
os.mkdir(new_checkpoint)
preserve_keys = [
"lr_scheduler",
"skipped_steps",
"global_steps",
"global_samples",
"dp_world_size",
"iteration",
"np_rng_state",
"random_rng_state",
"torch_rng_state",
"cuda_rng_state",
"rng_tracker_states",
]
if target_mp < len(filenames):
print("Decrease MP size.")
assert len(filenames) % target_mp == 0
ratio = len(filenames) // target_mp
for i in range(target_mp):
start = ratio * i
end = ratio * (i+1)
d = torch.load(filenames[start],
map_location='cpu')
for k in d.keys():
if k !='module':
if k in preserve_keys:
pass
elif k == "mp_world_size":
d[k] = target_mp
else:
d[k] = None
for j in range(start+1, end):
d_new = torch.load(filenames[j],
map_location='cpu')
for k, v in d_new['module'].items():
assert len(v.shape) < 3
if len(v.shape) == 2 and 'position' not in k:
if 'query' in k:
size_1 = d['module'][k].shape[0] // 3
size_2 = v.shape[0] // 3
target = d['module'][k]
d['module'][k] = torch.cat([
target[:size_1, :], v[:size_2, :],
target[size_1:size_1*2, :], v[size_2:size_2*2, :],
target[size_1*2:, :], v[size_2*2:, :]], 0)
elif 'word' in k or 'h_to_4h' in k:
d['module'][k] = torch.cat([d['module'][k], v], 0)
else:
d['module'][k] = torch.cat([d['module'][k], v], 1)
if len(v.shape) == 1 and 'query_key_value' in k:
size_1 = d['module'][k].shape[0] // 3
size_2 = v.shape[0] // 3
target = d['module'][k]
d['module'][k] = torch.cat([
target[:size_1], v[:size_2],
target[size_1:size_1*2], v[size_2:size_2*2],
target[size_1*2:], v[size_2*2:]], 0)
if len(v.shape) == 1 and 'dense_h_to_4h' in k:
d['module'][k] = torch.cat([d['module'][k], v], 0)
filename = os.path.join(new_checkpoint, "mp_rank_{:02d}_model_states.pt".format(i))
torch.save(d, filename)
if target_mp > len(filenames):
print("Increase MP size.")
assert target_mp % len(filenames) == 0
ratio = target_mp // len(filenames)
for i in range(len(filenames)):
start = ratio * i
end = ratio * (i+1)
d = torch.load(filenames[i],
map_location='cpu')
for j in range(start, end):
d_new = {}
shift = j - start
for k, v in d.items():
if k != 'module':
if k in preserve_keys:
d_new[k] = copy.deepcopy(d[k])
elif k == "mp_world_size":
d_new[k] = target_mp
else:
d_new[k] = None
d_new['module'] = {}
for k, v in d['module'].items():
assert len(v.shape) < 3
if len(v.shape) == 2 and 'position' not in k:
if 'query' in k:
part = v.shape[0] // ratio // 3
d_new['module'][k] = torch.cat([v[shift*part:(shift+1)*part, :], v[(shift+ratio)*part:(shift+1+ratio)*part, :], v[(shift+2*ratio)*part:(shift+1+2*ratio)*part, :]], 0)
elif 'word' in k or 'h_to_4h' in k:
part = v.shape[0] // ratio
d_new['module'][k] = v[shift*part:(shift+1)*part, :]
else:
part = v.shape[1] // ratio
d_new['module'][k] = v[:, shift*part:(shift+1)*part]
elif len(v.shape) == 1 and 'dense_h_to_4h' in k:
part = v.shape[0] // ratio
d_new['module'][k] = v[shift*part:(shift+1)*part]
elif len(v.shape) == 1 and 'query_key_value' in k:
part = v.shape[0] // ratio // 3
d_new['module'][k] = torch.cat([v[shift*part:(shift+1)*part], v[(shift+ratio)*part:(shift+1+ratio)*part], v[(shift+2*ratio)*part:(shift+1+2*ratio)*part]], 0)
else:
d_new['module'][k] = v
filename = os.path.join(new_checkpoint, "mp_rank_{:02d}_model_states.pt".format(j))
torch.save(d_new, filename)