MCPcopy Create free account
hub / github.com/alibaba/MNN / SymmetricQuantizeWeight

Function SymmetricQuantizeWeight

tools/cpp/revertMNNModel.cpp:24–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22#include "core/ConvolutionCommon.hpp"
23
24int SymmetricQuantizeWeight(const float* weight, const int size, int8_t* quantizedWeight, float* scale,
25 const int channels, float weightClampValue) {
26 const int channelStride = size / channels;
27 const int quantizedMaxValue = weightClampValue;
28
29 for (int c = 0; c < channels; ++c) {
30 const auto weightChannelStart = weight + c * channelStride;
31 auto quantizedWeightChannelStart = quantizedWeight + c * channelStride;
32 auto minmaxValue = std::minmax_element(weightChannelStart, weightChannelStart + channelStride);
33 const float dataAbsMax = std::fmax(std::fabs(*minmaxValue.first), std::fabs(*minmaxValue.second));
34
35 float scaleDataToInt8 = 1.0f;
36 if (dataAbsMax == 0) {
37 scale[c] = 0.0f;
38 } else {
39 scale[c] = dataAbsMax / quantizedMaxValue;
40 scaleDataToInt8 = quantizedMaxValue / dataAbsMax;
41 }
42
43 for (int i = 0; i < channelStride; ++i) {
44 const int32_t quantizedInt8Value = static_cast<int32_t>(roundf(weightChannelStart[i] * scaleDataToInt8));
45 quantizedWeightChannelStart[i] =
46 std::min(quantizedMaxValue, std::max(-quantizedMaxValue, quantizedInt8Value));
47 }
48 }
49
50 return 0;
51}
52
53
54Revert::Revert(const char* originalModelFileName) {

Callers 1

Calls 4

fmaxFunction · 0.85
fabsFunction · 0.85
minFunction · 0.50
maxFunction · 0.50

Tested by

no test coverage detected