Multi-gpu improvements (#799)

* initial commit

* minor changes

* small changes

* ddp seems to be working

* remove commented out code
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-10-20 01:11:40 -07:00
1 parent 863fc77ab5
commit 6f4fb506e2
8 files changed
+93 -107

No files matched your search

-14
View File
@@ -294,19 +294,12 @@ class VanillaDataManager(DataManager): # pylint: disable=abstract-method
def setup_train(self):
"""Sets up the data loaders for training"""
assert self.train_dataset is not None
if self.world_size > 1:
sampler = DistributedSampler(
self.train_dataset, num_replicas=self.world_size, rank=self.local_rank, shuffle=True, seed=42
)
else:
sampler = None
self.train_image_dataloader = CacheDataloader(
self.train_dataset,
num_images_to_sample_from=self.config.train_num_images_to_sample_from,
device=self.device,
num_workers=self.world_size * 4,
pin_memory=True,
sampler=sampler,
)
self.iter_train_image_dataloader = iter(self.train_image_dataloader)
self.train_pixel_sampler = PixelSampler(self.config.train_num_rays_per_batch)
@@ -321,19 +314,12 @@ class VanillaDataManager(DataManager): # pylint: disable=abstract-method
def setup_eval(self):
"""Sets up the data loader for evaluation"""
assert self.eval_dataset is not None
if self.world_size > 1:
sampler = DistributedSampler(
self.eval_dataset, num_replicas=self.world_size, rank=self.local_rank, shuffle=True, seed=42
)
else:
sampler = None
self.eval_image_dataloader = CacheDataloader(
self.eval_dataset,
num_images_to_sample_from=self.config.eval_num_images_to_sample_from,
device=self.device,
num_workers=self.world_size * 4,
pin_memory=True,
sampler=sampler,
)
self.iter_eval_image_dataloader = iter(self.eval_image_dataloader)
self.eval_pixel_sampler = PixelSampler(self.config.eval_num_rays_per_batch)
+2 -16
View File
@@ -21,7 +21,7 @@ import dataclasses
import functools
import os
import time
from typing import Any, Dict, List, Tuple
from typing import Dict, List, Tuple
import torch
from rich.console import Console
@@ -48,20 +48,6 @@ from nerfstudio.viewer.server import viewer_utils
CONSOLE = Console(width=120)
def train_loop(local_rank: int, world_size: int, config: cfg.Config) -> Any:
"""Main training function that sets up and runs the trainer per process
Args:
local_rank: current rank of process
world_size: total number of gpus available
config: config file specifying training regimen
"""
trainer = Trainer(config, local_rank, world_size)
trainer.setup()
trainer.train()
return 0
class Trainer:
"""Trainer class
@@ -122,7 +108,7 @@ class Trainer:
Args:
test_mode: Whether to setup for testing. Defaults to False.
"""
self.pipeline: VanillaPipeline = self.config.pipeline.setup(
self.pipeline = self.config.pipeline.setup(
device=self.device, test_mode=test_mode, world_size=self.world_size, local_rank=self.local_rank
)
self.optimizers = setup_optimizers(self.config, self.pipeline.get_param_groups())
+1 -3
View File
@@ -227,9 +227,7 @@ class VanillaPipeline(Pipeline):
self.world_size = world_size
if world_size > 1:
self._model = typing.cast(
Model, typing.cast(Model, DDP(self._model, device_ids=[local_rank], find_unused_parameters=True))
)
self._model = typing.cast(Model, DDP(self._model, device_ids=[local_rank], find_unused_parameters=True))
dist.barrier(device_ids=[local_rank])
@property
+16 -25
View File
@@ -13,40 +13,38 @@
# limitations under the License.
"""functionality to handle multiprocessing syncing and communicating"""
import torch
import torch.distributed as dist
_LOCAL_PROCESS_GROUP = None
LOCAL_PROCESS_GROUP = None
def is_dist_avail_and_initialized() -> bool:
"""Returns True if distributed is available and initialized."""
return dist.is_available() and dist.is_initialized()
def get_world_size() -> int:
"""Get total number of available gpus"""
if not dist.is_available():
return 1
if not dist.is_initialized():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank() -> int:
"""Get global rank of current thread"""
if not dist.is_available():
return 0
if not dist.is_initialized():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def get_local_rank() -> int:
"""The rank of the current process within the local (per-machine) process group."""
if not dist.is_available():
return 0
if not dist.is_initialized():
if not is_dist_avail_and_initialized():
return 0
assert (
_LOCAL_PROCESS_GROUP is not None
LOCAL_PROCESS_GROUP is not None
), "Local process group is not created! Please use launch() to spawn processes!"
return dist.get_rank(group=_LOCAL_PROCESS_GROUP)
return dist.get_rank(group=LOCAL_PROCESS_GROUP)
def get_local_size() -> int:
@@ -54,11 +52,9 @@ def get_local_size() -> int:
The size of the per-machine process group,
i.e. the number of processes per machine.
"""
if not dist.is_available():
if not is_dist_avail_and_initialized():
return 1
if not dist.is_initialized():
return 1
return dist.get_world_size(group=_LOCAL_PROCESS_GROUP)
return dist.get_world_size(group=LOCAL_PROCESS_GROUP)
def is_main_process() -> bool:
@@ -66,21 +62,16 @@ def is_main_process() -> bool:
return get_rank() == 0
def synchronize(world_size):
def synchronize():
"""
Helper function to synchronize (barrier) among all processes when
using distributed training
"""
if not dist.is_available():
return
if not dist.is_initialized():
return
world_size = dist.get_world_size()
if world_size == 1:
if dist.get_world_size() == 1:
return
if dist.get_backend() == dist.Backend.NCCL:
# This argument is needed to avoid warnings.
# It's valid only for NCCL backend.
dist.barrier(device_ids=[torch.cuda.current_device()])
dist.barrier(device_ids=[get_local_rank()])
else:
dist.barrier()
-1
View File
@@ -166,7 +166,6 @@ def write_out_storage():
EVENT_STORAGE.clear()
@check_main_thread
def setup_local_writer(config: cfg.LoggingConfig, max_iter: int, banner_messages: Optional[List[str]] = None) -> None:
"""Initialization of all event writers specified in config