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 2021/02/04 19:20:46 UTC

[GitHub] [incubator-mxnet] waytrue17 opened a new pull request #19844: [BUGFIX] Fix linspace

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


   ## Description ##
   Fix #19825
   Previously both `mx.np.linspace`  and `numpy.linspace` round towards infinity for negative int values. `numpy.lincpace` recently fixed this to round towards -infinity in: https://github.com/numpy/numpy/pull/16841. This PR makes the same change to `mx.np.linspace` and make it compatible with `numpy.linspcae`.
   
   
   ## Checklist ##
   ### Essentials ###
   - [ ] PR's title starts with a category (e.g. [BUGFIX], [MODEL], [TUTORIAL], [FEATURE], [DOC], etc)
   - [ ] Changes are complete (i.e. I finished coding on this PR)
   - [ ] All changes have test coverage
   - [ ] Code is well-documented
   
   ### 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] waytrue17 commented on pull request #19844: [BUGFIX] Fix linspace

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


   @mxnet-bot run ci [unix-cpu]


----------------------------------------------------------------
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] waytrue17 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -732,11 +732,20 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
 }
 
 struct linspace_fwd {
-  template<typename DType>
+  template<typename DType,
+           typename std::enable_if<!std::is_integral<DType>::value, bool>::type = true>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
+
+  // Floor for int dtype
+  template<typename DType,
+           typename std::enable_if<std::is_integral<DType>::value, bool>::type = true>
+  MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
+                                  int req, DType* out) {
+    KERNEL_ASSIGN(out[i], req, static_cast<DType>(std::floor(start + step * i)));
+  }
 };

Review comment:
       We already have the tests for negative int:)




----------------------------------------------------------------
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 #19844: [BUGFIX] Fix linspace

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


   Hey @waytrue17 , 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**: [windows-gpu, unix-cpu, website, centos-gpu, edge, clang, miscellaneous, centos-cpu, unix-gpu, windows-cpu, sanity]
   *** 
   _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



[GitHub] [incubator-mxnet] waytrue17 commented on pull request #19844: [BUGFIX] Fix linspace

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


   @mxnet-bot run ci [unix-cpu]


----------------------------------------------------------------
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] waytrue17 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       introduced `std::is_integral` for all int types. Thanks for the suggestion!




----------------------------------------------------------------
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] Zha0q1 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       I think here we can use a macro to generate templated structs for int32 int64 int16 and int8

##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       I think here we can use a macro to generate templated structs for int32 int64 int16 and int8 or use `std::is_integral`




----------------------------------------------------------------
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] waytrue17 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       Added `std::is_integral` to cover all int types. Thanks for the suggestion!




----------------------------------------------------------------
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 #19844: [BUGFIX] Fix linspace

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






----------------------------------------------------------------
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 #19844: [BUGFIX] Fix linspace

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


   Jenkins CI successfully triggered : [unix-cpu]


----------------------------------------------------------------
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] Zha0q1 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       I think here we can use a macro to generate templated structs for int32 int64 int16 and int8




----------------------------------------------------------------
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] Zha0q1 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -732,11 +732,20 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
 }
 
 struct linspace_fwd {
-  template<typename DType>
+  template<typename DType,
+           typename std::enable_if<!std::is_integral<DType>::value, bool>::type = true>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
+
+  // Floor for int dtype
+  template<typename DType,
+           typename std::enable_if<std::is_integral<DType>::value, bool>::type = true>
+  MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
+                                  int req, DType* out) {
+    KERNEL_ASSIGN(out[i], req, static_cast<DType>(std::floor(start + step * i)));
+  }
 };

Review comment:
       Shall we also add some unittest to tests this new behavior?




----------------------------------------------------------------
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] Zha0q1 commented on a change in pull request #19844: [BUGFIX] Fix linspace

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



##########
File path: src/operator/tensor/init_op.h
##########
@@ -731,14 +731,23 @@ inline bool RangeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+template<typename DType>
 struct linspace_fwd {
-  template<typename DType>
   MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double step,
                                   int req, DType* out) {
     KERNEL_ASSIGN(out[i], req, static_cast<DType>(start + step * i));
   }
 };
 
+// Round towards -infinity for int type

Review comment:
       I think here we can use a macro to generate templated structs for int32 int64 int16 and int8 or use `std::is_integral`




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