JiangZhaoh commented on a change in pull request #16990: [numpy] add op matmul
URL: https://github.com/apache/incubator-mxnet/pull/16990#discussion_r355237789
 
 

 ##########
 File path: src/operator/numpy/np_matmul_op-inl.h
 ##########
 @@ -0,0 +1,356 @@
+/*
+ * 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_matmul_op-inl.h
+ * \brief Function definition of matrix numpy-compatible matmul operator
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_NP_MATMUL_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_MATMUL_OP_INL_H_
+
+#include <mxnet/operator_util.h>
+#include <vector>
+#include <algorithm>
+#include <memory>
+#include "np_tensordot_op-inl.h"
+#include "np_dot-inl.h"
+
+namespace mxnet {
+namespace op {
+
+template<int ndim>
+mshadow::Shape<ndim> GetStride(mshadow::Shape<ndim> shape, size_t N) {
+  /*!
+   * \brief Calculate stride of each dim from shape 
+   */
+  mshadow::Shape<ndim>stride;
+  size_t tmp = 1;
+  for (int i = N - 1; i >= 0; --i) {
+    stride[i] = tmp;
+    tmp *= shape[i];
+  }
+  return stride;
+}
+
+template<int ndim>
+mshadow::Shape<ndim> GetKernelShape(const mxnet::TShape& shape,
+                                    size_t N, bool T = false) {
+  /*!
+   * \brief Get mshadow::Shape from mxnet::TShape. Extra dims is filled with 1.
+   * \param N - ndim of mshape::Shape shape.
+   * \param T - If T is True, transpose the last two axis, otherwise not.
+   */
+  mshadow::Shape<ndim>k_shape;
+  for (int i = shape.ndim() - 1, j = N - 1; i >= 0 || j >= 0 ; --i, --j) {
+    if (i >= 0) {
+      k_shape[j] = shape[i];
+    } else {
+      k_shape[j] = 1;
+    }
+  }
+  if (T) {  // transpose the latest two axes
+    size_t t = k_shape[N - 1];
+    k_shape[N - 1] = k_shape[N - 2];
+    k_shape[N - 2] = t;
+  }
+  return k_shape;
+}
+
+template<int ndim>
+mshadow::Shape<ndim> BroadcastKernelShape(mshadow::Shape<ndim> in_shape,
+                                          mshadow::Shape<ndim> broadcast_shape,
+                                          size_t N, size_t* size) {
+  /*!
+   * \brief Broadcast in_shape(ndim = N) to broadcast_shape(ndim = N) expect 
the last two axes.
+            Make sure that: If i < N - 2 and in_shape[i] != 
broadcast_shape[i], in_shape[i] == 1.
+   * \param N - ndim of both in_shape and broadcast_shape.
+   * \param size - The size of the broadcast_shape.
+   */
+  mshadow::Shape<ndim>out_shape(in_shape);
+  *size = 1;
+  for (size_t i = 0; i < N - 2; ++i) {
+    out_shape[i] = std::max(in_shape[i], broadcast_shape[i]);
+    *size *= out_shape[i];
+  }
+  *size *= (out_shape[N - 2] * out_shape[N - 1]);
+  return out_shape;
+}
+
+template<int req>
+struct NDMatmul {
+  /*!
+   * \brief matmul(a, b) in both N-D(N >= 2) case.
+            It is treated as a stack of matrices residing in the last two 
indexes and broadcast accordingly.
+   * \param out - output: insert 'value' to 'arr' according to 'index'.
+   * \param a - input: the first argument.
+   * \param b - input: the second argument.
+   * \param ndim - ndim of a, b and output. Because of broadcast, regard their 
ndim as equal.  
+   */
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, DType* out,
+                                  const DType* a, const DType* b,
+                                  const mshadow::Shape<10> a_stride,
+                                  const mshadow::Shape<10> b_stride,
+                                  const mshadow::Shape<10> out_stride,
+                                  const mshadow::Shape<10> a_shape,
+                                  const mshadow::Shape<10> b_shape,
+                                  const mshadow::Shape<10> out_shape,
+                                  const size_t ndim){
 
 Review comment:
   Thanks for your advice. But, may I ask how could I broadcast the shape if I 
don't store strides and shapes? 
   e.g. Matrix A in shape (2, 1, 3, 4, 5) and matrix B in shape (3, 1, 5, 2), 
C=np.matmul(A, B) would broadcast A and B to shape (2, 3, 3, 4, 5) and (3, 3, 
5, 2) respectively, and C's shape would be (2, 3, 3, 4, 2).
   If I didn't  store shape and stride, I think I should copy the content in 
each array to get the consistent shape firstly, and use your method then.  Is 
this your mean?

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

Reply via email to