| 557 | return 0; |
| 558 | } |
| 559 | std::shared_ptr<ConvolutionCommon::Int8Common> ConvolutionCommon::load(const Op* op, Backend* backend, bool forceFloat, bool forceInt8, void* weightPtr) { |
| 560 | auto conv = op->main_as_Convolution2D(); |
| 561 | auto quan = conv->quanParameter(); |
| 562 | std::shared_ptr<ConvolutionCommon::Int8Common> result(new Int8Common); |
| 563 | result->quan = quan; |
| 564 | size_t buffer_size = 0, alpha_size = 0; |
| 565 | const int8_t* buffer_ptr = nullptr; |
| 566 | const float* alpha_ptr = nullptr; |
| 567 | std::unique_ptr<int8_t[]> external_buffer; |
| 568 | size_t weightLength = 0; |
| 569 | int8_t *buffer = nullptr; |
| 570 | bool useCachedMmap = false; |
| 571 | if (backend && backend->getRuntime()) { |
| 572 | useCachedMmap = backend->getRuntime()->hint().useCachedMmap > 1; |
| 573 | } |
| 574 | if (USE_EXTERNAL_DATA(conv) && op->externalPath() && quan->type() == 8) { |
| 575 | std::unique_ptr<FileLoader> external(new FileLoader(op->externalPath()->c_str())); |
| 576 | auto param = op->main_as_Convolution2D(); |
| 577 | external->offset(param->external()->data()[0]); |
| 578 | if(weightPtr != nullptr) { |
| 579 | result->weightFloat.set((float *)weightPtr, false); |
| 580 | } else { |
| 581 | result->weightFloat.reset((int)(param->external()->data()[1] / sizeof(float))); |
| 582 | } |
| 583 | external->read((char*)(result->weightFloat.get()), param->external()->data()[1]); |
| 584 | return result; |
| 585 | } |
| 586 | // scaleStorage scalar survives weight externalization, unlike the alphaFp16 vector. |
| 587 | const bool alphaIsFp16 = (quan->scaleStorage() == ScaleStorageType_FP16); |
| 588 | if (USE_EXTERNAL_DATA(conv) && (op->externalPath() || useCachedMmap) && quan->buffer() == nullptr) { |
| 589 | auto external_info = conv->external()->data(); |
| 590 | buffer_size = external_info[1]; |
| 591 | const size_t alphaElemBytes = alphaIsFp16 ? sizeof(uint16_t) : sizeof(float); |
| 592 | alpha_size = external_info[2] / alphaElemBytes; |
| 593 | result->alphaSize = alpha_size; |
| 594 | if (useCachedMmap) { |
| 595 | weightLength = conv->common()->inputCount() * conv->common()->outputCount() * conv->common()->kernelX() * conv->common()->kernelY(); |
| 596 | result->originBits = getQuantBitFromExternalFile(op); |
| 597 | result->canUseInt4 = result->originBits <= 4; |
| 598 | } else { |
| 599 | // external data |
| 600 | std::unique_ptr<FileLoader> external_file(new FileLoader(op->externalPath()->c_str())); |
| 601 | external_file->offset(external_info[0]); |
| 602 | if (0 != buffer_size) { |
| 603 | if (1 == quan->type() && !forceFloat) { |
| 604 | buffer = IDSTDecoder::ReadQuanData_c(external_file.get(), &weightLength, result.get(), quan, forceInt8, forceFloat, weightPtr); |
| 605 | if(weightLength == 0){ |
| 606 | MNN_PRINT("ReadQuanData_c return weightLength is 0, maybe the weight data is invalid\n"); |
| 607 | return nullptr; |
| 608 | } |
| 609 | } else { |
| 610 | external_buffer.reset(new int8_t[buffer_size]); |
| 611 | buffer_ptr = external_buffer.get(); |
| 612 | external_file->read((char*)buffer_ptr, buffer_size); |
| 613 | } |
| 614 | } |
| 615 | if (0 != alpha_size) { |
| 616 | if (alphaIsFp16) { |
nothing calls this directly
no test coverage detected