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/03/16 12:40:47 UTC

[GitHub] [incubator-mxnet] Yiyan66 opened a new pull request #17846: [numpy] add op tri

Yiyan66 opened a new pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846
 
 
   ## Description ##
   add op tri 
   
     &#160;  | Old FFI (ctypes) (us) | New FFI (cython) (us)
   :-------: | :-------------------: | :-: 
   tri       | 37.32                 | 5.67
   
   ## 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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on a change in pull request #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#discussion_r393407808
 
 

 ##########
 File path: src/operator/numpy/np_tri_op-inl.h
 ##########
 @@ -0,0 +1,117 @@
+/*
+ * 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_tri_op-inl.h
+ * \brief Function definition of the tri op
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_TRI_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_TRI_OP_INL_H_
+
+#include <dmlc/parameter.h>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "../mxnet_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct TriParam : public dmlc::Parameter<TriParam> {
+  nnvm::dim_t N;
+  dmlc::optional<nnvm::dim_t> M;
+  int k;
+  int dtype;
+  DMLC_DECLARE_PARAMETER(TriParam) {
+    DMLC_DECLARE_FIELD(N)
+      .describe("Number of rows in the array.");
+    DMLC_DECLARE_FIELD(M)
+      .set_default(dmlc::optional<nnvm::dim_t>())
+      .describe("Number of columns in the array. "
+                "By default, M is taken equal to N.");
+    DMLC_DECLARE_FIELD(k)
+      .set_default(0)
+      .describe("The sub-diagonal at and below which the array is filled. "
+                "k = 0 is the main diagonal, while k < 0 is below it, "
+                "and k > 0 is above. The default is 0.");
+    DMLC_DECLARE_FIELD(dtype)
+    MXNET_ADD_ALL_TYPES
+    .set_default(mshadow::kFloat32);
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream N_s, M_s, k_s, dtype_s;
+    N_s << N;
+    M_s << M;
+    k_s << k;
+    dtype_s << dtype;
+    (*dict)["N"] = N_s.str();
+    (*dict)["M"] = M_s.str();
+    (*dict)["k"] = k_s.str();
+    (*dict)["dtype"] = dtype_s.str();
 
 Review comment:
   ```
   (*dict)["dtype"] = String2MXNetTypeWithBool(dtype);
   ```
   `String2MXNetTypeWithBool` lies in `incubator-mxnet/src/api/operator/op_utils.h`

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on a change in pull request #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#discussion_r393407808
 
 

 ##########
 File path: src/operator/numpy/np_tri_op-inl.h
 ##########
 @@ -0,0 +1,117 @@
+/*
+ * 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_tri_op-inl.h
+ * \brief Function definition of the tri op
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_TRI_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_TRI_OP_INL_H_
+
+#include <dmlc/parameter.h>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "../mxnet_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct TriParam : public dmlc::Parameter<TriParam> {
+  nnvm::dim_t N;
+  dmlc::optional<nnvm::dim_t> M;
+  int k;
+  int dtype;
+  DMLC_DECLARE_PARAMETER(TriParam) {
+    DMLC_DECLARE_FIELD(N)
+      .describe("Number of rows in the array.");
+    DMLC_DECLARE_FIELD(M)
+      .set_default(dmlc::optional<nnvm::dim_t>())
+      .describe("Number of columns in the array. "
+                "By default, M is taken equal to N.");
+    DMLC_DECLARE_FIELD(k)
+      .set_default(0)
+      .describe("The sub-diagonal at and below which the array is filled. "
+                "k = 0 is the main diagonal, while k < 0 is below it, "
+                "and k > 0 is above. The default is 0.");
+    DMLC_DECLARE_FIELD(dtype)
+    MXNET_ADD_ALL_TYPES
+    .set_default(mshadow::kFloat32);
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream N_s, M_s, k_s, dtype_s;
+    N_s << N;
+    M_s << M;
+    k_s << k;
+    dtype_s << dtype;
+    (*dict)["N"] = N_s.str();
+    (*dict)["M"] = M_s.str();
+    (*dict)["k"] = k_s.str();
+    (*dict)["dtype"] = dtype_s.str();
 
 Review comment:
   ```
   (*dict)["dtype"] = MXNetTypeWithBool2String(dtype);
   ```
   `MXNetTypeWithBool2String` lies in `incubator-mxnet/src/api/operator/op_utils.h`

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#discussion_r407872643
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/_op.py
 ##########
 @@ -2054,6 +2054,149 @@ def tril(m, k=0):
     return _api_internal.tril(m, k)
 
 
+@set_module('mxnet.ndarray.numpy')
+def tri(N, M=None, k=0, dtype=None, ctx=None):
+    r"""
+    An array with ones at and below the given diagonal and zeros elsewhere.
+
+    Parameters
+    ----------
+    N : int
+        Number of rows in the array.
+    M : int, optional
+        Number of columns in the array.
+        By default, `M` is taken equal to `N`.
+    k : int, optional
+        The sub-diagonal at and below which the array is filled.
+        `k` = 0 is the main diagonal, while `k` < 0 is below it,
+        and `k` > 0 is above.  The default is 0.
+    dtype : dtype, optional
+        Data type of the returned array.  The default is float.
+
+    Returns
+    -------
+    tri : ndarray of shape (N, M)
+        Array with its lower triangle filled with ones and zero elsewhere;
+        in other words ``T[i,j] == 1`` for ``i <= j + k``, 0 otherwise.
+
+    Examples
+    --------
+    >>> np.tri(3, 5, 2, dtype=int)
+    array([[1, 1, 1, 0, 0],
+           [1, 1, 1, 1, 0],
+           [1, 1, 1, 1, 1]])
+
+    >>> np.tri(3, 5, -1)
+    array([[0.,  0.,  0.,  0.,  0.],
+           [1.,  0.,  0.,  0.,  0.],
+           [1.,  1.,  0.,  0.,  0.]])
+    """
+    return _api_internal.tri(N, M, k, dtype, ctx)
 
 Review comment:
   same for symbol

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on a change in pull request #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#discussion_r393407889
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/_op.py
 ##########
 @@ -2015,6 +2015,51 @@ def tril(m, k=0):
     return _api_internal.tril(m, k)
 
 
+@set_module('mxnet.numpy')
+def tri(N, M=None, k=0, dtype=None):
+    r"""
+    An array with ones at and below the given diagonal and zeros elsewhere.
+
+    Parameters
+    ----------
+    N : int
+        Number of rows in the array.
+    M : int, optional
+        Number of columns in the array.
+        By default, `M` is taken equal to `N`.
+    k : int, optional
+        The sub-diagonal at and below which the array is filled.
+        `k` = 0 is the main diagonal, while `k` < 0 is below it,
+        and `k` > 0 is above.  The default is 0.
+    dtype : dtype, optional
+        Data type of the returned array.  The default is float.
+
+    Returns
+    -------
+    tri : ndarray of shape (N, M)
+        Array with its lower triangle filled with ones and zero elsewhere;
+        in other words ``T[i,j] == 1`` for ``i <= j + k``, 0 otherwise.
+
+    Examples
+    --------
+    >>> np.tri(3, 5, 2, dtype=int)
+    array([[1, 1, 1, 0, 0],
+           [1, 1, 1, 1, 0],
+           [1, 1, 1, 1, 1]])
+
+    >>> np.tri(3, 5, -1)
+    array([[0.,  0.,  0.,  0.,  0.],
+           [1.,  0.,  0.,  0.,  0.],
+           [1.,  1.,  0.,  0.,  0.]])
+    """
+    # if dtype is None:
+    #     dtype = _np.float32
+    # if M is None:
+    #     M = N
+    # return _npi.tri(N=N, M=M, k=k, dtype=dtype)
 
 Review comment:
   Dead code?

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#discussion_r407872540
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/_op.py
 ##########
 @@ -2054,6 +2054,149 @@ def tril(m, k=0):
     return _api_internal.tril(m, k)
 
 
+@set_module('mxnet.ndarray.numpy')
+def tri(N, M=None, k=0, dtype=None, ctx=None):
+    r"""
+    An array with ones at and below the given diagonal and zeros elsewhere.
+
+    Parameters
+    ----------
+    N : int
+        Number of rows in the array.
+    M : int, optional
+        Number of columns in the array.
+        By default, `M` is taken equal to `N`.
+    k : int, optional
+        The sub-diagonal at and below which the array is filled.
+        `k` = 0 is the main diagonal, while `k` < 0 is below it,
+        and `k` > 0 is above.  The default is 0.
+    dtype : dtype, optional
+        Data type of the returned array.  The default is float.
+
+    Returns
+    -------
+    tri : ndarray of shape (N, M)
+        Array with its lower triangle filled with ones and zero elsewhere;
+        in other words ``T[i,j] == 1`` for ``i <= j + k``, 0 otherwise.
+
+    Examples
+    --------
+    >>> np.tri(3, 5, 2, dtype=int)
+    array([[1, 1, 1, 0, 0],
+           [1, 1, 1, 1, 0],
+           [1, 1, 1, 1, 1]])
+
+    >>> np.tri(3, 5, -1)
+    array([[0.,  0.,  0.,  0.,  0.],
+           [1.,  0.,  0.,  0.,  0.],
+           [1.,  1.,  0.,  0.,  0.]])
+    """
+    return _api_internal.tri(N, M, k, dtype, ctx)
 
 Review comment:
   ```python
   if ctx is None:
       ctx = current_context()
   return _api_internal.tri(N, M, k, dtype, ctx)
   ```

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] hzfan commented on issue #17846: [numpy] add op tri

Posted by GitBox <gi...@apache.org>.
hzfan commented on issue #17846: [numpy] add op tri
URL: https://github.com/apache/incubator-mxnet/pull/17846#issuecomment-605766635
 
 
   FFI LGTM

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] mxnet-bot commented on issue #17846: [numpy] add op tri

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


   Jenkins CI successfully triggered : [unix-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 issue #17846: [numpy] add op tri

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


   @mxnet-bot run ci [unix-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] hzfan commented on a change in pull request #17846: [numpy] add op tri

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



##########
File path: src/api/operator/numpy/np_tri_op.cc
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file np_tri_op.cc
+ * \brief Implementation of the API of functions in src/operator/numpy/np_diff.cc
+ */
+#include <mxnet/api_registry.h>
+#include "../utils.h"
+#include "../../../operator/numpy/np_tri_op-inl.h"
+
+namespace mxnet {
+
+MXNET_REGISTER_API("_npi.tri")
+.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
+  using namespace runtime;
+  const nnvm::Op* op = Op::Get("_npi_tri");
+  nnvm::NodeAttrs attrs;
+  op::TriParam param;
+  param.N = args[0].operator nnvm::dim_t();
+  if (args[1].type_code() == kNull) {
+    param.M = dmlc::nullopt;
+  } else {
+    param.M = args[1].operator nnvm::dim_t();
+  }
+  param.k = args[2].operator int();
+  param.dtype = args[3].type_code() == kNull ? mshadow::kFloat32 :
+                String2MXNetTypeWithBool(args[3].operator std::string());
+  if (args[4].type_code() != kNull) {
+    attrs.dict["ctx"] = args[4].operator std::string();
+  }
+
+  attrs.parsed = param;
+  attrs.op = op;
+  SetAttrDict<op::TriParam>(&attrs);
+
+  int num_outputs = 1;

Review comment:
       num_outputs should be 0. It means the number of outputs provided by the user, not the number of outputs produced by the op.




----------------------------------------------------------------
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 #17846: [numpy] add op tri

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


   Jenkins CI successfully triggered : [unix-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] mxnet-bot commented on pull request #17846: [numpy] add op tri

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


   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] haojin2 commented on pull request #17846: [numpy] add op tri

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


   @mxnet-bot run ci [unix-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] mxnet-bot commented on issue #17846: [numpy] add op tri

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


   Jenkins CI successfully triggered : [unix-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 #17846: [numpy] add op tri

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


   @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] Yiyan66 commented on issue #17846: [numpy] add op tri

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


   @mxnet-bot run ci [unix-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