ckt624 commented on a change in pull request #15349: Numpy Tensordot Operator 
URL: https://github.com/apache/incubator-mxnet/pull/15349#discussion_r302849369
 
 

 ##########
 File path: src/operator/numpy/np_tensordot_op-inl.h
 ##########
 @@ -0,0 +1,575 @@
+/*
+ * 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_tensordot_op-inl.h
+ * \brief CPU Implementation of numpy-compatible tensordot
+ */
+#ifndef MXNET_OPERATOR_NUMPY_NP_TENSORDOT_OP_INL_H_
+#define MXNET_OPERATOR_NUMPY_NP_TENSORDOT_OP_INL_H_
+
+#include <vector>
+#include "np_matrix_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+using namespace mxnet;
+using namespace mshadow;
+
+struct TensordotParam : public dmlc::Parameter<TensordotParam> {
+  mxnet::Tuple<int> a_axes_summed, b_axes_summed;
+  DMLC_DECLARE_PARAMETER(TensordotParam) {
+    DMLC_DECLARE_FIELD(a_axes_summed);
+    DMLC_DECLARE_FIELD(b_axes_summed);
+  }
+};
+
+/**
+ * Gets matrix dimensions of a and b after transpose and reshape.
+ */
+inline void GetMatrixDimensions(
+    int* ad1,
+    int* ad2,
+    int* bd1,
+    int* bd2,
+    const mxnet::Tuple<int>& a_axes_remained,
+    const mxnet::Tuple<int>& a_axes_summed,
+    const mxnet::Tuple<int>& b_axes_remained,
+    const mxnet::Tuple<int>& b_axes_summed,
+    const mxnet::TShape& a_shape,
+    const mxnet::TShape& b_shape) {
+  *ad1 = 1;
+  *ad2 = 1;
+  *bd1 = 1;
+  *bd2 = 1;
+
+  for (int i = 0; i < a_axes_remained.ndim(); i++) {
+    *ad1 *= a_shape[a_axes_remained[i]];
+  }
+  for (int i = 0; i < a_axes_summed.ndim(); i++) {
+    *ad2 *= a_shape[a_axes_summed[i]];
+  }
+  for (int i = 0; i < b_axes_summed.ndim(); i++) {
+    *bd1 *= b_shape[b_axes_summed[i]];
+  }
+  for (int i = 0; i < b_axes_remained.ndim(); i++) {
+    *bd2 *= b_shape[b_axes_remained[i]];
+  }
+}
+
+/**
+ * gets new axes of a and b after transpose and reshape.
+ */
+inline void GetReorderedAxes(
+    const mxnet::Tuple<int>& a_axes_summed,
+    mxnet::Tuple<int>* a_axes_remained,
+    mxnet::Tuple<int>* a_axes,
+    const mxnet::Tuple<int>& b_axes_summed,
+    mxnet::Tuple<int>* b_axes_remained,
+    mxnet::Tuple<int>* b_axes,
+    const mxnet::TShape& a_shape,
+    const mxnet::TShape& b_shape) {
+  std::vector<int> a_axes_remained_vector;
+  for (int i = 0; i < a_shape.ndim(); i++) {
+    a_axes_remained_vector.push_back(i);
+  }
+  for (auto& i : a_axes_summed) {
+    a_axes_remained_vector.erase(std::find(a_axes_remained_vector.begin(),
+      a_axes_remained_vector.end(), i));
+  }
+  *a_axes_remained = mxnet::Tuple<int>(a_axes_remained_vector);
+
+  std::vector<int> a_axes_vector(a_axes_remained_vector);
+  for (auto& i : a_axes_summed) {
+    a_axes_vector.push_back(i);
+  }
+  *a_axes = mxnet::Tuple<int>(a_axes_vector);
+
+  std::vector<int> b_axes_remained_vector;
+  for (int i = 0; i < b_shape.ndim(); i++) {
+    b_axes_remained_vector.push_back(i);
+  }
+  for (auto& i : b_axes_summed) {
+    b_axes_remained_vector.erase(std::find(b_axes_remained_vector.begin(),
+      b_axes_remained_vector.end(), i));
+  }
+  *b_axes_remained = mxnet::Tuple<int>(b_axes_remained_vector);
+
+  std::vector<int> b_axes_vector;
+  for (auto& i : b_axes_summed) {
+    b_axes_vector.push_back(i);
+  }
+  for (auto& i : b_axes_remained_vector) {
+    b_axes_vector.push_back(i);
+  }
+  *b_axes = mxnet::Tuple<int>(b_axes_vector);
+}
+
+/**
+ * gets shapes of a and b after transpose and reshape.
+ */
+inline mxnet::TShape GetReorderedShape(const mxnet::TShape& shape, const 
mxnet::Tuple<int>& axes) {
+  mxnet::TShape new_shape(shape);
+  for (int i = 0; i < axes.ndim(); i++) {
+    new_shape[i] = shape[axes[i]];
+  }
+  return new_shape;
+}
+
+/**
+ * gets matrix dot. Reshapes tensor a as ad1-by-ad2 matrix, tensor b as 
bd1-by-bd2 matrix, then 
+ * calculates matrix dot a * b and stores in tensor out.
+ */
+template<typename xpu>
+void MatrixDot(
+    const OpContext& ctx,
+    const TBlob& a,
+    const TBlob& b,
+    const TBlob& out,
+    const OpReqType req,
+    const int ad1,
+    const int ad2,
+    const int bd1,
+    const int bd2,
+    const bool aT = false,
+    const bool bT = false) {
+  using namespace mshadow;
+  using namespace mshadow_op;
+
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+
+  MSHADOW_REAL_TYPE_SWITCH(out.type_flag_, DType, {
+    Tensor<xpu, 2, DType> a_tensor = a.get_with_shape<xpu, 2, 
DType>(Shape2(ad1, ad2), s);
+    Tensor<xpu, 2, DType> b_tensor = b.get_with_shape<xpu, 2, 
DType>(Shape2(bd1, bd2), s);
+
+    if (aT && bT) {
+      CHECK_EQ(ad1, bd2);
+      Tensor<xpu, 2, DType> out_tensor = out.get_with_shape<xpu, 2, 
DType>(Shape2(ad2, bd1), s);
+      ASSIGN_DISPATCH(out_tensor, req, dot(a_tensor.T(), b_tensor.T()));
+    } else if (aT && !bT) {
+      CHECK_EQ(ad1, bd1);
+      Tensor<xpu, 2, DType> out_tensor = out.get_with_shape<xpu, 2, 
DType>(Shape2(ad2, bd2), s);
+      ASSIGN_DISPATCH(out_tensor, req, dot(a_tensor.T(), b_tensor));
+    } else if (!aT && bT) {
+      CHECK_EQ(ad2, bd2);
+      Tensor<xpu, 2, DType> out_tensor = out.get_with_shape<xpu, 2, 
DType>(Shape2(ad1, bd1), s);
+      ASSIGN_DISPATCH(out_tensor, req, dot(a_tensor, b_tensor.T()));
+    } else {
+      CHECK_EQ(ad2, bd1);
+      Tensor<xpu, 2, DType> out_tensor = out.get_with_shape<xpu, 2, 
DType>(Shape2(ad1, bd2), s);
+      ASSIGN_DISPATCH(out_tensor, req, dot(a_tensor, b_tensor));
+    }
+  });
+}
+
+/**
+ * Calculates tensordot.
+ */
+template<typename xpu>
+void TensordotImpl(
 
 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

Reply via email to