bgawrych commented on a change in pull request #20713:
URL: https://github.com/apache/incubator-mxnet/pull/20713#discussion_r763899696
##########
File path: tests/python/unittest/test_operator.py
##########
@@ -1915,7 +1915,11 @@ def gen_broadcast_data(idx):
[[1, 1, 65, 2, 22], [1, 1, 65, 1, 1]],
[[1, 24, 103, 17, 18], [1, 24, 1, 1, 1]],
[[1, 1, 1, 1, 2], [1, 24, 194, 50, 1]],
- [[1, 1, 107, 84, 9], [1, 1, 1, 1, 1]]])
+ [[1, 1, 107, 84, 9], [1, 1, 1, 1, 1]],
+ [[8, 1, 6, 1], [7, 1, 5]], [[5, 4], [1]],
+ [[256, 256, 3], [3]], [[5, 4], [4]],
+ [[15, 3, 5], [3, 5]], [[15, 3, 5], [1, 5]],
+ [[15, 3, 5], [3, 1]]])
Review comment:
maybe some shapes with zeros?
##########
File path: src/operator/tensor/elemwise_binary_broadcast_op_basic.cc
##########
@@ -24,9 +24,76 @@
#include "./elemwise_unary_op.h"
#include "./elemwise_binary_op-inl.h"
#include "./elemwise_binary_broadcast_op.h"
+#if MXNET_USE_ONEDNN == 1
+#include "../nn/dnnl/dnnl_binary-inl.h"
+#endif // MXNET_USE_ONEDNN == 1
namespace mxnet {
namespace op {
+
+#if MXNET_USE_ONEDNN == 1
+template <dnnl::algorithm alg>
+void DNNLBinaryOpForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<NDArray>& outputs) {
+ mxnet::TShape new_lshape, new_rshape, new_oshape;
+ int ndim = BinaryBroadcastShapeCompact(inputs[0].shape(),
+ inputs[1].shape(),
+ outputs[0].shape(),
+ &new_lshape,
+ &new_rshape,
+ &new_oshape);
+ std::vector<NDArray> new_inputs;
+ std::vector<NDArray> new_outputs;
+ if (ndim) {
+ new_inputs = {inputs[0].Reshape(new_lshape),
inputs[1].Reshape(new_rshape)};
+ new_outputs = {outputs[0].Reshape(new_oshape)};
+ } else if (inputs[0].shape().Size() == 1 && inputs[0].shape().Size() == 1) {
Review comment:
check condition
##########
File path: src/operator/nn/dnnl/dnnl_binary.cc
##########
@@ -0,0 +1,77 @@
+/*
+ * 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_binary.cc
+ * \author: Adam Grabowski, [email protected]
+ */
+
+#if MXNET_USE_ONEDNN == 1
+#include "./dnnl_binary-inl.h"
+
+namespace mxnet {
+namespace op {
+
+DNNLBinaryOpFwd::DNNLBinaryOpFwd(const dnnl::algorithm alg,
+ const nnvm::NodeAttrs& attrs,
+ const std::vector<NDArray>& inputs,
+ const std::vector<NDArray>& outputs) {
+ auto src0_desc = inputs[0].GetDNNLData()->get_desc();
+ auto src1_desc = inputs[1].GetDNNLData()->get_desc();
+ auto dst_desc = outputs[0].GetDNNLData()->get_desc();
+
+ dnnl::binary::desc fwd_desc(alg, src0_desc, src1_desc, dst_desc);
+ fwd_pd = std::make_shared<binary_op_fwd_pd_t>(fwd_desc,
mxnet::CpuEngine::Get()->get_engine());
+ fwd = std::make_shared<binary_op_fwd_t>(*fwd_pd);
+}
+
+void DNNLBinaryOpFwd::Execute(const std::vector<NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<NDArray>& outputs) {
+ auto engine = mxnet::CpuEngine::Get()->get_engine();
+ auto src0 =
+ dnnl::memory(fwd_pd->src0_desc(), engine,
reinterpret_cast<void*>(inputs[0].data().dptr_));
Review comment:
Can't you just use GetDNNLData() here instead of creating memory by hand?
##########
File path: tests/python/unittest/test_operator.py
##########
@@ -2012,10 +2016,16 @@ def reduce_op(shape, x):
if shape == x.shape:
return x
keepdims_shape = list(x.shape)
+ #calculate difference between output and input ndims
Review comment:
add space after #
##########
File path: src/operator/tensor/elemwise_binary_broadcast_op_basic.cc
##########
@@ -24,9 +24,76 @@
#include "./elemwise_unary_op.h"
#include "./elemwise_binary_op-inl.h"
#include "./elemwise_binary_broadcast_op.h"
+#if MXNET_USE_ONEDNN == 1
+#include "../nn/dnnl/dnnl_binary-inl.h"
+#endif // MXNET_USE_ONEDNN == 1
namespace mxnet {
namespace op {
+
+#if MXNET_USE_ONEDNN == 1
+template <dnnl::algorithm alg>
+void DNNLBinaryOpForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<NDArray>& outputs) {
+ mxnet::TShape new_lshape, new_rshape, new_oshape;
+ int ndim = BinaryBroadcastShapeCompact(inputs[0].shape(),
+ inputs[1].shape(),
+ outputs[0].shape(),
+ &new_lshape,
+ &new_rshape,
+ &new_oshape);
+ std::vector<NDArray> new_inputs;
+ std::vector<NDArray> new_outputs;
+ if (ndim) {
+ new_inputs = {inputs[0].Reshape(new_lshape),
inputs[1].Reshape(new_rshape)};
+ new_outputs = {outputs[0].Reshape(new_oshape)};
+ } else if (inputs[0].shape().Size() == 1 && inputs[0].shape().Size() == 1) {
+ // BinaryBroadcastShapeCompact function doesn't reshape shape().Size() ==
1 tensors
+ // into shape (1). It is mandatory for oneDNN primitive to have this
reshape done.
+ mxnet::TShape one_shape = mxnet::TShape(1, 1);
+ new_inputs = {inputs[0].Reshape(one_shape),
inputs[1].Reshape(one_shape)};
+ new_outputs = {outputs[0].Reshape(one_shape)};
+ } else {
+ new_inputs = {inputs[0], inputs[1]};
+ new_outputs = {outputs[0]};
+ }
+
+ DNNLBinaryOpFwd& fwd = DNNLBinaryOpFwd::GetBinaryOpForward<alg>(attrs,
new_inputs, new_outputs);
+ fwd.Execute(new_inputs, req, new_outputs);
+}
+#endif
+
+template <typename OP>
+static void BinaryOperatorComputeExCPU(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<NDArray>& outputs) {
+#if MXNET_USE_ONEDNN == 1
+ if (common::ContainsOnlyStorage(inputs, kDefaultStorage)) {
+ if (SupportDNNLBinary(inputs)) {
+ const dnnl::algorithm alg = GetDNNLAlgorithm<OP>::dnnl_alg;
Review comment:
GetDNNLAlgorithm => DNNLAlgorithm ?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]