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/01/23 08:38:36 UTC

[GitHub] [incubator-mxnet] Alicia1529 opened a new pull request #17416: [Numpy] add polynomial polyval

Alicia1529 opened a new pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416
 
 
   add numpy polynomial family and operator polyval

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375561336
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cu
 ##########
 @@ -0,0 +1,97 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_polynomial_op.cu
+ */
+
+#include "np_polynomial_op-inl.h"
+#include "../../common/cuda_utils.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_gpu {
+    template<typename DType>
+    MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                    DType* igrad_x_dptr, DType* igrad_p_dptr,
+                                    const DType* ograd_dptr, const index_t p_size) {
+    DType igrad_p = 1;
+    DType igrad_x = 0;
+    index_t j = p_size - 1;
+    while (j > 0) {
+        // atomic add since different threads could update same variable
+        atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+        igrad_p *= x_dptr[i];
+        igrad_x = igrad_x * x_dptr[i] + p_dptr[p_size - j - 1] * j;
+        j--;
+    }
+    atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+    KERNEL_ASSIGN(igrad_x_dptr[i], req, igrad_x * ograd_dptr[i]);
+    }
+};
+
+void NumpyPolyvalBackwardGPU(const nnvm::NodeAttrs& attrs,
+                          const OpContext& ctx,
+                          const std::vector<TBlob>& inputs,
+                          const std::vector<OpReqType>& req,
+                          const std::vector<TBlob>& outputs) {
+  CHECK_EQ(inputs.size(), 3U);
+  CHECK_EQ(outputs.size(), 2U);
+  CHECK_NE(req[0], kWriteInplace);
+
+  MSHADOW_TYPE_SWITCH(inputs[1].type_flag_, PType, {
+    MSHADOW_TYPE_SWITCH(inputs[2].type_flag_, XType, {
+      if (!std::is_same<PType, XType>::value ||
+      std::is_integral<PType>::value ||
+      std::is_integral<XType>::value) {
+        return;
+      }
+    })
+  })
+
+  mshadow::Stream<gpu> *s = ctx.get_stream<gpu>();
+  const TBlob& ograd = inputs[0];
+  const TBlob& p = inputs[1];
+  const TBlob& x = inputs[2];
+  const TBlob& igrad_p = outputs[0];
+  const TBlob& igrad_x = outputs[1];
+  const size_t p_size = p.Size();
+
+  using namespace mxnet_op;
+  MSHADOW_REAL_TYPE_SWITCH(ograd.type_flag_, DType, {
+    MXNET_ASSIGN_REQ_SWITCH(req[0], req_type, {
+        Kernel<polyval_backward_gpu<req_type>, gpu>::Launch(
 
 Review comment:
   2-space indentations

----------------------------------------------------------------
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 merged pull request #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 merged pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416
 
 
   

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375561422
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cu
 ##########
 @@ -0,0 +1,97 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_polynomial_op.cu
+ */
+
+#include "np_polynomial_op-inl.h"
+#include "../../common/cuda_utils.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_gpu {
+    template<typename DType>
+    MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                    DType* igrad_x_dptr, DType* igrad_p_dptr,
+                                    const DType* ograd_dptr, const index_t p_size) {
+    DType igrad_p = 1;
+    DType igrad_x = 0;
+    index_t j = p_size - 1;
+    while (j > 0) {
+        // atomic add since different threads could update same variable
+        atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+        igrad_p *= x_dptr[i];
+        igrad_x = igrad_x * x_dptr[i] + p_dptr[p_size - j - 1] * j;
+        j--;
+    }
+    atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+    KERNEL_ASSIGN(igrad_x_dptr[i], req, igrad_x * ograd_dptr[i]);
+    }
+};
+
+void NumpyPolyvalBackwardGPU(const nnvm::NodeAttrs& attrs,
+                          const OpContext& ctx,
 
 Review comment:
   alignment

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375560377
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cc
 ##########
 @@ -0,0 +1,124 @@
+/*
+ * 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) 202o by Contributors
+ * \file np_polynomial_op.cc
+*/
+
+#include <math.h>
+#include "np_polynomial_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_x {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                  DType* igrad_x_dptr, const DType* ograd_dptr,
+                                  const index_t p_size) {
+    DType igrad_x = 0;
+    index_t j = p_size - 1;
+    while (j > 0) {
+        igrad_x = igrad_x * x_dptr[i] + p_dptr[p_size - j - 1] * j;
+        j--;
+    }
+    KERNEL_ASSIGN(igrad_x_dptr[i], req, igrad_x * ograd_dptr[i]);
+  }
+};
+
+template<int req>
+struct polyval_backward_p {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                  DType* igrad_p_dptr, const DType* ograd_dptr,
+                                  const index_t p_size, const index_t x_size) {
+    DType igrad_p = 0;
+    index_t j = x_size - 1;
+    while (j >= 0) {
+        igrad_p += pow(x_dptr[j], p_size - i - 1) * ograd_dptr[j];
+        j--;
+    }
+    KERNEL_ASSIGN(igrad_p_dptr[i], req, igrad_p);
+  }
+};
+
+void NumpyPolyvalBackwardCPU(const nnvm::NodeAttrs& attrs,
+                          const OpContext& ctx,
+                          const std::vector<TBlob>& inputs,
+                          const std::vector<OpReqType>& req,
+                          const std::vector<TBlob>& outputs) {
+  CHECK_EQ(inputs.size(), 3U);
+  CHECK_EQ(outputs.size(), 2U);
+  CHECK_NE(req[0], kWriteInplace);
+
+  MSHADOW_TYPE_SWITCH(inputs[1].type_flag_, PType, {
+    MSHADOW_TYPE_SWITCH(inputs[2].type_flag_, XType, {
+      if (!std::is_same<PType, XType>::value ||
+      std::is_integral<PType>::value ||
+      std::is_integral<XType>::value) {
 
 Review comment:
   you can directly do:
   ```c++
   if (inputs[1].type_flag_ != inputs[2].type_flag_ ||
       !common::is_float(inputs[1].type_flag_) ||
       !common::is_float(inputs[2].type_flag_)) {
     return;
   }
   ```

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375561270
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cu
 ##########
 @@ -0,0 +1,97 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_polynomial_op.cu
+ */
+
+#include "np_polynomial_op-inl.h"
+#include "../../common/cuda_utils.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_gpu {
+    template<typename DType>
+    MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                    DType* igrad_x_dptr, DType* igrad_p_dptr,
+                                    const DType* ograd_dptr, const index_t p_size) {
+    DType igrad_p = 1;
+    DType igrad_x = 0;
+    index_t j = p_size - 1;
+    while (j > 0) {
+        // atomic add since different threads could update same variable
+        atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+        igrad_p *= x_dptr[i];
+        igrad_x = igrad_x * x_dptr[i] + p_dptr[p_size - j - 1] * j;
+        j--;
+    }
+    atomicAdd(&igrad_p_dptr[j], igrad_p * ograd_dptr[i]);
+    KERNEL_ASSIGN(igrad_x_dptr[i], req, igrad_x * ograd_dptr[i]);
+    }
+};
+
+void NumpyPolyvalBackwardGPU(const nnvm::NodeAttrs& attrs,
+                          const OpContext& ctx,
+                          const std::vector<TBlob>& inputs,
+                          const std::vector<OpReqType>& req,
+                          const std::vector<TBlob>& outputs) {
+  CHECK_EQ(inputs.size(), 3U);
+  CHECK_EQ(outputs.size(), 2U);
+  CHECK_NE(req[0], kWriteInplace);
+
+  MSHADOW_TYPE_SWITCH(inputs[1].type_flag_, PType, {
+    MSHADOW_TYPE_SWITCH(inputs[2].type_flag_, XType, {
 
 Review comment:
   same here, you can avoid use `TYPE_SWITCH` to perform your desired check

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375559526
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op-inl.h
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * 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_polynomial_op.h
+ * \brief Functions for dealing with polynomials.
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_POLYNOMIAL_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_POLYNOMIAL_OP_INL_H_
+
+#include <mxnet/base.h>
+#include <string>
+#include <vector>
+#include <type_traits>
+#include "../mxnet_op.h"
+#include "../../common/utils.h"
+#include "../tensor/elemwise_binary_broadcast_op.h"
+
+
+namespace mxnet {
+namespace op {
+
+inline bool NumpyPolyvalShape(const nnvm::NodeAttrs& attrs,
+                            mxnet::ShapeVector *in_attrs,
 
 Review comment:
   alignment

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375559615
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cc
 ##########
 @@ -0,0 +1,124 @@
+/*
+ * 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) 202o by Contributors
+ * \file np_polynomial_op.cc
+*/
+
+#include <math.h>
+#include "np_polynomial_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_x {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                  DType* igrad_x_dptr, const DType* ograd_dptr,
+                                  const index_t p_size) {
+    DType igrad_x = 0;
+    index_t j = p_size - 1;
+    while (j > 0) {
+        igrad_x = igrad_x * x_dptr[i] + p_dptr[p_size - j - 1] * j;
+        j--;
+    }
+    KERNEL_ASSIGN(igrad_x_dptr[i], req, igrad_x * ograd_dptr[i]);
+  }
+};
+
+template<int req>
+struct polyval_backward_p {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, const DType* p_dptr, const DType* x_dptr,
+                                  DType* igrad_p_dptr, const DType* ograd_dptr,
+                                  const index_t p_size, const index_t x_size) {
+    DType igrad_p = 0;
+    index_t j = x_size - 1;
+    while (j >= 0) {
+        igrad_p += pow(x_dptr[j], p_size - i - 1) * ograd_dptr[j];
+        j--;
+    }
+    KERNEL_ASSIGN(igrad_p_dptr[i], req, igrad_p);
+  }
+};
+
+void NumpyPolyvalBackwardCPU(const nnvm::NodeAttrs& attrs,
+                          const OpContext& ctx,
 
 Review comment:
   alignment

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r378702418
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -7287,6 +7287,72 @@ def hybrid_forward(self, F, a):
     check_unary_func("isnan")
     check_unary_func("isinf")
 
+@with_seed()
 
 Review comment:
   blank line above.

----------------------------------------------------------------
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 #17416: [Numpy] add polynomial polyval

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17416: [Numpy] add polynomial polyval
URL: https://github.com/apache/incubator-mxnet/pull/17416#discussion_r375561015
 
 

 ##########
 File path: src/operator/numpy/np_polynomial_op.cu
 ##########
 @@ -0,0 +1,97 @@
+/*
+ * 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) 2020 by Contributors
+ * \file np_polynomial_op.cu
+ */
+
+#include "np_polynomial_op-inl.h"
+#include "../../common/cuda_utils.h"
+
+namespace mxnet {
+namespace op {
+
+template<int req>
+struct polyval_backward_gpu {
+    template<typename DType>
 
 Review comment:
   2-space indentations

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