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

 ##########
 File path: src/operator/numpy/np_tensordot_op.cc
 ##########
 @@ -0,0 +1,344 @@
+/*
+ * 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.cc
+ * \brief CPU Implementation of numpy-compatible tensordot
+ */
+
+#include <string>
+#include "np_tensordot_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+bool TensordotOpShape(
+    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;
+  }
+
+  if ((a_shape.ndim() < 1) || (b_shape.ndim() < 1)) {
+    return false;
+  }
+
+  const TensordotParam& param = nnvm::get<TensordotParam>(attrs.parsed);
+  const Tuple<int>& a_axes_summed = param.a_axes_summed;
+  const Tuple<int>& b_axes_summed = param.b_axes_summed;
+
+  Tuple<int> a_axes_remained;
+  Tuple<int> b_axes_remained;
+  Tuple<int> a_axes;
+  Tuple<int> b_axes;
+  GetReorderedAxes(a_axes_summed, &a_axes_remained, &a_axes, b_axes_summed, 
&b_axes_remained,
+                   &b_axes, a_shape, b_shape);
+
+  CHECK_EQ(a_axes_summed.ndim(), b_axes_summed.ndim());
+
+  mxnet::TShape out_shape(a_axes_remained.ndim() + b_axes_remained.ndim(), -1);
+  for (int i = 0; i < a_axes_remained.ndim(); i++) {
+    out_shape[i] = a_shape[a_axes_remained[i]];
+  }
+  for (int i = 0; i < b_axes_remained.ndim(); i++) {
+    out_shape[a_axes_remained.ndim() + i] = b_shape[b_axes_remained[i]];
+  }
+  SHAPE_ASSIGN_CHECK(*out_attrs, 0, out_shape);
+
+  mxnet::TShape tem_shape1(a_axes.ndim(), -1);
+  for (int i = 0; i < a_axes_remained.ndim(); i++) {
+    tem_shape1[a_axes_remained[i]] = out_shape[i];
+  }
+  for (int i = 0; i < a_axes_summed.ndim(); i++) {
+    tem_shape1[a_axes_summed[i]] = b_shape[b_axes_summed[i]];
+  }
+  SHAPE_ASSIGN_CHECK(*in_attrs, 0, tem_shape1);
+
+  mxnet::TShape tem_shape2(b_axes.ndim(), -1);
+  for (int i = 0; i < b_axes_remained.ndim(); i++) {
+    tem_shape2[b_axes_remained[i]] = out_shape[a_axes_remained.ndim() + i];
+  }
+  for (int i = 0; i < b_axes_summed.ndim(); i++) {
+    tem_shape2[b_axes_summed[i]] = a_shape[a_axes_summed[i]];
+  }
+  SHAPE_ASSIGN_CHECK(*in_attrs, 1, tem_shape2);
+
+  return shape_is_known(*in_attrs) && shape_is_known(*out_attrs);
+}
+
+DMLC_REGISTER_PARAMETER(TensordotParam);
+
+NNVM_REGISTER_OP(_npi_tensordot)
+.describe(R"code(tensordot(a, b, axes=2)
+
+    Compute tensor dot product along specified axes for arrays >= 1-D.
+
+    Given two tensors (arrays of dimension greater than or equal to one),
+    `a` and `b`, and an array_like object containing two array_like
+    objects, ``(a_axes, b_axes)``, sum the products of `a`'s and `b`'s
+    elements (components) over the axes specified by ``a_axes`` and
+    ``b_axes``. The third argument can be a single non-negative
+    integer_like scalar, ``N``; if it is such, then the last ``N``
+    dimensions of `a` and the first ``N`` dimensions of `b` are summed
+    over.
+
+    Parameters
+    ----------
+    a, b : ndarray, len(shape) >= 1
+        Tensors to "dot".
+
 
 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