PawelGlomski-Intel commented on a change in pull request #20981:
URL: https://github.com/apache/incubator-mxnet/pull/20981#discussion_r837304928
##########
File path: src/operator/tensor/elemwise_unary_op.h
##########
@@ -457,30 +457,67 @@ class UnaryOp : public OpBase {
};
#if MXNET_USE_ONEDNN == 1
-inline bool TanhStorageType(const nnvm::NodeAttrs& attrs,
- const int dev_mask,
- DispatchMode* dispatch_mode,
- std::vector<int>* in_attrs,
- std::vector<int>* out_attrs) {
+inline bool EltwiseStorageType(const nnvm::NodeAttrs& attrs,
+ const int dev_mask,
+ DispatchMode* dispatch_mode,
+ std::vector<int>* in_attrs,
+ std::vector<int>* out_attrs) {
CHECK_EQ(in_attrs->size(), 1);
CHECK_EQ(out_attrs->size(), 1);
return DNNLStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs,
out_attrs);
}
-inline void TanhComputeExCPU(const nnvm::NodeAttrs& attrs,
- const OpContext& ctx,
- const std::vector<mxnet::NDArray>& inputs,
- const std::vector<OpReqType>& req,
- const std::vector<mxnet::NDArray>& outputs) {
- if (SupportDNNLTanh(inputs[0], outputs[0])) {
+template <typename OP>
+struct DNNLAlgorithm {};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::plus> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_add;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::minus> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_sub;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::mul> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_mul;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::div> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_div;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::tanh> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_tanh;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::exp> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_exp;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::square> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_square;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::square_root> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_sqrt;
+};
Review comment:
I believe we can turn `DNNLAlgorithm` into a constexpr function which
would be more readable imo.
##########
File path: src/operator/tensor/elemwise_unary_op.h
##########
@@ -457,30 +457,67 @@ class UnaryOp : public OpBase {
};
#if MXNET_USE_ONEDNN == 1
-inline bool TanhStorageType(const nnvm::NodeAttrs& attrs,
- const int dev_mask,
- DispatchMode* dispatch_mode,
- std::vector<int>* in_attrs,
- std::vector<int>* out_attrs) {
+inline bool EltwiseStorageType(const nnvm::NodeAttrs& attrs,
+ const int dev_mask,
+ DispatchMode* dispatch_mode,
+ std::vector<int>* in_attrs,
+ std::vector<int>* out_attrs) {
CHECK_EQ(in_attrs->size(), 1);
CHECK_EQ(out_attrs->size(), 1);
return DNNLStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs,
out_attrs);
}
-inline void TanhComputeExCPU(const nnvm::NodeAttrs& attrs,
- const OpContext& ctx,
- const std::vector<mxnet::NDArray>& inputs,
- const std::vector<OpReqType>& req,
- const std::vector<mxnet::NDArray>& outputs) {
- if (SupportDNNLTanh(inputs[0], outputs[0])) {
+template <typename OP>
+struct DNNLAlgorithm {};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::plus> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_add;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::minus> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_sub;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::mul> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_mul;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::div> {
+ static const dnnl::algorithm value = dnnl::algorithm::binary_div;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::tanh> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_tanh;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::exp> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_exp;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::square> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_square;
+};
+template <>
+struct DNNLAlgorithm<op::mshadow_op::square_root> {
+ static const dnnl::algorithm value = dnnl::algorithm::eltwise_sqrt;
+};
+
+template <typename OP, bool computeMixed = true>
+inline void EltwiseComputeExCPU(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<mxnet::NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<mxnet::NDArray>& outputs) {
+ auto fallBackFunction =
+ computeMixed ? UnaryOp::ComputeMixedType<cpu, OP> :
UnaryOp::Compute<cpu, OP>;
+ if (SupportDNNLEltwise(inputs[0], outputs[0])) {
DNNL_OPCHECK_INIT(false, outputs.size(), inputs, outputs);
- DNNLRun(DNNLTanhForward, attrs, ctx, inputs[0], req[0], outputs[0]);
- DNNL_OPCHECK_RUN(
- (UnaryOp::ComputeMixedType<cpu, mshadow_op::tanh>), attrs, ctx,
inputs, req, outputs);
+ DNNLRun(
+ DNNLEltwiseForward<DNNLAlgorithm<OP>::value>, attrs, ctx, inputs[0],
req[0], outputs[0]);
+ DNNL_OPCHECK_RUN(fallBackFunction, attrs, ctx, inputs, req, outputs);
} else {
- FallBackCompute(
- UnaryOp::ComputeMixedType<cpu, mshadow_op::tanh>, attrs, ctx, inputs,
req, outputs);
+ FallBackCompute(fallBackFunction, attrs, ctx, inputs, req, outputs);
Review comment:
```suggestion
if constexpr (computeMixed) {
FallBackCompute(UnaryOp::ComputeMixedType<cpu, OP>, attrs, ctx,
inputs, req, outputs);
} else {
FallBackCompute(UnaryOp::Compute<cpu, OP>, attrs, ctx, inputs, req,
outputs);
}
```
##########
File path: src/operator/nn/dnnl/dnnl_eltwise-inl.h
##########
@@ -50,17 +52,18 @@ class DNNLTanhFwd {
std::shared_ptr<eltwise_fwd_pd_t> fwd_pd;
};
-inline void DNNLTanhForward(const nnvm::NodeAttrs& attrs,
- const OpContext& ctx,
- const NDArray& input,
- const OpReqType& req,
- const NDArray& output) {
- DNNLTanhFwd& fwd = DNNLTanhFwd::GetTanhForward(input, output);
+template <dnnl::algorithm algorithm>
+inline void DNNLEltwiseForward(const nnvm::NodeAttrs& attrs,
Review comment:
Any reason why this is a template?
--
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]