quantize_mkldnn_op.cc 3.7 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 26 27 28
using dnnl::memory;
using dnnl::primitive;
using dnnl::reorder;
using dnnl::stream;
29
using phi::DataLayout;
X
xiaoli.liu@intel.com 已提交
30 31 32 33 34

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

    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;

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

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

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

57 58
    const bool is_negative_input = ctx.Attr<bool>("is_negative_input");
    const bool bfloat16 = ctx.Attr<bool>("bfloat16");
X
xiaoli.liu@intel.com 已提交
59

60 61
    dnnl::primitive_attr attrs;
    static constexpr int32_t mask = 0;
62

63 64 65
    if (with_scale) {
      attrs.set_output_scales(mask, {quantization_scale});
    }
66 67

    if (with_shift) {
68 69
      attrs.set_zero_points(
          DNNL_ARG_DST, mask, {static_cast<int32_t>(quantization_shift)});
70 71
    }

72 73
    auto x_type = phi::funcs::ToOneDNNDataType(x->dtype());
    DataType out_dtype;
74 75

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

83 84 85 86
    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());
87 88

    auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory(
89
        x->mem_desc(), phi::funcs::to_void_cast(x->data<T>()));
90 91 92 93 94
    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);
95

96
    auto& astream = phi::OneDNNContext::tls().get_stream();
97
    reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p);
98
    astream.wait();
A
Adam 已提交
99

100
    out->set_mem_desc(reorder_dst_memory_p->get_desc());
X
xiaoli.liu@intel.com 已提交
101 102 103 104 105 106
  }
};
}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;

107 108
REGISTER_OP_KERNEL(quantize,
                   MKLDNN,
109
                   ::phi::CPUPlace,
X
xiaoli.liu@intel.com 已提交
110
                   ops::QuantOpKernel<float>);