MIR.utils.distributedΒΆ
Multi-GPU helpers for training and inference with DistributedDataParallel or DataParallel.
Launch with torchrun --nproc_per_node=N train.py and call init_distributed() once at start-up. The helpers
also work unchanged in a single process (one GPU or CPU), so the same script runs everywhere.
Checkpoints: wrapped models prefix their state_dict keys with module.. Save with state_dict_for_saving()
(or unwrap_model(model).state_dict()) so the file loads into a plain model with strict=True, exactly like the
published MIR weights; strip_module_prefix() fixes checkpoints that were saved from a wrapped model.
Example:
from MIR.utils import distributed as dist_utils
device = dist_utils.init_distributed() # torchrun env vars, or a single process
model = dist_utils.wrap_model(model.to(device), device=device)
sampler = dist_utils.distributed_sampler(train_set) # None in a single process
loader = DataLoader(train_set, batch_size=1, sampler=sampler, shuffle=sampler is None)
for epoch in range(n_epochs):
if sampler is not None:
sampler.set_epoch(epoch)
...
loss_value = dist_utils.all_reduce_mean(loss.detach())
if dist_utils.is_main_process():
torch.save({"state_dict": dist_utils.state_dict_for_saving(model)}, path)
Functions
|
Average a scalar or tensor over all processes (returned unchanged when not distributed). |
|
Synchronise all processes (no-op when not distributed). |
|
Destroy the process group (no-op when not distributed). |
|
|
|
Rank of this process (0 when not distributed). |
|
Number of processes (1 when not distributed). |
|
Initialise the default process group from the |
|
True when a |
|
True on rank 0 (and in a single process); use it to guard logging and checkpoint saving. |
|
|
|
Remove a leading |
|
Return the underlying module of a |
|
Wrap a model for multi-GPU training or inference. |