|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file dnnl_binary.cc |
| 22 | + * \author: Adam Grabowski, adam.grabowski@intel.com |
| 23 | + */ |
| 24 | + |
| 25 | +#if MXNET_USE_ONEDNN == 1 |
| 26 | +#include "./dnnl_binary-inl.h" |
| 27 | + |
| 28 | +namespace mxnet { |
| 29 | +namespace op { |
| 30 | + |
| 31 | +DNNLBinaryOpFwd::DNNLBinaryOpFwd(const dnnl::algorithm alg, |
| 32 | + const std::vector<NDArray>& inputs, |
| 33 | + const std::vector<NDArray>& outputs) { |
| 34 | + auto src0_desc = inputs[0].GetDNNLData()->get_desc(); |
| 35 | + auto src1_desc = inputs[1].GetDNNLData()->get_desc(); |
| 36 | + auto dst_desc = outputs[0].GetDNNLData()->get_desc(); |
| 37 | + |
| 38 | + dnnl::binary::desc fwd_desc(alg, src0_desc, src1_desc, dst_desc); |
| 39 | + fwd_pd = std::make_shared<binary_fwd_pd_t>(fwd_desc, mxnet::CpuEngine::Get()->get_engine()); |
| 40 | + fwd = std::make_shared<binary_fwd_t>(*fwd_pd); |
| 41 | +} |
| 42 | + |
| 43 | +void DNNLBinaryOpFwd::Execute(const std::vector<NDArray>& inputs, |
| 44 | + const std::vector<OpReqType>& req, |
| 45 | + const std::vector<NDArray>& outputs) { |
| 46 | + auto engine = mxnet::CpuEngine::Get()->get_engine(); |
| 47 | + auto src0 = inputs[0].GetDNNLData(); |
| 48 | + auto src1 = inputs[1].GetDNNLData(); |
| 49 | + dnnl_output_t out_mem; |
| 50 | + if (outputs[0].GetDNNLData()->get_data_handle() == inputs[1].GetDNNLData()->get_data_handle()) |
| 51 | + out_mem = CreateDNNLMem(outputs[0], fwd_pd->dst_desc(), req[0], &inputs[1]); |
| 52 | + else |
| 53 | + out_mem = CreateDNNLMem(outputs[0], fwd_pd->dst_desc(), req[0], &inputs[0]); |
| 54 | + |
| 55 | + dnnl_args_map_t args = { |
| 56 | + {DNNL_ARG_SRC_0, *src0}, |
| 57 | + {DNNL_ARG_SRC_1, *src1}, |
| 58 | + {DNNL_ARG_DST, *out_mem.second}, |
| 59 | + }; |
| 60 | + |
| 61 | + DNNLStream::Get()->RegisterPrimArgs(*fwd, args); |
| 62 | + CommitOutput(outputs[0], out_mem); |
| 63 | + DNNLStream::Get()->Submit(); |
| 64 | +} |
| 65 | + |
| 66 | +bool SupportDNNLBinary(const std::vector<NDArray>& inputs) { |
| 67 | + auto dtype = inputs[0].dtype(); |
| 68 | + auto ndim_0 = inputs[0].shape().ndim(); |
| 69 | + auto ndim_1 = inputs[1].shape().ndim(); |
| 70 | + return ndim_0 >= 1 && ndim_0 <= 6 && ndim_1 >= 1 && ndim_1 <= 6 && |
| 71 | + inputs[0].shape().Size() != 0 && inputs[1].shape().Size() != 0 && |
| 72 | + dtype == mshadow::kFloat32 && dtype == inputs[1].dtype(); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace op |
| 76 | +} // namespace mxnet |
| 77 | + |
| 78 | +#endif // MXNET_USE_ONEDNN == 1 |
0 commit comments