piiswrong commented on a change in pull request #10536: [MXNET-317] Add Data 
Parallel
URL: https://github.com/apache/incubator-mxnet/pull/10536#discussion_r189054787
 
 

 ##########
 File path: python/mxnet/gluon/contrib/parallel.py
 ##########
 @@ -0,0 +1,343 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# pylint: disable=broad-except, redefined-builtin
+"""Synchronized DataParallel"""
+import threading
+from ... import autograd
+from ...ndarray import NDArray
+from ..utils import split_and_load
+
+__all__ = ['DataParallelModel', 'DataParallelCriterion', 'Barrier']
+
+
+class Barrier(object):
+    """Shared NDArray for cross device operation.
+
+    A cross device operation that allows synchronized push and pull. It can be 
used in
+    Cross-gpu Sycnhronized Batch Normalization and Sparse Blocks.
+
+    Parameters
+    ----------
+    counter : int
+        Number of deivces.
+    operation : callable
+        The cross device operation is applying (e.g. AllReduce).
+    """
+    def __init__(self, counter, operation):
+        self.mutex = threading.Lock()
+        self.all_tasks_done = threading.Condition(self.mutex)
+        self.counter = counter
+        self.op = operation
+        self._clear()
+
+    def push(self, x):
+        """Push a NDArray from one of the device.
+        Input:
+            x (NDArray)
+
+        Output:
+            idx (int), the output index
+        """
+        with self.mutex:
+            if self.push_tasks == 0:
+                self._clear()
+            self.list.append(x)
+            idx = len(self.list) - 1
+            self.push_tasks -= 1
+
+        with self.all_tasks_done:
+            if self.push_tasks == 0:
+                self.all_tasks_done.notify_all()
+            while self.push_tasks:
+                self.all_tasks_done.wait()
+
+        self._sync_op()
+        return idx
+
+    def pull(self, idx):
+        """Pull the output to each device
+        Input:
+            idx (int)
+
+        Output:
+            out (NDArray)
+        """
+        return self.out[idx]
+
+    def _sync_op(self):
+        with self.mutex:
+            if self.reduce_tasks == 1:
+                assert(len(self.list) == self.counter)
+                self.out = self.op(*self.list)
+                if isinstance(self.out, (list, tuple)):
+                    for xi in self.out:
+                        xi.wait_to_read()
+                else:
+                    self.out.wait_to_read()
+                self.reduce_tasks -= 1
+            else:
+                self.reduce_tasks -= 1
+
+        with self.all_tasks_done:
+            if self.reduce_tasks == 0:
+                self.all_tasks_done.notify_all()
+            while self.reduce_tasks:
+                self.all_tasks_done.wait()
+
+    def _clear(self):
+        self.list = []
+        self.push_tasks = self.counter
+        self.reduce_tasks = self.counter
+
+    def __len__(self):
+        return len(self.list)
+
+    def __repr__(self):
+        return 'ParallelState'
+
+
+class DataParallelModel(object):
+    """Data parallelism
+
+    Hide the difference of single/multiple GPUs to the user.
+    Inputs and outputs are both list of NDArrays in different contexts.
+    In the forward pass, the module is replicated on each device,
+    and each replica handles a portion of the input. During the backwards
+    pass, gradients from each replica are summed into the original module.
+
+    Parameters
+    ----------
+    module : object
 
 Review comment:
   Block?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to