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/03/09 13:30:51 UTC

[GitHub] [incubator-mxnet] Tommliu opened a new pull request #17793: [Numpy] OP_interp

Tommliu opened a new pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793
 
 
   ## Description ##
   implementation of Numpy compatible operator _interp_
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [ ] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) created (except PRs with tiny changes)
   - [ ] Changes are complete (i.e. I finished coding on this PR)
   - [ ] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
   - [ ] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [ ] To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Feature1, tests, (and when applicable, API doc)
   - [ ] Feature2, tests, (and when applicable, API doc)
   
   ## Comments ##
   - If this change is a backward incompatible change, why must this change be made.
   - Interesting edge cases to note here
   

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-596823534
 
 
   Also address the sanity errors.

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603601402
 
 
   @mxnet-bot run ci [ci/jenkins/mxnet-validation/centos-gpu, ci/jenkins/mxnet-validation/unix-cpu,  ci/jenkins/mxnet-validation/windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603635844
 
 
   Jenkins CI successfully triggered : [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r390011369
 
 

 ##########
 File path: src/operator/numpy/np_interp_op-inl.h
 ##########
 @@ -0,0 +1,283 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_interp_op-inl.h
+*/
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+
+#include <vector>
+#include <string>
+#include <unordered_map>
+#include "../tensor/ordering_op-inl.h"
+#include "../tensor/matrix_op-inl.h"
+#include "../tensor/elemwise_binary_scalar_op.h"
+#include "../../common/utils.h"
+#include "../mshadow_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyInterpParam : public dmlc::Parameter<NumpyInterpParam> {
+  dmlc::optional<double> left;
+  dmlc::optional<double> right;
+  dmlc::optional<double> period;
+  dmlc::optional<double> x_scalar;
+  DMLC_DECLARE_PARAMETER(NumpyInterpParam) {
+    DMLC_DECLARE_FIELD(left)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x < xp[0], default is fp[0].");
+    DMLC_DECLARE_FIELD(right)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x > xp[-1], default is fp[-1].");
+    DMLC_DECLARE_FIELD(period)
+      .set_default(dmlc::optional<double>())
+      .describe("A period for the x-coordinates. This parameter allows"
+                "the proper interpolation of angular x-coordinates. Parameters"
+                "left and right are ignored if period is specified.");
+    DMLC_DECLARE_FIELD(x_scalar)
+      .set_default(dmlc::optional<double>())
+      .describe("Input x is a scalar");
+  }
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
 
 Review comment:
   add a blank line above

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r390010658
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -8228,6 +8228,74 @@ def hybrid_forward(self, F, a):
             assert_almost_equal(elem_mx.asnumpy(), elem_np, rtol=rtol, atol=atol)
 
 
+
 
 Review comment:
   no need for this blank line.

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r390729603
 
 

 ##########
 File path: src/api/operator/numpy/np_interp_op.cc
 ##########
 @@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file np_interp_op.cc
+ * \brief Implementation of the API of functions in src/operator/numpy/np_interp_op.cc
+ */
+#include <mxnet/api_registry.h>
+#include <mxnet/runtime/packed_func.h>
+#include "../utils.h"
+#include "../../../operator/numpy/np_interp_op-inl.h"
+
+namespace mxnet {
+
+MXNET_REGISTER_API("_npi.interp")
+.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
+  using namespace runtime;
+  const nnvm::Op* op = Op::Get("_npi_interp");
+  nnvm::NodeAttrs attrs;
+  op::NumpyInterpParam param;
+  if (args[3].type_code() == kNull) {
+    param.left = dmlc::nullopt;
+  } else {
+    param.left = args[3].operator double();
+  }
+  if (args[4].type_code() == kNull) {
+    param.right = dmlc::nullopt;
+  } else {
+    param.right = args[4].operator double();
+  }
+  if (args[5].type_code() == kNull) {
+    param.period = dmlc::nullopt;
+  } else {
+    param.period = args[5].operator double();
+  }
+  if (args[2].type_code() == kDLInt || args[2].type_code() == kDLFloat) {
+    param.x_scalar = args[2].operator double();
+    attrs.op = op;
+    attrs.parsed = std::move(param);
+    SetAttrDict<op::NumpyInterpParam>(&attrs);
+    int num_outputs = 0;
+    NDArray* inputs[] = {args[0].operator mxnet::NDArray*(), args[1].operator mxnet::NDArray*()};
+    auto ndoutputs = Invoke(op, &attrs, 2, inputs, &num_outputs, nullptr);
 
 Review comment:
   `2` seems like a magic number
   ```
   int num_inputs = 2;
   Invoke(..., num_inputs, ...)
   ```

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603602177
 
 
   @mxnet-bot run ci [centos-gpu, unix-cpu, windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603673116
 
 
   Jenkins CI successfully triggered : [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-604193004
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu removed a comment on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu removed a comment on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-604193004
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r390011098
 
 

 ##########
 File path: src/operator/numpy/np_interp_op-inl.h
 ##########
 @@ -0,0 +1,283 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_interp_op-inl.h
+*/
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+
+#include <vector>
+#include <string>
+#include <unordered_map>
+#include "../tensor/ordering_op-inl.h"
+#include "../tensor/matrix_op-inl.h"
+#include "../tensor/elemwise_binary_scalar_op.h"
+#include "../../common/utils.h"
+#include "../mshadow_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyInterpParam : public dmlc::Parameter<NumpyInterpParam> {
+  dmlc::optional<double> left;
+  dmlc::optional<double> right;
+  dmlc::optional<double> period;
+  dmlc::optional<double> x_scalar;
+  DMLC_DECLARE_PARAMETER(NumpyInterpParam) {
+    DMLC_DECLARE_FIELD(left)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x < xp[0], default is fp[0].");
+    DMLC_DECLARE_FIELD(right)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x > xp[-1], default is fp[-1].");
+    DMLC_DECLARE_FIELD(period)
+      .set_default(dmlc::optional<double>())
+      .describe("A period for the x-coordinates. This parameter allows"
+                "the proper interpolation of angular x-coordinates. Parameters"
+                "left and right are ignored if period is specified.");
+    DMLC_DECLARE_FIELD(x_scalar)
+      .set_default(dmlc::optional<double>())
+      .describe("Input x is a scalar");
+  }
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream left_s, right_s, period_s, x_scalar_s;
+    left_s << left;
+    right_s << right;
+    period_s << period;
+    x_scalar_s << x_scalar;
+    (*dict)["left"] = left_s.str();
+    (*dict)["right"] = right_s.str();
+    (*dict)["period"] = period_s.str();
+    (*dict)["x_scalar"] = x_scalar_s.str();
+  }
+};
+
+struct interp {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const int dsize,
+                                  const double left,
+                                  const double right,
+                                  const bool has_left,
+                                  const bool has_right) {
+    double x_value = x[i];
+    double xp_low = xp[0];
+    double xp_above = xp[dsize-1];
+    double lval = has_left ? left : fp[0];
+    double rval = has_right ? right : fp[dsize-1];
+
+    if (x_value > xp_above) {
+      out[i] = rval;
+    } else if (x_value < xp_low) {
+      out[i] = lval;
+    } else {
+      int imin = 0;
+      int imax = dsize;
+      int imid;
+      while (imin < imax) {
+        imid = static_cast<int>((imax + imin) / 2);
+        if (x_value >= xp[imid]) {
+          imin = imid + 1;
+        } else {
+          imax = imid;
+        }
+      }  // biserction search
+
+      int j = imin;
+      if (j == dsize) {
+        out[i] = fp[dsize-1];
+      } else if (x_value == xp[j-1]) {
+        out[i] = fp[j-1];  // void potential non-finite interpolation
+      } else {
+        double xp_below = xp[j-1];
+        double xp_above = xp[j];
+        double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+        double weigth_below = 1 - weight_above;
+        double x1 = fp[j-1] * weigth_below;
+        double x2 = fp[j] * weight_above;
+        out[i] = x1 + x2;
+      }
+    }
+  }
+};
+
+struct interp_period {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const index_t* idx,
+                                  const int dsize,
+                                  const double period) {
+    double x_value = x[i];
+    int imin = 0;
+    int imax = dsize;
+    int imid;
+    while (imin < imax) {
+      imid = static_cast<int>((imax + imin) / 2);
+      if (x_value >= xp[idx[imid]]) {
+        imin = imid + 1;
+      } else {
+        imax = imid;
+      }
+    }  // biserction search
+
+    int j = imin;
+    double xp_below, xp_above;
+    double fp1, fp2;
+    if (j == 0) {
+      xp_below = xp[idx[dsize-1]] - period;
+      xp_above = xp[idx[0]];
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else if (j == dsize) {
+      xp_below = xp[idx[dsize-1]];
+      xp_above = xp[idx[0]] + period;
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else {
+      xp_below = xp[idx[j-1]];
+      xp_above = xp[idx[j]];
+      fp1 = fp[idx[j-1]];
+      fp2 = fp[idx[j]];
+    }
+    double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+    double weigth_below = 1 - weight_above;
+    double x1 = fp1 * weigth_below;
+    double x2 = fp2 * weight_above;
+    out[i] = x1 + x2;
+  }
+};
+
+template<typename xpu, typename OP>
+void NumpyInterpForward(const nnvm::NodeAttrs& attrs,
+                        const OpContext &ctx,
+                        const std::vector<TBlob> &inputs,
+                        const std::vector<OpReqType> &req,
+                        const std::vector<TBlob> &outputs) {
+  if (req[0] == kNullOp) return;
+  using namespace mxnet;
+  using namespace mxnet_op;
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  CHECK_GE(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const NumpyInterpParam& param = nnvm::get<NumpyInterpParam>(attrs.parsed);
+  dmlc::optional<double> left = param.left;
+  dmlc::optional<double> right = param.right;
+  dmlc::optional<double> period = param.period;
+  dmlc::optional<double> x_scalar = param.x_scalar;
+
+  TBlob xp = inputs[0];
+  const TBlob &fp = inputs[1];
+  const TBlob &out = outputs[0];
+  bool has_left = left.has_value() ? true : false;
+  bool has_right = right.has_value() ? true : false;
+  double left_value = left.has_value() ? left.value() : 0.0;
+  double right_value = right.has_value() ? right.value() : 0.0;
+
+  CHECK_GE(xp.Size(), 1U) <<"ValueError: array of sample points is empty";
+
+  TopKParam topk_param = TopKParam();
+  topk_param.axis = dmlc::optional<int>(-1);
+  topk_param.is_ascend = true;
+  topk_param.k = 0;
+  topk_param.ret_typ = topk_enum::kReturnIndices;
+
+  size_t topk_temp_size;  // Used by Sort
+  size_t topk_workspace_size = TopKWorkspaceSize<xpu, double>(xp, topk_param, &topk_temp_size);
+  size_t size_x = x_scalar.has_value() ? 8 : 0;
+  size_t size_norm_x = x_scalar.has_value() ? 8 : inputs[2].Size() * sizeof(double);
+  size_t size_norm_xp = xp.Size() * sizeof(double);
+  size_t size_norm = period.has_value()? size_norm_x + size_norm_xp : 0;
+  size_t size_idx = period.has_value()? xp.Size() * sizeof(index_t) : 0;
+  size_t workspace_size =
+    topk_workspace_size + size_x + size_norm + size_idx;
+
+  Tensor<xpu, 1, char> temp_mem =
+    ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+
+  char* workspace_curr_ptr = temp_mem.dptr_;
+
+  TBlob x, idx;
+  double x_value, period_value;
+  if (x_scalar.has_value()) {
+    x_value = x_scalar.value();
+    Tensor<cpu, 1, double> host_x(&x_value, Shape1(1), ctx.get_stream<cpu>());
+    Tensor<xpu, 1, double> device_x(reinterpret_cast<double*>(workspace_curr_ptr),
+                                    Shape1(1), ctx.get_stream<xpu>());
+    Copy(device_x, host_x, ctx.get_stream<xpu>());
+    x = TBlob(device_x.dptr_, TShape(0, 1), xpu::kDevMask);
+    workspace_curr_ptr += 8;
+  } else {
+    x = inputs[2];
+  }   // handle input x is a scalar
+
+  // normalize the input data by periodic boundaries.
+  if (period.has_value()) {
+    double* norm_xp_ptr;
+    double* norm_x_ptr;
+    index_t* idx_ptr;
+    period_value = period.value();
+    CHECK_NE(period_value, 0.0)<< "period must be a non-zero value";
+
+    norm_xp_ptr = reinterpret_cast<double*>(workspace_curr_ptr);
+    norm_x_ptr = reinterpret_cast<double*>(workspace_curr_ptr + size_norm_xp);
+    idx_ptr = reinterpret_cast<index_t*>(workspace_curr_ptr + size_norm_xp + size_norm_x);
+
+    TBlob norm_x = TBlob(norm_x_ptr, x.shape_, xpu::kDevMask);
+    TBlob norm_xp = TBlob(norm_xp_ptr, xp.shape_, xpu::kDevMask);
+    const OpReqType ReqType = kWriteTo;
+    Kernel<op_with_req<OP, ReqType>, xpu>::Launch(
+      s, x.Size(), norm_x.dptr<double>(), x.dptr<double>(), period_value);
+    Kernel<op_with_req<OP, ReqType>, xpu>::Launch(
+      s, xp.Size(), norm_xp.dptr<double>(), xp.dptr<double>(), period_value);
+
+    workspace_curr_ptr += size_x + size_norm + size_idx;
+    idx = TBlob(idx_ptr, xp.shape_, xpu::kDevMask);
+    std::vector<OpReqType> req_TopK = {kWriteTo};
+    std::vector<TBlob> ret = {idx};
+
+    TopKImplwithWorkspace<xpu, double, index_t>(ctx.run_ctx, req_TopK, norm_xp, ret, topk_param,
+                                              workspace_curr_ptr, topk_temp_size, s);
 
 Review comment:
   alignment.

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r392527962
 
 

 ##########
 File path: src/operator/numpy/np_interp_op-inl.h
 ##########
 @@ -0,0 +1,284 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_interp_op-inl.h
+*/
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+
+#include <vector>
+#include <string>
+#include <unordered_map>
+#include "../tensor/ordering_op-inl.h"
+#include "../tensor/matrix_op-inl.h"
+#include "../tensor/elemwise_binary_scalar_op.h"
+#include "../../common/utils.h"
+#include "../mshadow_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyInterpParam : public dmlc::Parameter<NumpyInterpParam> {
+  dmlc::optional<double> left;
+  dmlc::optional<double> right;
+  dmlc::optional<double> period;
+  dmlc::optional<double> x_scalar;
+  DMLC_DECLARE_PARAMETER(NumpyInterpParam) {
+    DMLC_DECLARE_FIELD(left)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x < xp[0], default is fp[0].");
+    DMLC_DECLARE_FIELD(right)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x > xp[-1], default is fp[-1].");
+    DMLC_DECLARE_FIELD(period)
+      .set_default(dmlc::optional<double>())
+      .describe("A period for the x-coordinates. This parameter allows"
+                "the proper interpolation of angular x-coordinates. Parameters"
+                "left and right are ignored if period is specified.");
+    DMLC_DECLARE_FIELD(x_scalar)
+      .set_default(dmlc::optional<double>())
+      .describe("Input x is a scalar");
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream left_s, right_s, period_s, x_scalar_s;
+    left_s << left;
+    right_s << right;
+    period_s << period;
+    x_scalar_s << x_scalar;
+    (*dict)["left"] = left_s.str();
+    (*dict)["right"] = right_s.str();
+    (*dict)["period"] = period_s.str();
+    (*dict)["x_scalar"] = x_scalar_s.str();
+  }
+};
+
+struct interp {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const int dsize,
+                                  const double left,
+                                  const double right,
+                                  const bool has_left,
+                                  const bool has_right) {
+    double x_value = x[i];
+    double xp_low = xp[0];
+    double xp_above = xp[dsize-1];
+    double lval = has_left ? left : fp[0];
+    double rval = has_right ? right : fp[dsize-1];
+
+    if (x_value > xp_above) {
+      out[i] = rval;
+    } else if (x_value < xp_low) {
+      out[i] = lval;
+    } else {
+      int imin = 0;
+      int imax = dsize;
+      int imid;
+      while (imin < imax) {
+        imid = static_cast<int>((imax + imin) / 2);
+        if (x_value >= xp[imid]) {
+          imin = imid + 1;
+        } else {
+          imax = imid;
+        }
+      }  // biserction search
+
+      int j = imin;
+      if (j == dsize) {
+        out[i] = fp[dsize-1];
+      } else if (x_value == xp[j-1]) {
+        out[i] = fp[j-1];  // void potential non-finite interpolation
+      } else {
+        double xp_below = xp[j-1];
+        double xp_above = xp[j];
+        double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+        double weigth_below = 1 - weight_above;
+        double x1 = fp[j-1] * weigth_below;
+        double x2 = fp[j] * weight_above;
+        out[i] = x1 + x2;
+      }
+    }
+  }
+};
+
+struct interp_period {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const index_t* idx,
+                                  const int dsize,
+                                  const double period) {
+    double x_value = x[i];
+    int imin = 0;
+    int imax = dsize;
+    int imid;
+    while (imin < imax) {
+      imid = static_cast<int>((imax + imin) / 2);
+      if (x_value >= xp[idx[imid]]) {
+        imin = imid + 1;
+      } else {
+        imax = imid;
+      }
+    }  // biserction search
+
+    int j = imin;
+    double xp_below, xp_above;
+    double fp1, fp2;
+    if (j == 0) {
+      xp_below = xp[idx[dsize-1]] - period;
+      xp_above = xp[idx[0]];
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else if (j == dsize) {
+      xp_below = xp[idx[dsize-1]];
+      xp_above = xp[idx[0]] + period;
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else {
+      xp_below = xp[idx[j-1]];
+      xp_above = xp[idx[j]];
+      fp1 = fp[idx[j-1]];
+      fp2 = fp[idx[j]];
+    }
+    double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+    double weigth_below = 1 - weight_above;
+    double x1 = fp1 * weigth_below;
+    double x2 = fp2 * weight_above;
+    out[i] = x1 + x2;
+  }
+};
+
+template<typename xpu, typename OP>
+void NumpyInterpForward(const nnvm::NodeAttrs& attrs,
+                        const OpContext &ctx,
+                        const std::vector<TBlob> &inputs,
+                        const std::vector<OpReqType> &req,
+                        const std::vector<TBlob> &outputs) {
+  if (req[0] == kNullOp) return;
+  using namespace mxnet;
+  using namespace mxnet_op;
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  CHECK_GE(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const NumpyInterpParam& param = nnvm::get<NumpyInterpParam>(attrs.parsed);
+  dmlc::optional<double> left = param.left;
+  dmlc::optional<double> right = param.right;
+  dmlc::optional<double> period = param.period;
+  dmlc::optional<double> x_scalar = param.x_scalar;
+
+  TBlob xp = inputs[0];
+  const TBlob &fp = inputs[1];
+  const TBlob &out = outputs[0];
+  bool has_left = left.has_value() ? true : false;
+  bool has_right = right.has_value() ? true : false;
+  double left_value = left.has_value() ? left.value() : 0.0;
+  double right_value = right.has_value() ? right.value() : 0.0;
+
+  CHECK_GE(xp.Size(), 1U) <<"ValueError: array of sample points is empty";
+
+  TopKParam topk_param = TopKParam();
+  topk_param.axis = dmlc::optional<int>(-1);
+  topk_param.is_ascend = true;
+  topk_param.k = 0;
+  topk_param.ret_typ = topk_enum::kReturnIndices;
+
+  size_t topk_temp_size;  // Used by Sort
+  size_t topk_workspace_size = TopKWorkspaceSize<xpu, double>(xp, topk_param, &topk_temp_size);
+  size_t size_x = x_scalar.has_value() ? 8 : 0;
+  size_t size_norm_x = x_scalar.has_value() ? 8 : inputs[2].Size() * sizeof(double);
+  size_t size_norm_xp = xp.Size() * sizeof(double);
+  size_t size_norm = period.has_value()? size_norm_x + size_norm_xp : 0;
+  size_t size_idx = period.has_value()? xp.Size() * sizeof(index_t) : 0;
+  size_t workspace_size =
+    topk_workspace_size + size_x + size_norm + size_idx;
+
+  Tensor<xpu, 1, char> temp_mem =
+    ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+
+  char* workspace_curr_ptr = temp_mem.dptr_;
+
+  TBlob x, idx;
+  double x_value, period_value;
+  if (x_scalar.has_value()) {
+    x_value = x_scalar.value();
+    Tensor<cpu, 1, double> host_x(&x_value, Shape1(1), ctx.get_stream<cpu>());
+    Tensor<xpu, 1, double> device_x(reinterpret_cast<double*>(workspace_curr_ptr),
+                                    Shape1(1), ctx.get_stream<xpu>());
+    Copy(device_x, host_x, ctx.get_stream<xpu>());
+    x = TBlob(device_x.dptr_, TShape(0, 1), xpu::kDevMask);
+    workspace_curr_ptr += 8;
+  } else {
+    x = inputs[2];
+  }   // handle input x is a scalar
+
+  // normalize the input data by periodic boundaries.
+  if (period.has_value()) {
+    double* norm_xp_ptr;
+    double* norm_x_ptr;
+    index_t* idx_ptr;
+    period_value = period.value();
 
 Review comment:
   since `period_value` is only used in this branch, just do:
   ```c++
   double period_value = period.value();
   ```

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603673084
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-610187227
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603635809
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu removed a comment on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu removed a comment on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603601402
 
 
   @mxnet-bot run ci [ci/jenkins/mxnet-validation/centos-gpu, ci/jenkins/mxnet-validation/unix-cpu,  ci/jenkins/mxnet-validation/windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Tommliu removed a comment on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
Tommliu removed a comment on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603673084
 
 
   @mxnet-bot run ci [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603602219
 
 
   Jenkins CI successfully triggered : [centos-gpu, windows-gpu, unix-cpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 merged pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 merged pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793
 
 
   

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-603601412
 
 
   None of the jobs entered are supported. 
   Jobs entered by user: [ci/jenkins/mxnet-validation/centos-gpu, ci/jenkins/mxnet-validation/unix-cpu, ci/jenkins/mxnet-validation/windows-gpu]
   CI supported Jobs: [clang, unix-cpu, sanity, edge, miscellaneous, windows-gpu, windows-cpu, unix-gpu, website, centos-gpu, centos-cpu]
   

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-610187268
 
 
   Jenkins CI successfully triggered : [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r392527905
 
 

 ##########
 File path: src/operator/numpy/np_interp_op-inl.h
 ##########
 @@ -0,0 +1,284 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_interp_op-inl.h
+*/
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_INTERP_OP_INL_H_
+
+#include <vector>
+#include <string>
+#include <unordered_map>
+#include "../tensor/ordering_op-inl.h"
+#include "../tensor/matrix_op-inl.h"
+#include "../tensor/elemwise_binary_scalar_op.h"
+#include "../../common/utils.h"
+#include "../mshadow_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyInterpParam : public dmlc::Parameter<NumpyInterpParam> {
+  dmlc::optional<double> left;
+  dmlc::optional<double> right;
+  dmlc::optional<double> period;
+  dmlc::optional<double> x_scalar;
+  DMLC_DECLARE_PARAMETER(NumpyInterpParam) {
+    DMLC_DECLARE_FIELD(left)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x < xp[0], default is fp[0].");
+    DMLC_DECLARE_FIELD(right)
+      .set_default(dmlc::optional<double>())
+      .describe("Value to return for x > xp[-1], default is fp[-1].");
+    DMLC_DECLARE_FIELD(period)
+      .set_default(dmlc::optional<double>())
+      .describe("A period for the x-coordinates. This parameter allows"
+                "the proper interpolation of angular x-coordinates. Parameters"
+                "left and right are ignored if period is specified.");
+    DMLC_DECLARE_FIELD(x_scalar)
+      .set_default(dmlc::optional<double>())
+      .describe("Input x is a scalar");
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream left_s, right_s, period_s, x_scalar_s;
+    left_s << left;
+    right_s << right;
+    period_s << period;
+    x_scalar_s << x_scalar;
+    (*dict)["left"] = left_s.str();
+    (*dict)["right"] = right_s.str();
+    (*dict)["period"] = period_s.str();
+    (*dict)["x_scalar"] = x_scalar_s.str();
+  }
+};
+
+struct interp {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const int dsize,
+                                  const double left,
+                                  const double right,
+                                  const bool has_left,
+                                  const bool has_right) {
+    double x_value = x[i];
+    double xp_low = xp[0];
+    double xp_above = xp[dsize-1];
+    double lval = has_left ? left : fp[0];
+    double rval = has_right ? right : fp[dsize-1];
+
+    if (x_value > xp_above) {
+      out[i] = rval;
+    } else if (x_value < xp_low) {
+      out[i] = lval;
+    } else {
+      int imin = 0;
+      int imax = dsize;
+      int imid;
+      while (imin < imax) {
+        imid = static_cast<int>((imax + imin) / 2);
+        if (x_value >= xp[imid]) {
+          imin = imid + 1;
+        } else {
+          imax = imid;
+        }
+      }  // biserction search
+
+      int j = imin;
+      if (j == dsize) {
+        out[i] = fp[dsize-1];
+      } else if (x_value == xp[j-1]) {
+        out[i] = fp[j-1];  // void potential non-finite interpolation
+      } else {
+        double xp_below = xp[j-1];
+        double xp_above = xp[j];
+        double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+        double weigth_below = 1 - weight_above;
+        double x1 = fp[j-1] * weigth_below;
+        double x2 = fp[j] * weight_above;
+        out[i] = x1 + x2;
+      }
+    }
+  }
+};
+
+struct interp_period {
+  MSHADOW_XINLINE static void Map(int i,
+                                  double* out,
+                                  const double* x,
+                                  const double* xp,
+                                  const double* fp,
+                                  const index_t* idx,
+                                  const int dsize,
+                                  const double period) {
+    double x_value = x[i];
+    int imin = 0;
+    int imax = dsize;
+    int imid;
+    while (imin < imax) {
+      imid = static_cast<int>((imax + imin) / 2);
+      if (x_value >= xp[idx[imid]]) {
+        imin = imid + 1;
+      } else {
+        imax = imid;
+      }
+    }  // biserction search
+
+    int j = imin;
+    double xp_below, xp_above;
+    double fp1, fp2;
+    if (j == 0) {
+      xp_below = xp[idx[dsize-1]] - period;
+      xp_above = xp[idx[0]];
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else if (j == dsize) {
+      xp_below = xp[idx[dsize-1]];
+      xp_above = xp[idx[0]] + period;
+      fp1 = fp[idx[dsize-1]];
+      fp2 = fp[idx[0]];
+    } else {
+      xp_below = xp[idx[j-1]];
+      xp_above = xp[idx[j]];
+      fp1 = fp[idx[j-1]];
+      fp2 = fp[idx[j]];
+    }
+    double weight_above = (x_value - xp_below) / (xp_above - xp_below);
+    double weigth_below = 1 - weight_above;
+    double x1 = fp1 * weigth_below;
+    double x2 = fp2 * weight_above;
+    out[i] = x1 + x2;
+  }
+};
+
+template<typename xpu, typename OP>
+void NumpyInterpForward(const nnvm::NodeAttrs& attrs,
+                        const OpContext &ctx,
+                        const std::vector<TBlob> &inputs,
+                        const std::vector<OpReqType> &req,
+                        const std::vector<TBlob> &outputs) {
+  if (req[0] == kNullOp) return;
+  using namespace mxnet;
+  using namespace mxnet_op;
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  CHECK_GE(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const NumpyInterpParam& param = nnvm::get<NumpyInterpParam>(attrs.parsed);
+  dmlc::optional<double> left = param.left;
+  dmlc::optional<double> right = param.right;
+  dmlc::optional<double> period = param.period;
+  dmlc::optional<double> x_scalar = param.x_scalar;
+
+  TBlob xp = inputs[0];
+  const TBlob &fp = inputs[1];
+  const TBlob &out = outputs[0];
+  bool has_left = left.has_value() ? true : false;
+  bool has_right = right.has_value() ? true : false;
+  double left_value = left.has_value() ? left.value() : 0.0;
+  double right_value = right.has_value() ? right.value() : 0.0;
+
+  CHECK_GE(xp.Size(), 1U) <<"ValueError: array of sample points is empty";
+
+  TopKParam topk_param = TopKParam();
+  topk_param.axis = dmlc::optional<int>(-1);
+  topk_param.is_ascend = true;
+  topk_param.k = 0;
+  topk_param.ret_typ = topk_enum::kReturnIndices;
+
+  size_t topk_temp_size;  // Used by Sort
+  size_t topk_workspace_size = TopKWorkspaceSize<xpu, double>(xp, topk_param, &topk_temp_size);
+  size_t size_x = x_scalar.has_value() ? 8 : 0;
+  size_t size_norm_x = x_scalar.has_value() ? 8 : inputs[2].Size() * sizeof(double);
+  size_t size_norm_xp = xp.Size() * sizeof(double);
+  size_t size_norm = period.has_value()? size_norm_x + size_norm_xp : 0;
+  size_t size_idx = period.has_value()? xp.Size() * sizeof(index_t) : 0;
+  size_t workspace_size =
+    topk_workspace_size + size_x + size_norm + size_idx;
+
+  Tensor<xpu, 1, char> temp_mem =
+    ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+
+  char* workspace_curr_ptr = temp_mem.dptr_;
+
+  TBlob x, idx;
+  double x_value, period_value;
+  if (x_scalar.has_value()) {
+    x_value = x_scalar.value();
 
 Review comment:
   since `x_value` is only used in this branch, just do:
   ```c++
   double x_value = x_scalar.value();
   ```

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on a change in pull request #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#discussion_r390729763
 
 

 ##########
 File path: src/api/operator/numpy/np_interp_op.cc
 ##########
 @@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file np_interp_op.cc
+ * \brief Implementation of the API of functions in src/operator/numpy/np_interp_op.cc
+ */
+#include <mxnet/api_registry.h>
+#include <mxnet/runtime/packed_func.h>
+#include "../utils.h"
+#include "../../../operator/numpy/np_interp_op-inl.h"
+
+namespace mxnet {
+
+MXNET_REGISTER_API("_npi.interp")
+.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
+  using namespace runtime;
+  const nnvm::Op* op = Op::Get("_npi_interp");
 
 Review comment:
   `static const`? 

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17793: [Numpy] OP_interp

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on issue #17793: [Numpy] OP_interp
URL: https://github.com/apache/incubator-mxnet/pull/17793#issuecomment-604193043
 
 
   Jenkins CI successfully triggered : [windows-gpu]

----------------------------------------------------------------
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


With regards,
Apache Git Services