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/06/24 12:56:09 UTC

[GitHub] [incubator-mxnet] bgawrych commented on a diff in pull request #20976: [FEATURE] Add _npi_power_scalar and _npi_multiply_scalar fuse

bgawrych commented on code in PR #20976:
URL: https://github.com/apache/incubator-mxnet/pull/20976#discussion_r906009360


##########
src/operator/subgraph/dnnl/dnnl_pow_mul_scalar.cc:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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_pow_mul_scalar.cc
+ * \brief DNNL pow_mul_scalar operator based on subgraph
+ */
+
+#if MXNET_USE_ONEDNN == 1
+
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "operator/nn/dnnl/dnnl_base-inl.h"
+#include "operator/nn/dnnl/dnnl_pow_mul_scalar-inl.h"
+#include "operator/subgraph/common.h"
+
+namespace mxnet {
+namespace op {
+bool DNNLPowMulScalarType(const nnvm::NodeAttrs& attrs,
+                          std::vector<int>* in_attrs,
+                          std::vector<int>* out_attrs) {
+  CHECK_EQ(in_attrs->size(), 1U);
+  CHECK_EQ(out_attrs->size(), 1U);
+  const DNNLPowMulScalarParam& param = nnvm::get<DNNLPowMulScalarParam>(attrs.parsed);
+  bool scalar_is_int                 = param.exp_is_int && param.mul_is_int;
+  if (common::is_int(in_attrs->at(0)) && !scalar_is_int) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, mshadow::kFloat64);
+  } else if (in_attrs->at(0) == mshadow::kBool) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, scalar_is_int ? mshadow::kInt64 : mshadow::kFloat64);
+  } else {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, in_attrs->at(0));
+  }
+  return out_attrs->at(0) != -1;
+}
+
+inline static bool DNNLPowMulScalarStorageType(const nnvm::NodeAttrs& attrs,
+                                               const int dev_mask,
+                                               DispatchMode* dispatch_mode,
+                                               std::vector<int>* in_attrs,
+                                               std::vector<int>* out_attrs) {
+  return DNNLStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs, out_attrs);
+}
+
+template <typename OP>
+static void ComputeOP(const nnvm::NodeAttrs& attrs,
+                      const OpContext& ctx,
+                      mshadow::Stream<cpu>* s,
+                      const TBlob& input,
+                      const TBlob& output,
+                      const double scalar) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  MSHADOW_TYPE_SWITCH(output.type_flag_, DType, {
+    auto temp_req    = input.dptr_ == output.dptr_ ? kWriteInplace : kWriteTo;
+    TBlob temp_tblob = input;
+    if (input.type_flag_ != output.type_flag_) {
+      temp_tblob = TBlob(ctx.requested[0].get_space_typed<cpu, 1, DType>(Shape1(output.Size()), s));
+      CastCompute<cpu>(attrs, ctx, {input}, {kWriteTo}, {temp_tblob});
+    }
+    MXNET_ASSIGN_REQ_SWITCH(temp_req, Req, {
+      mxnet_op::Kernel<mxnet_op::op_with_req<OP, Req>, cpu>::Launch(
+          s, input.Size(), output.dptr<DType>(), temp_tblob.dptr<DType>(), DType(scalar));
+    });
+  });
+}
+
+static void PowMulScalarCompute(const nnvm::NodeAttrs& attrs,
+                                const OpContext& ctx,
+                                const std::vector<TBlob>& inputs,
+                                const std::vector<OpReqType>& req,
+                                const std::vector<TBlob>& outputs) {
+  mshadow::Stream<cpu>* s = ctx.get_stream<cpu>();
+  DCHECK_EQ(inputs.size(), 1);
+  DCHECK_EQ(outputs.size(), 1);
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  const DNNLPowMulScalarParam& param = nnvm::get<DNNLPowMulScalarParam>(attrs.parsed);
+  // temp_mid_tblob is output of power operation and input of multiplication.
+  // Its dtype depends on input dtype and scalar type.
+  TBlob temp_mid_tblob =
+      ((common::is_int(inputs[0].type_flag_) || inputs[0].type_flag_ == kBool) &&
+       !param.exp_is_int) ?
+          outputs[0] :
+          inputs[0].type_flag_ == kBool ?
+          TBlob(ctx.requested[0].get_space_typed<cpu, 1, int64_t>(Shape1(inputs[0].Size()), s)) :
+          inputs[0];

Review Comment:
   please use regular if else statements, it is not readable at all



##########
src/operator/nn/dnnl/dnnl_pow_mul_scalar.cc:
##########
@@ -18,49 +18,59 @@
  */
 
 /*!
- * \file dnnl_power_scalar.cc
- * \author: Adam Grabowski, adam.grabowski@intel.com
+ * \file dnnl_pow_mul_scalar.cc
  */
 
 #if MXNET_USE_ONEDNN == 1
 
-#include "dnnl_power_scalar-inl.h"
+#include "dnnl_pow_mul_scalar-inl.h"
 
 namespace mxnet {
 namespace op {
 
-DNNLPowerFwd& DNNLPowerFwd::GetPowerForward(const nnvm::NodeAttrs& attrs,
-                                            const NDArray& input,
-                                            const NDArray& output) {
-  const NumpyBinaryScalarParam& param = nnvm::get<NumpyBinaryScalarParam>(attrs.parsed);
+bool SupportDNNLPower(const NDArray& input) {
+  return input.shape().Size() != 0 && input.shape().ndim() > 0 && input.shape().ndim() <= 6 &&
+         input.dtype() == mshadow::kFloat32;
+}

Review Comment:
   rebase and apply refactored SupportDNNL functions



##########
src/operator/subgraph/dnnl/dnnl_pow_mul_scalar.cc:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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_pow_mul_scalar.cc
+ * \brief DNNL pow_mul_scalar operator based on subgraph
+ */
+
+#if MXNET_USE_ONEDNN == 1
+
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "operator/nn/dnnl/dnnl_base-inl.h"
+#include "operator/nn/dnnl/dnnl_pow_mul_scalar-inl.h"
+#include "operator/subgraph/common.h"
+
+namespace mxnet {
+namespace op {
+bool DNNLPowMulScalarType(const nnvm::NodeAttrs& attrs,
+                          std::vector<int>* in_attrs,
+                          std::vector<int>* out_attrs) {
+  CHECK_EQ(in_attrs->size(), 1U);
+  CHECK_EQ(out_attrs->size(), 1U);
+  const DNNLPowMulScalarParam& param = nnvm::get<DNNLPowMulScalarParam>(attrs.parsed);
+  bool scalar_is_int                 = param.exp_is_int && param.mul_is_int;
+  if (common::is_int(in_attrs->at(0)) && !scalar_is_int) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, mshadow::kFloat64);
+  } else if (in_attrs->at(0) == mshadow::kBool) {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, scalar_is_int ? mshadow::kInt64 : mshadow::kFloat64);
+  } else {
+    TYPE_ASSIGN_CHECK(*out_attrs, 0, in_attrs->at(0));
+  }
+  return out_attrs->at(0) != -1;
+}
+
+inline static bool DNNLPowMulScalarStorageType(const nnvm::NodeAttrs& attrs,
+                                               const int dev_mask,
+                                               DispatchMode* dispatch_mode,
+                                               std::vector<int>* in_attrs,
+                                               std::vector<int>* out_attrs) {
+  return DNNLStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs, out_attrs);
+}
+
+template <typename OP>
+static void ComputeOP(const nnvm::NodeAttrs& attrs,
+                      const OpContext& ctx,
+                      mshadow::Stream<cpu>* s,
+                      const TBlob& input,
+                      const TBlob& output,
+                      const double scalar) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  MSHADOW_TYPE_SWITCH(output.type_flag_, DType, {
+    auto temp_req    = input.dptr_ == output.dptr_ ? kWriteInplace : kWriteTo;
+    TBlob temp_tblob = input;
+    if (input.type_flag_ != output.type_flag_) {
+      temp_tblob = TBlob(ctx.requested[0].get_space_typed<cpu, 1, DType>(Shape1(output.Size()), s));
+      CastCompute<cpu>(attrs, ctx, {input}, {kWriteTo}, {temp_tblob});
+    }
+    MXNET_ASSIGN_REQ_SWITCH(temp_req, Req, {
+      mxnet_op::Kernel<mxnet_op::op_with_req<OP, Req>, cpu>::Launch(
+          s, input.Size(), output.dptr<DType>(), temp_tblob.dptr<DType>(), DType(scalar));
+    });
+  });
+}
+
+static void PowMulScalarCompute(const nnvm::NodeAttrs& attrs,
+                                const OpContext& ctx,
+                                const std::vector<TBlob>& inputs,
+                                const std::vector<OpReqType>& req,
+                                const std::vector<TBlob>& outputs) {
+  mshadow::Stream<cpu>* s = ctx.get_stream<cpu>();
+  DCHECK_EQ(inputs.size(), 1);
+  DCHECK_EQ(outputs.size(), 1);
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  const DNNLPowMulScalarParam& param = nnvm::get<DNNLPowMulScalarParam>(attrs.parsed);
+  // temp_mid_tblob is output of power operation and input of multiplication.
+  // Its dtype depends on input dtype and scalar type.
+  TBlob temp_mid_tblob =
+      ((common::is_int(inputs[0].type_flag_) || inputs[0].type_flag_ == kBool) &&
+       !param.exp_is_int) ?
+          outputs[0] :
+          inputs[0].type_flag_ == kBool ?
+          TBlob(ctx.requested[0].get_space_typed<cpu, 1, int64_t>(Shape1(inputs[0].Size()), s)) :
+          inputs[0];

Review Comment:
   also are you sure that inputs[0] should be here as temp_mid_tblob?



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