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

Function QuantizeWeightADMM

tools/quantization/quantizeWeight.cpp:73–102  ·  view source on GitHub ↗

weight format is [co, ci, kh, kw]

Source from the content-addressed store, hash-verified

71
72// weight format is [co, ci, kh, kw]
73int QuantizeWeightADMM(const float* weight, const int weightNum, int8_t* quantizedWeight, float* alpha,
74 const int kernelNum, const float weightClampValue) {
75 // channels: co
76 DCHECK((weightNum % kernelNum) == 0) << "weight size error!";
77 const int kernelDim = weightNum / kernelNum; // ci * kh * kw
78
79 InitAlpha(weight, weightNum, kernelNum, alpha, weightClampValue);
80
81 int iter = 0;
82 float diffRate = 1;
83 float preSum = 0;
84 float curSum = 0;
85 const int maxIter = 1000;
86
87 for (int i = 0; i < weightNum; i++){
88 preSum += std::fabs(weight[i]);
89 }
90 // update weights quan
91 while(iter < maxIter) {
92 UpdateQuantizedWeights(weight, weightNum, kernelNum, alpha, weightClampValue, quantizedWeight);
93 UpdateAlpha(weight, weightNum, kernelNum, alpha, quantizedWeight);
94 iter++;
95 }
96
97 for (int i = 0; i < weightNum; i++){
98 curSum += std::fabs(quantizedWeight[i]*alpha[i/kernelDim]);
99 }
100 DLOG(INFO) << "iter: " << iter << " with diff "<< preSum-curSum;
101 return 0;
102}
103
104// weight format is [co, ci, kh, kw]
105int SymmetricQuantizeWeight(const float* weight, const int size, int8_t* quantizedWeight, float* scale,

Callers 3

_insertScaleMethod · 0.85
QuantizeConvPerChannelFunction · 0.85
QuantizeDepthwiseConvFunction · 0.85

Calls 4

InitAlphaFunction · 0.85
fabsFunction · 0.85
UpdateQuantizedWeightsFunction · 0.85
UpdateAlphaFunction · 0.85

Tested by

no test coverage detected