You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2017/09/04 23:24:03 UTC

[incubator-mxnet] branch master updated: Fix for hang in depthwise convolution for CUDA 9 (#7685)

This is an automated email from the ASF dual-hosted git repository.

jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 842c096  Fix for hang in depthwise convolution for CUDA 9 (#7685)
842c096 is described below

commit 842c096284623d9a5bc195259a157d7749fcd593
Author: Przemyslaw Tredak <pt...@gmail.com>
AuthorDate: Tue Sep 5 01:24:00 2017 +0200

    Fix for hang in depthwise convolution for CUDA 9 (#7685)
    
    * Updates depthwise_convolution for CUDA9.
    
    * Fixes for depthwise convolution
    
    * Fix lint
    
    * Fixed hang in test_deconvolution.  Added test_depthwise_convolution coverage.
    
    * Adding comment
    
    * Avoid overflow when kAccumPixels is 32
---
 src/operator/depthwise_convolution-inl.h  | 20 ++++++----
 src/operator/depthwise_convolution_tf.cuh | 25 +++++++++++-
 tests/python/unittest/test_operator.py    | 65 ++++++++++++++++---------------
 3 files changed, 68 insertions(+), 42 deletions(-)

diff --git a/src/operator/depthwise_convolution-inl.h b/src/operator/depthwise_convolution-inl.h
index 5beea45..e43fd08 100644
--- a/src/operator/depthwise_convolution-inl.h
+++ b/src/operator/depthwise_convolution-inl.h
@@ -33,6 +33,8 @@
 #include <cub/cub.cuh>
 #include "./depthwise_convolution_tf.cuh"
 
+#define ROUND_TO_MULTIPLE(x, m) ((((x) + (m) - 1) / (m)) * (m))
+
 namespace mxnet {
 namespace op {
 using namespace tf::depthwise_conv;
@@ -103,7 +105,6 @@ DepthwiseConv2dBackwardFilterKernel(const DepthwiseArgs args,
   const int out_pixels = out_height * out_width;
   const int in_pixels = in_height * in_width;
   const int batch_channel_num = channel * args.batch;
-  const int candidate_reduce_thread_num = out_pixels % blockDim.x;
 
   for (int b = blockIdx.x; b < batch_channel_num; b += gridDim.x) {
     const int local_batch = b / channel;
@@ -112,14 +113,15 @@ DepthwiseConv2dBackwardFilterKernel(const DepthwiseArgs args,
     const int out_grad_offset_temp = (local_batch * channel * out_pixels) +
         (local_channel * out_pixels);
 
-    for (int out_id = threadIdx.x; out_id < out_pixels; out_id += blockDim.x) {
-      const int reduce_thread_num = ((out_pixels - out_id) > candidate_reduce_thread_num) ?
-          blockDim.x : candidate_reduce_thread_num;
-
+    // Make sure all threads enter the loop so they get to the enclosed __syncthreads()
+    for (int out_id = threadIdx.x;
+         out_id < ROUND_TO_MULTIPLE(out_pixels,
+         blockDim.x); out_id += blockDim.x) {
       const int out_w = out_id % out_width;
       const int out_h = (out_id / out_width) % out_height;
       const int out_grad_offset = out_grad_offset_temp + (out_h * out_width) + (out_w);
-      const DType out_g = ldg(out_grad + out_grad_offset);
+      // Set out_g to 0 if the thread would normally have not entered the loop.
+      const DType out_g = out_id < out_pixels ? ldg(out_grad + out_grad_offset) : DType(0);
 
       const int in_h_start = out_h * stride_height - pad_height;
       const int in_w_start = out_w * stride_width - pad_width;
@@ -134,16 +136,18 @@ DepthwiseConv2dBackwardFilterKernel(const DepthwiseArgs args,
           DType partial_grad = DType(0.0f);
           if (in_h >= 0 && in_h < in_height && in_w >= 0 && in_w < in_width) {
             const int input_offset = input_offset_temp + in_w;
-            partial_grad = ldg(input + input_offset) * out_g;
+            // Set partial_grad to 0 if the thread would normally not have entered the loop.
+            partial_grad = out_id < out_pixels ? ldg(input + input_offset) * out_g : DType(0);
           }
           // reduce all valid partial grad in a block
           typedef cub::BlockReduce<DType, mshadow::cuda::kBaseThreadNum> BlockReduceT;
           __shared__ typename BlockReduceT::TempStorage temp_storage_reduce;
-          DType aggregate = BlockReduceT(temp_storage_reduce).Sum(partial_grad, reduce_thread_num);
+          DType aggregate = BlockReduceT(temp_storage_reduce).Sum(partial_grad, blockDim.x);
           if (threadIdx.x == 0) {
             DType* addr = filter_grad + f_w + filter_offset_h + filter_offset_temp;
             atomicAdd(addr, aggregate);
           }
+          // The presense of __syncthreads() here means all threads must enter enclosing for-loops.
           __syncthreads();
         }  // for filter_width
       }  // for filter_height
diff --git a/src/operator/depthwise_convolution_tf.cuh b/src/operator/depthwise_convolution_tf.cuh
index a1538b6..0e9ee18 100644
--- a/src/operator/depthwise_convolution_tf.cuh
+++ b/src/operator/depthwise_convolution_tf.cuh
@@ -31,6 +31,18 @@
 
 namespace tf {
 namespace depthwise_conv {
+
+#define FULL_WARP_MASK 0xFFFFFFFF
+#if CUDA_VERSION < 9000
+#define __shfl_xor_sync(mask, ...) __shfl_xor(__VA_ARGS__)
+#define __shfl_down_sync(mask, ...) __shfl_down(__VA_ARGS__)
+// shuffle masks not used before CUDA 9.
+#define CREATE_SHFL_MASK(mask, predicate)
+#else
+#define CREATE_SHFL_MASK(mask, predicate) \
+    unsigned mask = __ballot_sync(FULL_WARP_MASK, (predicate))
+#endif
+
 struct DepthwiseArgs {
   // Input layer dimensions
   int batch;
@@ -464,6 +476,10 @@ __launch_bounds__(1024, 2) void DepthwiseConv2dBackwardFilterKernelSmall(
     // Note: the condition to reach this is uniform across the entire block.
     __syncthreads();
 
+    // Not all threads of a warp may reach the __shfl_down_sync instruction
+    // so we cannot use the FULL_WARP_MASK there
+    CREATE_SHFL_MASK(active_threads, slice_in_range);
+
     if (slice_in_range) {
       const DType* const out_ptr = inout_offset + output;
       const DType out1 = ldg(out_ptr);
@@ -476,7 +492,7 @@ __launch_bounds__(1024, 2) void DepthwiseConv2dBackwardFilterKernelSmall(
           DType val = out1 * tile_ptr[0] + out2 * tile_ptr[tile_offset];
           // Warp-accumulate pixels of the same depth and write to accumulator.
           for (int delta = 16 / kBlockSlices; delta > 0; delta /= 2) {
-            val += __shfl_down(val, delta);
+            val += __shfl_down_sync(active_threads, val, delta);
           }
           if (!(thread_idx & 32 / kBlockSlices - 1)) {
             *accum_ptr = val;
@@ -503,8 +519,13 @@ __launch_bounds__(1024, 2) void DepthwiseConv2dBackwardFilterKernelSmall(
       if (filter_channel < in_channel) {
         DType val = accum_data[i];
         // Warp-accumulate pixels of the same depth from the accumulator.
+        int lane_id;
+        asm volatile ("mov.u32 %0, %laneid;" : "=r"(lane_id));
+        int sub_warp = lane_id / kAccumPixels;
+        int zeros = sub_warp * kAccumPixels;
+        unsigned mask = (kAccumPixels == 32) ? FULL_WARP_MASK : (((1U << kAccumPixels) - 1) << zeros);
         for (int delta = kAccumPixels / 2; delta > 0; delta /= 2) {
-          val += __shfl_down(val, delta);
+          val += __shfl_xor_sync(mask, val, delta);
         }
         if (!(thread_idx & kAccumPixels - 1)) {
           atomicAdd(filter_offset + filter, val);
diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py
index 6d11391..41e9fbd 100644
--- a/tests/python/unittest/test_operator.py
+++ b/tests/python/unittest/test_operator.py
@@ -1001,41 +1001,42 @@ def test_convolution_grouping():
 
 
 def test_depthwise_convolution():
-    for num_base in [32, 64]:
+    for num_base in [1, 4, 16, 32, 64]:
         for kernel in [(3,3), (5,5)]:
             for stride in [(1,1), (2,2)]:
                 for pad in [(0,0), (1,1)]:
-                    num_filter = num_base
-                    num_group = num_base
-                    shape = (2, num_base, 32, 32)
-
-                    x = mx.sym.Variable('x')
-                    w = mx.sym.Variable('w')
-                    b = mx.sym.Variable('b')
-                    y1 = mx.sym.Convolution(data=x, weight=w, bias=b, num_filter=num_filter, num_group=num_group,
-                            kernel=kernel, stride=stride, pad=pad)
-                    xslice = mx.sym.SliceChannel(data=x, num_outputs=num_group, axis=1)
-                    wslice = mx.sym.SliceChannel(data=w, num_outputs=num_group, axis=0)
-                    bslice = mx.sym.SliceChannel(data=b, num_outputs=num_group, axis=0)
-                    y2 = mx.sym.Concat(*[mx.sym.Convolution(data=xslice[i], weight=wslice[i], bias=bslice[i],
-                                                            num_filter=num_filter//num_group, kernel=kernel,
-                                                            stride=stride, pad=pad)
-                                       for i in range(num_group)])
-
-                    dev = default_context()
-                    exe1 = y1.simple_bind(dev, x=shape)
-                    exe2 = y2.simple_bind(mx.cpu(), x=shape, w=(num_filter, shape[1]//num_group, kernel[0], kernel[1]),
-                            b=(num_filter,))
-                    for arr1, arr2 in zip(exe1.arg_arrays, exe2.arg_arrays):
-                        arr1[:] = np.random.normal(size=arr1.shape)
-                        arr2[:] = arr1
-                    exe1.forward(is_train=True)
-                    exe1.backward(exe1.outputs[0])
-                    exe2.forward(is_train=True)
-                    exe2.backward(exe2.outputs[0])
-
-                    for arr1, arr2 in zip(exe1.outputs + exe1.grad_arrays, exe2.outputs + exe2.grad_arrays):
-                        np.testing.assert_allclose(arr1.asnumpy(), arr2.asnumpy(), rtol=1e-3, atol=1e-4)
+                    for in_size in [7, 32]:
+                        num_filter = num_base
+                        num_group = num_base
+                        shape = (2, num_base, in_size, in_size)
+
+                        x = mx.sym.Variable('x')
+                        w = mx.sym.Variable('w')
+                        b = mx.sym.Variable('b')
+                        y1 = mx.sym.Convolution(data=x, weight=w, bias=b, num_filter=num_filter, num_group=num_group,
+                                kernel=kernel, stride=stride, pad=pad)
+                        xslice = mx.sym.SliceChannel(data=x, num_outputs=num_group, axis=1)
+                        wslice = mx.sym.SliceChannel(data=w, num_outputs=num_group, axis=0)
+                        bslice = mx.sym.SliceChannel(data=b, num_outputs=num_group, axis=0)
+                        y2 = mx.sym.Concat(*[mx.sym.Convolution(data=xslice[i], weight=wslice[i], bias=bslice[i],
+                                                                num_filter=num_filter//num_group, kernel=kernel,
+                                                                stride=stride, pad=pad)
+                                           for i in range(num_group)])
+
+                        dev = default_context()
+                        exe1 = y1.simple_bind(dev, x=shape)
+                        exe2 = y2.simple_bind(mx.cpu(), x=shape, w=(num_filter, shape[1]//num_group, kernel[0], kernel[1]),
+                                b=(num_filter,))
+                        for arr1, arr2 in zip(exe1.arg_arrays, exe2.arg_arrays):
+                            arr1[:] = np.random.normal(size=arr1.shape)
+                            arr2[:] = arr1
+                        exe1.forward(is_train=True)
+                        exe1.backward(exe1.outputs[0])
+                        exe2.forward(is_train=True)
+                        exe2.backward(exe2.outputs[0])
+
+                        for arr1, arr2 in zip(exe1.outputs + exe1.grad_arrays, exe2.outputs + exe2.grad_arrays):
+                            np.testing.assert_allclose(arr1.asnumpy(), arr2.asnumpy(), rtol=1e-3, atol=1e-4)
 
 
 def gen_broadcast_data(idx):

-- 
To stop receiving notification emails like this one, please contact
['"commits@mxnet.apache.org" <co...@mxnet.apache.org>'].