|
| 1 | +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <vector> |
| 19 | +#include "paddle/fluid/operators/fused/cublaslt.h" |
| 20 | +#include "paddle/fluid/operators/fused/quant_dequant_kernel.h" |
| 21 | +#include "paddle/fluid/platform/device/gpu/gpu_info.h" |
| 22 | +#include "paddle/fluid/platform/float16.h" |
| 23 | +#include "paddle/phi/kernels/funcs/broadcast_function.h" |
| 24 | +#include "paddle/phi/kernels/funcs/elementwise_functor.h" |
| 25 | + |
| 26 | +namespace paddle { |
| 27 | +namespace operators { |
| 28 | + |
| 29 | +using Tensor = framework::Tensor; |
| 30 | + |
| 31 | +template <typename T> |
| 32 | +class AttnMatmulINT8 { |
| 33 | + public: |
| 34 | + AttnMatmulINT8( |
| 35 | + const phi::GPUContext& dev_ctx, int m, int n, int k, bool compute_bias) |
| 36 | + : dev_ctx_(dev_ctx), m_(m), n_(n), k_(k), compute_bias_(compute_bias) { |
| 37 | + auto helper = std::make_shared<CublasLtHelper>(m, k, n); |
| 38 | + helpers_.emplace_back(helper); |
| 39 | + } |
| 40 | + ~AttnMatmulINT8() {} |
| 41 | + |
| 42 | + // This function is used to execute GEMM, with input and output's types are |
| 43 | + // both T. |
| 44 | + void ComputeForward(const framework::Tensor* weight, |
| 45 | + const framework::Tensor* input, |
| 46 | + framework::Tensor* input_tmp, |
| 47 | + const framework::Tensor* bias, |
| 48 | + framework::Tensor* output, |
| 49 | + framework::Tensor* output_tmp, |
| 50 | + framework::Tensor* bias_out, |
| 51 | + const float quant_in_scale, |
| 52 | + const framework::Tensor* dequant_out_scale, |
| 53 | + const int quant_out_scale_offset, |
| 54 | + const int quant_round_type = 1, |
| 55 | + const float quant_max_bound = 127.0, |
| 56 | + const float quant_min_bound = -127.0) { |
| 57 | + quantize_kernel_launcher<T>(input->data<T>(), |
| 58 | + input_tmp->data<int8_t>(), |
| 59 | + quant_in_scale, |
| 60 | + m_, |
| 61 | + k_, |
| 62 | + quant_round_type, |
| 63 | + quant_max_bound, |
| 64 | + quant_min_bound, |
| 65 | + dev_ctx_.stream()); |
| 66 | + |
| 67 | + helpers_[0]->GEMM(input_tmp->data<int8_t>(), |
| 68 | + weight->data<int8_t>(), |
| 69 | + output_tmp->data<int32_t>(), |
| 70 | + dev_ctx_.stream()); |
| 71 | + |
| 72 | + dequantize_kernel_launcher<T>(output_tmp->data<int32_t>(), |
| 73 | + output->data<T>(), |
| 74 | + m_, |
| 75 | + n_, |
| 76 | + dev_ctx_.stream(), |
| 77 | + quant_in_scale, |
| 78 | + dequant_out_scale->data<float>(), |
| 79 | + quant_out_scale_offset); |
| 80 | + |
| 81 | + if (compute_bias_) { |
| 82 | + // bias_out = output + bias |
| 83 | + std::vector<const framework::Tensor*> ins = {output, bias}; |
| 84 | + std::vector<framework::Tensor*> outs = {bias_out}; |
| 85 | + phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>( |
| 86 | + dev_ctx_, ins, &outs, -1, phi::funcs::AddFunctor<T>()); |
| 87 | + PADDLE_ENFORCE_EQ(cudaGetLastError(), |
| 88 | + cudaSuccess, |
| 89 | + platform::errors::Fatal( |
| 90 | + "cuda error occured after computing bias. " |
| 91 | + "But it does not mean this error is caused by " |
| 92 | + "bias computing")); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // This function is used to execute GEMM, with input and output's types are |
| 97 | + // both INT8. |
| 98 | + void ComputeForwardINT8ToINT8(const framework::Tensor* weight, |
| 99 | + framework::Tensor* input, |
| 100 | + const framework::Tensor* bias, |
| 101 | + framework::Tensor* output, |
| 102 | + framework::Tensor* bias_out) { |
| 103 | + helpers_[0]->GEMM(input->data<int8_t>(), |
| 104 | + weight->data<int8_t>(), |
| 105 | + output->data<int32_t>(), |
| 106 | + dev_ctx_.stream()); |
| 107 | + } |
| 108 | + |
| 109 | + // This function is used to execute GEMM, with input and output's types are |
| 110 | + // INT8 and T. |
| 111 | + void ComputeForwardINT8ToT(const framework::Tensor* weight, |
| 112 | + const float quant_in_scale, |
| 113 | + framework::Tensor* input, |
| 114 | + const framework::Tensor* bias, |
| 115 | + framework::Tensor* output, |
| 116 | + framework::Tensor* output_tmp, |
| 117 | + framework::Tensor* bias_out, |
| 118 | + const framework::Tensor* dequant_out_scale, |
| 119 | + const int quant_out_scale_offset) { |
| 120 | + helpers_[0]->GEMM(input->data<int8_t>(), |
| 121 | + weight->data<int8_t>(), |
| 122 | + output_tmp->data<int32_t>(), |
| 123 | + dev_ctx_.stream()); |
| 124 | + |
| 125 | + dequantize_kernel_launcher<T>(output_tmp->data<int32_t>(), |
| 126 | + output->data<T>(), |
| 127 | + m_, |
| 128 | + n_, |
| 129 | + dev_ctx_.stream(), |
| 130 | + quant_in_scale, |
| 131 | + dequant_out_scale->data<float>(), |
| 132 | + quant_out_scale_offset); |
| 133 | + |
| 134 | + if (compute_bias_) { |
| 135 | + // bias_out = output + bias |
| 136 | + std::vector<const framework::Tensor*> ins = {output, bias}; |
| 137 | + std::vector<framework::Tensor*> outs = {bias_out}; |
| 138 | + phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>( |
| 139 | + dev_ctx_, ins, &outs, -1, phi::funcs::AddFunctor<T>()); |
| 140 | + PADDLE_ENFORCE_EQ(cudaGetLastError(), |
| 141 | + cudaSuccess, |
| 142 | + platform::errors::Fatal( |
| 143 | + "cuda error occured after computing bias. " |
| 144 | + "But it does not mean this error is caused by " |
| 145 | + "bias computing")); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // This function is used to execute GEMM, with input and output's types are T |
| 150 | + // and INT8. |
| 151 | + void ComputeForwardTToINT8(const framework::Tensor* weight, |
| 152 | + const float quant_in_scale, |
| 153 | + const framework::Tensor* input, |
| 154 | + framework::Tensor* input_tmp, |
| 155 | + const framework::Tensor* bias, |
| 156 | + framework::Tensor* output, |
| 157 | + framework::Tensor* bias_out, |
| 158 | + const int quant_round_type = 1, |
| 159 | + const float quant_max_bound = 127.0, |
| 160 | + const float quant_min_bound = -127.0) { |
| 161 | + quantize_kernel_launcher<T>(input->data<T>(), |
| 162 | + input_tmp->data<int8_t>(), |
| 163 | + quant_in_scale, |
| 164 | + m_, |
| 165 | + k_, |
| 166 | + quant_round_type, |
| 167 | + quant_max_bound, |
| 168 | + quant_min_bound, |
| 169 | + dev_ctx_.stream()); |
| 170 | + |
| 171 | + helpers_[0]->GEMM(input_tmp->data<int8_t>(), |
| 172 | + weight->data<int8_t>(), |
| 173 | + output->data<int32_t>(), |
| 174 | + dev_ctx_.stream()); |
| 175 | + } |
| 176 | + |
| 177 | + private: |
| 178 | + const phi::GPUContext& dev_ctx_; |
| 179 | + |
| 180 | + int m_; // m |
| 181 | + int n_; // n |
| 182 | + int k_; // k |
| 183 | + |
| 184 | + int compute_bias_; |
| 185 | + std::vector<std::shared_ptr<CublasLtHelper>> helpers_; |
| 186 | +}; |
| 187 | + |
| 188 | +} // namespace operators |
| 189 | +} // namespace paddle |
0 commit comments