|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <mkldnn/include/mkldnn.hpp> |
| 16 | +#include "paddle/fluid/operators/elementwise/elementwise_op.h" |
| 17 | +#include "paddle/fluid/operators/elementwise/elementwise_op_function.h" |
| 18 | + |
| 19 | +#include "paddle/fluid/platform/mkldnn_helper.h" |
| 20 | + |
| 21 | +#include "paddle/fluid/operators/math/jit_kernel.h" |
| 22 | +#include "xbyak.h" |
| 23 | +#include "xbyak_util.h" |
| 24 | + |
| 25 | +namespace paddle { |
| 26 | +namespace operators { |
| 27 | + |
| 28 | +using framework::DataLayout; |
| 29 | +using mkldnn::memory; |
| 30 | + |
| 31 | +static mkldnn::memory::format StringToMKLDNNFormat(std::string& format) { |
| 32 | + std::transform(format.begin(), format.end(), format.begin(), ::tolower); |
| 33 | + |
| 34 | + if (!format.compare("nchw")) { |
| 35 | + return memory::format::nchw; |
| 36 | + } else if (!format.compare("nchw16c")) { |
| 37 | + return memory::format::nChw16c; |
| 38 | + } else if (!format.compare("nchw8c")) { |
| 39 | + return memory::format::nChw8c; |
| 40 | + } else if (!format.compare("nhwc")) { |
| 41 | + return memory::format::nhwc; |
| 42 | + } else { |
| 43 | + return memory::format::any; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +static void UpdateDataFormat(const framework::ExecutionContext& ctx, |
| 48 | + framework::Tensor* tensor, const char* attribute) { |
| 49 | + if (ctx.op().HasAttr(attribute)) { |
| 50 | + auto format_as_string = ctx.Attr<std::string>(attribute); |
| 51 | + auto format = StringToMKLDNNFormat(format_as_string); |
| 52 | + if (format != memory::format::any) { |
| 53 | + tensor->set_format(format); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +template <typename T> |
| 59 | +static void ReorderInput(framework::Tensor* tensor, |
| 60 | + const platform::Place& place, |
| 61 | + const mkldnn::engine& engine, bool isFourDim) { |
| 62 | + using platform::to_void_cast; |
| 63 | + auto dims = paddle::framework::vectorize2int(tensor->dims()); |
| 64 | + framework::Tensor out_tensor; |
| 65 | + out_tensor.Resize(tensor->dims()); |
| 66 | + out_tensor.set_format(isFourDim ? memory::format::nchw : memory::format::nc); |
| 67 | + out_tensor.set_layout(tensor->layout()); |
| 68 | + mkldnn::memory input_memory = { |
| 69 | + {{dims, platform::MKLDNNGetDataType<T>(), tensor->format()}, engine}, |
| 70 | + to_void_cast<T>(tensor->data<T>())}; |
| 71 | + mkldnn::memory output_memory = { |
| 72 | + {{dims, platform::MKLDNNGetDataType<T>(), out_tensor.format()}, engine}, |
| 73 | + to_void_cast<T>(out_tensor.mutable_data<T>(place))}; |
| 74 | + platform::Reorder(input_memory, output_memory); |
| 75 | + tensor->ShareDataWith(out_tensor); |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +class ElementwiseMulMKLDNNKernel : public framework::OpKernel<T> { |
| 80 | + public: |
| 81 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 82 | + using Tensor = framework::Tensor; |
| 83 | + |
| 84 | + int axis = ctx.Attr<int>("axis"); |
| 85 | + auto* x = ctx.Input<Tensor>("X"); |
| 86 | + auto* y = ctx.Input<Tensor>("Y"); |
| 87 | + auto* z = ctx.Output<Tensor>("Out"); |
| 88 | + const T* x_data = x->data<T>(); |
| 89 | + const T* y_data = y->data<T>(); |
| 90 | + T* z_data = z->mutable_data<T>(ctx.GetPlace()); |
| 91 | + |
| 92 | + auto x_dims = x->dims(); |
| 93 | + auto y_dims_untrimmed = y->dims(); |
| 94 | + auto x_int_dims = paddle::framework::vectorize2int(x_dims); |
| 95 | + |
| 96 | + UpdateDataFormat(ctx, (Tensor*)x, "x_data_format"); |
| 97 | + UpdateDataFormat(ctx, (Tensor*)y, "y_data_format"); |
| 98 | + |
| 99 | + Xbyak::util::Cpu cpu; |
| 100 | + const bool is_avx512_enabled = cpu.has(Xbyak::util::Cpu::tAVX512F); |
| 101 | + const bool are_dims_divisable = !(x_int_dims[1] % 16); |
| 102 | + const bool is_x_format_correct = x->format() == memory::format::nChw16c; |
| 103 | + const bool is_y_format_correct = y->format() == memory::format::nc; |
| 104 | + if (is_x_format_correct && is_y_format_correct && are_dims_divisable && |
| 105 | + is_avx512_enabled) { |
| 106 | + int pre, n, post; |
| 107 | + get_mid_dims(x_dims, y_dims_untrimmed, axis, &pre, &n, &post); |
| 108 | + |
| 109 | + if (post == 1) { |
| 110 | + PADDLE_THROW("Not implemented when post is 1"); |
| 111 | + } else { |
| 112 | + // Just check whether it works for RE-Resnext. |
| 113 | + PADDLE_ENFORCE_EQ(x_dims.size(), 4, "X should have 4 dimensions"); |
| 114 | + |
| 115 | + int n = x_dims[0]; |
| 116 | + int c = x_dims[1]; |
| 117 | + int h = x_dims[2]; |
| 118 | + int w = x_dims[3]; |
| 119 | + |
| 120 | + PADDLE_ENFORCE(y_dims_untrimmed[0] == n && y_dims_untrimmed[1] == c, |
| 121 | + "Y should be in nc format"); |
| 122 | + |
| 123 | + constexpr int simd_width = 16; |
| 124 | + int C = c / simd_width; |
| 125 | + |
| 126 | + const auto& multiply = |
| 127 | + math::jitkernel::KernelPool::Instance() |
| 128 | + .template Get<math::jitkernel::EltwiseMulnChw16cNCKernel<T>>(n); |
| 129 | + |
| 130 | +#pragma omp parallel for collapse(2) |
| 131 | + for (int ni = 0; ni < n; ni++) { |
| 132 | + for (int ci = 0; ci < C; ci++) { |
| 133 | + auto ptr_x = |
| 134 | + x_data + ni * C * h * w * simd_width + ci * h * w * simd_width; |
| 135 | + |
| 136 | + auto ptr_y = y_data + ni * C * simd_width + ci * simd_width; |
| 137 | + auto ptr_z = |
| 138 | + z_data + ni * C * h * w * simd_width + ci * h * w * simd_width; |
| 139 | + |
| 140 | + multiply->Compute(ptr_x, ptr_y, ptr_z, h, w); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + z->set_layout(DataLayout::kMKLDNN); |
| 146 | + z->set_format(x->format()); |
| 147 | + } else { |
| 148 | + // Fallback to naive version: |
| 149 | + const bool are_inputs_in_same_format = x->format() == y->format(); |
| 150 | + const bool is_x_nchw = x->format() == memory::format::nchw; |
| 151 | + const bool is_x_nc = x->format() == memory::format::nc; |
| 152 | + const bool is_y_nchw = y->format() == memory::format::nchw; |
| 153 | + const bool is_y_nc = y->format() == memory::format::nc; |
| 154 | + if (!are_inputs_in_same_format) { |
| 155 | + using platform::MKLDNNDeviceContext; |
| 156 | + auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>(); |
| 157 | + const auto& mkldnn_engine = dev_ctx.GetEngine(); |
| 158 | + if (!(is_x_nchw || is_x_nc)) |
| 159 | + ReorderInput<T>((Tensor*)x, ctx.GetPlace(), mkldnn_engine, |
| 160 | + x->dims().size() == 4); |
| 161 | + if (!(is_y_nchw || is_y_nc)) |
| 162 | + ReorderInput<T>((Tensor*)y, ctx.GetPlace(), mkldnn_engine, |
| 163 | + y->dims().size() == 4); |
| 164 | + } |
| 165 | + |
| 166 | + auto mul_func = [](T a, T b) -> T { return a * b; }; |
| 167 | + |
| 168 | + TransformFunctor<decltype(mul_func), T, |
| 169 | + paddle::platform::CPUDeviceContext, T> |
| 170 | + functor( |
| 171 | + x, y, z, |
| 172 | + ctx.template device_context<paddle::platform::CPUDeviceContext>(), |
| 173 | + mul_func); |
| 174 | + |
| 175 | + axis = (axis == -1 ? x_dims.size() - y_dims_untrimmed.size() : axis); |
| 176 | + PADDLE_ENFORCE(axis >= 0 && axis < x_dims.size(), |
| 177 | + "Axis should be in range [0, x_dims)"); |
| 178 | + |
| 179 | + auto y_dims = trim_trailing_singular_dims(y_dims_untrimmed); |
| 180 | + axis = (y_dims.size() == 0) ? x_dims.size() : axis; |
| 181 | + |
| 182 | + int pre, n, post; |
| 183 | + get_mid_dims(x_dims, y_dims, axis, &pre, &n, &post); |
| 184 | + |
| 185 | + if (post == 1) { |
| 186 | + functor.RunRowWise(n, pre); |
| 187 | + } else { |
| 188 | + functor.RunMidWise(n, pre, post); |
| 189 | + } |
| 190 | + z->set_layout(DataLayout::kMKLDNN); |
| 191 | + z->set_format(x->format()); |
| 192 | + } |
| 193 | + } |
| 194 | +}; |
| 195 | +} // namespace operators |
| 196 | +} // namespace paddle |
| 197 | + |
| 198 | +namespace ops = paddle::operators; |
| 199 | + |
| 200 | +REGISTER_OP_KERNEL(elementwise_mul, MKLDNN, ::paddle::platform::CPUPlace, |
| 201 | + ops::ElementwiseMulMKLDNNKernel<float>) |
0 commit comments