2019-03-01 23:51:45 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2018, Salesforce, Inc.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
# list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# * Neither the name of the copyright holder nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2018-06-20 06:22:34 +00:00
|
|
|
import torch
|
|
|
|
from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors
|
|
|
|
import torch.distributed as dist
|
|
|
|
from torch.nn.modules import Module
|
2019-03-02 01:35:04 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-06-20 06:22:34 +00:00
|
|
|
|
|
|
|
class DistributedDataParallel(Module):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
super(DistributedDataParallel, self).__init__()
|
|
|
|
self.warn_on_half = True#$ True if dist._backend == dist.dist_backend.GLOO else False
|
|
|
|
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
for p in self.module.state_dict().values():
|
|
|
|
if torch.is_tensor(p):
|
|
|
|
dist.broadcast(p, 0)
|
|
|
|
|
|
|
|
def allreduce_params():
|
|
|
|
if(self.needs_reduction):
|
|
|
|
self.needs_reduction = False
|
|
|
|
buckets = {}
|
|
|
|
for param in self.module.parameters():
|
|
|
|
if param.requires_grad and param.grad is not None:
|
|
|
|
tp = type(param.data)
|
|
|
|
if tp not in buckets:
|
|
|
|
buckets[tp] = []
|
|
|
|
buckets[tp].append(param)
|
|
|
|
if self.warn_on_half:
|
|
|
|
if torch.cuda.HalfTensor in buckets:
|
2019-03-02 01:35:04 +00:00
|
|
|
logger.warning("gloo dist backend for half parameters may be extremely slow." +
|
|
|
|
" It is recommended to use the NCCL backend in this case.")
|
2018-06-20 06:22:34 +00:00
|
|
|
self.warn_on_half = False
|
|
|
|
|
|
|
|
for tp in buckets:
|
|
|
|
bucket = buckets[tp]
|
|
|
|
grads = [param.grad.data for param in bucket]
|
|
|
|
coalesced = _flatten_dense_tensors(grads)
|
|
|
|
dist.all_reduce(coalesced)
|
|
|
|
coalesced /= dist.get_world_size()
|
|
|
|
for buf, synced in zip(grads, _unflatten_dense_tensors(coalesced, grads)):
|
|
|
|
buf.copy_(synced)
|
|
|
|
|
|
|
|
for param in list(self.module.parameters()):
|
|
|
|
if param.requires_grad:
|
|
|
|
def allreduce_hook(*unused):
|
|
|
|
param._execution_engine.queue_callback(allreduce_params)
|
|
|
|
param.register_hook(allreduce_hook)
|
|
|
|
|
|
|
|
def forward(self, *inputs, **kwargs):
|
|
|
|
self.needs_reduction = True
|
|
|
|
return self.module(*inputs, **kwargs)
|