You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/10/20 03:22:37 UTC

[GitHub] [incubator-mxnet] Zha0q1 commented on a change in pull request #19339: Numpy Argmax Rewrite

Zha0q1 commented on a change in pull request #19339:
URL: https://github.com/apache/incubator-mxnet/pull/19339#discussion_r508185614



##########
File path: src/operator/numpy/np_broadcast_reduce_op.cuh
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2015-2020 by Contributors
+ * \file np_broadcast_reduce-inl.cuh
+ * \brief GPU implementations for numpy binary broadcast ops
+ * \author Zhaoqi Zhu
+*/
+#ifndef MXNET_OPERATOR_NUMPY_NP_BROADCAST_REDUCE_OP_CUH_
+#define MXNET_OPERATOR_NUMPY_NP_BROADCAST_REDUCE_OP_CUH_
+
+using namespace mshadow::cuda;
+using namespace mshadow;
+using namespace broadcast;
+
+#define KERNEL_UNROLL_SWITCH(do_unroll, unrollAmount, unrollVar, ...) \
+  if (do_unroll) {                                                    \
+    const int unrollVar = unrollAmount;                               \
+    {__VA_ARGS__}                                                     \
+  } else {                                                            \
+    const int unrollVar = 1;                                          \
+    {__VA_ARGS__}                                                     \
+  }
+
+template<typename Reducer, int NDim, typename DType, typename OType>
+void NumpyArgMinMaxReduce(Stream<gpu> *s, const TBlob& in_data, const TBlob& out_data,

Review comment:
       Existing reduce uses MSHADOW_TYPE_SWITCH which only supports the mshadow data types. Since I am using a custom struct as accumulation type and output type I unrolled that switch into this custom reduce function

##########
File path: src/operator/mshadow_op.h
##########
@@ -128,6 +128,27 @@ using std::is_integral;
 
 MXNET_UNARY_MATH_OP_NC(identity, a);
 
+template <typename IType, typename DType>
+struct IndexedNum {
+  IType idx;
+  DType num;
+
+  MSHADOW_XINLINE IndexedNum() : idx(0), num(0) {}
+
+  MSHADOW_XINLINE IndexedNum(DType n) : idx(0), num(n) {}
+
+  MSHADOW_XINLINE IndexedNum& operator+=(const IndexedNum& rhs){
+    return *this;
+  }
+};
+
+template<typename DType, typename OType>
+struct arg_min_max_map : public mxnet_op::tunable {

Review comment:
       Here OType is actually IndexedNum and identity is just 
   ```
     struct name : public mxnet_op::tunable { 
       template<typename DType> 
       MSHADOW_XINLINE static DType Map(DType a) { 
         return a; 
       } 
   ```

##########
File path: src/operator/tensor/broadcast_reduce-inl.cuh
##########
@@ -60,16 +60,19 @@ __global__ void reduce_kernel(const int N, const int M, const bool addto,
           for (int u=0;u < unroll;u++) {
             idx_big[u] = idx_big0 + mxnet_op::unravel_dot(k + u*by, big_shape, big_stride);
           }
-          DType tmp[unroll];
+          AType tmp[unroll];
           #pragma unroll
           for (int u=0;u < unroll;u++) {
             if (k + u*by < Mend) {
               tmp[u] = OP::Map(big[idx_big[u]]);
+              // argmin/max, set IndexedNum.idx
+	      if (use_index)
+                *(reinterpret_cast<int*>(&tmp[u])) = k + u*by;

Review comment:
       The plan was sort of to come back here and use c++ 17 `constexpr if` in the future when we support only cuda 11 and up. But yeah I have thought of this solution too and I will change it tomorrow




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org