fake_quantize_op.h 4.2 KB
Newer Older
视言's avatar
视言 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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. */

#pragma once

#include <string>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/blas.h"

namespace paddle {
namespace operators {

D
Dang Qingqing 已提交
25
template <typename DeviceContext, typename T>
D
Dang Qingqing 已提交
26 27 28
struct FindAbsMaxFunctor {
  void operator()(const DeviceContext& ctx, const T* in, const int num, T* out);
};
D
Dang Qingqing 已提交
29

D
Dang Qingqing 已提交
30 31 32
template <typename DeviceContext, typename T>
struct ClipAndFakeQuantFunctor {
  void operator()(const DeviceContext& ctx, const framework::Tensor& in,
33
                  const framework::Tensor& scale, const int bin_cnt,
D
Dang Qingqing 已提交
34 35 36 37 38
                  framework::Tensor* out);
};

template <typename DeviceContext, typename T>
struct FindRangeAbsMaxFunctor {
39
  void operator()(const DeviceContext& ctx, const framework::Tensor& cur_scale,
D
Dang Qingqing 已提交
40 41
                  const framework::Tensor& last_scale,
                  const framework::Tensor& iter, const int window_size,
42
                  framework::Tensor* scales_arr, framework::Tensor* out_scale);
D
Dang Qingqing 已提交
43 44 45 46
};

template <typename DeviceContext, typename T>
class FakeQuantizeAbsMaxKernel : public framework::OpKernel<T> {
47 48
 public:
  void Compute(const framework::ExecutionContext& context) const override {
D
Dang Qingqing 已提交
49 50
    auto* in = context.Input<framework::Tensor>("X");

D
Dang Qingqing 已提交
51 52 53
    auto* out = context.Output<framework::Tensor>("Out");
    auto* out_scale = context.Output<framework::Tensor>("OutScale");
    T* out_s = out_scale->mutable_data<T>(context.GetPlace());
D
Dang Qingqing 已提交
54 55 56 57

    int bit_length = context.Attr<int>("bit_length");
    int bin_cnt = std::pow(2, bit_length - 1) - 1;

D
Dang Qingqing 已提交
58 59
    auto& dev_ctx = context.template device_context<DeviceContext>();
    const T* in_data = in->data<T>();
60
    FindAbsMaxFunctor<DeviceContext, T>()(dev_ctx, in_data, in->numel(), out_s);
D
Dang Qingqing 已提交
61 62
    ClipAndFakeQuantFunctor<DeviceContext, T>()(dev_ctx, *in, *out_scale,
                                                bin_cnt, out);
D
Dang Qingqing 已提交
63 64 65
  }
};

视言's avatar
视言 已提交
66
template <typename DeviceContext, typename T>
D
Dang Qingqing 已提交
67
class FakeQuantizeRangeAbsMaxKernel : public framework::OpKernel<T> {
68 69
 public:
  void Compute(const framework::ExecutionContext& context) const override {
视言's avatar
视言 已提交
70
    auto* in = context.Input<framework::Tensor>("X");
71
    auto* in_scale = context.Input<framework::Tensor>("InScale");
视言's avatar
视言 已提交
72

D
Dang Qingqing 已提交
73 74
    auto* out = context.Output<framework::Tensor>("Out");
    out->mutable_data<T>(context.GetPlace());
视言's avatar
视言 已提交
75

D
Dang Qingqing 已提交
76
    bool is_test = context.Attr<bool>("is_test");
视言's avatar
视言 已提交
77 78
    int bit_length = context.Attr<int>("bit_length");
    int bin_cnt = std::pow(2, bit_length - 1) - 1;
D
Dang Qingqing 已提交
79
    auto& dev_ctx = context.template device_context<DeviceContext>();
视言's avatar
视言 已提交
80

D
Dang Qingqing 已提交
81 82 83 84 85
    // testing
    if (is_test) {
      ClipAndFakeQuantFunctor<DeviceContext, T>()(dev_ctx, *in, *in_scale,
                                                  bin_cnt, out);
      return;
视言's avatar
视言 已提交
86 87
    }

D
Dang Qingqing 已提交
88 89
    // training
    auto* out_scale = context.Output<framework::Tensor>("OutScale");
90
    auto* out_scales = context.Output<framework::Tensor>("OutScales");
D
Dang Qingqing 已提交
91 92
    auto* iter = context.Input<framework::Tensor>("Iter");

93
    int window_size = context.Attr<int>("window_size");
D
Dang Qingqing 已提交
94 95
    out_scale->mutable_data<T>(context.GetPlace());

96
    framework::Tensor cur_scale;
D
Dang Qingqing 已提交
97 98 99
    T* cur_scale_data = cur_scale.mutable_data<T>({1}, context.GetPlace());
    FindAbsMaxFunctor<DeviceContext, T>()(dev_ctx, in->data<T>(), in->numel(),
                                          cur_scale_data);
100 101 102
    FindRangeAbsMaxFunctor<DeviceContext, T>()(dev_ctx, cur_scale, *in_scale,
                                               *iter, window_size, out_scales,
                                               out_scale);
D
Dang Qingqing 已提交
103 104
    ClipAndFakeQuantFunctor<DeviceContext, T>()(dev_ctx, *in, *out_scale,
                                                bin_cnt, out);
视言's avatar
视言 已提交
105 106 107 108 109
  }
};

}  // namespace operators
}  // namespace paddle