transpose_mkldnn_op.cc 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 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/framework/data_layout_transform.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/malloc.h"
18
#include "paddle/fluid/operators/transpose_op.h"
19 20 21 22 23 24 25 26 27 28 29 30
#include "paddle/fluid/platform/mkldnn_reuse.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using framework::DataLayout;

template <typename T>
class TransposeMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
31 32 33
    PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true,
                      paddle::platform::errors::PreconditionNotMet(
                          "Operator DNNL Transpose must use CPUPlace"));
34 35 36 37 38 39 40 41 42 43
    auto& dev_ctx =
        ctx.template device_context<paddle::platform::MKLDNNDeviceContext>();
    const auto& mkldnn_engine = dev_ctx.GetEngine();
    std::vector<int> axis = ctx.Attr<std::vector<int>>("axis");
    int ndims = axis.size();
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
    const T* input_data = input->data<T>();

    if (ndims == 1) {
44 45
      framework::TensorCopy(*input, input->place(), output);
      output->set_format(input->format());
46 47 48
      return;
    }

A
Adam 已提交
49
    auto nchw_tz = paddle::framework::vectorize<int64_t>(input->dims());
50

H
hong 已提交
51
    const std::string key = platform::CreateKey(nchw_tz, ctx.OutputName("Out"));
52

53 54
    platform::TransposeMKLDNNHandler<T> handler(nchw_tz, axis, dev_ctx,
                                                mkldnn_engine, key);
55

56
    auto transpose_src_memory_p = handler.AcquireSrcMemory(
57
        input->format(), platform::to_void_cast<T>(input_data));
58 59 60 61
    auto transpose_dst_memory_p =
        handler.AcquireDstMemory(output, ctx.GetPlace());
    auto transpose_p = handler.AcquireTranspose(transpose_dst_memory_p,
                                                transpose_src_memory_p);
62

A
Adam 已提交
63 64 65 66
    mkldnn::stream astream(mkldnn_engine);
    transpose_p->execute(astream, *transpose_src_memory_p,
                         *transpose_dst_memory_p);
    astream.wait();
67

68
    output->set_layout(DataLayout::kNCHW);
A
Adam 已提交
69
    output->set_format(MKLDNNMemoryFormat::undef);
70 71 72
  }
};

73 74 75 76
template <typename T>
class TransposeMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
77 78 79
    PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true,
                      paddle::platform::errors::PreconditionNotMet(
                          "Operator DNNL TransposeGrad must use CPUPlace"));
80 81 82 83 84 85 86 87 88 89 90
    auto* out_grad =
        ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* x_grad = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    if (!x_grad) return;
    auto& dev_ctx =
        ctx.template device_context<paddle::platform::MKLDNNDeviceContext>();
    const auto& mkldnn_engine = dev_ctx.GetEngine();
    std::vector<int> axis = ctx.Attr<std::vector<int>>("axis");
    std::vector<int> reversed_axis(axis);
    int ndims = axis.size();
    if (ndims == 1) {
91 92
      framework::TensorCopy(*out_grad, out_grad->place(), x_grad);
      x_grad->set_format(out_grad->format());
93 94 95 96 97 98 99 100 101 102
      return;
    }

    for (size_t i = 0; i < axis.size(); i++) {
      reversed_axis[axis[i]] = i;
    }

    const T* out_grad_data = out_grad->data<T>();
    x_grad->mutable_data<T>(ctx.GetPlace());

A
Adam 已提交
103
    auto nchw_tz = paddle::framework::vectorize<int64_t>(out_grad->dims());
104

105
    const std::string key = platform::CreateKey(
H
hong 已提交
106
        nchw_tz, ctx.OutputName(framework::GradVarName("X")));
107

108 109
    platform::TransposeMKLDNNHandler<T> handler(nchw_tz, reversed_axis, dev_ctx,
                                                mkldnn_engine, key);
110

111 112
    auto transpose_src_memory_p = handler.AcquireSrcMemory(
        out_grad->format(), platform::to_void_cast<T>(out_grad_data));
113 114 115 116 117
    auto transpose_dst_memory_p =
        handler.AcquireDstMemory(x_grad, ctx.GetPlace());
    auto transpose_p = handler.AcquireTranspose(transpose_dst_memory_p,
                                                transpose_src_memory_p);

A
Adam 已提交
118 119 120 121
    mkldnn::stream astream(mkldnn_engine);
    transpose_p->execute(astream, *transpose_src_memory_p,
                         *transpose_dst_memory_p);
    astream.wait();
122 123 124
  }
};

125 126 127 128 129
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(transpose2, MKLDNN,
                                    ::paddle::platform::CPUPlace, FP32,
                                    ops::kTransposeMKLDNNFP32,
                                    ops::TransposeMKLDNNOpKernel<float>);

REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(transpose2, MKLDNN,
                                    ::paddle::platform::CPUPlace, U8,
                                    ops::kTransposeMKLDNNINT8,
                                    ops::TransposeMKLDNNOpKernel<uint8_t>);

REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(transpose2, MKLDNN,
                                    ::paddle::platform::CPUPlace, S8,
                                    ops::kTransposeMKLDNNINT8,
                                    ops::TransposeMKLDNNOpKernel<int8_t>);

145 146 147 148 149
REGISTER_OP_KERNEL_WITH_CUSTOM_TYPE(
    transpose2, MKLDNN, ::paddle::platform::CPUPlace, BF16,
    ops::kTransposeMKLDNNFP32,
    ops::TransposeMKLDNNOpKernel<paddle::platform::bfloat16>);

150 151
REGISTER_OP_KERNEL(transpose, MKLDNN, ::paddle::platform::CPUPlace,
                   ops::TransposeMKLDNNOpKernel<float>);
152 153 154

REGISTER_OP_KERNEL(transpose_grad, MKLDNN, ::paddle::platform::CPUPlace,
                   ops::TransposeMKLDNNGradOpKernel<float>);
155

156 157
REGISTER_OP_KERNEL(transpose2_grad, MKLDNN, ::paddle::platform::CPUPlace,
                   ops::TransposeMKLDNNGradOpKernel<float>);