-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.py
225 lines (185 loc) · 7.3 KB
/
utils.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import imageio
import numpy as np
from typing import List
from io import BytesIO
from PIL import Image
import subprocess
from time import sleep
import os
import torch
import torch.distributed as dist
from torch.distributed.fsdp import (
FullyShardedDataParallel as FSDP,
StateDictType, FullStateDictConfig,
)
from torch.distributed.checkpoint.state_dict import (
StateDictOptions,
get_model_state_dict,
get_optimizer_state_dict,
set_optimizer_state_dict
)
_INTRA_NODE_PROCESS_GROUP, _INTER_NODE_PROCESS_GROUP = None, None
_LOCAL_RANK, _LOCAL_WORLD_SIZE = -1, -1
def images_to_gif_bytes(images: List, duration: int = 1000) -> bytes:
with BytesIO() as output_buffer:
# Save the first image
images[0].save(output_buffer,
format='GIF',
save_all=True,
append_images=images[1:],
duration=duration,
loop=0) # 0 means the GIF will loop indefinitely
# Get the byte array from the buffer
gif_bytes = output_buffer.getvalue()
return gif_bytes
def save_as_gif(images: List, file_path: str, duration: int = 1000):
with open(file_path, "wb") as f:
f.write(images_to_gif_bytes(images, duration))
def images_to_mp4_bytes(images: List[Image.Image], duration: float = 1000) -> bytes:
with BytesIO() as output_buffer:
with imageio.get_writer(output_buffer, format='mp4', fps=1 / (duration / 1000)) as writer:
for img in images:
writer.append_data(np.array(img))
mp4_bytes = output_buffer.getvalue()
return mp4_bytes
def save_as_mp4(images: List[Image.Image], file_path: str, duration: float = 1000):
with open(file_path, "wb") as f:
f.write(images_to_mp4_bytes(images, duration))
def get_local_rank() -> int:
return _LOCAL_RANK
def get_local_world_size() -> int:
return _LOCAL_WORLD_SIZE
def _setup_dist_env_from_slurm(args):
while not os.environ.get("MASTER_ADDR", ""):
try:
os.environ["MASTER_ADDR"] = subprocess.check_output(
"sinfo -Nh -n %s | head -n 1 | awk '{print $1}'" %
os.environ['SLURM_NODELIST'],
shell=True,
).decode().strip()
except:
pass
sleep(1)
os.environ["MASTER_PORT"] = str(int(args.master_port)+1)
os.environ["RANK"] = os.environ["SLURM_PROCID"]
os.environ["WORLD_SIZE"] = os.environ["SLURM_NPROCS"]
os.environ["LOCAL_RANK"] = os.environ["SLURM_LOCALID"]
os.environ["LOCAL_WORLD_SIZE"] = os.environ["SLURM_NTASKS_PER_NODE"]
def init_process_groups(args):
if any([
x not in os.environ
for x in ["RANK", "WORLD_SIZE", "MASTER_PORT", "MASTER_ADDR"]
]):
_setup_dist_env_from_slurm(args)
dist.init_process_group("nccl")
torch.cuda.set_device(dist.get_rank() % torch.cuda.device_count())
global _LOCAL_RANK, _LOCAL_WORLD_SIZE
_LOCAL_RANK = int(os.environ["LOCAL_RANK"])
_LOCAL_WORLD_SIZE = int(os.environ["LOCAL_WORLD_SIZE"])
global _INTRA_NODE_PROCESS_GROUP, _INTER_NODE_PROCESS_GROUP
local_ranks, local_world_sizes = [torch.empty(
[dist.get_world_size()], dtype=torch.long, device="cuda"
) for _ in (0, 1)]
dist.all_gather_into_tensor(local_ranks, torch.tensor(get_local_rank(), device="cuda"))
dist.all_gather_into_tensor(local_world_sizes, torch.tensor(get_local_world_size(), device="cuda"))
local_ranks, local_world_sizes = local_ranks.tolist(), local_world_sizes.tolist()
node_ranks = [[0]]
for i in range(1, dist.get_world_size()):
if len(node_ranks[-1]) == local_world_sizes[i - 1]:
node_ranks.append([])
else:
assert local_world_sizes[i] == local_world_sizes[i - 1]
node_ranks[-1].append(i)
for ranks in node_ranks:
group = dist.new_group(ranks)
if dist.get_rank() in ranks:
assert _INTRA_NODE_PROCESS_GROUP is None
_INTRA_NODE_PROCESS_GROUP = group
assert _INTRA_NODE_PROCESS_GROUP is not None
if min(local_world_sizes) == max(local_world_sizes):
for i in range(get_local_world_size()):
group = dist.new_group(list(range(i, dist.get_world_size(), get_local_world_size())))
if i == get_local_rank():
assert _INTER_NODE_PROCESS_GROUP is None
_INTER_NODE_PROCESS_GROUP = group
assert _INTER_NODE_PROCESS_GROUP is not None
def get_intra_node_process_group():
assert _INTRA_NODE_PROCESS_GROUP is not None, \
"Intra-node process group is not initialized."
return _INTRA_NODE_PROCESS_GROUP
def get_inter_node_process_group():
assert _INTRA_NODE_PROCESS_GROUP is not None, \
"Intra- and inter-node process groups are not initialized."
return _INTER_NODE_PROCESS_GROUP
def save_model_fsdp_only(rank, model, output_folder, filename):
with FSDP.state_dict_type(
model,
StateDictType.FULL_STATE_DICT,
FullStateDictConfig(rank0_only=True, offload_to_cpu=True),
):
consolidated_model_state_dict = model.state_dict()
if rank == 0:
torch.save(
consolidated_model_state_dict,
os.path.join(output_folder, filename),
)
del consolidated_model_state_dict
dist.barrier()
def save_model(rank, model, output_folder, filename):
state_dict = get_model_state_dict(
model,
options=StateDictOptions(
full_state_dict=True,
cpu_offload=True,
),
)
if rank == 0:
torch.save(state_dict, os.path.join(output_folder, filename))
del state_dict
dist.barrier()
def load_model(rank, model, output_folder, filename, strict=True, logger=None):
if rank == 0:
missing_keys, unexpected_keys = model.load_state_dict(
torch.load(os.path.join(output_folder, filename), map_location="cpu"),
strict=strict
)
if logger is not None:
logger.info("Model initialization result:")
logger.info(f" Missing keys: {missing_keys}")
logger.info(f" Unexpected keys: {unexpected_keys}")
dist.barrier()
def save_optimizer_fsdp_only(model, optimizer, output_folder, filename):
with FSDP.state_dict_type(
model,
StateDictType.LOCAL_STATE_DICT,
):
torch.save(optimizer.state_dict(), os.path.join(output_folder, filename))
dist.barrier()
def load_optimizer_fsdp_only(optimizer, output_folder, filename):
optimizer.load_state_dict(
torch.load(os.path.join(output_folder, filename), map_location="cpu")
)
dist.barrier()
def save_optimizer(model, optimizer, output_folder, filename):
state_dict = get_optimizer_state_dict(
model,
optimizer,
options=StateDictOptions(
full_state_dict=False,
cpu_offload=True,
),
)
torch.save(state_dict, os.path.join(output_folder, filename))
dist.barrier()
def load_optimizer(model, optimizer, output_folder, filename):
state_dict = torch.load(os.path.join(output_folder, filename), map_location="cpu")
set_optimizer_state_dict(
model,
optimizer,
optim_state_dict=state_dict,
options=StateDictOptions(
full_state_dict=False,
strict=True
),
)
dist.barrier()