MCPcopy Create free account
hub / github.com/IceClear/CLIP-IQA / DistributedDataParallelWrapper

Class DistributedDataParallelWrapper

mmedit/core/distributed_wrapper.py:10–139  ·  view source on GitHub ↗

A DistributedDataParallel wrapper for models in MMediting. In MMedting, there is a need to wrap different modules in the models with separate DistributedDataParallel. Otherwise, it will cause errors for GAN training. More specific, the GAN model, usually has two sub-modules: gen

Source from the content-addressed store, hash-verified

8
9@MODULE_WRAPPERS.register_module()
10class DistributedDataParallelWrapper(nn.Module):
11 """A DistributedDataParallel wrapper for models in MMediting.
12
13 In MMedting, there is a need to wrap different modules in the models
14 with separate DistributedDataParallel. Otherwise, it will cause
15 errors for GAN training.
16 More specific, the GAN model, usually has two sub-modules:
17 generator and discriminator. If we wrap both of them in one
18 standard DistributedDataParallel, it will cause errors during training,
19 because when we update the parameters of the generator (or discriminator),
20 the parameters of the discriminator (or generator) is not updated, which is
21 not allowed for DistributedDataParallel.
22 So we design this wrapper to separately wrap DistributedDataParallel
23 for generator and discriminator.
24
25 In this wrapper, we perform two operations:
26 1. Wrap the modules in the models with separate MMDistributedDataParallel.
27 Note that only modules with parameters will be wrapped.
28 2. Do scatter operation for 'forward', 'train_step' and 'val_step'.
29
30 Note that the arguments of this wrapper is the same as those in
31 `torch.nn.parallel.distributed.DistributedDataParallel`.
32
33 Args:
34 module (nn.Module): Module that needs to be wrapped.
35 device_ids (list[int | `torch.device`]): Same as that in
36 `torch.nn.parallel.distributed.DistributedDataParallel`.
37 dim (int, optional): Same as that in the official scatter function in
38 pytorch. Defaults to 0.
39 broadcast_buffers (bool): Same as that in
40 `torch.nn.parallel.distributed.DistributedDataParallel`.
41 Defaults to False.
42 find_unused_parameters (bool, optional): Same as that in
43 `torch.nn.parallel.distributed.DistributedDataParallel`.
44 Traverse the autograd graph of all tensors contained in returned
45 value of the wrapped module’s forward function. Defaults to False.
46 kwargs (dict): Other arguments used in
47 `torch.nn.parallel.distributed.DistributedDataParallel`.
48 """
49
50 def __init__(self,
51 module,
52 device_ids,
53 dim=0,
54 broadcast_buffers=False,
55 find_unused_parameters=False,
56 **kwargs):
57 super().__init__()
58 assert len(device_ids) == 1, (
59 'Currently, DistributedDataParallelWrapper only supports one'
60 'single CUDA device for each process.'
61 f'The length of device_ids must be 1, but got {len(device_ids)}.')
62 self.module = module
63 self.dim = dim
64 self.to_ddp(
65 device_ids=device_ids,
66 dim=dim,
67 broadcast_buffers=broadcast_buffers,

Callers 2

mainFunction · 0.90
_dist_trainFunction · 0.90

Calls

no outgoing calls

Tested by 1

mainFunction · 0.72