/* 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 #include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/math/blas.h" namespace paddle { namespace operators { template struct FindAbsMaxFunctor { void operator()(const DeviceContext& ctx, const T* in, const int num, T* out); }; template struct ClipAndFakeQuantFunctor { void operator()(const DeviceContext& ctx, const framework::Tensor& in, const framework::Tensor& scale, const int bin_cnt, framework::Tensor* out); }; template struct FindRangeAbsMaxFunctor { void operator()(const DeviceContext& ctx, const framework::Tensor& cur_scale, const framework::Tensor& last_scale, const framework::Tensor& iter, const int window_size, framework::Tensor* scales_arr, framework::Tensor* out_scale); }; template struct FindMovingAverageAbsMaxFunctor { void operator()(const DeviceContext& ctx, const framework::Tensor& in_accum, const framework::Tensor& in_state, const framework::Tensor& cur_scale, framework::Tensor* out_state, framework::Tensor* out_accum, framework::Tensor* out_scale); }; template class FakeQuantizeAbsMaxKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in = context.Input("X"); auto* out = context.Output("Out"); auto* out_scale = context.Output("OutScale"); T* out_s = out_scale->mutable_data(context.GetPlace()); int bit_length = context.Attr("bit_length"); int bin_cnt = std::pow(2, bit_length - 1) - 1; auto& dev_ctx = context.template device_context(); const T* in_data = in->data(); FindAbsMaxFunctor()(dev_ctx, in_data, in->numel(), out_s); ClipAndFakeQuantFunctor()(dev_ctx, *in, *out_scale, bin_cnt, out); } }; template class FakeChannelWiseQuantizeAbsMaxKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in = context.Input("X"); auto* out = context.Output("Out"); auto* out_scales = context.Output("OutScales"); T* out_scales_data = out_scales->mutable_data(context.GetPlace()); out->mutable_data(context.GetPlace()); int bit_length = context.Attr("bit_length"); int bin_cnt = std::pow(2, bit_length - 1) - 1; auto& dev_ctx = context.template device_context(); auto find_abs_max = FindAbsMaxFunctor(); for (int64_t i = 0; i < in->dims()[0]; i++) { framework::Tensor one_channel = in->Slice(i, i + 1); const T* one_channel_data = one_channel.data(); find_abs_max(dev_ctx, one_channel_data, one_channel.numel(), &out_scales_data[i]); } auto clip_quant = ClipAndFakeQuantFunctor(); for (int64_t i = 0; i < in->dims()[0]; i++) { framework::Tensor one_channel_in = in->Slice(i, i + 1); framework::Tensor one_channel_out = out->Slice(i, i + 1); framework::Tensor one_channel_scale = out_scales->Slice(i, i + 1); clip_quant(dev_ctx, one_channel_in, one_channel_scale, bin_cnt, &one_channel_out); } } }; template class FakeQuantizeRangeAbsMaxKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in = context.Input("X"); auto* in_scale = context.Input("InScale"); auto* out = context.Output("Out"); out->mutable_data(context.GetPlace()); bool is_test = context.Attr("is_test"); int bit_length = context.Attr("bit_length"); int bin_cnt = std::pow(2, bit_length - 1) - 1; auto& dev_ctx = context.template device_context(); // testing if (is_test) { ClipAndFakeQuantFunctor()(dev_ctx, *in, *in_scale, bin_cnt, out); return; } // training auto* out_scale = context.Output("OutScale"); auto* out_scales = context.Output("OutScales"); auto* iter = context.Input("Iter"); int window_size = context.Attr("window_size"); out_scale->mutable_data(context.GetPlace()); framework::Tensor cur_scale; T* cur_scale_data = cur_scale.mutable_data({1}, context.GetPlace()); FindAbsMaxFunctor()(dev_ctx, in->data(), in->numel(), cur_scale_data); FindRangeAbsMaxFunctor()(dev_ctx, cur_scale, *in_scale, *iter, window_size, out_scales, out_scale); ClipAndFakeQuantFunctor()(dev_ctx, *in, *out_scale, bin_cnt, out); } }; template class FakeQuantizeMovingAverageAbsMaxKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in = context.Input("X"); auto* in_scale = context.Input("InScale"); auto* out = context.Output("Out"); out->mutable_data(context.GetPlace()); bool is_test = context.Attr("is_test"); int bit_length = context.Attr("bit_length"); int bin_cnt = std::pow(2, bit_length - 1) - 1; auto& dev_ctx = context.template device_context(); // testing if (is_test) { ClipAndFakeQuantFunctor()(dev_ctx, *in, *in_scale, bin_cnt, out); return; } // training auto* in_accum = context.Input("InAccum"); auto* in_state = context.Input("InState"); auto& allocator = platform::DeviceTemporaryAllocator::Instance().Get(dev_ctx); auto cur_scale = allocator.Allocate(1 * sizeof(T)); T* cur_scale_data = static_cast(cur_scale->ptr()); FindAbsMaxFunctor()(dev_ctx, in->data(), in->numel(), cur_scale_data); auto* out_state = context.Output("OutState"); auto* out_accum = context.Output("OutAccum"); auto* out_scale = context.Output("OutScale"); out_state->mutable_data(context.GetPlace()); out_accum->mutable_data(context.GetPlace()); out_scale->mutable_data(context.GetPlace()); float moving_rate = context.Attr("moving_rate"); FindMovingAverageAbsMaxFunctor()( dev_ctx, *in_accum, *in_state, cur_scale_data, moving_rate, out_state, out_accum, out_scale); ClipAndFakeQuantFunctor()(dev_ctx, *in, *out_scale, bin_cnt, out); } }; } // namespace operators } // namespace paddle