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 2022/03/15 11:31:51 UTC

[GitHub] [incubator-mxnet] bartekkuncer commented on a change in pull request #20911: [operator] Integrate oneDNN matmul primitive to mxnet dot operator

bartekkuncer commented on a change in pull request #20911:
URL: https://github.com/apache/incubator-mxnet/pull/20911#discussion_r826863212



##########
File path: src/operator/nn/dnnl/dnnl_dot.cc
##########
@@ -0,0 +1,145 @@
+/*
+ * 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 dnnl_dot.cc
+ */
+
+#if MXNET_USE_ONEDNN == 1
+
+#include "dnnl_dot-inl.h"
+
+namespace mxnet {
+namespace op {
+
+bool SupportDNNLDot(const std::vector<NDArray>& inputs, const NDArray& output) {
+#if MXNET_USE_BLAS_MKL == 1
+  return false;
+#endif
+  return inputs[DotIn::lhs].shape().Size() > 1 && inputs[DotIn::rhs].shape().Size() > 1 &&
+         inputs[DotIn::lhs].shape().ndim() > 0 && inputs[DotIn::rhs].shape().ndim() > 0 &&
+         output.shape().Size() != 0 && output.shape().ndim() > 0 && output.shape().ndim() <= 12 &&
+         (inputs[DotIn::lhs].dtype() == mshadow::kFloat32 ||
+          inputs[DotIn::lhs].dtype() == mshadow::kBfloat16);
+}
+
+DNNLDotFwd& DNNLDotFwd::GetCached(const DotParam& param,
+                                  const std::vector<NDArray>& inputs,
+                                  const std::vector<NDArray>& outputs,
+                                  const bool isNumpy) {
+  using dot_fwd_map = std::unordered_map<DotSignature, DNNLDotFwd, OpHash>;
+#if DMLC_CXX11_THREAD_LOCAL
+  static thread_local dot_fwd_map fwds;
+#else
+  static MX_THREAD_LOCAL dot_fwd_map fwds;
+#endif
+
+  DotSignature key(param);
+  key.AddSign(inputs[DotIn::lhs]);
+  key.AddSign(inputs[DotIn::rhs]);
+  key.AddSign(outputs[DotOut::out]);
+  key.AddSign(static_cast<int>(isNumpy));
+
+  auto it = fwds.find(key);
+  if (it == fwds.end()) {
+    const DNNLDotFwd fwd(param, inputs, outputs, isNumpy);
+    it = AddToCache(&fwds, key, fwd);
+  }
+  return it->second;
+}
+
+auto GetMemoryDesc(const NDArray& tensor, int firstDim, int secondDim, const bool transpose) {
+  return dnnl::memory::desc(
+      dnnl::memory::dims{firstDim, secondDim},
+      get_dnnl_type(tensor.dtype()),
+      transpose ? dnnl::memory::format_tag::ba : dnnl::memory::format_tag::ab);
+}
+
+DNNLDotFwd::DNNLDotFwd(const DotParam& param,
+                       const std::vector<NDArray>& inputs,
+                       const std::vector<NDArray>& outputs,
+                       const bool isNumpy) {
+  auto shapeLhs = inputs[DotIn::lhs].shape(), shapeRhs = inputs[DotIn::rhs].shape();
+  auto ndimLhs = shapeLhs.ndim(), ndimRhs = shapeRhs.ndim();
+  dnnl::memory::desc lhs_md, rhs_md, out_md;
+  // NumPy expects more than 2 dimensional rhs tensor as Ax...xKxN which is different than NDArray's
+  // KxAx...xN format. For NumPy shape in rhs memory descriptor is going to be Kx(A*...*N),
+  // similarly to NDArray, but for it to match the actual data there will be an additional reorder

Review comment:
       ```suggestion
     // similarly to NDArray, but for it to match the actual data there is an additional reorder
   ```




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

To unsubscribe, e-mail: commits-unsubscribe@mxnet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org