mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-24 00:26:08 +08:00
added basic distributed logic for trainer
This commit is contained in:
1 parent
4b131d4358
commit
7dc9d445b2
4 files changed
+71
-9
No files matched your search
@@ -1,6 +1,16 @@
|
||||
"""
|
||||
Code to train model.
|
||||
"""
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
import torch.multiprocessing as mp
|
||||
from omegaconf import DictConfig
|
||||
from torch.nn.parallel import DistributedDataParallel as DDP
|
||||
from torch.utils.data.distributed import DistributedSampler
|
||||
|
||||
from mattport.nerf.dataset.build import build_dataset
|
||||
from mattport.nerf.optimizer import Optimizer
|
||||
|
||||
@@ -44,14 +54,52 @@ class Trainer:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def train(self):
|
||||
def train(self, gpu: int) -> None:
|
||||
"""_summary_"""
|
||||
self.setup_ddp(gpu, self.config.world_size)
|
||||
|
||||
# TODO(): implement training pipeline. below I add skeleton code for compatibility with DDP
|
||||
# self.setup_graph()
|
||||
# self.graph = DDP(self.graph, device_ids=[gpu])
|
||||
# self.setup_dataset()
|
||||
# train_sampler = DistributedSampler(self.dataset, rank=gpu, num_replicas=self.config.world_size, shuffle=True)
|
||||
# TODO(): create dataloader that takes as input the train_sampler
|
||||
# self.setup_optimizer()
|
||||
|
||||
# TODO(): calls to train_epoch and train_iteration loop
|
||||
|
||||
dist.destroy_process_group()
|
||||
raise NotImplementedError
|
||||
|
||||
def train_epoch(self, gpu: int):
|
||||
"""_summary_"""
|
||||
raise NotImplementedError
|
||||
|
||||
def train_epoch(self):
|
||||
def train_iteration(self, gpu: int):
|
||||
"""_summary_"""
|
||||
# TODO(): save checkpoints, and do logging only on rank 0 device only
|
||||
# if gpu == 0:
|
||||
raise NotImplementedError
|
||||
|
||||
def train_iteration(self):
|
||||
"""_summary_"""
|
||||
raise NotImplementedError
|
||||
def setup_ddp(self, gpu: int, world_size: int) -> None:
|
||||
"""Initialization of processes for distributed training.
|
||||
|
||||
Args:
|
||||
gpu (int): process rank number
|
||||
world_size (int): total number of gpus available for training
|
||||
"""
|
||||
os.environ["MASTER_ADDR"] = "localhost"
|
||||
os.environ["MASTER_PORT"] = "12356"
|
||||
dist.init_process_group("nccl", init_method="env://", rank=gpu, world_size=world_size)
|
||||
torch.cuda.set_device(gpu)
|
||||
|
||||
torch.manual_seed(0)
|
||||
torch.backends.cudnn.deterministic = True
|
||||
torch.backends.cudnn.benchmark = False
|
||||
np.random.seed(0)
|
||||
|
||||
def run_multiprocess_train(self) -> None:
|
||||
"""function to spawn multiple processes for training."""
|
||||
if self.config.world_size < 1:
|
||||
self.config.world_size = torch.cuda.device_count()
|
||||
mp.spawn(self.train, nprocs=self.config.world_size)
|
||||
Reference in new issue
Block a user