Spaces:
Runtime error
Runtime error
File size: 10,179 Bytes
b73936d |
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import os
import random
from datetime import timedelta
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.nn as nn
from torch.distributed import checkpoint as dist_checkpoint
from torch.distributed import fsdp
import functools
import itertools
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import Dataset
from typing import Any, Dict, Optional
from surya.utils.schemas import TrainState
def init_dist(device: str, rank: int, world_size: int):
torch.distributed.init_process_group(
device,
init_method="env://",
world_size=world_size,
rank=rank,
timeout=timedelta(minutes=60),
)
def init_ddp(use_gpu: bool):
local_rank = int(os.environ["LOCAL_RANK"])
rank = int(os.environ["RANK"])
world_size = int(os.environ["WORLD_SIZE"])
if use_gpu:
assert (
torch.cuda.is_available()
), "GPU requested but none was found in the system."
if use_gpu:
init_dist("nccl", rank, world_size)
torch.cuda.set_device(local_rank)
os.environ["TORCH_SHOW_CPP_STACKTRACES"] = str(1)
os.environ["TORCH_NCCL_ASYNC_ERROR_HANDLING"] = str(1)
os.environ["TORCH_DISTRIBUTED_DEBUG"] = "DETAIL"
cudnn.benchmark = True
else:
init_dist("gloo", rank, world_size)
return local_rank, rank
def set_global_seed(rank):
random.seed(42 + rank)
torch.cuda.manual_seed(42 + rank)
torch.manual_seed(42 + rank)
np.random.seed(42 + rank)
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
# def save_model_singular(model, *args, **kwargs):
# """Stream all model parameters to rank 0 on the CPU, then pass all
# other given arguments to `torch.save` to save the model, but only on
# the root process.
# """
# save_policy = fsdp.FullStateDictConfig(
# offload_to_cpu=True, rank0_only=True)
# with fsdp.FullyShardedDataParallel.state_dict_type(
# model,
# fsdp.StateDictType.FULL_STATE_DICT,
# save_policy,
# ):
# cpu_state = model.state_dict()
# # We do *not* want to write to the same location with multiple
# # processes at the same time.
# if is_root_process():
# torch.save(cpu_state, *args, **kwargs)
def save_model(model, save_dir):
"""Obtain sharded model parameters from the GPU, then save the model
as a distributed checkpoint to the given directory. Saving a
distributed checkpoint means that the checkpoint will be split into
individual files, one for each process.
"""
state_dict_config = fsdp.ShardedStateDictConfig(offload_to_cpu=False)
with fsdp.FullyShardedDataParallel.state_dict_type(
model,
fsdp.StateDictType.SHARDED_STATE_DICT,
state_dict_config,
):
cp_state_dict = {"model": model.state_dict()}
dist_checkpoint.save_state_dict(
cp_state_dict,
dist_checkpoint.FileSystemWriter(save_dir),
)
def load_model(model, load_dir):
"""Set the given model's state dictionary in-place from the given
distributed checkpoint directory.
"""
state_dict_config = fsdp.ShardedStateDictConfig(offload_to_cpu=False)
with fsdp.FullyShardedDataParallel.state_dict_type(
model,
fsdp.StateDictType.SHARDED_STATE_DICT,
state_dict_config,
):
cp_state_dict = {"model": model.state_dict()}
dist_checkpoint.load_state_dict(
cp_state_dict,
dist_checkpoint.FileSystemReader(load_dir),
)
model.load_state_dict(cp_state_dict["model"])
@functools.lru_cache(maxsize=None)
def is_root_process():
"""Return whether this process is the root process."""
return torch.distributed.get_rank() == 0
# The reason we define this is that `torch.distributed` does not
# implement it; for the global rank, there's
# `torch.distributed.get_rank()`.
@functools.lru_cache(maxsize=None)
def get_local_rank():
"""Return the local rank of this process."""
return int(os.getenv("LOCAL_RANK"))
def print0(*args, **kwargs):
"""Print something only on the root process."""
if (not dist.is_initialized()) or is_root_process():
print(*args, **kwargs)
def save_model_singular(model, save_path, parallelism, *args, **kwargs):
"""Stream all model parameters to rank 0 on the CPU, then pass all
other given arguments to `torch.save` to save the model, but only on
the root process.
"""
match parallelism:
case "fsdp":
save_policy = fsdp.FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
with fsdp.FullyShardedDataParallel.state_dict_type(
model,
fsdp.StateDictType.FULL_STATE_DICT,
save_policy,
):
cpu_state = model.state_dict()
# We do *not* want to write to the same location with multiple
# processes at the same time.
if is_main_process():
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path), exist_ok=True)
torch.save(obj=cpu_state, f=save_path, *args, **kwargs)
case "ddp":
if is_main_process():
torch.save(obj=model.module.state_dict(), f=save_path, *args, **kwargs)
dist.barrier()
case _:
raise ValueError(
f'`parallelism` should be one of "ddp" and "fsdp". Got {parallelism}.'
)
def save_optim_singular(
model: nn.Module,
optimizer: torch.optim.Optimizer,
save_path: str,
parallelism: str = "fsdp",
):
match parallelism:
case "fsdp":
optim_state_dict_config = fsdp.FullOptimStateDictConfig(
offload_to_cpu=True, rank0_only=True
)
with fsdp.FullyShardedDataParallel.state_dict_type(
model,
fsdp.StateDictType.FULL_STATE_DICT,
optim_state_dict_config=optim_state_dict_config,
):
optim_state_dict = fsdp.FullyShardedDataParallel.optim_state_dict(
model, optimizer
)
if is_main_process():
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path), exist_ok=True)
checkpoint = {
"optimizer_state_dict": optim_state_dict,
}
torch.save(checkpoint, f=save_path)
case "ddp":
if is_main_process():
optim_state_dict = optimizer.state_dict()
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path), exist_ok=True)
torch.save(obj=optim_state_dict, f=save_path)
dist.barrier()
case _:
raise ValueError(
f'`parallelism` should be one of "ddp" and "fsdp". Got {parallelism}.'
)
def collect_optim_singular(
model: nn.Module, optimizer: torch.optim.Optimizer, parallelism: str = "fsdp"
) -> dict:
optim_state_dict = {}
match parallelism:
case "fsdp":
optim_state_dict_config = fsdp.FullOptimStateDictConfig(
offload_to_cpu=True, rank0_only=True
)
with fsdp.FullyShardedDataParallel.state_dict_type(
model,
fsdp.StateDictType.FULL_STATE_DICT,
optim_state_dict_config=optim_state_dict_config,
):
optim_state_dict = fsdp.FullyShardedDataParallel.optim_state_dict(
model, optimizer
)
case "ddp":
if is_main_process():
optim_state_dict = optimizer.state_dict()
dist.barrier()
case _:
raise ValueError(
f'`parallelism` should be one of "ddp" and "fsdp". Got {parallelism}.'
)
return optim_state_dict
def save_state_singular(states: TrainState, save_path, *args, **kwargs):
"""Stream all model parameters to rank 0 on the CPU, then pass all
other given arguments to `torch.save` to save paramters, but only on
the root process.
"""
if is_main_process():
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path), exist_ok=True)
torch.save(obj=states, f=save_path, *args, **kwargs)
dist.barrier()
class StatefulDistributedSampler(DistributedSampler):
_YIELDED = "yielded"
def __init__(
self,
dataset: Dataset,
num_replicas: Optional[int] = None,
rank: Optional[int] = None,
shuffle: bool = True,
seed: int = 0,
drop_last: bool = False,
) -> None:
super().__init__(dataset, num_replicas, rank, shuffle, seed, drop_last)
self.yielded = 0
self.next_yielded = None
def __iter__(self):
self.yielded = 0
if self.next_yielded is not None:
self.yielded = self.next_yielded
self.next_yielded = None
it = super().__iter__()
for idx in itertools.islice(it, self.yielded, None):
self.yielded += 1
yield idx
def state_dict(self) -> Dict[str, Any]:
return {self._YIELDED: self.yielded}
def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
if self._YIELDED not in state_dict:
raise ValueError("Invalid state_dict")
if state_dict[self._YIELDED] < 0:
raise ValueError("Cannot load state_dict with negative yielded value")
self.next_yielded = state_dict[self._YIELDED]
|