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/05/20 05:15:49 UTC

[GitHub] [incubator-mxnet] Yiyan66 opened a new pull request #18371: [numpy] fix op repeat with list input

Yiyan66 opened a new pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371


   ## Description ##
   #16357 repeat
   
   ## 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



[GitHub] [incubator-mxnet] mxnet-bot commented on pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#issuecomment-632486123


   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



[GitHub] [incubator-mxnet] Yiyan66 commented on pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
Yiyan66 commented on pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#issuecomment-632486091


   @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



[GitHub] [incubator-mxnet] yzhliu merged pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
yzhliu merged pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371


   


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



[GitHub] [incubator-mxnet] yzhliu commented on pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
yzhliu commented on pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#issuecomment-641603058


   Thanks @Yiyan66 


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



[GitHub] [incubator-mxnet] yzhliu commented on a change in pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
yzhliu commented on a change in pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#discussion_r429588818



##########
File path: src/operator/numpy/np_repeat_op-inl.h
##########
@@ -0,0 +1,224 @@
+/*
+ * 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) {
+  *repeats = 0;
+  const mxnet::Tuple<int> &repts = param.repeats.value();
+  for (int i=0; i < repts.ndim(); i++) {
+    CHECK_GE(*repeats, 0) << "repeats cannot be a negative number";

Review comment:
       I think this check can be removed as it can never be negative according to the logic.

##########
File path: src/operator/numpy/np_repeat_op-inl.h
##########
@@ -0,0 +1,224 @@
+/*
+ * 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) {
+  *repeats = 0;
+  const mxnet::Tuple<int> &repts = param.repeats.value();
+  for (int i=0; i < repts.ndim(); i++) {
+    CHECK_GE(*repeats, 0) << "repeats cannot be a negative number";
+    *repeats += repts[i];
+  }
+  *axisOpt = param.axis;
+  if (static_cast<bool>(*axisOpt)) {
+    int ndims = ishape.ndim();
+    int 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;
+  GetRepeatsParams(param, ishape, &repeats, &axisOpt);
+  // 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)) {
+    int ndims = ishape.ndim();
+    int axis = axisOpt.value();
+    if (axis < 0) {
+      axis += ndims;
+    }

Review comment:
       can we unify the logic with those in `GetRepeatsParams`

##########
File path: python/mxnet/symbol/numpy/_symbol.py
##########
@@ -2446,7 +2446,15 @@ def repeat(a, repeats, axis=None):
            [3, 4],
            [3, 4]])
     """
-    return _npi.repeat(a, repeats=repeats, axis=axis)
+    if isinstance(repeats, numeric_types):
+        return _npi.repeat(a, repeats=repeats, axis=axis)
+    if len(repeats) == 1:
+        return _npi.repeat(a, repeats[0], axis)
+    if axis is not None:
+        tmp = swapaxes(a, 0, axis)
+        res = _npi.repeats(tmp, repeats=repeats, axis=0)
+        return swapaxes(res, 0, axis)
+    return _npi.repeats(a, repeats=repeats, axis=axis)

Review comment:
       can we unify these two ops? numeric_type can be passed as `[repeats]`




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



[GitHub] [incubator-mxnet] yzhliu commented on a change in pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [incubator-mxnet] mxnet-bot commented on pull request #18371: [numpy] fix op repeat with list input

Posted by GitBox <gi...@apache.org>.
mxnet-bot commented on pull request #18371:
URL: https://github.com/apache/incubator-mxnet/pull/18371#issuecomment-631241573


   Hey @Yiyan66 , Thanks for submitting the PR 
   All tests are already queued to run once. If tests fail, you can trigger one or more tests again with the following commands: 
   - To trigger all jobs: @mxnet-bot run ci [all] 
   - To trigger specific jobs: @mxnet-bot run ci [job1, job2] 
   *** 
   **CI supported jobs**: [centos-cpu, miscellaneous, clang, unix-gpu, centos-gpu, windows-gpu, unix-cpu, sanity, windows-cpu, website, edge]
   *** 
   _Note_: 
    Only following 3 categories can trigger CI :PR Author, MXNet Committer, Jenkins Admin. 
   All CI tests must pass before the PR can be merged. 
   


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