/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. Licensed 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. */ #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/mkldnn_reuse.h" namespace paddle { namespace operators { using paddle::framework::Tensor; template class SliceMKLDNNKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { this->RunKernel(ctx); } void RunKernel(const framework::ExecutionContext& ctx) const { const auto& dev_ctx = ctx.template device_context(); const auto& onednn_engine = dev_ctx.GetEngine(); auto* x = ctx.Input("Input"); auto* out = ctx.Output("Out"); auto x_vec_dims = phi::vectorize(x->dims()); auto axes_int = ctx.Attr>("axes"); auto starts_int = ctx.Attr>("starts"); auto ends_int = ctx.Attr>("ends"); std::vector axes(ctx.Attr>("axes").begin(), ctx.Attr>("axes").end()); std::vector starts(ctx.Attr>("starts").begin(), ctx.Attr>("starts").end()); std::vector ends(ctx.Attr>("ends").begin(), ctx.Attr>("ends").end()); auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { starts = GetDataFromTensor(ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto decrease_axis = ctx.Attr>("decrease_axis"); auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { ends = GetDataFromTensor(ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } std::vector offsets(x_vec_dims.size(), 0); std::vector slice_dims(x_vec_dims); for (size_t i = 0; i < axes.size(); ++i) { starts[i] = starts[i] < 0 ? x_vec_dims[axes[i]] + starts[i] : starts[i]; ends[i] = ends[i] < 0 ? x_vec_dims[axes[i]] + ends[i] : std::min(ends[i], x_vec_dims[axes[i]]); offsets[axes[i]] = starts[i]; slice_dims[axes[i]] = std::max(static_cast(0), ends[i] - starts[i]); } out->Resize(phi::make_ddim(slice_dims)); // Note(0x45f): To support slice Tensors with shapes like [0, 0, 0]. if (!x->initialized()) { out->mutable_data(x->place(), x->dtype()); out->set_layout(experimental::DataLayout::kMKLDNN); return; } dnnl::memory::data_type x_type = framework::ToMKLDNNDataType(framework::TransToProtoVarType(x->dtype())); platform::ReorderMKLDNNHandler reorder_handler( x_vec_dims, framework::TransToProtoVarType(x->dtype()), x_type, onednn_engine); auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( x->mem_desc(), platform::to_void_cast(x->data())); auto slice_mem_p = reorder_handler.AcquireSubmemory( slice_dims, offsets, reorder_src_memory_p); auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( out, slice_dims, platform::GetPlainMKLDNNFormat(x_vec_dims.size()), ctx.GetPlace()); auto reorder_p = reorder_handler.AcquireReorder(reorder_dst_memory_p, slice_mem_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); reorder_p->execute(astream, *slice_mem_p, *reorder_dst_memory_p); std::vector new_out_dims(slice_dims.size() - decrease_axis.size()); if (new_out_dims.size() == 0) { new_out_dims.emplace_back(1); } else { for (const auto& axis : decrease_axis) { slice_dims[axis] = 0; } int i = 0; for (const auto& slice_dim : slice_dims) { if (slice_dim != 0) new_out_dims[i++] = slice_dim; } } astream.wait(); out->Resize(phi::make_ddim(new_out_dims)); out->set_mem_desc(reorder_dst_memory_p->get_desc().reshape(new_out_dims)); } }; template class SliceGradMKLDNNKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { this->RunKernel(ctx); } void RunKernel(const framework::ExecutionContext& ctx) const { const auto& dev_ctx = ctx.template device_context(); const auto& onednn_engine = dev_ctx.GetEngine(); auto* dout = ctx.Input(framework::GradVarName("Out")); auto* dx = ctx.Output(framework::GradVarName("Input")); auto dx_vec_dims = phi::vectorize(dx->dims()); auto dout_vec_dims = phi::vectorize(dout->dims()); auto axes_int = ctx.Attr>("axes"); auto starts_int = ctx.Attr>("starts"); auto ends_int = ctx.Attr>("ends"); std::vector axes(ctx.Attr>("axes").begin(), ctx.Attr>("axes").end()); std::vector starts(ctx.Attr>("starts").begin(), ctx.Attr>("starts").end()); std::vector ends(ctx.Attr>("ends").begin(), ctx.Attr>("ends").end()); auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { starts = GetDataFromTensor(ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { ends = GetDataFromTensor(ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } auto decrease_axis = ctx.Attr>("decrease_axis"); std::vector offsets(dx_vec_dims.size(), 0); std::vector slice_dims(dx_vec_dims); for (size_t i = 0; i < axes.size(); ++i) { starts[i] = starts[i] < 0 ? dx_vec_dims[axes[i]] + starts[i] : starts[i]; ends[i] = ends[i] < 0 ? dx_vec_dims[axes[i]] + ends[i] : std::min(ends[i], dx_vec_dims[axes[i]]); offsets[axes[i]] = starts[i]; slice_dims[axes[i]] = ends[i] - starts[i]; } dnnl::memory::data_type dout_type = framework::ToMKLDNNDataType( framework::TransToProtoVarType(dout->dtype())); platform::ReorderMKLDNNHandler reorder_handler( slice_dims, framework::TransToProtoVarType(dout->dtype()), dout_type, onednn_engine); auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory( dout->mem_desc().reshape(slice_dims), platform::to_void_cast(dout->data())); auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory( dx, dx_vec_dims, platform::GetPlainMKLDNNFormat(dx_vec_dims.size()), ctx.GetPlace()); memset(dx->data(), 0, reorder_dst_memory_p->get_desc().get_size()); auto slice_mem_p = reorder_handler.AcquireSubmemory( slice_dims, offsets, reorder_dst_memory_p); auto reorder_p = reorder_handler.AcquireReorder(slice_mem_p, reorder_src_memory_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); reorder_p->execute(astream, *reorder_src_memory_p, *slice_mem_p); astream.wait(); dx->set_mem_desc(reorder_dst_memory_p->get_desc()); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_KERNEL(slice, MKLDNN, paddle::platform::CPUPlace, ops::SliceMKLDNNKernel, ops::SliceMKLDNNKernel, ops::SliceMKLDNNKernel, ops::SliceMKLDNNKernel); namespace ops = paddle::operators; REGISTER_OP_KERNEL(slice_grad, MKLDNN, paddle::platform::CPUPlace, ops::SliceGradMKLDNNKernel, ops::SliceGradMKLDNNKernel);