quantize_mkldnn_op.cc 4.6 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 16
#include "paddle/fluid/operators/quantize_op.h"

17
#include "paddle/fluid/framework/op_registry.h"
X
xiaoli.liu@intel.com 已提交
18 19
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
20
#include "paddle/phi/backends/onednn/onednn_reuse.h"
X
xiaoli.liu@intel.com 已提交
21 22 23 24

namespace paddle {
namespace operators {

25
using dnnl::memory;
X
xiaoli.liu@intel.com 已提交
26 27 28 29 30

template <typename T>
class QuantOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
31 32
    auto* x = ctx.Input<phi::DenseTensor>("Input");
    auto* out = ctx.Output<phi::DenseTensor>("Output");
33 34

    const auto quantization_scale = ctx.Attr<float>("Scale");
35 36
    const auto quantization_shift =
        static_cast<int32_t>(ctx.Attr<float>("Shift"));
37 38 39
    const bool with_scale = quantization_scale != 1.0f;
    const bool with_shift = quantization_shift != 0.0f;

40 41
    PADDLE_ENFORCE_NE(quantization_scale,
                      0.0f,
42 43
                      platform::errors::InvalidArgument(
                          "Quantization scale must be different than 0.0f"));
44 45 46 47 48
    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));
49

50
    auto& dev_ctx = ctx.template device_context<phi::OneDNNContext>();
X
xiaoli.liu@intel.com 已提交
51

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

54 55
    const bool is_negative_input = ctx.Attr<bool>("is_negative_input");
    const bool bfloat16 = ctx.Attr<bool>("bfloat16");
X
xiaoli.liu@intel.com 已提交
56

57 58
    dnnl::primitive_attr attrs;
    static constexpr int32_t mask = 0;
59

60
    if (with_scale) {
61
      attrs.set_scales_mask(DNNL_ARG_SRC, mask);
62
    }
63 64

    if (with_shift) {
65
      attrs.set_zero_points_mask(DNNL_ARG_DST, mask);
66 67
    }

68 69
    auto x_type = phi::funcs::ToOneDNNDataType(x->dtype());
    DataType out_dtype;
70 71

    if (bfloat16) {
72
      out_dtype = DataType::BFLOAT16;
73
    } else if (is_negative_input && !with_shift) {
74
      out_dtype = DataType::INT8;
X
xiaoli.liu@intel.com 已提交
75
    } else {
76
      out_dtype = DataType::UINT8;
X
xiaoli.liu@intel.com 已提交
77
    }
78

79 80 81 82
    auto out_type = phi::funcs::ToOneDNNDataType(out_dtype);

    phi::funcs::ReorderOneDNNHandler reorder_handler(
        x_tz, x->dtype(), x_type, out_dtype, out_type, dev_ctx.GetEngine());
83 84

    auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory(
85
        x->mem_desc(), phi::funcs::to_void_cast(x->data<T>()));
86 87 88 89 90
    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);
91

92
    auto& astream = phi::OneDNNContext::tls().get_stream();
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    auto scales_md = dnnl::memory::desc(
        {1}, dnnl::memory::data_type::f32, dnnl::memory::format_tag::x);
    auto scales_mem =
        dnnl::memory(scales_md,
                     dev_ctx.GetEngine(),
                     phi::funcs::to_void_cast<float>(&quantization_scale));
    auto zero_points_md = dnnl::memory::desc(
        {1}, dnnl::memory::data_type::s32, dnnl::memory::format_tag::x);
    auto zero_points_mem =
        dnnl::memory(zero_points_md,
                     dev_ctx.GetEngine(),
                     phi::funcs::to_void_cast<int32_t>(&quantization_shift));

    std::unordered_map<int, dnnl::memory> reorder_args;
    reorder_args.insert({DNNL_ARG_SRC, *reorder_src_memory_p});
    reorder_args.insert({DNNL_ARG_DST, *reorder_dst_memory_p});
    if (with_scale) {
      reorder_args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_SRC, scales_mem});
    }
    if (with_shift) {
      reorder_args.insert(
          {DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_DST, zero_points_mem});
    }

    reorder_p->execute(astream, reorder_args);
119
    astream.wait();
A
Adam 已提交
120

121
    out->set_mem_desc(reorder_dst_memory_p->get_desc());
X
xiaoli.liu@intel.com 已提交
122 123 124 125 126 127
  }
};
}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;

128 129
REGISTER_OP_KERNEL(quantize,
                   MKLDNN,
130
                   ::phi::CPUPlace,
X
xiaoli.liu@intel.com 已提交
131
                   ops::QuantOpKernel<float>);