haojin2 commented on a change in pull request #17328: [numpy] add op pad URL: https://github.com/apache/incubator-mxnet/pull/17328#discussion_r367144346
########## File path: src/operator/numpy/np_pad_op-inl.h ########## @@ -0,0 +1,735 @@ +/* + * 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 <size_t ndim, typename xpu> +MSHADOW_XINLINE index_t rravel(const mshadow::Shape<ndim>& coord, + const mshadow::Tensor<xpu, 1, index_t>& shape) { + index_t ret = 0; + #pragma unroll + for (int i = 0; i < ndim; ++i) { + ret = ret * shape[i] + (shape[i] > coord[i]) * coord[i]; + } + return ret; +} + +template<size_t ndim, typename xpu> +MSHADOW_XINLINE mshadow::Shape<ndim> uunravel(index_t idx, + const mshadow::Tensor<xpu, 1, index_t>& shape) { + mshadow::Shape<ndim> ret; + #pragma unroll + for (index_t 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<Tuple<int>> pad_width; + int mode; + std::string reflect_type; + double constant_value; + 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) + .describe("str or function, optional"); + DMLC_DECLARE_FIELD(reflect_type) + .set_default("even") + .describe("Used in ‘reflect’, and ‘symmetric’. " + "The ‘even’ style is the default with an unaltered reflection around " + "the edge value. For the ‘odd’ style," + "the extended part of the array is created by subtracting the " + "reflected values from two times the edge value."); + DMLC_DECLARE_FIELD(constant_value) + .set_default(0.0) + .describe("Used in ‘constant’. The values to set the padded values for each axis." + "((before_1, after_1), ... (before_N, after_N)) unique pad constants for" + "each axis." + "((before, after),) yields same before and after constants for each axis." + "(constant,) or constant is a shortcut for before = after = constant for all" + "axes." + "Default is 0."); + } +}; + +inline mxnet::TShape NumpyPadShapeImpl(const mxnet::TShape& ishape, + const mxnet::Tuple<Tuple<int>> pad_width) { + if (ishape.ndim() == 1) { + auto s = ishape[0] + pad_width[0][0] + pad_width[1][0]; + return mxnet::TShape({s}); + } else if (ishape.ndim() >= 2) { + int i; + int sshape_number = ishape.ndim(); + mxnet::TShape oshape(ishape.ndim(), -1); + for (i = ishape.ndim() - 1; i >=0; i--) { + int base = ishape[i]; + base = base + pad_width[i][0] + pad_width[i][1]; + oshape[i] = base; + } + return oshape; + } + return mxnet::TShape({-1, -1}); +} + +inline bool NumpyPadOpShape(const nnvm::NodeAttrs& attrs, + mxnet::ShapeVector* in_attrs, + mxnet::ShapeVector* out_attrs) { + CHECK_EQ(in_attrs->size(), 1U); + CHECK_EQ(out_attrs->size(), 1U); + + const mxnet::TShape& ishape = (*in_attrs)[0]; + if (!mxnet::ndim_is_known(ishape)) { + return false; + } + const NumpyPadParam& param = nnvm::get<NumpyPadParam>(attrs.parsed); + + mxnet::TShape oshape = NumpyPadShapeImpl(ishape, param.pad_width); + + if (shape_is_none(oshape)) { + LOG(FATAL) << "Pad does not exist."; + } + SHAPE_ASSIGN_CHECK(*out_attrs, 0, oshape); + + return shape_is_known(out_attrs->at(0)); +} + + +inline bool NumpyPadOpType(const nnvm::NodeAttrs &attrs, Review comment: Move this function to `.cc` ---------------------------------------------------------------- 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
