/* 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/framework/tensor_util.h" #include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/platform/transform.h" #include "paddle/phi/core/hostdevice.h" #include "paddle/phi/kernels/funcs/blas/blas.h" namespace paddle { namespace operators { template inline HOSTDEVICE T inverse(T s) { T eps = static_cast(1e-6); T one = static_cast(1.0); return s <= static_cast(1e-30) ? one / (s + eps) : one / s; } template inline HOSTDEVICE T roundWithTiesToEven(T x) { T xLower = floor(x); T xUpper = ceil(x); // x is in interval [xl,xu]. Choose closest of two bounds, breaking ties to // even. T dLower = x - xLower; T dUpper = xUpper - x; return static_cast( (dLower == dUpper ? fmod(xLower, 2.0F) == 0.0F : dLower < dUpper) ? xLower : xUpper); } template class QuantTensorFunctor { public: explicit QuantTensorFunctor(const T bin_cnt, const T inv_s) : bin_cnt_(bin_cnt), inv_s_(inv_s) {} HOSTDEVICE T operator()(const T x) const { T out = bin_cnt_ * inv_s_ * x; out = roundWithTiesToEven(out); T max_bound = bin_cnt_; T min_bound = -bin_cnt_ - static_cast(1); out = out > max_bound ? max_bound : out; out = out < min_bound ? min_bound : out; return out; } private: T bin_cnt_; T inv_s_; }; 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, const int round_type, framework::Tensor *out); }; template struct ClipAndFakeQuantDequantFunctor { void operator()(const DeviceContext &ctx, const framework::Tensor &in, const framework::Tensor &scale, const int bin_cnt, int round_type, 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 FindChannelAbsMaxFunctor { void operator()(const DeviceContext &ctx, const framework::Tensor &in_tensor, const int quant_axis, T *out_abs_max); }; template struct ChannelClipAndFakeQuantFunctor { void operator()(const DeviceContext &ctx, const framework::Tensor &in, const framework::Tensor &scale, const int bin_cnt, const int round_type, const int quant_axis, framework::Tensor *out); }; template struct ChannelClipFakeQuantDequantFunctor { void operator()(const DeviceContext &ctx, const framework::Tensor &in, const framework::Tensor &scale, const int bin_cnt, int round_type, const int quant_axis, framework::Tensor *out); }; template struct FindMovingAverageAbsMaxFunctor { void operator()(const DeviceContext &ctx, const framework::Tensor &in_accum, const framework::Tensor &in_state, const T *cur_scale, const float rate, framework::Tensor *out_state, framework::Tensor *out_accum, framework::Tensor *out_scale); }; template class FakeAbsMaxKernelBase : 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 round_type = context.Attr("round_type"); 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); RunClipFunctor(dev_ctx, *in, *out_scale, bin_cnt, round_type, out); } virtual ~FakeAbsMaxKernelBase() = default; protected: virtual void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &scale, int bin_cnt, int round_type, framework::Tensor *out) const = 0; }; template class FakeQuantizeAbsMaxKernel : public FakeAbsMaxKernelBase { protected: void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &scale, int bin_cnt, int round_type, framework::Tensor *out) const override { ClipAndFakeQuantFunctor()( dev_ctx, in, scale, bin_cnt, round_type, out); } }; template class FakeQuantizeDequantizeAbsMaxKernel : public FakeAbsMaxKernelBase { protected: void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &scale, int bin_cnt, int round_type, framework::Tensor *out) const override { ClipAndFakeQuantDequantFunctor()( dev_ctx, in, scale, bin_cnt, round_type, 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_scale = context.Output("OutScale"); out->mutable_data(context.GetPlace()); int bit_length = context.Attr("bit_length"); int round_type = context.Attr("round_type"); int bin_cnt = std::pow(2, bit_length - 1) - 1; int quant_axis = context.Attr("quant_axis"); bool is_test = context.Attr("is_test"); auto &dev_ctx = context.template device_context(); if (!is_test) { T *out_scale_data = out_scale->mutable_data(context.GetPlace()); FindChannelAbsMaxFunctor()( dev_ctx, *in, quant_axis, out_scale_data); } ChannelClipAndFakeQuantFunctor()( dev_ctx, *in, *out_scale, bin_cnt, round_type, quant_axis, out); } }; template class FakeChannelWiseQuantizeDequantizeAbsMaxKernel : 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_scale_data = out_scale->mutable_data(context.GetPlace()); auto &dev_ctx = context.template device_context(); out->mutable_data(dev_ctx.GetPlace()); int bit_length = context.Attr("bit_length"); int round_type = context.Attr("round_type"); int bin_cnt = std::pow(2, bit_length - 1) - 1; int quant_axis = context.Attr("quant_axis"); FindChannelAbsMaxFunctor()( dev_ctx, *in, quant_axis, out_scale_data); ChannelClipFakeQuantDequantFunctor()( dev_ctx, *in, *out_scale, bin_cnt, round_type, quant_axis, 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 round_type = context.Attr("round_type"); 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, round_type, 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, round_type, out); } }; template class FakeMovingAverageAbsMaxKernelBase : 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 round_type = context.Attr("round_type"); int bin_cnt = std::pow(2, bit_length - 1) - 1; auto &dev_ctx = context.template device_context(); // testing if (is_test) { RunClipFunctor(dev_ctx, *in, *in_scale, bin_cnt, round_type, out); return; } // training auto *in_accum = context.Input("InAccum"); auto *in_state = context.Input("InState"); auto cur_scale = memory::Alloc(dev_ctx, 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); RunClipFunctor(dev_ctx, *in, *out_scale, bin_cnt, round_type, out); } virtual ~FakeMovingAverageAbsMaxKernelBase() = default; protected: virtual void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &in_scale, int bin_cnt, int round_type, framework::Tensor *out) const = 0; }; template class FakeQuantizeMovingAverageAbsMaxKernel : public FakeMovingAverageAbsMaxKernelBase { protected: void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &in_scale, int bin_cnt, int round_type, framework::Tensor *out) const override { ClipAndFakeQuantFunctor()( dev_ctx, in, in_scale, bin_cnt, round_type, out); } }; template class FakeQuantizeDequantizeMovingAverageAbsMaxKernel : public FakeMovingAverageAbsMaxKernelBase { protected: void RunClipFunctor(const DeviceContext &dev_ctx, const framework::Tensor &in, const framework::Tensor &in_scale, int bin_cnt, int round_type, framework::Tensor *out) const override { ClipAndFakeQuantDequantFunctor()( dev_ctx, in, in_scale, bin_cnt, round_type, out); } }; template class MovingAverageAbsMaxScaleKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { auto *in = context.Input("X"); auto &dev_ctx = context.template device_context(); if (context.HasOutput("Out")) { auto *out = context.Output("Out"); out->mutable_data(context.GetPlace()); framework::TensorCopy(*in, context.GetPlace(), dev_ctx, out); } bool is_test = context.Attr("is_test"); // testing if (is_test) { return; } // training auto *in_accum = context.Input("InAccum"); auto *in_state = context.Input("InState"); auto cur_scale = memory::Alloc(dev_ctx, 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); } }; template class StrightThroughEstimatorGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { auto *d_out = context.Input(framework::GradVarName("Out")); auto x_grad_name = framework::GradVarName("X"); auto *d_x = context.Output(x_grad_name); PADDLE_ENFORCE_NOT_NULL(d_x, platform::errors::PreconditionNotMet( "StrightThroughEstimatorGradKernel " "doesn't have the output named %s.", x_grad_name)); // Initialize dx as same as d_out d_x->mutable_data(context.GetPlace()); framework::TensorCopy(*d_out, context.GetPlace(), d_x); } }; } // namespace operators } // namespace paddle