[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-18 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r315041367
 
 

 ##
 File path: src/operator/numpy/np_kron.cc
 ##
 @@ -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.
+ */
+
+/*!
+ * \file np_kron.cc
+ * \brief CPU Implementation of numpy-compatible Kronecker product
+ */
+
+#include "./np_kron-inl.h"
+
+namespace mxnet {
+namespace op {
+
+inline bool KronOpShape(const nnvm::NodeAttrs& attrs,
+  mxnet::ShapeVector *in_attrs,
+  mxnet::ShapeVector *out_attrs) {
 
 Review comment:
   fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198878
 
 

 ##
 File path: src/operator/numpy/np_kron-inl.h
 ##
 @@ -0,0 +1,86 @@
+/*
+ * 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_kron-inl.h
+ * \brief Function definition of matrix numpy-compatible kron operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+
+#include 
+#include 
+#include "../tensor/dot-inl.h"
+#include "../tensor/elemwise_binary_op.h"
+#include "../tensor/broadcast_reduce_op.h"
+#include "../channel_op_common.h"
+#include "np_tensordot_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template
+inline void KronOpForward(const nnvm::NodeAttrs& attrs,
+  const OpContext& ctx,
+  const std::vector& inputs,
+  const std::vector& req,
+  const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+
+  CHECK_EQ(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  const TBlob& a = inputs[0];
+  const TBlob& b = inputs[1];
+  const TBlob& out = outputs[0];
+  const mxnet::TShape a_shape = a.shape_;
+  const mxnet::TShape b_shape = b.shape_;
+
+  TensordotIntAxesImpl(0, ctx, a, b, out, req[0]);
+}
+
+template
+inline void KronOpBackward(const nnvm::NodeAttrs& attrs,
+   const OpContext& ctx,
+   const std::vector& inputs,
+   const std::vector& req,
+   const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mshadow_op;
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198932
 
 

 ##
 File path: src/operator/numpy/np_kron-inl.h
 ##
 @@ -0,0 +1,86 @@
+/*
+ * 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_kron-inl.h
+ * \brief Function definition of matrix numpy-compatible kron operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+
+#include 
+#include 
+#include "../tensor/dot-inl.h"
+#include "../tensor/elemwise_binary_op.h"
+#include "../tensor/broadcast_reduce_op.h"
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198810
 
 

 ##
 File path: src/operator/numpy/np_kron-inl.h
 ##
 @@ -0,0 +1,86 @@
+/*
+ * 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_kron-inl.h
+ * \brief Function definition of matrix numpy-compatible kron operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+
+#include 
+#include 
+#include "../tensor/dot-inl.h"
+#include "../tensor/elemwise_binary_op.h"
+#include "../tensor/broadcast_reduce_op.h"
+#include "../channel_op_common.h"
+#include "np_tensordot_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template
+inline void KronOpForward(const nnvm::NodeAttrs& attrs,
+  const OpContext& ctx,
+  const std::vector& inputs,
+  const std::vector& req,
+  const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+
+  CHECK_EQ(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  const TBlob& a = inputs[0];
+  const TBlob& b = inputs[1];
+  const TBlob& out = outputs[0];
+  const mxnet::TShape a_shape = a.shape_;
+  const mxnet::TShape b_shape = b.shape_;
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198834
 
 

 ##
 File path: src/operator/numpy/np_kron-inl.h
 ##
 @@ -0,0 +1,86 @@
+/*
+ * 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_kron-inl.h
+ * \brief Function definition of matrix numpy-compatible kron operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+
+#include 
+#include 
+#include "../tensor/dot-inl.h"
+#include "../tensor/elemwise_binary_op.h"
+#include "../tensor/broadcast_reduce_op.h"
+#include "../channel_op_common.h"
+#include "np_tensordot_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template
+inline void KronOpForward(const nnvm::NodeAttrs& attrs,
+  const OpContext& ctx,
+  const std::vector& inputs,
+  const std::vector& req,
+  const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+
+  CHECK_EQ(inputs.size(), 2U);
+  CHECK_EQ(outputs.size(), 1U);
+
+  const TBlob& a = inputs[0];
+  const TBlob& b = inputs[1];
+  const TBlob& out = outputs[0];
+  const mxnet::TShape a_shape = a.shape_;
+  const mxnet::TShape b_shape = b.shape_;
+
+  TensordotIntAxesImpl(0, ctx, a, b, out, req[0]);
+}
+
+template
+inline void KronOpBackward(const nnvm::NodeAttrs& attrs,
+   const OpContext& ctx,
+   const std::vector& inputs,
+   const std::vector& req,
+   const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mshadow_op;
+
+  CHECK_EQ(inputs.size(), 3U);
+  CHECK_EQ(outputs.size(), 2U);
+
+  const TBlob& ograd = inputs[0];
+  const TBlob& a = inputs[1];
+  const TBlob& b = inputs[2];
+  const TBlob& grad_a = outputs[0];
+  const TBlob& grad_b = outputs[1];
+  const mxnet::TShape a_shape = a.shape_;
+  const mxnet::TShape b_shape = b.shape_;
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198900
 
 

 ##
 File path: src/operator/numpy/np_kron-inl.h
 ##
 @@ -0,0 +1,86 @@
+/*
+ * 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_kron-inl.h
+ * \brief Function definition of matrix numpy-compatible kron operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_KRON_INL_H_
+
+#include 
+#include 
+#include "../tensor/dot-inl.h"
+#include "../tensor/elemwise_binary_op.h"
+#include "../tensor/broadcast_reduce_op.h"
+#include "../channel_op_common.h"
+#include "np_tensordot_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template
+inline void KronOpForward(const nnvm::NodeAttrs& attrs,
+  const OpContext& ctx,
+  const std::vector& inputs,
+  const std::vector& req,
+  const std::vector& outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198782
 
 

 ##
 File path: src/operator/numpy/np_kron.cu
 ##
 @@ -0,0 +1,37 @@
+/*
+ * 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_kron.cu
+ * \brief GPU Implementation of numpy-compatible kron
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198745
 
 

 ##
 File path: tests/python/unittest/test_numpy_op.py
 ##
 @@ -829,6 +829,57 @@ def get_indices(axis_size):
 assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, 
atol=1e-5)
 
 
+@with_seed()
+@use_np
+def test_np_kron():
+class TestKron(HybridBlock):
+def __init__(self):
+super(TestKron, self).__init__()
+
+def hybrid_forward(self, F, a, b):
+return F.np.kron(a, b)
+
+# test input
+tensor_shapes = [
+((3,), (3,)),
+((2, 3), (3,)),
+((2, 3, 4), (2,)),
+((3, 2), ())
+]
+
+for hybridize in [True, False]:
+for a_shape, b_shape in tensor_shapes:
+for dtype in [_np.float32, _np.float64]:
+test_kron = TestKron()
+if hybridize:
+test_kron.hybridize()
+a = rand_ndarray(shape=a_shape, dtype=dtype).as_np_ndarray()
+b = rand_ndarray(shape=b_shape, dtype=dtype).as_np_ndarray()
+a.attach_grad()
+b.attach_grad()
+
+np_out = _np.kron(a.asnumpy(), b.asnumpy())
+with mx.autograd.record():
+mx_out = test_kron(a, b)
+assert mx_out.shape == np_out.shape
+assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, 
atol=1e-5)
+mx_out.backward()
+
+# Test imperative once again
+mx_out = np.kron(a, b)
+np_out = _np.kron(a.asnumpy(), b.asnumpy())
+assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, 
atol=1e-5)
+
+# test numeric gradient
+if (len(a_shape) > 0 and len(b_shape) > 0 and 
_np.prod(a_shape) > 0 
+  and _np.prod(b_shape) > 0):
+a_sym = mx.sym.Variable("a").as_np_ndarray()
+b_sym = mx.sym.Variable("b").as_np_ndarray()
+mx_sym = mx.sym.np.kron(a_sym, b_sym).as_nd_ndarray()
+check_numeric_gradient(mx_sym, [a.as_nd_ndarray(), 
b.as_nd_ndarray()],
+  rtol=1e-1, atol=1e-1, dtype=dtype)
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] ckt624 commented on a change in pull request #15893: Numpy kron operator

2019-08-15 Thread GitBox
ckt624 commented on a change in pull request #15893: Numpy kron operator
URL: https://github.com/apache/incubator-mxnet/pull/15893#discussion_r314198695
 
 

 ##
 File path: src/operator/numpy/np_kron.cc
 ##
 @@ -0,0 +1,98 @@
+/*
+ * 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_kron.cc
+ * \brief CPU Implementation of numpy-compatible kron
+ */
+
+#include "./np_kron-inl.h"
+
+namespace mxnet {
+namespace op {
+
+inline bool KronOpShape(const nnvm::NodeAttrs& attrs,
+  mxnet::ShapeVector *in_attrs,
+  mxnet::ShapeVector *out_attrs) {
+  CHECK_EQ(in_attrs->size(), 2U);
+  CHECK_EQ(out_attrs->size(), 1U);
+
+  const mxnet::TShape& a_shape = in_attrs->at(0);
+  const mxnet::TShape& b_shape = in_attrs->at(1);
+
+  if (!ndim_is_known(a_shape) || !ndim_is_known(b_shape)) {
+return false;
+  }
+
+  CHECK_LE(b_shape.ndim(), 1)
+<< "b should be a vector or scalar.";
+
+  mxnet::TShape out_shape(std::max(a_shape.ndim(), b_shape.ndim()), -1);
+  if (a_shape.ndim() > b_shape.ndim()) {
+for (int i = 0; i < a_shape.ndim() - b_shape.ndim(); i++) {
+  out_shape[i] = a_shape[i];
+}
+for (int i = a_shape.ndim() - b_shape.ndim(); i < a_shape.ndim(); i++) {
+  out_shape[i] = a_shape[i] * b_shape[i - a_shape.ndim() + b_shape.ndim()];
+}
+  } else {
+for (int i = 0; i < b_shape.ndim() - a_shape.ndim(); i++) {
+  out_shape[i] = b_shape[i];
+}
+for (int i = b_shape.ndim() - a_shape.ndim(); i < b_shape.ndim(); i++) {
+  out_shape[i] = b_shape[i] * a_shape[i - b_shape.ndim() + a_shape.ndim()];
+}
+  }
+
+  SHAPE_ASSIGN_CHECK(*out_attrs, 0, out_shape);
+
+  return shape_is_known(*in_attrs) && shape_is_known(*out_attrs);
+}
+
+NNVM_REGISTER_OP(_np_kron)
+.add_alias("_npi_kron")
 
 Review comment:
   Fixed. Thx.


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:
us...@infra.apache.org


With regards,
Apache Git Services