lrn_mkldnn_op.cc 5.3 KB
Newer Older
T
Tomasz Patejko 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.

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/tensor.h"
#include "paddle/fluid/operators/lrn_op.h"
17
#include "paddle/fluid/platform/mkldnn_reuse.h"
T
Tomasz Patejko 已提交
18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

using paddle::framework::Tensor;
using paddle::platform::MKLDNNDeviceContext;

template <typename T>
class LRNMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
M
minqiyang 已提交
29
    const bool is_float_type = std::is_same<T, float>::value;
30 31 32 33 34 35
    PADDLE_ENFORCE_EQ(
        is_float_type, true,
        platform::errors::PreconditionNotMet("DNNL LRN must use float data."));
    PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true,
                      paddle::platform::errors::PreconditionNotMet(
                          "Operator DNNL LRN must use CPUPlace"));
36 37 38
    auto& dev_ctx =
        ctx.template device_context<platform::MKLDNNDeviceContext>();
    const auto& mkldnn_engine = dev_ctx.GetEngine();
T
Tomasz Patejko 已提交
39 40 41 42 43

    auto x = ctx.Input<Tensor>("X");
    auto out = ctx.Output<Tensor>("Out");
    auto mid = ctx.Output<Tensor>("MidOut");

44 45
    platform::LRNMKLDNNHandler<T> handler(
        ctx, dev_ctx, mkldnn_engine, ctx.GetPlace(), x, ctx.OutputName("Out"));
J
Jacek Czaja 已提交
46 47 48 49

    auto src_memory = handler.AcquireSrcMemory(x);
    auto dst_memory = handler.AcquireDstMemory(out);

A
Adam 已提交
50 51 52 53 54 55 56
    auto lrn_p = handler.AcquireForwardPrimitive();

    auto workspace_memory = handler.AcquireWorkspaceMemory(mid);
    mid->set_layout(framework::DataLayout::kMKLDNN);

    mkldnn::stream astream(dev_ctx.GetEngine());
    if (!workspace_memory->get_desc().is_zero()) {
57
      mid->set_format(platform::GetMKLDNNFormat(*workspace_memory));
A
Adam 已提交
58 59 60
      lrn_p->execute(astream, {{MKLDNN_ARG_SRC, *src_memory},
                               {MKLDNN_ARG_DST, *dst_memory},
                               {MKLDNN_ARG_WORKSPACE, *workspace_memory}});
J
Jacek Czaja 已提交
61
    } else {
A
Adam 已提交
62 63 64 65
      lrn_p->execute(astream, {{MKLDNN_ARG_SRC, *src_memory},
                               {MKLDNN_ARG_DST, *dst_memory}});
    }
    astream.wait();
66 67

    out->set_layout(framework::DataLayout::kMKLDNN);
A
Adam 已提交
68
    out->set_format(platform::GetMKLDNNFormat(*dst_memory));
T
Tomasz Patejko 已提交
69 70 71 72 73 74 75
  }
};

template <typename T>
class LRNMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
M
minqiyang 已提交
76
    const bool is_float_type = std::is_same<T, float>::value;
77 78
    PADDLE_ENFORCE_EQ(is_float_type, true,
                      platform::errors::PreconditionNotMet(
J
Jacek Czaja 已提交
79
                          "DNNL LRN GradOpKernel must use float data."));
80 81 82 83 84 85 86
    PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true,
                      paddle::platform::errors::PreconditionNotMet(
                          "Operator DNNL LRNGrad must use CPUPlace"));
    PADDLE_ENFORCE_EQ(
        ctx.Attr<bool>("is_test"), false,
        platform::errors::PreconditionNotMet(
            "is_test attribute should be set to False in training phase."));
T
Tomasz Patejko 已提交
87 88

    auto x = ctx.Input<Tensor>("X");
J
Jacek Czaja 已提交
89
    auto mid = ctx.Input<Tensor>("MidOut");
T
Tomasz Patejko 已提交
90 91 92 93 94

    auto out_grad = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));

    const int n = ctx.Attr<int>("n");
95
    const float alpha = ctx.Attr<float>("alpha") * static_cast<float>(n);
T
Tomasz Patejko 已提交
96 97 98 99 100
    const float beta = ctx.Attr<float>("beta");
    const float k = ctx.Attr<float>("k");

    auto& dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();

A
Adam 已提交
101
    auto dims = paddle::framework::vectorize<int64_t>(x->dims());
T
Tomasz Patejko 已提交
102

H
hong 已提交
103 104 105
    platform::LRNMKLDNNHandler<T> handler(dims, n, alpha, beta, k, x->format(),
                                          out_grad->format(), dev_ctx,
                                          ctx.GetPlace(), ctx.InputName("Out"));
T
Tomasz Patejko 已提交
106

J
Jacek Czaja 已提交
107 108 109 110
    auto src_memory = handler.AcquireSrcMemory(x);
    auto workspace = handler.AcquireBackwardWorkspaceMemory(mid);
    auto diff_dst_memory = handler.AcquireDiffDstMemory(out_grad);
    auto diff_src_memory = handler.AcquireDiffSrcMemory(x_grad);
T
Tomasz Patejko 已提交
111

A
Adam 已提交
112
    auto lrn_bwd = handler.AcquireBackwardPrimitive();
T
Tomasz Patejko 已提交
113

A
Adam 已提交
114 115 116 117 118 119
    mkldnn::stream astream(dev_ctx.GetEngine());
    lrn_bwd->execute(astream, {{MKLDNN_ARG_SRC, *src_memory},
                               {MKLDNN_ARG_DIFF_DST, *diff_dst_memory},
                               {MKLDNN_ARG_DIFF_SRC, *diff_src_memory},
                               {MKLDNN_ARG_WORKSPACE, *workspace}});
    astream.wait();
120 121

    x_grad->set_layout(framework::DataLayout::kMKLDNN);
A
Adam 已提交
122
    x_grad->set_format(platform::GetMKLDNNFormat(*diff_src_memory));
T
Tomasz Patejko 已提交
123 124 125 126 127 128 129 130 131 132 133
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_KERNEL(lrn, MKLDNN, paddle::platform::CPUPlace,
                   ops::LRNMKLDNNOpKernel<float>);
REGISTER_OP_KERNEL(lrn_grad, MKLDNN, paddle::platform::CPUPlace,
                   ops::LRNMKLDNNGradOpKernel<float>);