dequantize_mkldnn_op.cc 3.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/dequantize_op.h"

X
xiaoli.liu@intel.com 已提交
17 18
#include "paddle/fluid/framework/data_layout_transform.h"
#include "paddle/fluid/framework/tensor.h"
19
#include "paddle/fluid/platform/errors.h"
X
xiaoli.liu@intel.com 已提交
20
#include "paddle/fluid/platform/mkldnn_helper.h"
21
#include "paddle/fluid/platform/mkldnn_reuse.h"
X
xiaoli.liu@intel.com 已提交
22 23 24 25

namespace paddle {
namespace operators {

26 27 28
using dnnl::memory;
using dnnl::primitive;
using dnnl::reorder;
29
using Tensor = phi::DenseTensor;
30
using dnnl::stream;
X
xiaoli.liu@intel.com 已提交
31 32 33 34 35

template <typename T>
class DeQuantOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
36
    auto* x = ctx.Input<phi::DenseTensor>("Input");
37 38 39
    const auto quantization_scale = ctx.Attr<float>("Scale");
    const auto quantization_shift = ctx.Attr<float>("Shift");
    const bool with_shift = quantization_shift != 0.0f;
40
    auto* out = ctx.Output<phi::DenseTensor>("Output");
41 42 43 44 45

    PADDLE_ENFORCE(quantization_scale != 0.0f,
                   platform::errors::InvalidArgument(
                       "Dequantization scale must be different than 0.0f"));

46 47 48 49 50
    PADDLE_ENFORCE(quantization_shift <= 255 && quantization_shift >= 0,
                   platform::errors::InvalidArgument(
                       "Dequantization shift must be lower or equal to ",
                       "255 and greater or equal to 0, but got %f",
                       quantization_shift));
51

52
    auto& dev_ctx = ctx.template device_context<phi::OneDNNContext>();
53 54

    auto x_tz = phi::vectorize<int64_t>(x->dims());
55 56
    auto x_type = phi::funcs::ToOneDNNDataType(x->dtype());
    auto out_type = phi::funcs::ToOneDNNDataType(out->dtype());
57 58 59 60 61 62 63 64

    dnnl::primitive_attr attrs;
    static constexpr int32_t mask = 0;  // same shift and scale for whole tensor

    const float reorder_scale = 1. / quantization_scale;
    attrs.set_output_scales(mask, {reorder_scale});

    if (with_shift) {
65 66
      attrs.set_zero_points(
          DNNL_ARG_SRC, mask, {static_cast<int32_t>(quantization_shift)});
67
    }
X
xiaoli.liu@intel.com 已提交
68

69 70
    phi::funcs::ReorderOneDNNHandler reorder_handler(
        x_tz, x->dtype(), x_type, out->dtype(), out_type, dev_ctx.GetEngine());
71 72

    auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory(
73
        x->mem_desc(), phi::funcs::to_void_cast(x->data<T>()));
74 75 76 77 78 79
    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);

80
    auto& astream = phi::OneDNNContext::tls().get_stream();
81
    reorder_p->execute(astream, *reorder_src_memory_p, *reorder_dst_memory_p);
A
Adam 已提交
82
    astream.wait();
X
xiaoli.liu@intel.com 已提交
83

84
    out->set_mem_desc(reorder_dst_memory_p->get_desc());
X
xiaoli.liu@intel.com 已提交
85 86 87 88 89 90 91 92
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

93 94 95 96 97
REGISTER_OP_KERNEL(dequantize,
                   MKLDNN,
                   ::paddle::platform::CPUPlace,
                   ops::DeQuantOpKernel<uint8_t>,
                   ops::DeQuantOpKernel<int8_t>,
98
                   ops::DeQuantOpKernel<paddle::platform::bfloat16>);