hgt312 commented on a change in pull request #16829: [Numpy][Operator] 'where' 
Implementation in MXNet
URL: https://github.com/apache/incubator-mxnet/pull/16829#discussion_r347077419
 
 

 ##########
 File path: src/operator/numpy/np_where_op-inl.h
 ##########
 @@ -0,0 +1,266 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_where_op.cc
+ * \brief Function definition of numpy operator where
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_WHERE_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_WHERE_OP_INL_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <vector>
+#include "../../common/utils.h"
+#include "../mxnet_op.h"
+#include "../mshadow_op.h"
+#include "../operator_common.h"
+#include "np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+#define NUMPY_WHERE_MAX_DIM 5
+
+using namespace mshadow;
+
+template<int ndim>
+struct numpy_where_kernel {
+  template<typename CType, typename DType>
+  MSHADOW_XINLINE static void Map(index_t base, OpReqType req, const 
Shape<ndim> &cstride,
+                                  const Shape<ndim> &xstride, const 
Shape<ndim> &ystride,
+                                  const Shape<ndim> &oshape, CType *datac, 
DType *datax,
+                                  DType *datay, DType *out) {
+    Shape<ndim> coord = mxnet_op::unravel(base, oshape);
+    auto cidx = static_cast<index_t>(mxnet_op::dot(coord, cstride));
+    auto xidx = static_cast<index_t>(mxnet_op::dot(coord, xstride));
+    auto yidx = static_cast<index_t>(mxnet_op::dot(coord, ystride));
+    KERNEL_ASSIGN(out[base], req, datac[cidx] != CType(0) ? datax[xidx] : 
datay[yidx]);
+  }
+};
+
+template<int ndim, bool is_left>
+struct numpy_where_backward_kernel {
+  template<typename CType, typename DType>
+  MSHADOW_XINLINE static void Map(index_t base, OpReqType req,
+                                  const Shape<ndim> &cstride, const 
Shape<ndim> &oshape,
+                                  CType *datac, DType *datao, DType *grad) {
+    Shape<ndim> coord = mxnet_op::unravel(base, oshape);
+    auto cidx = static_cast<index_t>(mxnet_op::dot(coord, cstride));
+    if (is_left) {
+      KERNEL_ASSIGN(grad[base], req, datac[cidx] != CType(0) ? datao[base] : 
DType(0));
+    } else {
+      KERNEL_ASSIGN(grad[base], req, datac[cidx] == CType(0) ? datao[base] : 
DType(0));
+    }
+  }
+};
+
+inline bool NumpyWhereOpShape(const nnvm::NodeAttrs& attrs,
+                              mxnet::ShapeVector* in_attrs,
+                              mxnet::ShapeVector* out_attrs) {
+  CHECK_EQ(in_attrs->size(), 3U);
+  CHECK_EQ(out_attrs->size(), 1U);
+  mxnet::TShape& operand1 = (*in_attrs)[0];
+  mxnet::TShape& operand2 = (*in_attrs)[1];
+  mxnet::TShape& operand3 = (*in_attrs)[2];
+
+  if (operand1 == operand2 && operand2 == operand3) {
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, operand1);
+    return shape_is_known(out_attrs->at(0));
+  }
+  mxnet::TShape out(std::max({operand1.ndim(), operand2.ndim(), 
operand3.ndim()}), -1);
+  const int b1 = out.ndim() - operand1.ndim();
+  const int b2 = out.ndim() - operand2.ndim();
+  const int b3 = out.ndim() - operand3.ndim();
+  for (int i = 0; i < out.ndim(); ++i) {
+    int s1 = 1, s2 = 1, s3 = 1;
+    if (i >= b1) s1 = operand1[i-b1];
+    if (i >= b2) s2 = operand2[i-b2];
+    if (i >= b3) s3 = operand3[i-b3];
+    if (!(s1 == s2 && s2 == s3)) {
+      CHECK((s1 == 1 && s2 == 1) || (s1 == 1 && s3 == 1) || (s2 == 1 && s3 == 
1) ||
+            (s1 == 1 && s2 == s3) || (s2 == 1 && s1 == s3) || (s3 == 1 && s1 
== s2))
+        << "Operands could not be broadcast together.";
+      out[i] = std::max({s1, s2, s3});
+    } else {
+      out[i] = s1;
+    }
+  }
+  SHAPE_ASSIGN_CHECK(*out_attrs, 0, out);
+  return shape_is_known(out);
+}
+
+inline bool NumpyWhereOpType(const nnvm::NodeAttrs& attrs,
+                             std::vector<int>* in_attrs,
+                             std::vector<int>* out_attrs) {
+  CHECK_EQ(in_attrs->size(), 3U)
+    << "where operator takes 3 arguments (" << in_attrs->size() << " given)";
+  CHECK_EQ(out_attrs->size(), 1U);
+  CHECK_EQ(in_attrs->at(1), in_attrs->at(2));
+  TYPE_ASSIGN_CHECK(*out_attrs, 0, in_attrs->at(1));
 
 Review comment:
   solved

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to