| 41 | } |
| 42 | |
| 43 | static MNN::Quantization::HQQQuantizer::QuantizationResult _HQQQuant(const std::vector<float>& weights, int weightQuantBits, int weightQuantBlock, bool asymmetricQuantFlag) { |
| 44 | MNN::Quantization::HQQQuantizer::QuantizationConfig hqqConfig; |
| 45 | hqqConfig.bits = weightQuantBits; |
| 46 | hqqConfig.group_size = weightQuantBlock; |
| 47 | MNN::Quantization::HQQQuantizer hqq(hqqConfig); |
| 48 | auto res = hqq.quantize(weights); |
| 49 | #if 0 |
| 50 | auto dequantized_weights = hqq.dequantize(res); |
| 51 | // 计算量化误差 |
| 52 | float mse = 0.0f; |
| 53 | float max_abs_error = 0.0f; |
| 54 | |
| 55 | for (size_t i = 0; i < weights.size(); ++i) { |
| 56 | float error = weights[i] - dequantized_weights->readMap<float>()[i]; |
| 57 | float abs_error = std::abs(error); |
| 58 | |
| 59 | mse += error * error; |
| 60 | max_abs_error = std::max(max_abs_error, abs_error); |
| 61 | } |
| 62 | |
| 63 | mse /= weights.size(); |
| 64 | float rmse = std::sqrt(mse); |
| 65 | |
| 66 | std::cout << "量化误差分析:" << std::endl; |
| 67 | std::cout << " 均方误差 (MSE): " << mse << std::endl; |
| 68 | std::cout << " 均方根误差 (RMSE): " << rmse << std::endl; |
| 69 | std::cout << " 最大绝对误差: " << max_abs_error << std::endl; |
| 70 | #endif |
| 71 | return res; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | void WeightQuantAndCoding(std::unique_ptr<MNN::OpT>& op, const modelConfig& config, const PostTreatContext* context) { |
| 76 | const auto opType = op->type; |
no test coverage detected