haojin2 commented on a change in pull request #17234: Op Quantile/Percentile 
[Numpy]
URL: https://github.com/apache/incubator-mxnet/pull/17234#discussion_r364658691
 
 

 ##########
 File path: src/operator/numpy/np_percentile_op-inl.h
 ##########
 @@ -0,0 +1,307 @@
+/*
+ * 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) 2019 by Contributors
+ * \file np_percentile_op-inl.h
+*/
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_PERCENTILE_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_PERCENTILE_OP_INL_H_
+
+#include <vector>
+#include "../tensor/ordering_op-inl.h"
+#include "../tensor/matrix_op-inl.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 {
+
+namespace percentile_enum {
+enum PercentileType {kLinear, kLower, kHigher, kMidpoint, kNearest};
+}  // percentile_enum
+
+struct NumpyPercentileParam : public dmlc::Parameter<NumpyPercentileParam> {
+  dmlc::optional<mxnet::Tuple<int>> axis;
+  int interpolation;
+  bool keepdims;
+  DMLC_DECLARE_PARAMETER(NumpyPercentileParam) {
+    DMLC_DECLARE_FIELD(axis)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe("Axis or axes along which a sum is performed. The default, 
axis=None, will sum "
+                "all of the elements of the input array. If axis is negative 
it counts from the "
+                "last to the first axis.");
+    DMLC_DECLARE_FIELD(interpolation).set_default(percentile_enum::kLinear)
+      .add_enum("linear", percentile_enum::kLinear)
+      .add_enum("lower", percentile_enum::kLower)
+      .add_enum("higher", percentile_enum::kHigher)
+      .add_enum("midpoint", percentile_enum::kMidpoint)
+      .add_enum("nearest", percentile_enum::kNearest)
+      .describe("his optional parameter specifies the interpolation method to 
use when the"
+                "desired percentile lies between two data points i < j");
+    DMLC_DECLARE_FIELD(keepdims).set_default(false)
+      .describe("If this is set to `True`, the reduced axes are left "
+                "in the result as dimension with size one.");
+  }
+};
+
+template<int NDim>
+struct percentile_take {
+  template<typename DType, typename QType, typename OType>
+  MSHADOW_XINLINE static void Map(int i,
+                                  OType* out,
+                                  const QType* q,
+                                  const DType* a_sort,
+                                  const int interpolation,
+                                  mshadow::Shape<NDim> t_shape,
+                                  mshadow::Shape<NDim> r_shape) {
+    using namespace mshadow;
+    using namespace mxnet_op;
+
+    auto r_coord = unravel(i, r_shape);
+    size_t q_idx = r_coord[0];
+
+    Shape<NDim> t_coord(t_shape);
+
+    for (int j = 0; j < NDim-1; ++j) {
+      t_coord[j] = r_coord[j+1];
+    }
 
 Review comment:
   better pre-compute this result outside the kernel, doing it on GPU threads 
could be too expensive.

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