|
| 1 | +// Copyright (c) 2024 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 | +#include "dtype.h" |
| 15 | +#include "matmul_helper.h" |
| 16 | +#include "my_types.h" |
| 17 | +#include "paddle/extension.h" |
| 18 | +git adtemplate <typename T> |
| 19 | +void AvxCompute(const paddle::Tensor &x, |
| 20 | + const paddle::Tensor &weight, |
| 21 | + bool trans, |
| 22 | + const std::string alog, |
| 23 | + paddle::Tensor &out, |
| 24 | + xft::Matrix<T> &quantizedWeight, |
| 25 | + xft::Vector<float> &WeightScale, |
| 26 | + xft::Vector<float> &WeightZero, |
| 27 | + xft::Vector<float> &WeightSum, |
| 28 | + MMHelper *mmHelper) { |
| 29 | + auto out_data = out.data<float>(); |
| 30 | + const float *x_data = reinterpret_cast<const float *>(x.data<float>()); |
| 31 | + const float *bias_data = nullptr; |
| 32 | + int m = 1; |
| 33 | + for (int i = 0; i < x.shape().size() - 1; i++) { |
| 34 | + m = m * x.shape()[i]; |
| 35 | + } |
| 36 | + int k = x.shape()[x.shape().size() - 1]; |
| 37 | + int l = weight.shape()[1]; |
| 38 | + int n = weight.shape()[1]; |
| 39 | + |
| 40 | + mmHelper->compute(false, |
| 41 | + m, |
| 42 | + n, |
| 43 | + k, |
| 44 | + 1.0f, |
| 45 | + x_data, |
| 46 | + k, |
| 47 | + quantizedWeight.Data(), |
| 48 | + WeightScale.Data(), |
| 49 | + WeightZero.Data(), |
| 50 | + WeightSum.Data(), |
| 51 | + 0.0, |
| 52 | + out_data, |
| 53 | + l); |
| 54 | +}; |
| 55 | +template <typename T> |
| 56 | +void AvxWeightOnly(const paddle::Tensor &x, |
| 57 | + const paddle::Tensor &weight, |
| 58 | + bool trans, |
| 59 | + const std::string alog, |
| 60 | + paddle::Tensor &out) { |
| 61 | + static std::unordered_map<std::string, |
| 62 | + std::tuple<xft::Matrix<T> *, |
| 63 | + xft::Vector<float> *, |
| 64 | + xft::Vector<float> *, |
| 65 | + xft::Vector<float> *>> |
| 66 | + weight_only_hub; |
| 67 | + std::stringstream weights_addr; |
| 68 | + weights_addr << weight.data<float>() << alog; |
| 69 | + std::string weight_only_key = weights_addr.str(); |
| 70 | + auto it_created = weight_only_hub.find(weight_only_key); |
| 71 | + static MMHelper *mmHelper; |
| 72 | + int rows = weight.shape()[0], cols = weight.shape()[1]; |
| 73 | + xft::Vector<float> *WeightScale = |
| 74 | + new xft::Vector<float>(); // if weight is int8 |
| 75 | + xft::Vector<float> *WeightZero = |
| 76 | + new xft::Vector<float>(); // if weight is int8 |
| 77 | + xft::Vector<float> *WeightSum = |
| 78 | + new xft::Vector<float>(); // if weight is int8 |
| 79 | + xft::Matrix<T> *quantizedWeight = new xft::Matrix<T>(); |
| 80 | + if (it_created == weight_only_hub.end()) { |
| 81 | + auto weight_ptr = reinterpret_cast<const float *>(weight.data<float>()); |
| 82 | + xft::Matrix<T> convertedWeight; |
| 83 | + mmHelper = new MMHelper(xft::DeviceKind::iCPU, 0); |
| 84 | + mmHelper->convertWeight(trans, |
| 85 | + rows, |
| 86 | + cols, |
| 87 | + weight_ptr, |
| 88 | + nullptr, |
| 89 | + nullptr, |
| 90 | + convertedWeight, |
| 91 | + *WeightScale, |
| 92 | + *WeightZero, |
| 93 | + *WeightSum); |
| 94 | + quantizedWeight->Resize(rows, cols); |
| 95 | + mmHelper->packWeight(trans, convertedWeight, *quantizedWeight); |
| 96 | + weight_only_hub[weight_only_key] = |
| 97 | + std::make_tuple(quantizedWeight, WeightScale, WeightZero, WeightSum); |
| 98 | + AvxCompute<T>(x, |
| 99 | + weight, |
| 100 | + trans, |
| 101 | + alog, |
| 102 | + out, |
| 103 | + *quantizedWeight, |
| 104 | + *WeightScale, |
| 105 | + *WeightZero, |
| 106 | + *WeightSum, |
| 107 | + mmHelper); |
| 108 | + } else { |
| 109 | + AvxCompute<T>(x, |
| 110 | + weight, |
| 111 | + trans, |
| 112 | + alog, |
| 113 | + out, |
| 114 | + *(std::get<0>(it_created->second)), |
| 115 | + *(std::get<1>(it_created->second)), |
| 116 | + *(std::get<2>(it_created->second)), |
| 117 | + *(std::get<3>(it_created->second)), |
| 118 | + mmHelper); |
| 119 | + } |
| 120 | +} |
| 121 | +std::vector<paddle::Tensor> InvokeAvxWeightOnly(const paddle::Tensor &x, |
| 122 | + const paddle::Tensor &weight, |
| 123 | + const std::string &alog, |
| 124 | + bool trans) { |
| 125 | + auto out_shape = x.shape(); |
| 126 | + out_shape[out_shape.size() - 1] = weight.shape()[1]; |
| 127 | + auto out = paddle::empty(out_shape, x.dtype(), paddle::CPUPlace()); |
| 128 | + if (alog == "int8") { |
| 129 | + AvxWeightOnly<int8_t>(x, weight, trans, alog, out); |
| 130 | + } else if (alog == "fp16") { |
| 131 | + AvxWeightOnly<float16_t>(x, weight, trans, alog, out); |
| 132 | + } else { |
| 133 | + AvxWeightOnly<float16_t>(x, weight, trans, alog, out); |
| 134 | + } |
| 135 | + return {out}; |
| 136 | +} |
| 137 | + |
| 138 | +std::vector<std::vector<int64_t>> AvxWeightOnlyInferShape( |
| 139 | + std::vector<int64_t> x_shape, |
| 140 | + std::vector<int64_t> weigh_shape) { |
| 141 | + int m = 1; |
| 142 | + for (int i = 0; i < x_shape.size() - 1; i++) { |
| 143 | + m = m * x_shape[i]; |
| 144 | + } |
| 145 | + return {std::vector<int64_t>{m, weigh_shape[1]}}; |
| 146 | +} |
| 147 | + |
| 148 | +std::vector<paddle::DataType> AvxWeightOnlyInferDtype( |
| 149 | + paddle::DataType x_dtype, |
| 150 | + paddle::DataType weight_dtype) { |
| 151 | + return {x_dtype}; |
| 152 | +} |
| 153 | + |
| 154 | +PD_BUILD_OP(avx_weight_only) |
| 155 | + .Inputs({"x", "weight"}) |
| 156 | + .Outputs({"out"}) |
| 157 | + .Attrs({"alog: std::string", "trans:bool"}) |
| 158 | + .SetKernelFn(PD_KERNEL(InvokeAvxWeightOnly)) |
| 159 | + .SetInferShapeFn(PD_INFER_SHAPE(AvxWeightOnlyInferShape)) |
| 160 | + .SetInferDtypeFn(PD_INFER_DTYPE(AvxWeightOnlyInferDtype)); |
0 commit comments