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

Function ReadQuanData_c

source/core/ConvolutionCommon.cpp:230–389  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

228}
229
230static int8_t *ReadQuanData_c(BaseLoader* s, size_t* len, ConvolutionCommon::Int8Common* result, const IDSTQuan* quan, bool forceQuant, bool forceFloat, void* outputPtr) {
231 int8_t *blob = nullptr;
232 uint8_t *idxBuf = nullptr;
233 size_t dataCnt = 1;
234 bool shapeInt32 = quan->shapeInt32();
235 do {
236 // blob shape
237 unsigned int shape[32] = {0};
238 uint32_t shapeDim = (uint32_t)ReadBlobDim(s, shape, 32, shapeInt32);
239 if (shapeDim == 0 || shapeDim > 32)
240 break;
241 for (uint32_t i = 0; i < shapeDim; i++)
242 dataCnt *= shape[i];
243
244 // sample
245 uint32_t sampleCnt = 0;
246 s->read((char*)&sampleCnt, 1);
247 if (sampleCnt == 0) {
248 sampleCnt = 256;
249 }
250 result->weightMap.resize(sampleCnt);
251 auto samples = result->weightMap.data();
252 if (samples == nullptr)
253 break;
254 s->read((char*)samples, sampleCnt);
255 SimpleRank(samples, sampleCnt, 1);
256 uint32_t idxBitsCnt = atLestBitsCnt(sampleCnt);
257 idxBitsCnt = idxBitsCnt < 1 ? 1 : idxBitsCnt;
258 bool linear = isLinearSample(result->weightMap, idxBitsCnt);
259 // index
260 bool canSetOutputPtr = outputPtr != nullptr;
261 if(!forceQuant && (forceFloat || !quan->has_scaleInt())) {
262 canSetOutputPtr = false;
263 }
264 bool directSet = canSetOutputPtr && linear && (idxBitsCnt == 4 || idxBitsCnt == 8) && (forceQuant || idxBitsCnt == 8);
265 size_t idxBufSize = ceil(idxBitsCnt * dataCnt * 0.125);
266 if(directSet) {
267 idxBuf = (uint8_t *)outputPtr;
268 } else {
269 idxBuf = (uint8_t *)MNNMemoryAllocAlign(idxBufSize, MNN_MEMORY_ALIGN_DEFAULT);
270 }
271 if (nullptr == idxBuf) {
272 MNN_ERROR("Not enought memory\n");
273 break;
274 }
275 s->read((char*)idxBuf, idxBufSize);
276 if (linear) {
277 result->originBits = idxBitsCnt;
278 }
279 if (linear && (idxBitsCnt == 4 || idxBitsCnt == 8)) {
280 if (!forceQuant && idxBitsCnt == 4) {
281 // back to float, 4bit to 8bit
282 *len = dataCnt;
283 if(canSetOutputPtr) {
284 blob = (int8_t *)outputPtr;
285 } else {
286 blob = (int8_t *)MNNMemoryAllocAlignZeroAlign((size_t)UP_DIV(dataCnt, 2) * 2);
287 }

Callers 1

loadMethod · 0.85

Calls 12

ReadBlobDimFunction · 0.85
SimpleRankFunction · 0.85
atLestBitsCntFunction · 0.85
isLinearSampleFunction · 0.85
MNNMemoryAllocAlignFunction · 0.85
SplitBufToArrayFunction · 0.85
MNNMemoryFreeAlignFunction · 0.85
ceilFunction · 0.50
readMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected