quantize_mkldnn_op.cc 4.1 KB
Newer Older
X
xiaoli.liu@intel.com 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 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. */

15
#include "dnnl.hpp"
16
#include "paddle/fluid/framework/data_layout_transform.h"
X
xiaoli.liu@intel.com 已提交
17 18 19
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/operators/quantize_op.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
X
xiaoli.liu@intel.com 已提交
20
#include "paddle/fluid/platform/mkldnn_reuse.h"
X
xiaoli.liu@intel.com 已提交
21 22 23 24

namespace paddle {
namespace operators {

25 26 27
using dnnl::memory;
using dnnl::primitive;
using dnnl::reorder;
X
xiaoli.liu@intel.com 已提交
28 29
using platform::to_void_cast;
using Tensor = framework::Tensor;
30
using dnnl::stream;
31
using framework::DataLayout;
X
xiaoli.liu@intel.com 已提交
32 33 34 35 36 37
using platform::GetMKLDNNFormat;

template <typename T>
class QuantOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
38 39 40 41 42 43 44 45
    auto* x = ctx.Input<Tensor>("Input");
    auto* out = ctx.Output<Tensor>("Output");

    const auto quantization_scale = ctx.Attr<float>("Scale");
    const auto quantization_shift = ctx.Attr<float>("Shift");
    const bool with_scale = quantization_scale != 1.0f;
    const bool with_shift = quantization_shift != 0.0f;

46 47
    PADDLE_ENFORCE_NE(quantization_scale,
                      0.0f,
48 49
                      platform::errors::InvalidArgument(
                          "Quantization scale must be different than 0.0f"));
50 51 52 53 54
    PADDLE_ENFORCE(quantization_shift <= 255 && quantization_shift >= 0,
                   platform::errors::InvalidArgument(
                       "Quantization shift must be lower or equal to ",
                       "255 and greater or equal to 0, but got %f",
                       quantization_shift));
55

X
xiaoli.liu@intel.com 已提交
56 57 58
    auto& dev_ctx =
        ctx.template device_context<platform::MKLDNNDeviceContext>();

59
    auto x_tz = phi::vectorize<int64_t>(x->dims());
X
xiaoli.liu@intel.com 已提交
60

61 62
    const bool is_negative_input = ctx.Attr<bool>("is_negative_input");
    const bool bfloat16 = ctx.Attr<bool>("bfloat16");
X
xiaoli.liu@intel.com 已提交
63

64 65
    dnnl::primitive_attr attrs;
    static constexpr int32_t mask = 0;
66

67 68 69
    if (with_scale) {
      attrs.set_output_scales(mask, {quantization_scale});
    }
70 71

    if (with_shift) {
72 73
      attrs.set_zero_points(
          DNNL_ARG_DST, mask, {static_cast<int32_t>(quantization_shift)});
74 75
    }

76 77 78
    framework::proto::VarType::Type x_paddle_dtype =
        framework::TransToProtoVarType(x->dtype());
    framework::proto::VarType::Type out_paddle_dtype;
79 80

    if (bfloat16) {
81
      out_paddle_dtype = framework::proto::VarType::BF16;
82
    } else if (is_negative_input && !with_shift) {
83
      out_paddle_dtype = framework::proto::VarType::INT8;
X
xiaoli.liu@intel.com 已提交
84
    } else {
85
      out_paddle_dtype = framework::proto::VarType::UINT8;
X
xiaoli.liu@intel.com 已提交
86
    }
87 88

    platform::ReorderMKLDNNHandler reorder_handler(
89 90 91 92 93
        x_tz,
        x_paddle_dtype,
        framework::ToMKLDNNDataType(x_paddle_dtype),
        out_paddle_dtype,
        framework::ToMKLDNNDataType(out_paddle_dtype),
94 95 96 97 98 99 100 101 102
        dev_ctx.GetEngine());

    auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory(
        x->mem_desc(), platform::to_void_cast(x->data<T>()));
    auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory(
        out, x->mem_desc(), dev_ctx.GetPlace());

    auto reorder_p = reorder_handler.AcquireReorder(
        reorder_dst_memory_p, reorder_src_memory_p, attrs);
103

104
    auto& astream = platform::MKLDNNDeviceContext::tls().get_stream();
105
    reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p);
106
    astream.wait();
A
Adam 已提交
107

108
    out->set_mem_desc(reorder_dst_memory_p->get_desc());
X
xiaoli.liu@intel.com 已提交
109 110 111 112 113 114
  }
};
}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;

115 116 117
REGISTER_OP_KERNEL(quantize,
                   MKLDNN,
                   ::paddle::platform::CPUPlace,
X
xiaoli.liu@intel.com 已提交
118
                   ops::QuantOpKernel<float>);