yzhliu commented on a change in pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#discussion_r437095991



##########
File path: src/operator/numpy/np_repeat_op-inl.h
##########
@@ -0,0 +1,227 @@
+/*
+ * 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_repeat_op-inl.h
+ * \brief Function definition of the repeat op
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_REPEAT_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_REPEAT_OP_INL_H_
+
+#include <mxnet/operator_util.h>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <utility>
+#include <type_traits>
+#include <unordered_map>
+#include "../mshadow_op.h"
+#include "../elemwise_op_common.h"
+#include "../channel_op_common.h"
+#include "../mxnet_op.h"
+#include "../../common/static_array.h"
+
+namespace mxnet {
+namespace op {
+
+struct RepeatsParam : public dmlc::Parameter<RepeatsParam> {
+  dmlc::optional<mxnet::Tuple<int>> repeats;
+  dmlc::optional<int> axis;
+  DMLC_DECLARE_PARAMETER(RepeatsParam) {
+    DMLC_DECLARE_FIELD(repeats)
+      .describe("The number of repetitions for each element.");
+    DMLC_DECLARE_FIELD(axis)
+      .set_default(dmlc::optional<int>())
+      .describe("The axis along which to repeat values."
+                " The negative numbers are interpreted counting from the 
backward."
+                " By default, use the flattened input array,"
+                " and return a flat output array.");
+  }
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream repeats_s, axis_s;
+    repeats_s << repeats;
+    axis_s << axis;
+    (*dict)["repeats"] = repeats_s.str();
+    (*dict)["axis"] = axis_s.str();
+  }
+};
+
+inline void GetRepeatsParams(const RepeatsParam& param, const mxnet::TShape& 
ishape,
+                             int* repeats, dmlc::optional<int>* axisOpt, int* 
axis) {
+  *repeats = 0;
+  const mxnet::Tuple<int> &repts = param.repeats.value();
+  for (int i=0; i < repts.ndim(); i++) {
+    CHECK_GE(repts[i], 0) << "repeats cannot be a negative number";
+    *repeats += repts[i];
+  }
+  *axisOpt = param.axis;
+  if (static_cast<bool>(*axisOpt)) {
+    int ndims = ishape.ndim();
+    *axis = axisOpt->value();
+    if (*axis < 0) {
+      *axis += ndims;
+    }
+    CHECK(*axis >= 0 && *axis < ndims) << "axis = " << axisOpt->value() << " 
out of bounds";
+  }
+}
+
+inline bool RepeatsOpShape(const nnvm::NodeAttrs& attrs,
+                           mxnet::ShapeVector *in_attrs,
+                           mxnet::ShapeVector *out_attrs) {
+  const RepeatsParam& param = nnvm::get<RepeatsParam>(attrs.parsed);
+  CHECK_EQ(in_attrs->size(), 1U);
+  CHECK_EQ(out_attrs->size(), 1U);
+  const mxnet::TShape& ishape = (*in_attrs)[0];
+  int repeats = 0;
+  dmlc::optional<int> axisOpt;
+  int axis = -1;
+  GetRepeatsParams(param, ishape, &repeats, &axisOpt, &axis);
+  // If 0 repeats, return an empty 1-dim, 0-size array
+  if (0 == repeats) {
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, mxnet::TShape(1, 0));
+    return true;
+  }
+
+  // If repeats > 0, multiply the size of the corresponding axis by repeats
+  if (static_cast<bool>(axisOpt)) {
+    mxnet::TShape shape(ishape.ndim(), -1);
+    for (int i = 0; i < ishape.ndim(); ++i) {
+      if (i == axis) {
+        shape[i] = param.repeats.value().ndim() == 1 ? repeats * ishape[i] : 
repeats;
+      } else {
+        shape[i] = ishape[i];
+      }
+    }
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, shape);
+  } else {  // If axis is not input by user, return a flat 1D array of size = 
repeats
+    repeats = param.repeats.value().ndim() == 1 ? ishape.Size() * repeats : 
repeats;
+    mxnet::TShape shape(1, repeats);
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, shape);
+  }
+  return shape_is_known(out_attrs->at(0));
+}
+
+namespace {  // unnamed namespace to keep scope of the struct within the file
+struct RepeatInOutIndex {
+  int index[50];
+};
+}  // unnamed namespace

Review comment:
       seems to be an unused struct?

##########
File path: python/mxnet/ndarray/numpy/_op.py
##########
@@ -4076,7 +4076,13 @@ def repeat(a, repeats, axis=None):
            [3, 4],
            [3, 4]])
     """
-    return _api_internal.repeat(a, repeats, axis)
+    if isinstance(repeats, numeric_types):
+        repeats = [repeats]
+    if axis is not None:
+        tmp = swapaxes(a, 0, axis)
+        res = _api_internal.repeats(tmp, repeats, 0)

Review comment:
       can we unify .repeats -> .repeat?




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


Reply via email to