|
| 1 | +/* Copyright (c) 2018 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 "paddle/fluid/memory/memcpy.h" |
| 16 | +#include "paddle/fluid/operators/elementwise_add_op.h" |
| 17 | +#include "paddle/fluid/operators/elementwise_op_function.h" |
| 18 | + |
| 19 | +#include "paddle/fluid/platform/mkldnn_helper.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace operators { |
| 23 | + |
| 24 | +using framework::DataLayout; |
| 25 | +using framework::Tensor; |
| 26 | +using mkldnn::memory; |
| 27 | +using mkldnn::reorder; |
| 28 | +using mkldnn::primitive; |
| 29 | +using mkldnn::stream; |
| 30 | +using mkldnn::sum; |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> { |
| 34 | + public: |
| 35 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 36 | + auto& dev_ctx = |
| 37 | + ctx.template device_context<paddle::platform::MKLDNNDeviceContext>(); |
| 38 | + const auto& mkldnn_engine = dev_ctx.GetEngine(); |
| 39 | + |
| 40 | + auto* x = ctx.Input<Tensor>("X"); |
| 41 | + auto* y = ctx.Input<Tensor>("Y"); |
| 42 | + auto* z = ctx.Output<Tensor>("Out"); |
| 43 | + const T* x_data = x->data<T>(); |
| 44 | + const T* y_data = y->data<T>(); |
| 45 | + T* z_data = z->mutable_data<T>(ctx.GetPlace()); |
| 46 | + |
| 47 | + int axis = ctx.Attr<int>("axis"); |
| 48 | + |
| 49 | + auto x_dims = x->dims(); |
| 50 | + auto y_dims = y->dims(); |
| 51 | + auto z_dims = z->dims(); |
| 52 | + |
| 53 | + // Execute default elementwise_add operator when |
| 54 | + // broadcast operations need to performed. |
| 55 | + if (x_dims != y_dims) { |
| 56 | + auto sum_func = [](T a, T b) -> T { return a + b; }; |
| 57 | + |
| 58 | + TransformFunctor<decltype(sum_func), T, |
| 59 | + paddle::platform::CPUDeviceContext, T> |
| 60 | + functor( |
| 61 | + x, y, z, |
| 62 | + ctx.template device_context<paddle::platform::CPUDeviceContext>(), |
| 63 | + sum_func); |
| 64 | + |
| 65 | + axis = (axis == -1 ? x_dims.size() - y_dims.size() : axis); |
| 66 | + PADDLE_ENFORCE(axis >= 0 && axis < x_dims.size(), |
| 67 | + "Axis should be in range [0, x_dims)"); |
| 68 | + |
| 69 | + trim_trailing_singular_dims(&y_dims); |
| 70 | + axis = (y_dims.size() == 0) ? x_dims.size() : axis; |
| 71 | + |
| 72 | + int pre, n, post; |
| 73 | + get_mid_dims(x_dims, y_dims, axis, &pre, &n, &post); |
| 74 | + |
| 75 | + if (post == 1) { |
| 76 | + functor.RunRowWise(n, pre); |
| 77 | + } else { |
| 78 | + functor.RunMidWise(n, pre, post); |
| 79 | + } |
| 80 | + z->set_layout(DataLayout::kMKLDNN); |
| 81 | + z->set_format(x->format()); |
| 82 | + } else { |
| 83 | + PADDLE_ENFORCE(x->layout() == DataLayout::kMKLDNN && |
| 84 | + x->format() != memory::format::format_undef, |
| 85 | + "Wrong layout/format set for X tensor"); |
| 86 | + PADDLE_ENFORCE(y->layout() == DataLayout::kMKLDNN && |
| 87 | + y->format() != memory::format::format_undef, |
| 88 | + "Wrong layout/format set for X tensor"); |
| 89 | + |
| 90 | + std::vector<int> src_x_tz = framework::vectorize2int(x_dims); |
| 91 | + std::vector<int> src_y_tz = framework::vectorize2int(y_dims); |
| 92 | + std::vector<int> dst_tz = framework::vectorize2int(z_dims); |
| 93 | + |
| 94 | + std::vector<memory::primitive_desc> srcs_pd; |
| 95 | + std::vector<memory> srcs; |
| 96 | + std::vector<float> scales = {1.0f, 1.0f}; |
| 97 | + |
| 98 | + auto src_x_pd = memory::primitive_desc( |
| 99 | + {{src_x_tz}, memory::data_type::f32, x->format()}, mkldnn_engine); |
| 100 | + auto src_y_pd = memory::primitive_desc( |
| 101 | + {{src_y_tz}, memory::data_type::f32, y->format()}, mkldnn_engine); |
| 102 | + auto src_x_memory = |
| 103 | + memory(src_x_pd, paddle::platform::to_void_cast(x_data)); |
| 104 | + auto src_y_memory = |
| 105 | + memory(src_y_pd, paddle::platform::to_void_cast(y_data)); |
| 106 | + |
| 107 | + srcs_pd.push_back(src_x_pd); |
| 108 | + srcs_pd.push_back(src_y_pd); |
| 109 | + srcs.push_back(src_x_memory); |
| 110 | + srcs.push_back(src_y_memory); |
| 111 | + |
| 112 | + auto dst_md = |
| 113 | + memory::desc({dst_tz}, memory::data_type::f32, memory::format::any); |
| 114 | + |
| 115 | + // create primitive descriptor for sum |
| 116 | + auto sum_pd = sum::primitive_desc(dst_md, scales, srcs_pd); |
| 117 | + |
| 118 | + // create mkldnn memory for dst |
| 119 | + memory dst_memory = memory(sum_pd.dst_primitive_desc(), z_data); |
| 120 | + |
| 121 | + std::vector<primitive::at> inputs; |
| 122 | + inputs.push_back(srcs[0]); |
| 123 | + inputs.push_back(srcs[1]); |
| 124 | + |
| 125 | + // create sum primitive |
| 126 | + auto sum_prim = sum(sum_pd, inputs, dst_memory); |
| 127 | + |
| 128 | + std::vector<primitive> pipeline; |
| 129 | + pipeline.push_back(sum_prim); |
| 130 | + stream(stream::kind::eager).submit(pipeline).wait(); |
| 131 | + |
| 132 | + z->set_layout(DataLayout::kMKLDNN); |
| 133 | + z->set_format( |
| 134 | + (memory::format)dst_memory.get_primitive_desc().desc().data.format); |
| 135 | + } |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +template <typename T> |
| 140 | +class EltwiseAddMKLDNNGradKernel : public framework::OpKernel<T> { |
| 141 | + public: |
| 142 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 143 | + using Tensor = framework::Tensor; |
| 144 | + |
| 145 | + auto* x = ctx.Input<Tensor>("X"); |
| 146 | + auto* y = ctx.Input<Tensor>("Y"); |
| 147 | + auto* out = ctx.Input<Tensor>("Out"); |
| 148 | + auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out")); |
| 149 | + auto* dx = ctx.Output<Tensor>(framework::GradVarName("X")); |
| 150 | + auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y")); |
| 151 | + int axis = ctx.Attr<int>("axis"); |
| 152 | + |
| 153 | + auto set_mkldnn_format = [](Tensor* in, const Tensor* out) { |
| 154 | + in->set_layout(DataLayout::kMKLDNN); |
| 155 | + in->set_format(out->format()); |
| 156 | + }; |
| 157 | + |
| 158 | + if (x->dims() == y->dims()) { |
| 159 | + auto blas = math::GetBlas<paddle::platform::CPUDeviceContext, T>(ctx); |
| 160 | + if (dx) { |
| 161 | + blas.VCOPY(dout->numel(), dout->data<T>(), |
| 162 | + dx->mutable_data<T>(ctx.GetPlace())); |
| 163 | + set_mkldnn_format(dx, dout); |
| 164 | + } |
| 165 | + |
| 166 | + if (dy) { |
| 167 | + blas.VCOPY(dout->numel(), dout->data<T>(), |
| 168 | + dy->mutable_data<T>(ctx.GetPlace())); |
| 169 | + set_mkldnn_format(dy, dout); |
| 170 | + } |
| 171 | + } else { |
| 172 | + // Execute default kernel when broadcast is needed |
| 173 | + ElemwiseGradCompute<paddle::platform::CPUDeviceContext, T, |
| 174 | + IdentityGrad<T>, IdentityGrad<T>>( |
| 175 | + ctx, *x, *y, *out, *dout, axis, dx, dy, IdentityGrad<T>(), |
| 176 | + IdentityGrad<T>()); |
| 177 | + } |
| 178 | + } |
| 179 | +}; |
| 180 | + |
| 181 | +} // namespace operators |
| 182 | +} // namespace paddle |
| 183 | + |
| 184 | +namespace ops = paddle::operators; |
| 185 | + |
| 186 | +REGISTER_OP_KERNEL(elementwise_add, MKLDNN, ::paddle::platform::CPUPlace, |
| 187 | + ops::EltwiseAddMKLDNNKernel<float>) |
| 188 | + |
| 189 | +REGISTER_OP_KERNEL(elementwise_add_grad, MKLDNN, ::paddle::platform::CPUPlace, |
| 190 | + ops::EltwiseAddMKLDNNGradKernel<float>) |
0 commit comments