haojin2 commented on a change in pull request #17328: [numpy] add op pad
URL: https://github.com/apache/incubator-mxnet/pull/17328#discussion_r378677706
 
 

 ##########
 File path: src/operator/numpy/np_pad_op-inl.h
 ##########
 @@ -0,0 +1,725 @@
+/*
+ * 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_pad_op-inl.h
+ * \brief Function definition of matrix related operators
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_PAD_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_PAD_OP_INL_H_
+
+#include <vector>
+#include <algorithm>
+#include <string>
+#include <utility>
+#include "../tensor/matrix_op-inl.h"
+#include "../nn/concat-inl.h"
+#include "../../common/utils.h"
+#include "../mxnet_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "../tensor/broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+template <int ndim, typename DTypeShape>
+MSHADOW_XINLINE index_t rravel(const mshadow::Shape<ndim>& coord,
+                               const DTypeShape* shape) {
+  index_t ret = 0;
+  int nndim = ndim;
+  #pragma unroll
+  for (int i = 0; i < nndim; ++i) {
+    ret = ret * shape[i] + (shape[i] > coord[i]) * coord[i];
+  }
+  return ret;
+}
+
+/* Compute coordinates from flattened index given shape */
+template<int ndim, typename DTypeShape>
+MSHADOW_XINLINE mshadow::Shape<ndim> uunravel(const int idx,
+                                              const DTypeShape* shape) {
+  mshadow::Shape<ndim> ret;
+  #pragma unroll
+  for (int i = ndim-1, j = idx; i >=0; --i) {
+    auto tmp = j / shape[i];
+    ret[i] = j - tmp*shape[i];
+    j = tmp;
+  }
+  return ret;
+}
+
+struct NumpyPadParam : public dmlc::Parameter<NumpyPadParam> {
+  mxnet::Tuple<mxnet::Tuple<int>> pad_width;
+  int mode;
+  double constant_value;
+  std::string reflect_type;
+  DMLC_DECLARE_PARAMETER(NumpyPadParam) {
+    DMLC_DECLARE_FIELD(pad_width)
+    .describe("Number of values padded to the edges of each axis. "
+              "((before_1, after_1), … (before_N,"
+              "after_N)) unique pad widths for each axis. ((before, after),) "
+              "yields same before and"
+              "after pad for each axis. "
+              "(pad,) or int is a shortcut for before = after = pad width for 
all"
+              "axes.");
+    DMLC_DECLARE_FIELD(mode)
+    .set_default(1)
 
 Review comment:
   suggest better representation of the modes, you could use a combination of 
enum type and `.add_enum` like:
   ```
   enum NumpyPadMode {kConstant, ...};
   // ...
   DMLC_DECLARE_FIELD(mode)
     .add_enum("constant", kConstant)
     // 'add_enum' for all other modes
     .set_default(kConstant)
   // ...
   
   ```

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to