2021-06-11 17:17:13 +08:00
|
|
|
// ConvInt8TiledExecutor.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/5/17.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2023-06-16 09:42:45 +08:00
|
|
|
#include "ConvInt8TiledExecutor.hpp"
|
|
|
|
#include "ConvolutionTiledExecutor.hpp"
|
2021-06-11 17:17:13 +08:00
|
|
|
#include "core/Macro.h"
|
2023-06-16 09:42:45 +08:00
|
|
|
#include "core/BufferAllocator.hpp"
|
2021-06-11 17:17:13 +08:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include "backend/cpu/CPUBackend.hpp"
|
|
|
|
#include "core/Concurrency.h"
|
|
|
|
#include "core/TensorUtils.hpp"
|
2025-02-12 11:14:19 +08:00
|
|
|
|
|
|
|
#define QUANT_INFO_BYTES 4
|
2021-06-11 17:17:13 +08:00
|
|
|
namespace MNN {
|
|
|
|
|
2024-08-24 15:46:21 +08:00
|
|
|
ConvInt8TiledExecutor::ConvInt8TiledExecutor(Backend* backend, const Op* op): CPUConvolution(op->main_as_Convolution2D()->common(), backend) {}
|
|
|
|
|
|
|
|
ConvInt8TiledExecutor::ConvInt8TiledExecutor(Backend* backend, const Op* op, std::shared_ptr<ResourceInt8> res): CPUConvolution(op->main_as_Convolution2D()->common(), backend), mResourceInt8(res) {
|
2024-09-12 12:57:57 +08:00
|
|
|
if (!res->mDynamicQuant) {
|
|
|
|
mMutableResource.reset(new MutableResourceInt8(res, backend));
|
|
|
|
mValid = mMutableResource->mValid;
|
|
|
|
}
|
2021-09-18 15:52:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConvInt8TiledExecutor::~ConvInt8TiledExecutor() {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConvInt8TiledExecutor::onClone(Backend* bn, const Op* op, Execution** dst) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode ConvInt8TiledExecutor::onResize(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
|
2024-09-12 12:57:57 +08:00
|
|
|
if (nullptr != mMutableResource) {
|
|
|
|
mMutableResource->updateInputOutputScale(TensorUtils::getQuantInfo(inputs[0]), TensorUtils::getQuantInfo(outputs[0]));
|
|
|
|
}
|
2021-09-18 15:52:30 +08:00
|
|
|
CPUConvolution::onResize(inputs, outputs);
|
2023-06-16 09:42:45 +08:00
|
|
|
ConvolutionTiledExecutor::setIm2ColParameter(mIm2ColParamter, mCommon, inputs[0], outputs[0], mPadX, mPadY, static_cast<CPUBackend*>(backend())->functions(), static_cast<CPUBackend*>(backend())->int8Functions());
|
2021-09-18 15:52:30 +08:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
void ConvInt8TiledExecutor::initializeConvInt8QuantInfo(std::shared_ptr<CPUConvolution::ResourceInt8> &resourceInt8, const Convolution2D *conv2D) {
|
|
|
|
// input/output scale&zeorpoint
|
|
|
|
resourceInt8->mActBits = 8;
|
|
|
|
resourceInt8->mWeightAsymmetricQuant = false;
|
|
|
|
if (conv2D->symmetricQuan()) {
|
|
|
|
resourceInt8->mActBits = conv2D->symmetricQuan()->nbits();
|
|
|
|
}
|
|
|
|
if (conv2D->bias() && conv2D->quanParameter()->alpha()) {
|
|
|
|
resourceInt8->mUseConvQuan = false;
|
|
|
|
}
|
|
|
|
resourceInt8->mInputZeroPoint = 0;
|
|
|
|
resourceInt8->mOutputZeroPoint = 0;
|
|
|
|
resourceInt8->mClampMin = -128;
|
|
|
|
resourceInt8->mClampMax = 127;
|
|
|
|
if (conv2D->symmetricQuan()) {
|
|
|
|
resourceInt8->mInputZeroPoint = conv2D->symmetricQuan()->zeroPoint();
|
|
|
|
resourceInt8->mOutputZeroPoint = conv2D->symmetricQuan()->outputZeroPoint();
|
|
|
|
resourceInt8->mClampMin = conv2D->symmetricQuan()->clampMin();
|
|
|
|
resourceInt8->mClampMax = conv2D->symmetricQuan()->clampMax();
|
|
|
|
}
|
|
|
|
if (conv2D->quanParameter() != nullptr) {
|
|
|
|
resourceInt8->mInputScale = conv2D->quanParameter()->scaleIn();
|
|
|
|
resourceInt8->mOutputScale = conv2D->quanParameter()->scaleOut();
|
|
|
|
}
|
|
|
|
resourceInt8->mRelu = conv2D->common()->relu() || conv2D->common()->relu6();
|
|
|
|
if (conv2D->symmetricQuan() && conv2D->symmetricQuan()->outputDataType() == MNN::DataType_DT_FLOAT) {
|
|
|
|
resourceInt8->mOutputZeroPoint = 0;
|
|
|
|
resourceInt8->mOutputScale = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-12 11:35:16 +08:00
|
|
|
void ConvInt8TiledExecutor::reorderWeight(uint8_t* dst, const uint8_t* src, int32_t* info, int32_t initval, float* kernelsum, weightSummerFuncion summerFunc) {
|
2025-05-08 12:39:44 +08:00
|
|
|
// weight shape = {UP_DIV(oc, UNIT), blockNum, kernelCount* UP_DIV(ic / blockNum, SRC_UNIT), UNIT, SRC_UNIT};
|
2025-02-12 11:14:19 +08:00
|
|
|
MNN_ASSERT(dst != nullptr && src != nullptr);
|
|
|
|
|
|
|
|
int blockNum = info[0];
|
|
|
|
int oc = info[1];
|
|
|
|
int ic = info[2];
|
|
|
|
int kernelCount = info[3];
|
|
|
|
int UNIT = info[4];
|
|
|
|
int SRC_UNIT = info[5];
|
|
|
|
|
|
|
|
int blockL = UP_DIV(ic / blockNum, SRC_UNIT) * kernelCount;
|
2025-05-08 12:39:44 +08:00
|
|
|
int stride0 = blockNum * SRC_UNIT * blockL * UNIT; // weight->stride(0)
|
2025-02-12 11:14:19 +08:00
|
|
|
int stride1 = blockL * SRC_UNIT * UNIT; // weight->stride(1)
|
|
|
|
int stride2 = UNIT * SRC_UNIT; // weight->stride(2)
|
2025-05-08 12:39:44 +08:00
|
|
|
int weightlen = stride0 * UP_DIV(oc, UNIT);
|
2025-02-12 11:14:19 +08:00
|
|
|
memset(dst, initval, weightlen);
|
|
|
|
|
2025-03-12 11:35:16 +08:00
|
|
|
auto hU = UP_DIV(oc, UNIT);
|
|
|
|
auto lU = UP_DIV(ic / blockNum, SRC_UNIT) * kernelCount;
|
|
|
|
bool fast = (kernelCount == 1 && ROUND_UP(oc, UNIT) == oc && ROUND_UP(ic, SRC_UNIT) == ic);
|
|
|
|
if (fast) {
|
|
|
|
for (int i = 0; i < hU; ++i) {
|
|
|
|
for (int k = 0; k < UNIT; ++k) {
|
|
|
|
for (int bl = 0; bl < blockNum; ++bl) {
|
2025-05-08 12:39:44 +08:00
|
|
|
for (int j = 0; j < blockL; ++j) {
|
2025-03-12 11:35:16 +08:00
|
|
|
int srcindex = (i * UNIT + k) * ic + bl * (lU * SRC_UNIT) + j * SRC_UNIT;
|
2025-05-08 12:39:44 +08:00
|
|
|
int dstindex = i * stride0 + bl * stride1 + j * stride2 + k * SRC_UNIT;
|
2025-03-12 11:35:16 +08:00
|
|
|
memcpy(dst + dstindex, src + srcindex, SRC_UNIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AutoStorage<uint8_t> tmpBuffer(ic * kernelCount * ROUND_UP(oc, UNIT));
|
|
|
|
memset(tmpBuffer.get(), 0, tmpBuffer.size());
|
|
|
|
|
|
|
|
auto area = ic * kernelCount;
|
|
|
|
// [oc, ic, k2] -> [hU, ic, k2, hP]
|
|
|
|
for (int i = 0; i < oc; ++i) {
|
|
|
|
auto outId = i / UNIT;
|
|
|
|
auto inId = i % UNIT;
|
|
|
|
for (int j = 0; j < area; ++j) {
|
|
|
|
tmpBuffer.get()[outId * area * UNIT + j * UNIT + inId] = src[i * area + j];
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
// [hU, ic, (k2, hP)] -> [hU, blocknum, lU, (k2, hP), lP]
|
2025-03-12 11:35:16 +08:00
|
|
|
AutoStorage<uint8_t> packedBuffer(weightlen);
|
|
|
|
memset(packedBuffer.get(), 0, weightlen);
|
|
|
|
area = kernelCount * UNIT;
|
2025-04-28 11:38:44 +08:00
|
|
|
auto blockic = ic / blockNum;
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int i = 0; i < hU; ++i) {
|
|
|
|
for (int j = 0; j < ic; ++j) {
|
2025-04-28 11:38:44 +08:00
|
|
|
int bk = j / blockic;
|
|
|
|
int blu = (j % blockic) / SRC_UNIT;
|
|
|
|
int blp = (j % blockic) % SRC_UNIT;
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int k = 0; k < area; ++k) {
|
2025-05-08 12:39:44 +08:00
|
|
|
int dstindex = i * stride0 + bk * stride1 + blu * kernelCount * stride2 + k * SRC_UNIT + blp;
|
2025-03-12 11:35:16 +08:00
|
|
|
int srcindex = i * ic * area + j * area + k;
|
|
|
|
packedBuffer.get()[dstindex] = tmpBuffer.get()[srcindex];
|
|
|
|
}
|
2023-06-16 09:42:45 +08:00
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
// [(hU, blocknum), lU, k2, (hP, lP)] -> [(hU, blocknum), k2, lU, (hP, lP)]
|
2025-03-12 11:35:16 +08:00
|
|
|
area = UNIT * SRC_UNIT;
|
|
|
|
auto bklU = UP_DIV(ic, SRC_UNIT) / blockNum;
|
|
|
|
for (int bk = 0; bk < blockNum * hU; ++bk) {
|
|
|
|
for (int i = 0; i < kernelCount; ++i) {
|
|
|
|
for (int j = 0; j < bklU; ++j) {
|
|
|
|
memcpy(dst + bk * stride1 + i * bklU * area + j * area, packedBuffer.get() + bk * stride1 + i * area + j * kernelCount * area, area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // not fast
|
|
|
|
|
|
|
|
if (summerFunc != nullptr && kernelsum != nullptr) {
|
|
|
|
summerFunc(kernelsum, (int8_t*)dst, blockNum * hU, blockL, UNIT, SRC_UNIT);
|
2023-06-16 09:42:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-12 11:14:19 +08:00
|
|
|
void ConvInt8TiledExecutor::packWeightAndQuantInfo(int8_t* dstbuffer, const int8_t* weight, const int8_t* quantInfo, int32_t* info, int infoBytes) {
|
|
|
|
int blockNum = info[0];
|
|
|
|
int ocDiv = info[1];
|
|
|
|
int blockL = info[2];
|
|
|
|
int UNIT = info[3];
|
|
|
|
int SRC_UNIT = info[4];
|
|
|
|
auto ocUp4 = info[5];
|
2025-05-08 12:39:44 +08:00
|
|
|
auto src0 = weight; // int8 weight: [oc/hp, blocknum, ic/lp*(kx*ky)/blocknum, hp, lp]
|
|
|
|
auto src1 = quantInfo; // dequant scale: [blocknum, ocUp4]
|
|
|
|
auto src2 = src1 + infoBytes * ocUp4 * blockNum; // dequant bias: [blocknum, ocUp4]
|
|
|
|
int stride0 = info[0] * info[2] * info[3] * info[4];
|
2025-02-12 11:14:19 +08:00
|
|
|
int stride1 = info[2] * info[3] * info[4];
|
2025-05-08 12:39:44 +08:00
|
|
|
|
|
|
|
// dst: [oc/hp, blocknum, packedUnit]
|
|
|
|
// packedUnit: [ic/lp*(kx*ky)/blocknum, hp, lp] + [hp] + [hp]
|
|
|
|
|
|
|
|
for (int hU = 0; hU < ocDiv; ++hU) {
|
|
|
|
auto huPtr = dstbuffer + hU * blockNum * (stride1 + 2 * UNIT * infoBytes);
|
|
|
|
int scaleCount = ALIMIN(ocUp4 - hU * UNIT, UNIT);
|
|
|
|
for (int bl = 0; bl < blockNum; ++bl) {
|
|
|
|
auto blockPtr = huPtr + bl * (stride1 + 2 * scaleCount * infoBytes);
|
|
|
|
memcpy(blockPtr, src0 + bl * stride1 + hU * stride0, stride1);
|
|
|
|
memcpy(blockPtr + stride1, src1 + (bl * ocUp4 + hU * UNIT) * infoBytes, scaleCount * infoBytes);
|
|
|
|
memcpy(blockPtr + stride1 + scaleCount * infoBytes, src2 + (bl * ocUp4 + hU * UNIT) * infoBytes, scaleCount * infoBytes);
|
2024-12-31 15:34:08 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
static void _computeReorderQuantInfo(std::shared_ptr<CPUConvolution::ResourceInt8> resource, std::shared_ptr<ConvolutionCommon::Int8Common> quantCommon, int outputCount, int kernelCount, int pack, AutoStorage<int8_t>& reorderedQuantInfo, float* ikernelSum, int HP, bool realInt4OrInt8) {
|
2025-03-12 11:35:16 +08:00
|
|
|
// Only used for dynamic quant:
|
|
|
|
// copy gemm bias
|
|
|
|
// copy/compute real dequant bias/scale
|
|
|
|
// dequant weight kernel sum
|
2025-05-08 12:39:44 +08:00
|
|
|
int ocUp4 = ROUND_UP(outputCount, pack);
|
|
|
|
int ocUpHp = ROUND_UP(outputCount, HP);
|
2024-08-24 15:46:21 +08:00
|
|
|
|
2025-03-12 11:35:16 +08:00
|
|
|
int blockNum = resource->mBlockNum;
|
2024-07-22 19:51:53 +08:00
|
|
|
int scaleSize = blockNum * ocUp4; // pack size.
|
2025-03-12 11:35:16 +08:00
|
|
|
int blockSize = kernelCount / blockNum;
|
2024-07-22 19:51:53 +08:00
|
|
|
int originOffset = 0;
|
|
|
|
if (quantCommon->canUseInt4) {
|
|
|
|
originOffset = -8;
|
2024-12-31 15:34:08 +08:00
|
|
|
}
|
|
|
|
// Save weight quant alpha and zero: wf=alpha*wi+zero
|
2025-02-12 11:14:19 +08:00
|
|
|
auto alphaPtr = reinterpret_cast<float*>(reorderedQuantInfo.get());
|
|
|
|
auto biasPtr = reinterpret_cast<float*>(reinterpret_cast<uint8_t*>(alphaPtr) + scaleSize * QUANT_INFO_BYTES);
|
2025-05-08 12:39:44 +08:00
|
|
|
if (outputCount % pack != 0) {
|
2025-02-12 11:14:19 +08:00
|
|
|
::memset(alphaPtr, 0, scaleSize * QUANT_INFO_BYTES);
|
|
|
|
::memset(biasPtr, 0, scaleSize * QUANT_INFO_BYTES);
|
2024-11-18 14:37:45 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
auto quanInfoPtr = quantCommon->alpha.get();
|
2025-03-12 11:35:16 +08:00
|
|
|
auto weightKernelSum = resource->mWeightKernelSum->host<float>();
|
|
|
|
::memset(weightKernelSum, 0, resource->mWeightKernelSum->size());
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
bool blockQuantInput = (resource->mWeightKernelSum->length(0) / QUANT_INFO_BYTES == ocUp4) ? false : true;
|
|
|
|
int ocDiv4 = UP_DIV(outputCount, pack);
|
|
|
|
// dstkernelsum: [hU,blocknum,min(hP, pack)]
|
2024-07-22 19:51:53 +08:00
|
|
|
if (quantCommon->asymmetric) {
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int i = 0; i < outputCount; ++i) {
|
|
|
|
float accum = 0.f;
|
2025-05-08 12:39:44 +08:00
|
|
|
auto ocOutside = i / HP;
|
|
|
|
auto ocInside = i % HP;
|
|
|
|
int remain = ALIMIN(HP, ocUp4 - ocOutside * HP);
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int j = 0; j < blockNum; ++j) {
|
|
|
|
int index = i * blockNum + j;
|
2025-05-08 12:39:44 +08:00
|
|
|
int srcSumIndex = ocOutside * blockNum * HP + j * HP + ocInside; // ikernelsum: [hU,blocknum,hP]
|
2025-03-12 11:35:16 +08:00
|
|
|
alphaPtr[j * ocUp4 + i] = quanInfoPtr[2 * index + 1];
|
|
|
|
biasPtr[j * ocUp4 + i] = quanInfoPtr[2 * index] + (float)originOffset * quanInfoPtr[2 * index + 1];
|
|
|
|
if (realInt4OrInt8) {
|
2025-05-08 12:39:44 +08:00
|
|
|
accum += (ikernelSum[srcSumIndex] * quanInfoPtr[2 * index + 1] + blockSize * biasPtr[j * ocUp4 + i]);
|
2025-03-12 11:35:16 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
accum += ((ikernelSum[srcSumIndex] - blockSize * 8)* quanInfoPtr[2 * index + 1] + blockSize * quanInfoPtr[2 * index]);
|
2025-03-12 11:35:16 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (blockQuantInput) {
|
|
|
|
int dstSumIndex = ocOutside * blockNum * HP + j * remain + ocInside;
|
|
|
|
weightKernelSum[dstSumIndex] = accum;
|
|
|
|
accum = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!blockQuantInput) {
|
|
|
|
weightKernelSum[i] = accum;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int i = 0; i < outputCount; ++i) {
|
|
|
|
float accum = 0.f;
|
2025-05-08 12:39:44 +08:00
|
|
|
auto ocOutside = i / HP;
|
|
|
|
auto ocInside = i % HP;
|
|
|
|
int remain = ALIMIN(HP, ocUp4 - ocOutside * HP);
|
2025-03-12 11:35:16 +08:00
|
|
|
for (int j = 0; j < blockNum; ++j) {
|
|
|
|
int index = i * blockNum + j;
|
2025-05-08 12:39:44 +08:00
|
|
|
int srcSumIndex = ocOutside * blockNum * HP + j * HP + ocInside; // ikernelsum: [hU,blocknum,hP]
|
2025-03-12 11:35:16 +08:00
|
|
|
alphaPtr[j * ocUp4 + i] = quanInfoPtr[index];
|
|
|
|
biasPtr[j * ocUp4 + i] = (float)originOffset * quanInfoPtr[index];
|
|
|
|
if (realInt4OrInt8) {
|
2025-05-08 12:39:44 +08:00
|
|
|
accum += (ikernelSum[srcSumIndex] * quanInfoPtr[index] + blockSize * biasPtr[j * ocUp4 + i]);
|
2025-03-12 11:35:16 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
accum += ((ikernelSum[srcSumIndex] - blockSize * 8) * quanInfoPtr[index]);
|
2024-08-24 15:46:21 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (blockQuantInput) {
|
|
|
|
int dstSumIndex = ocOutside * blockNum * HP + j * remain + ocInside;
|
|
|
|
weightKernelSum[dstSumIndex] = accum;
|
|
|
|
accum = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!blockQuantInput) {
|
|
|
|
weightKernelSum[i] = accum;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
DenseConvInt8TiledExecutor::DenseConvInt8TiledExecutor(Backend* backend, const Op* op, std::shared_ptr<ConvolutionCommon::Int8Common> quanCommon, bool isDynamicQuant) : ConvInt8TiledExecutor(backend, op) {
|
2025-03-12 11:35:16 +08:00
|
|
|
// convolution info
|
2024-08-24 15:46:21 +08:00
|
|
|
auto convOp = op->main_as_Convolution2D();
|
2025-03-12 11:35:16 +08:00
|
|
|
int kernelCount = mCommon->kernelX() * mCommon->kernelY();
|
|
|
|
int oc = convOp->common()->outputCount();
|
|
|
|
int ic = convOp->common()->inputCount();
|
2025-05-08 12:39:44 +08:00
|
|
|
|
|
|
|
int blockNum = 1;
|
|
|
|
if (quanCommon) {
|
|
|
|
int dequantCnt = quanCommon->alphaSize;
|
|
|
|
if (quanCommon->asymmetric) {
|
|
|
|
dequantCnt /= 2;
|
|
|
|
}
|
|
|
|
blockNum = dequantCnt / oc;
|
2025-03-12 11:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// backend info
|
2021-06-11 17:17:13 +08:00
|
|
|
auto core = static_cast<CPUBackend*>(backend)->int8Functions();
|
2024-07-22 19:51:53 +08:00
|
|
|
auto gcore = static_cast<CPUBackend*>(backend)->functions();
|
|
|
|
int UNIT, SRC_UNIT, DST_XUNIT;
|
|
|
|
core->MNNGetGemmUnit(&UNIT, &SRC_UNIT, &DST_XUNIT);
|
2024-08-24 15:46:21 +08:00
|
|
|
int pack = gcore->pack;
|
2025-03-12 11:35:16 +08:00
|
|
|
|
|
|
|
// compute info
|
|
|
|
int ocUp4 = ROUND_UP(oc, pack);
|
|
|
|
int lU = UP_DIV(ic / blockNum, SRC_UNIT) * kernelCount;
|
|
|
|
int scaleSize = ocUp4 * blockNum;
|
|
|
|
std::vector<int> shape = {blockNum, UP_DIV(oc, UNIT), lU, UNIT, SRC_UNIT};
|
|
|
|
mResourceInt8.reset(new CPUConvolution::ResourceInt8);
|
2025-05-08 12:39:44 +08:00
|
|
|
mResourceInt8->mWeightAsymmetricQuant = quanCommon ? quanCommon->asymmetric : false;
|
2025-03-12 11:35:16 +08:00
|
|
|
mResourceInt8->mActBits = 8;
|
|
|
|
mResourceInt8->mBlockNum = blockNum;
|
2025-05-08 12:39:44 +08:00
|
|
|
if (quanCommon && quanCommon->canUseInt4) {
|
2025-03-12 11:35:16 +08:00
|
|
|
shape[4] = SRC_UNIT / 2;
|
|
|
|
mResourceInt8->mActBits = 4;
|
|
|
|
mResourceInt8->mWeightAsymmetricQuant = true; // offset: 8 from uint8_t
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (isDynamicQuant) {
|
|
|
|
mResourceInt8->mDynamicQuant = true;
|
|
|
|
// Relu/Relu6 post parameters
|
|
|
|
auto postPtr = getPostParameters();
|
|
|
|
mResourceInt8->mReluThreshold.resize(2);
|
|
|
|
mResourceInt8->mReluThreshold[0] = postPtr[2];
|
|
|
|
mResourceInt8->mReluThreshold[1] = postPtr[3];
|
|
|
|
if (gcore->bytes == 2) {
|
|
|
|
gcore->MNNFp32ToLowp(mResourceInt8->mReluThreshold.data(), reinterpret_cast<int16_t*>(mResourceInt8->mReluThreshold.data()), 2);
|
|
|
|
}
|
2025-03-12 11:35:16 +08:00
|
|
|
}
|
|
|
|
// buffer allocate
|
|
|
|
auto quantlen = 2 * blockNum * ROUND_UP(oc, pack) * QUANT_INFO_BYTES;
|
|
|
|
auto weightlen = shape[0] * shape[1] * shape[2] * shape[3] * shape[4];
|
|
|
|
mResourceInt8->mWeightInt8.reset(Tensor::createDevice<uint8_t>({weightlen + quantlen}));
|
|
|
|
mResourceInt8->mOriginBias.reset(Tensor::createDevice<int32_t>({ocUp4})); // float
|
2025-05-08 12:39:44 +08:00
|
|
|
auto dynamicOption = static_cast<CPUBackend*>(backend)->getRuntime()->hint().dynamicQuantOption; // input ic block quant.
|
|
|
|
if (dynamicOption != 2) {
|
|
|
|
mResourceInt8->mWeightKernelSum.reset(Tensor::createDevice<uint8_t>({QUANT_INFO_BYTES * ocUp4}));
|
|
|
|
} else {
|
|
|
|
mResourceInt8->mWeightKernelSum.reset(Tensor::createDevice<uint8_t>({blockNum * QUANT_INFO_BYTES * ocUp4}));
|
|
|
|
}
|
2025-03-12 11:35:16 +08:00
|
|
|
|
|
|
|
auto res = backend->onAcquireBuffer(mResourceInt8->mOriginBias.get(), Backend::STATIC);
|
|
|
|
res &= backend->onAcquireBuffer(mResourceInt8->mWeightKernelSum.get(), Backend::STATIC);
|
|
|
|
res &= backend->onAcquireBuffer(mResourceInt8->mWeightInt8.get(), Backend::STATIC);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
MNN_ERROR("weight acquire buffer error\n");
|
|
|
|
return;
|
|
|
|
}
|
2024-12-31 15:34:08 +08:00
|
|
|
bool useCachedMmap = backend->getRuntime()->hint().useCachedMmap > 1;
|
2025-03-12 11:35:16 +08:00
|
|
|
if (useCachedMmap) {
|
|
|
|
return;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
|
|
|
// read weight, weight's scale&bias, convolution bias
|
2025-04-24 08:32:00 +08:00
|
|
|
::memset(mResourceInt8->mOriginBias->host<float>(), 0, ocUp4 * sizeof(float));
|
2025-05-08 12:39:44 +08:00
|
|
|
if (!isDynamicQuant) {
|
|
|
|
mResourceInt8->mDynamicQuant = false;
|
|
|
|
|
|
|
|
const int8_t* weightNotPacked;
|
|
|
|
std::shared_ptr<float> scaleAndBias(new float[ocUp4 * 2], [](void* ptr) {
|
|
|
|
delete [] (float*)ptr;
|
|
|
|
});
|
|
|
|
memset(scaleAndBias.get(), 0, ocUp4 * 2 * sizeof(float));
|
|
|
|
int weightSize;
|
|
|
|
ConvolutionCommon::getConvInt8Parameters(op, quanCommon, backend, weightNotPacked, weightSize, (float*)scaleAndBias.get(), mResourceInt8->mOriginBias->host<int32_t>(), ocUp4);
|
|
|
|
initializeConvInt8QuantInfo(mResourceInt8, convOp);
|
|
|
|
|
|
|
|
auto L = ic * kernelCount;
|
|
|
|
auto ptrWeight = (int8_t*)weightNotPacked;
|
|
|
|
auto ptrWeightScale = scaleAndBias.get();
|
|
|
|
auto ptrWeightBias = ptrWeightScale + ocUp4;
|
|
|
|
for (int i = 0; i < oc; i++) {
|
|
|
|
int temp = 0;
|
|
|
|
for (int j = 0; j < L; j++) {
|
|
|
|
temp += (int)ptrWeight[j + i * L];
|
|
|
|
}
|
|
|
|
mResourceInt8->mWeightKernelSum->host<float>()[i] = (temp * ptrWeightScale[i] + L * ptrWeightBias[i]);
|
|
|
|
#ifdef MNN_USE_SSE
|
|
|
|
if (mResourceInt8->mUseConvQuan) {
|
|
|
|
mResourceInt8->mOriginBias->host<int32_t>()[i] -= 128 * temp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
mMutableResource.reset(new MutableResourceInt8(mResourceInt8, backend, scaleAndBias.get()));
|
|
|
|
|
|
|
|
// reorder weight&scale&bias
|
|
|
|
int bytes = 4;
|
|
|
|
auto resourceInt8 = mResourceInt8;
|
|
|
|
auto reorderFunction = [weightNotPacked, weightlen, oc, ic, kernelCount, UNIT, SRC_UNIT, shape, resourceInt8, bytes, L, ocUp4, quantlen, scaleAndBias]()
|
|
|
|
{
|
|
|
|
AutoStorage<uint8_t> weightReordered(weightlen);
|
|
|
|
if (!weightReordered.get()) {
|
|
|
|
MNN_ERROR("Memory not enough for quant model weight reorder\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int32_t info[6] = {1, oc, ic, kernelCount, UNIT, SRC_UNIT};
|
|
|
|
ConvInt8TiledExecutor::reorderWeight((uint8_t*)weightReordered.get(), (uint8_t*)weightNotPacked, info, 0);
|
|
|
|
int32_t params[6] = {shape[0], shape[1], shape[2], shape[3], shape[4], quantlen/ (2 * QUANT_INFO_BYTES * 1)};
|
|
|
|
ConvInt8TiledExecutor::packWeightAndQuantInfo(resourceInt8->mWeightInt8->host<int8_t>(), (int8_t*)weightReordered.get(), (int8_t*)scaleAndBias.get(), params, QUANT_INFO_BYTES);
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
static_cast<CPUBackend*>(backend)->enqueueTask(reorderFunction);
|
|
|
|
|
|
|
|
// gemmInt8 kernel
|
|
|
|
mGemmKernel = core->Int8GemmKernel;
|
|
|
|
#ifdef MNN_USE_SSE
|
|
|
|
int actBits = convOp->symmetricQuan()->nbits();
|
|
|
|
if (actBits <= 7) {
|
|
|
|
mGemmKernel = core->Int8GemmKernelFast;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(convOp->symmetricQuan()->method() == QuantizeAlgo_OVERFLOW_AWARE){
|
|
|
|
mGemmKernel = core->Int8GemmKernelFast;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return; // offline quant
|
2025-04-24 08:32:00 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
|
|
|
// dynamic quant
|
|
|
|
bool directReadInt4weight = (kernelCount == 1 && ROUND_UP(oc, UNIT) == oc && ROUND_UP(ic, SRC_UNIT) == ic);
|
2024-10-22 14:37:28 +08:00
|
|
|
#ifdef MNN_KLEIDIAI_ENABLED
|
2025-01-07 10:18:20 +08:00
|
|
|
if(quanCommon->canUseInt4) {
|
|
|
|
bool bFP16 = gcore->bytes == 2 ? true : false;
|
|
|
|
bool bAsym = quanCommon->asymmetric;
|
2025-04-24 08:32:00 +08:00
|
|
|
size_t blkSize = mResourceInt8->mBlockNum == 1 ? 0 : ic / mResourceInt8->mBlockNum;
|
2025-01-07 10:18:20 +08:00
|
|
|
KleidiAI::AccelType accelType = KleidiAI::getQIntAccelType(4, bAsym, blkSize);
|
|
|
|
|
2025-04-24 08:32:00 +08:00
|
|
|
if (!KleidiAI::mKaiInitialized) {
|
|
|
|
KleidiAI& kai = KleidiAI::getInstance(*MNNGetCPUInfo(), bFP16, false);
|
|
|
|
}
|
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
KleidiAI& kai = KleidiAI::getInstance();
|
|
|
|
if(!kai.isLoaded(accelType)) {
|
|
|
|
kai.setLoaded(accelType);
|
|
|
|
kai.printInfo(accelType);
|
2024-10-22 14:37:28 +08:00
|
|
|
}
|
2024-10-24 14:07:29 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
if(kai.canAccelerate(accelType)) {
|
2025-04-27 15:57:31 +08:00
|
|
|
AutoStorage<int8_t> reorderedQuantInfo;
|
|
|
|
reorderedQuantInfo.reset(2 * scaleSize * QUANT_INFO_BYTES);
|
|
|
|
if (reorderedQuantInfo.get() == nullptr) {
|
|
|
|
MNN_ERROR("Memory not enough\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Prepare scale and zero data.
|
|
|
|
{
|
|
|
|
int outputCount = convOp->common()->outputCount();
|
|
|
|
int originOffset = -8;
|
|
|
|
auto quanInfoPtr = quanCommon->alpha.get();
|
|
|
|
auto scalePtr = reinterpret_cast<float*>(reorderedQuantInfo.get());
|
|
|
|
auto zeroPtr = reinterpret_cast<float*>(reinterpret_cast<uint8_t*>(scalePtr) + scaleSize * QUANT_INFO_BYTES);
|
|
|
|
if (quanCommon->asymmetric) {
|
|
|
|
for (int i = 0; i < blockNum; ++i) {
|
|
|
|
auto dstScale = scalePtr + i * ocUp4;
|
|
|
|
auto dstZero = zeroPtr + i * ocUp4;
|
|
|
|
for (int j = 0; j < outputCount; ++j) {
|
|
|
|
int scaleIndex = j * blockNum + i;
|
|
|
|
dstScale[j] = quanInfoPtr[2 * scaleIndex + 1];
|
|
|
|
dstZero[j] = quanInfoPtr[2 * scaleIndex] + (float)originOffset * dstScale[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < blockNum; ++i) {
|
|
|
|
auto dstScale = scalePtr + i * ocUp4;
|
|
|
|
auto dstZero = zeroPtr + i * ocUp4;
|
|
|
|
for (int j = 0; j < outputCount; ++j) {
|
|
|
|
int scaleIndex = j * blockNum + i;
|
|
|
|
dstScale[j] = quanInfoPtr[scaleIndex];
|
|
|
|
dstZero[j] = (float)originOffset * dstScale[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
mAccelType = accelType;
|
|
|
|
int n = oc;
|
|
|
|
int k = ic;
|
|
|
|
int packedWeightSize = kai.getRhsPackedSize(mAccelType, n, k, blkSize);
|
2024-10-24 14:07:29 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
//Alloc packed weight tensor.
|
|
|
|
mResourceInt8->mWeightInt8.reset(Tensor::createDevice<uint8_t>({packedWeightSize}));
|
|
|
|
bool success = backend->onAcquireBuffer(mResourceInt8->mWeightInt8.get(), Backend::STATIC);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
MNN_ERROR("Out of static memory!\n");
|
|
|
|
return;
|
|
|
|
}
|
2024-10-28 16:53:06 +08:00
|
|
|
|
2025-04-24 08:32:00 +08:00
|
|
|
size_t paraNum = scaleSize;
|
|
|
|
float *scalePtr = reinterpret_cast<float*>(reorderedQuantInfo.get());
|
|
|
|
float *zeroPtr = reinterpret_cast<float*>(reorderedQuantInfo.get()) + paraNum;
|
2025-01-07 10:18:20 +08:00
|
|
|
float *biasPtr = mResourceInt8->mOriginBias->host<float>();
|
|
|
|
//Reload some parameters to fit ukernels' layout.
|
|
|
|
auto quanInfoPtr = quanCommon->alpha.get();
|
|
|
|
if(bAsym) {
|
|
|
|
for(int i = 0; i < paraNum; i++) {
|
|
|
|
zeroPtr[i] = quanInfoPtr[i * 2];
|
|
|
|
scalePtr[i] = quanInfoPtr[i * 2 + 1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(blkSize != 0) {
|
|
|
|
memcpy(scalePtr, (uint8_t*)quanInfoPtr, paraNum * sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run rhs pack.
|
|
|
|
auto weightPackedData = mResourceInt8->mWeightInt8->host<uint8_t>();
|
|
|
|
kai.runRhsPack(mAccelType, 1, n, k, blkSize, 0/*unused*/,
|
|
|
|
(uint8_t*)quanCommon->weight.get(),
|
|
|
|
(const void*)scalePtr, (const void*)zeroPtr, (const void*)biasPtr,
|
|
|
|
weightPackedData, directReadInt4weight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 14:37:28 +08:00
|
|
|
#endif
|
2025-03-12 11:35:16 +08:00
|
|
|
auto target = mResourceInt8;
|
2025-05-08 12:39:44 +08:00
|
|
|
// Save bias
|
|
|
|
if (convOp->bias()) {
|
|
|
|
::memcpy(mResourceInt8->mOriginBias->host<float>(), convOp->bias()->data(), oc * sizeof(float));
|
|
|
|
}
|
|
|
|
auto function = [shape, UNIT, SRC_UNIT, quanCommon, weightlen, scaleSize, directReadInt4weight, blockNum, ic, oc, quantlen, kernelCount, pack, convOp, gcore, target]() -> int {
|
2025-03-12 11:35:16 +08:00
|
|
|
auto sh = shape;
|
|
|
|
AutoStorage<int8_t> weightReordered(weightlen);
|
|
|
|
AutoStorage<int8_t> reorderedQuantInfo(2 * scaleSize * QUANT_INFO_BYTES);
|
2025-05-08 12:39:44 +08:00
|
|
|
AutoStorage<int8_t> kernelsum(blockNum * ROUND_UP(oc, UNIT) * QUANT_INFO_BYTES);
|
|
|
|
if (weightReordered.get() == nullptr || reorderedQuantInfo.get() == nullptr || kernelsum.get() == nullptr) {
|
2025-03-12 11:35:16 +08:00
|
|
|
MNN_ERROR("Memory not enough\n");
|
|
|
|
return -1;
|
2024-08-24 15:46:21 +08:00
|
|
|
}
|
2025-03-12 11:35:16 +08:00
|
|
|
/* 1. reorder weight */
|
|
|
|
if (quanCommon->canUseInt4 && directReadInt4weight) {
|
|
|
|
auto srcPtr = (uint8_t*)quanCommon->weight.get();
|
|
|
|
auto dstPtr = (uint8_t*)weightReordered.get();
|
|
|
|
::memset(dstPtr, 0, weightlen);
|
|
|
|
gcore->MNNReorderWeightInt4(dstPtr, srcPtr, sh.data(), sh.size(), (float*)kernelsum.get());
|
2025-05-08 12:39:44 +08:00
|
|
|
} else { // int4 weight but oc/ic not packed
|
2025-03-12 11:35:16 +08:00
|
|
|
auto weightLength = quanCommon->weight.size();
|
|
|
|
int blocksize = ic * kernelCount / blockNum;
|
|
|
|
int originOffset = 0;
|
|
|
|
int32_t info[6] = {blockNum, oc, ic, kernelCount, UNIT, SRC_UNIT};
|
|
|
|
if (quanCommon->canUseInt4) {
|
|
|
|
originOffset = -8;
|
|
|
|
auto srcPtr = reinterpret_cast<uint8_t*>(quanCommon->weight.get());
|
|
|
|
std::vector<uint8_t> tmpWeight(weightLength * 2);
|
|
|
|
for (int j = 0; j < oc; ++j) {
|
|
|
|
for (int k = 0; k < blockNum; ++k) {
|
|
|
|
for (int i = 0; i < blocksize; ++i) {
|
|
|
|
int index = j * blockNum * blocksize + k * blocksize + i;
|
|
|
|
uint8_t w_ = srcPtr[index / 2];
|
|
|
|
int truew = index % 2 ? (w_ & 0x0f) : (w_ >> 4);
|
|
|
|
tmpWeight[index] = truew;
|
|
|
|
}
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
2025-03-12 11:35:16 +08:00
|
|
|
AutoStorage<uint8_t> packedInt8weight(weightlen * 2);
|
|
|
|
if (packedInt8weight.get() == nullptr) {
|
|
|
|
MNN_ERROR("Weight reorder memory not enough!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
reorderWeight(packedInt8weight.get(), (uint8_t*)tmpWeight.data(), info, 0, (float*)kernelsum.get(), gcore->MNNSumWeightInt8);
|
2025-03-12 11:35:16 +08:00
|
|
|
// pack two int4 to int8
|
|
|
|
int leng = weightlen * 2;
|
|
|
|
auto srcint4Ptr = (uint8_t*)packedInt8weight.get();
|
|
|
|
auto dstint4Ptr = (uint8_t*)weightReordered.get();
|
|
|
|
int permuteUnit = UNIT * SRC_UNIT;
|
|
|
|
int halfPermuteStride = static_cast<int32_t>(permuteUnit / 2);
|
|
|
|
for (int i = 0; i < leng / permuteUnit; ++i) {
|
|
|
|
auto src0 = srcint4Ptr + i * permuteUnit;
|
|
|
|
auto dst0 = dstint4Ptr + i * halfPermuteStride;
|
|
|
|
for (int j = 0; j < halfPermuteStride; ++j) {
|
|
|
|
int s0 = src0[j];
|
|
|
|
int s1 = src0[j + halfPermuteStride];
|
|
|
|
int d = (s0) * 16 + (s1);
|
|
|
|
dst0[j] = d;
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
} else { // int8 weight
|
2025-03-12 11:35:16 +08:00
|
|
|
reorderWeight((uint8_t*)weightReordered.get(), (uint8_t*)quanCommon->weight.get(), info, 0, (float*)kernelsum.get(), gcore->MNNSumWeightInt8);
|
2024-08-24 15:46:21 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
/* 2. compute and order dequant scale&bias */
|
|
|
|
bool notConvertInt4ToInt8 = true;
|
|
|
|
if (quanCommon->canUseInt4 && !directReadInt4weight) {
|
|
|
|
notConvertInt4ToInt8 = false;
|
|
|
|
}
|
|
|
|
_computeReorderQuantInfo(target, quanCommon, oc, kernelCount * ic, pack, reorderedQuantInfo, (float*)kernelsum.get(), UNIT, notConvertInt4ToInt8);
|
|
|
|
/* 3. put weight and quantInfo together */
|
2025-03-12 11:35:16 +08:00
|
|
|
int32_t params[6] = {shape[0], shape[1], shape[2], shape[3], shape[4], quantlen / (2 * QUANT_INFO_BYTES * blockNum)};
|
|
|
|
ConvInt8TiledExecutor::packWeightAndQuantInfo(target->mWeightInt8->host<int8_t>(), (int8_t*)weightReordered.get(), reorderedQuantInfo.get(), params, QUANT_INFO_BYTES);
|
2024-08-24 15:46:21 +08:00
|
|
|
|
2025-03-12 11:35:16 +08:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
static_cast<CPUBackend*>(backend)->enqueueTask(std::move(function));
|
2024-09-12 12:57:57 +08:00
|
|
|
}
|
|
|
|
|
2024-08-24 15:46:21 +08:00
|
|
|
DenseConvInt8TiledExecutor::DenseConvInt8TiledExecutor(Backend* backend, const Op* op, const DenseConvInt8TiledExecutor& exe)
|
2024-09-12 12:57:57 +08:00
|
|
|
: ConvInt8TiledExecutor(backend, op, exe.mResourceInt8), mGemmKernel(exe.mGemmKernel) {
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
DenseConvInt8TiledExecutor::~DenseConvInt8TiledExecutor() {
|
2021-06-11 17:17:13 +08:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
bool DenseConvInt8TiledExecutor::onClone(Backend* bn, const Op* op, Execution** dst) {
|
2021-06-11 17:17:13 +08:00
|
|
|
if (nullptr == dst) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-08-24 15:46:21 +08:00
|
|
|
auto exe = new DenseConvInt8TiledExecutor(bn, op, *this);
|
2021-06-11 17:17:13 +08:00
|
|
|
if (!exe->valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
2025-01-07 10:18:20 +08:00
|
|
|
#ifdef MNN_KLEIDIAI_ENABLED
|
|
|
|
exe->mAccelType = this->mAccelType;
|
|
|
|
#endif
|
2021-06-11 17:17:13 +08:00
|
|
|
*dst = exe;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
void DenseConvInt8TiledExecutor::getPackParameter(int* Unit, int* srcUnit, int* DestUnit, const CoreInt8Functions* core) {
|
|
|
|
core->MNNGetGemmUnit(Unit, srcUnit, DestUnit);
|
|
|
|
}
|
2021-06-11 17:17:13 +08:00
|
|
|
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
ErrorCode DenseConvInt8TiledExecutor::onResize(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
|
2025-05-08 12:39:44 +08:00
|
|
|
// Initialize.
|
2025-01-22 14:47:50 +08:00
|
|
|
mUseBatchQuan = false;
|
2025-05-08 12:39:44 +08:00
|
|
|
mIm2ColBasedInt8 = true;
|
|
|
|
|
2025-01-22 14:47:50 +08:00
|
|
|
auto option = static_cast<CPUBackend*>(backend())->getRuntime()->hint().dynamicQuantOption;
|
|
|
|
int batch = inputs[0]->batch();
|
|
|
|
int inC = inputs[0]->channel();
|
|
|
|
auto output = outputs[0];
|
|
|
|
int inputPlane = batch * inputs[0]->width() * inputs[0]->height();
|
|
|
|
auto planeSize = output->width() * output->height() * output->batch();
|
2021-09-18 15:52:30 +08:00
|
|
|
auto core = static_cast<CPUBackend*>(backend())->int8Functions();
|
2024-07-22 19:51:53 +08:00
|
|
|
auto gcore =static_cast<CPUBackend*>(backend())->functions();
|
2021-09-18 15:52:30 +08:00
|
|
|
int UNIT, SRC_UNIT, DST_XUNIT;
|
2024-07-22 19:51:53 +08:00
|
|
|
core->MNNGetGemmUnit(&UNIT, &SRC_UNIT, &DST_XUNIT);
|
2025-01-22 14:47:50 +08:00
|
|
|
int kernelCount = mCommon->kernelY() * mCommon->kernelX();
|
|
|
|
bool fastway = (kernelCount == 1) && (output->width() == inputs[0]->width()) && (output->height() == inputs[0]->height()) && (mCommon->strideX() * mCommon->strideY()) == 1;
|
|
|
|
if (inputPlane > 1) {
|
|
|
|
mUseBatchQuan = true;
|
|
|
|
}
|
|
|
|
if (!fastway) { // general conv
|
2025-05-08 12:39:44 +08:00
|
|
|
mIm2ColBasedInt8 = false;
|
2025-01-22 14:47:50 +08:00
|
|
|
if (planeSize > 1) {
|
|
|
|
mUseBatchQuan = true;
|
|
|
|
}
|
|
|
|
if (option == 1) { // lowest level.
|
2025-05-08 12:39:44 +08:00
|
|
|
mIm2ColBasedInt8 = true;
|
2025-01-22 14:47:50 +08:00
|
|
|
mUseBatchQuan = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 10:12:08 +08:00
|
|
|
float weightBytes = mResourceInt8->mActBits == 4 ? 0.5 : 1;
|
2025-02-12 11:14:19 +08:00
|
|
|
mBlockNum = mResourceInt8->mBlockNum;
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-10-22 14:37:28 +08:00
|
|
|
#ifdef MNN_KLEIDIAI_ENABLED
|
|
|
|
KleidiAI& kai = KleidiAI::getInstance();
|
2025-01-07 10:18:20 +08:00
|
|
|
if(mResourceInt8->mDynamicQuant && mResourceInt8->mActBits == 4 && kai.canAccelerate(mAccelType)) {
|
|
|
|
MNN_ASSERT(kai.isLoaded(mAccelType));
|
|
|
|
const size_t m = inputs[0]->batch(); //lhs vector number.
|
|
|
|
const size_t n = outputs[0]->channel(); //rhs vector number.
|
|
|
|
const size_t k = inputs[0]->channel(); //vector size.
|
|
|
|
const size_t blkSize = mBlockNum == 1 ? 0 : k / mBlockNum;
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
int packedSize = kai.getLhsQuantedPackedSize(mAccelType, m, k, blkSize);
|
|
|
|
int elementSize = kai.isHalf() ? sizeof(__fp16) : sizeof(float);
|
|
|
|
if(m > 1 && !kai.isLinear()) {
|
|
|
|
int srcSize = m * k * elementSize;
|
|
|
|
int dstSize = m * n * elementSize;
|
|
|
|
int extraSize = srcSize > dstSize ? srcSize : dstSize;
|
|
|
|
packedSize += extraSize;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
//Split mTempIm2ColBuffer as two parts for linear/tile transfer:
|
|
|
|
//Part0: Lhs_packed.
|
|
|
|
//Part1: Lhs/Dst before transfer.
|
2024-10-22 14:37:28 +08:00
|
|
|
mTempIm2ColBuffer.reset(Tensor::createDevice<int8_t>({packedSize}));
|
|
|
|
bool success = backend()->onAcquireBuffer(mTempIm2ColBuffer.get(), Backend::DYNAMIC);
|
|
|
|
if (!success) {
|
|
|
|
MNN_ERROR("Out of dynamic memory!\n");
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-10-22 14:37:28 +08:00
|
|
|
backend()->onReleaseBuffer(mTempIm2ColBuffer.get(), Backend::DYNAMIC);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2025-05-08 12:39:44 +08:00
|
|
|
CPUConvolution::onResize(inputs, outputs);
|
2024-09-12 12:57:57 +08:00
|
|
|
if (mResourceInt8->mDynamicQuant == false) {
|
2024-08-24 15:46:21 +08:00
|
|
|
mMutableResource->updateInputOutputScale(TensorUtils::getQuantInfo(inputs[0]), TensorUtils::getQuantInfo(outputs[0]));
|
2025-05-08 12:39:44 +08:00
|
|
|
if (!mMutableResource->mResource->mUseConvQuan) {
|
|
|
|
// In some previous quantized models, input's scale already fused with weight's scale and output's scale.
|
|
|
|
// So there is no need to read input's scale additionally.
|
|
|
|
mBatchQuantInfo.reset(Tensor::createDevice<int8_t>({1, DST_XUNIT * QUANT_INFO_BYTES}));
|
|
|
|
auto success = backend()->onAcquireBuffer(mBatchQuantInfo.get(), Backend::DYNAMIC);
|
|
|
|
if (!success) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
2025-02-12 11:14:19 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
mBlockNum = 1;
|
2025-05-08 12:39:44 +08:00
|
|
|
mIm2ColBasedInt8 = true;
|
2025-01-22 14:47:50 +08:00
|
|
|
mUseBatchQuan = false;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
ConvolutionTiledExecutor::setIm2ColParameter(mIm2ColParamter, mCommon, inputs[0], outputs[0], mPadX, mPadY, gcore, core);
|
2024-07-22 19:51:53 +08:00
|
|
|
// input scale buffer
|
2025-01-22 14:47:50 +08:00
|
|
|
const int threads = static_cast<CPUBackend*>(backend())->threadNumber();
|
2024-07-22 19:51:53 +08:00
|
|
|
|
|
|
|
// Im2col info
|
2025-01-22 14:47:50 +08:00
|
|
|
int im2colBytes = 1;
|
2023-06-16 09:42:45 +08:00
|
|
|
const int L2Size = 2048;
|
2025-01-22 14:47:50 +08:00
|
|
|
int tileLimitByC = UP_DIV(L2Size, mIm2ColParamter.kernelCountUnit * SRC_UNIT);
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColBasedInt8 == false) {
|
2025-01-22 14:47:50 +08:00
|
|
|
im2colBytes = gcore->bytes;
|
|
|
|
tileLimitByC = 1;
|
|
|
|
}
|
|
|
|
int ic = inputs[0]->channel();
|
2024-07-22 19:51:53 +08:00
|
|
|
int tileLimit = 0;
|
|
|
|
int outC = output->channel();
|
|
|
|
int outC4 = UP_DIV(outC, gcore->pack);
|
2025-05-08 12:39:44 +08:00
|
|
|
auto kernelCountUnit = mIm2ColParamter.kernelCountUnit;
|
2024-12-02 10:12:08 +08:00
|
|
|
mSplitByOc = true;
|
|
|
|
|
|
|
|
// flop and io
|
2025-05-08 12:39:44 +08:00
|
|
|
float flop = gcore->bytes * planeSize * (ROUND_UP(output->channel(), gcore->pack) * kernelCountUnit * SRC_UNIT / 1024.0 / 1024.0 / 1024.0);
|
2024-12-02 10:12:08 +08:00
|
|
|
float ios = (((CPUBackend*)backend())->getTensorSize(outputs[0], true) + ((CPUBackend*)backend())->getTensorSize(inputs[0], true) + ((CPUBackend*)backend())->getTensorSize(mResourceInt8->mWeightInt8.get()) * weightBytes) / (1024.0 * 1024.0 * 1024.0);
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
if (threads < planeSize) { // Thread split by output nhw.
|
|
|
|
tileLimit = ALIMIN(tileLimitByC, UP_DIV(planeSize, threads));
|
2024-12-02 10:12:08 +08:00
|
|
|
mIm2ColCount = UP_DIV(tileLimit, DST_XUNIT);
|
|
|
|
auto DynamicDestUnit = DST_XUNIT * mIm2ColCount;
|
|
|
|
mTileCount = UP_DIV(planeSize, DynamicDestUnit);
|
|
|
|
if (mTileCount > threads) {
|
|
|
|
mSplitByOc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (mSplitByOc) {
|
2024-07-22 19:51:53 +08:00
|
|
|
tileLimit = ALIMIN(tileLimitByC, planeSize);
|
2024-12-02 10:12:08 +08:00
|
|
|
mIm2ColCount = UP_DIV(tileLimit, DST_XUNIT);
|
|
|
|
auto DynamicDestUnit = DST_XUNIT * mIm2ColCount;
|
|
|
|
mTileCount = UP_DIV(planeSize, DynamicDestUnit);
|
2024-07-22 19:51:53 +08:00
|
|
|
auto ocPerThread = UP_DIV(outC4, threads);
|
|
|
|
auto threadNeed = UP_DIV(outC4, ocPerThread);
|
2024-08-24 15:46:21 +08:00
|
|
|
int totalWork = outC4;
|
|
|
|
int part = 1;
|
2024-07-22 19:51:53 +08:00
|
|
|
if (UNIT > gcore->pack) { // AVX512:UNIT=64,pack=16
|
|
|
|
MNN_ASSERT(UNIT % gcore->pack == 0);
|
|
|
|
int ocDivUnit = UP_DIV(outC4 * gcore->pack, UNIT);
|
|
|
|
ocPerThread = UP_DIV(ocDivUnit, threads);
|
|
|
|
threadNeed = UP_DIV(ocDivUnit, ocPerThread);
|
2024-08-24 15:46:21 +08:00
|
|
|
totalWork = ocDivUnit;
|
|
|
|
part = UNIT / gcore->pack;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
mThreadNums = ALIMIN(threads, threadNeed);
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
mDivides.resize(threads+1);
|
|
|
|
mDivides[0] = 0;
|
2024-12-02 10:12:08 +08:00
|
|
|
static_cast<CPUBackend *>(backend())->computeDivideSizes(totalWork, mDivides.data() + 1, flop / ios);
|
2024-08-24 15:46:21 +08:00
|
|
|
for (int i = 0; i < mDivides.size(); ++i) {
|
|
|
|
mDivides[i] *= part;
|
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-12-02 10:12:08 +08:00
|
|
|
if (!mSplitByOc) {
|
2024-07-22 19:51:53 +08:00
|
|
|
mThreadNums = ALIMIN(threads, mTileCount);
|
|
|
|
mDivides.resize(threads+1);
|
|
|
|
mDivides[0] = 0;
|
2024-12-02 10:12:08 +08:00
|
|
|
static_cast<CPUBackend *>(backend())->computeDivideSizes(mTileCount, mDivides.data() + 1, flop / ios);
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
int ocUp4 = ROUND_UP(outC, gcore->pack);
|
2024-12-02 10:12:08 +08:00
|
|
|
int k = mThreadNums;
|
|
|
|
int workPT = DST_XUNIT * mIm2ColCount;
|
|
|
|
if (mSplitByOc) {
|
|
|
|
k = 1; // Use one thread to finish im2col.
|
|
|
|
workPT = mTileCount * DST_XUNIT * mIm2ColCount;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2023-06-16 09:42:45 +08:00
|
|
|
auto bufferAlloc = static_cast<CPUBackend*>(backend())->getBufferAllocator();
|
2024-12-02 10:12:08 +08:00
|
|
|
auto blitInfoSize = ConvolutionTiledExecutor::computeBlitInfoSize(workPT, mIm2ColParamter.ow, mIm2ColParamter.kernelX * mIm2ColParamter.kernelY, k);
|
2024-07-22 19:51:53 +08:00
|
|
|
mBlitInfoStride = blitInfoSize.second;
|
2023-06-16 09:42:45 +08:00
|
|
|
mBlitInfo = bufferAlloc->alloc(blitInfoSize.first);
|
2025-05-08 12:39:44 +08:00
|
|
|
const int unitColBufferSize = kernelCountUnit * DST_XUNIT * SRC_UNIT * sizeof(int8_t);
|
|
|
|
const int colBufferSize = unitColBufferSize * mIm2ColCount;
|
|
|
|
|
2024-12-02 10:12:08 +08:00
|
|
|
if (!mSplitByOc) {
|
2025-05-08 12:39:44 +08:00
|
|
|
mTempIm2ColBuffer.reset(Tensor::createDevice<int8_t>({threads, colBufferSize * im2colBytes}));
|
|
|
|
mTempSrcSum = bufferAlloc->alloc(threads * mBlockNum * DST_XUNIT * mIm2ColCount * QUANT_INFO_BYTES);
|
2024-12-02 10:12:08 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
mTempIm2ColBuffer.reset(Tensor::createDevice<int8_t>({mTileCount, colBufferSize * im2colBytes}));
|
|
|
|
mTempSrcSum = bufferAlloc->alloc(mTileCount * mBlockNum * DST_XUNIT * mIm2ColCount * QUANT_INFO_BYTES);
|
2024-12-02 10:12:08 +08:00
|
|
|
}
|
2025-02-12 11:14:19 +08:00
|
|
|
auto success = backend()->onAcquireBuffer(mTempIm2ColBuffer.get(), Backend::DYNAMIC);
|
2025-05-08 12:39:44 +08:00
|
|
|
if (!success || mBlitInfo.invalid() || mTempSrcSum.invalid()) {
|
2023-06-16 09:42:45 +08:00
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
2024-09-12 12:57:57 +08:00
|
|
|
if (false == mResourceInt8->mDynamicQuant) {
|
2024-07-22 19:51:53 +08:00
|
|
|
bufferAlloc->free(mBlitInfo);
|
2025-05-08 12:39:44 +08:00
|
|
|
bufferAlloc->free(mTempSrcSum);
|
2024-07-22 19:51:53 +08:00
|
|
|
backend()->onReleaseBuffer(mTempIm2ColBuffer.get(), Backend::DYNAMIC);
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mBatchQuantInfo.get()) {
|
2025-02-12 11:14:19 +08:00
|
|
|
backend()->onReleaseBuffer(mBatchQuantInfo.get(), Backend::DYNAMIC);
|
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2023-06-16 09:42:45 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
#ifdef MNN_LOW_MEMORY
|
|
|
|
{ // Dynamic Quant kernels
|
|
|
|
mGemmKernel = core->Int8GemmKernel;
|
|
|
|
if (mResourceInt8->mActBits == 4) {
|
|
|
|
mGemmKernel = core->Int8GemmKernel_W4;
|
|
|
|
}
|
|
|
|
mQuantFunc = core->MNNFloat2Int8;
|
|
|
|
if (gcore->bytes == 2 && gcore->pack == 8) {
|
|
|
|
mGemmKernel = core->MNNGemmInt8AddBiasScale_Unit_FP16;
|
|
|
|
if (mResourceInt8->mActBits == 4) {
|
|
|
|
mGemmKernel = core->MNNGemmInt8AddBiasScale_w4_Unit_FP16;
|
|
|
|
}
|
|
|
|
mQuantFunc = core->DynamicQuanInput_ARM82;
|
|
|
|
mQuantAndReorderFunc = core->DynamicQuanInputAndReorder_ARM82;
|
|
|
|
|
|
|
|
}
|
|
|
|
// A axisSum kernel
|
|
|
|
mSumByAxisLFunc = gcore->MNNSumByAxisLForMatmul_A;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInputBlockNum = (option == 2) ? mBlockNum : 1;
|
|
|
|
bool symmetricQuant = (option != 2 && mUseBatchQuan) ? true : false;
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
if (!mUseBatchQuan) { // single quant
|
|
|
|
if (mSplitByOc) {
|
|
|
|
size = 2 * mInputBlockNum * ALIMIN(DST_XUNIT, planeSize) * QUANT_INFO_BYTES;
|
|
|
|
} else {
|
|
|
|
size = 2 * mInputBlockNum * mIm2ColCount * DST_XUNIT * QUANT_INFO_BYTES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mUseBatchQuan) {
|
|
|
|
if (mIm2ColBasedInt8) {
|
|
|
|
size = 2 * mInputBlockNum * inputPlane * QUANT_INFO_BYTES;
|
|
|
|
} else if (!mSplitByOc){ // only threads buffer needed by this case
|
|
|
|
size = 2 * mInputBlockNum * mIm2ColCount * DST_XUNIT * QUANT_INFO_BYTES;
|
|
|
|
} else {
|
|
|
|
size = 2 * mInputBlockNum * planeSize * QUANT_INFO_BYTES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (symmetricQuant) { // symmetric quant
|
|
|
|
size /= 2;
|
|
|
|
}
|
|
|
|
if (!mIm2ColBasedInt8 && !mSplitByOc) {
|
|
|
|
mBatchQuantInfo.reset(Tensor::createDevice<int8_t>({threads, size}));
|
|
|
|
} else {
|
|
|
|
mBatchQuantInfo.reset(Tensor::createDevice<int8_t>({1, size})); // keep dimensions=2!
|
|
|
|
}
|
|
|
|
success &= backend()->onAcquireBuffer(mBatchQuantInfo.get(), Backend::DYNAMIC);
|
|
|
|
|
|
|
|
// Dynamic quant.
|
2024-07-22 19:51:53 +08:00
|
|
|
// set im2col tensor info
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColBasedInt8) {
|
2025-01-22 14:47:50 +08:00
|
|
|
mQuantInput.reset((Tensor::createDevice<int8_t>({batch, mIm2ColParamter.ih, mIm2ColParamter.iw, ROUND_UP(inC, gcore->pack)})));
|
|
|
|
} else if (!mSplitByOc){
|
2025-05-08 12:39:44 +08:00
|
|
|
mQuantInput.reset((Tensor::createDevice<int8_t>({threads, colBufferSize * 1})));
|
2025-01-22 14:47:50 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
mQuantInput.reset((Tensor::createDevice<int8_t>({mTileCount, colBufferSize * 1})));
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
success &= backend()->onAcquireBuffer(mQuantInput.get(), Backend::DYNAMIC);
|
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
// set compute buffer
|
2025-05-08 12:39:44 +08:00
|
|
|
int tempSize = threads * 2 * mInputBlockNum * inputPlane;
|
|
|
|
if (!mIm2ColBasedInt8) {
|
|
|
|
if (!mSplitByOc) {
|
|
|
|
tempSize = threads * 2 * mInputBlockNum * DST_XUNIT * mIm2ColCount;
|
2025-01-22 14:47:50 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
tempSize = threads * 2 * mInputBlockNum * ROUND_UP(planeSize, DST_XUNIT);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (symmetricQuant) { // symmetric batch quant.
|
|
|
|
tempSize /= 2;
|
|
|
|
}
|
|
|
|
mSizeInputBlockQuant = tempSize / threads;
|
|
|
|
mTempMaxMinValueBuffer = bufferAlloc->alloc(tempSize * gcore->bytes);
|
|
|
|
mQScaleZero = bufferAlloc->alloc(tempSize * QUANT_INFO_BYTES);
|
|
|
|
|
|
|
|
if (mQScaleZero.invalid()) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mToFuseInputbias2Bias = (!mUseBatchQuan && option != 2) ? true : false;
|
|
|
|
if (mToFuseInputbias2Bias) { // input data has only one bias&scale
|
|
|
|
if (mIm2ColBasedInt8) {
|
|
|
|
mBiasBufferFusedInputzero = bufferAlloc->alloc(ocUp4 * QUANT_INFO_BYTES);
|
|
|
|
} else {
|
|
|
|
mBiasBufferFusedInputzero = bufferAlloc->alloc(threads *ocUp4 * QUANT_INFO_BYTES);
|
|
|
|
}
|
|
|
|
if (mBiasBufferFusedInputzero.invalid()) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAccumBuffer.reset(Tensor::createDevice<int32_t>({threads, DST_XUNIT * ALIMAX(UNIT, gcore->pack)}));
|
2025-02-12 11:14:19 +08:00
|
|
|
success &= backend()->onAcquireBuffer(mAccumBuffer.get(), Backend::DYNAMIC);
|
2024-08-24 15:46:21 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mBlockNum > 1 && kernelCount > 1) {
|
|
|
|
if (mSplitByOc) {
|
|
|
|
mReorderBuffer = bufferAlloc->alloc(UP_DIV(planeSize, DST_XUNIT) * unitColBufferSize);
|
|
|
|
} else {
|
|
|
|
mReorderBuffer = bufferAlloc->alloc(threads * colBufferSize);
|
|
|
|
}
|
|
|
|
if (mReorderBuffer.invalid()) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success || mTempMaxMinValueBuffer.invalid()) {
|
2024-07-22 19:51:53 +08:00
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
bufferAlloc->free(mBlitInfo);
|
2025-05-08 12:39:44 +08:00
|
|
|
bufferAlloc->free(mTempSrcSum);
|
|
|
|
bufferAlloc->free(mTempMaxMinValueBuffer);
|
|
|
|
bufferAlloc->free(mQScaleZero);
|
|
|
|
if (mBlockNum >1 && kernelCount > 1) {
|
|
|
|
bufferAlloc->free(mReorderBuffer);
|
2025-04-28 11:38:44 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mToFuseInputbias2Bias) {
|
|
|
|
bufferAlloc->free(mBiasBufferFusedInputzero);
|
|
|
|
}
|
|
|
|
|
2021-06-11 17:17:13 +08:00
|
|
|
backend()->onReleaseBuffer(mTempIm2ColBuffer.get(), Backend::DYNAMIC);
|
2025-02-12 11:14:19 +08:00
|
|
|
backend()->onReleaseBuffer(mBatchQuantInfo.get(), Backend::DYNAMIC);
|
2024-07-22 19:51:53 +08:00
|
|
|
backend()->onReleaseBuffer(mQuantInput.get(), Backend::DYNAMIC);
|
2025-02-12 11:14:19 +08:00
|
|
|
backend()->onReleaseBuffer(mAccumBuffer.get(), Backend::DYNAMIC);
|
|
|
|
|
2021-06-11 17:17:13 +08:00
|
|
|
return NO_ERROR;
|
2025-05-08 12:39:44 +08:00
|
|
|
#endif
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
ErrorCode DenseConvInt8TiledExecutor::onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
|
2021-06-11 17:17:13 +08:00
|
|
|
const auto input = inputs[0];
|
|
|
|
auto output = outputs[0];
|
|
|
|
auto core = static_cast<CPUBackend*>(backend())->int8Functions();
|
2024-07-22 19:51:53 +08:00
|
|
|
auto gcore = static_cast<CPUBackend*>(backend())->functions();
|
2025-05-08 12:39:44 +08:00
|
|
|
auto dynamicOption = static_cast<CPUBackend*>(backend())->getRuntime()->hint().dynamicQuantOption;
|
2021-09-18 15:52:30 +08:00
|
|
|
|
2024-10-21 14:32:47 +08:00
|
|
|
#ifdef MNN_KLEIDIAI_ENABLED
|
2024-08-16 09:05:35 +08:00
|
|
|
KleidiAI& kai = KleidiAI::getInstance();
|
2025-01-07 10:18:20 +08:00
|
|
|
if(mResourceInt8->mDynamicQuant && mResourceInt8->mActBits == 4 && kai.canAccelerate(mAccelType)) {
|
|
|
|
MNN_ASSERT(kai.isLoaded(mAccelType));
|
2024-10-22 14:37:28 +08:00
|
|
|
const size_t m = input->batch(); //lhs vector number.
|
|
|
|
const size_t n = output->channel(); //rhs vector number.
|
|
|
|
const size_t k = input->channel(); //vector size.
|
2025-01-07 10:18:20 +08:00
|
|
|
const size_t blkSize = mBlockNum == 1 ? 0 : k / mBlockNum;
|
|
|
|
|
|
|
|
bool bHalf = kai.isHalf();
|
|
|
|
size_t elementSize = bHalf ? sizeof(__fp16) : sizeof(float);
|
|
|
|
size_t lhsPackedSize = kai.getLhsQuantedPackedSize(mAccelType, m, k, blkSize);
|
2024-08-16 09:05:35 +08:00
|
|
|
|
2024-10-22 14:37:28 +08:00
|
|
|
auto lhs = input->host<uint8_t>();
|
|
|
|
auto lhsPacked = mTempIm2ColBuffer->host<int8_t>();
|
|
|
|
auto rhsPacked = mResourceInt8->mWeightInt8->host<uint8_t>();
|
|
|
|
auto dst = output->host<uint8_t>();
|
2024-10-21 14:32:47 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
uint8_t *linearLhs, *linearDst;
|
|
|
|
if(m > 1 && !kai.isLinear()) {
|
|
|
|
linearLhs = (uint8_t *)lhsPacked + lhsPackedSize;
|
|
|
|
linearDst = linearLhs;
|
|
|
|
} else {
|
|
|
|
linearLhs = lhs;
|
|
|
|
linearDst = dst;
|
|
|
|
}
|
|
|
|
|
2024-10-22 14:37:28 +08:00
|
|
|
int threadNum = static_cast<CPUBackend*>(backend())->threadNumber();
|
|
|
|
int threadNeed, vecPerThread;
|
2024-08-16 09:05:35 +08:00
|
|
|
|
2024-10-22 14:37:28 +08:00
|
|
|
//Dynamic quant pack lhs.
|
|
|
|
if(m == 1) {
|
2025-01-07 10:18:20 +08:00
|
|
|
kai.runLhsQuantPack(mAccelType, 1, k, blkSize, 1, linearLhs, lhsPacked);
|
2024-10-22 14:37:28 +08:00
|
|
|
} else {
|
2025-01-07 10:18:20 +08:00
|
|
|
if(!kai.isLinear()) {
|
|
|
|
if(bHalf) {
|
|
|
|
KleidiAIUtil::transferNC4HW4ToNCHW((__fp16 *)lhs, (__fp16 *)linearLhs, m, k);
|
|
|
|
} else {
|
|
|
|
KleidiAIUtil::transferNC4HW4ToNCHW((float *)lhs, (float *)linearLhs, m, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vecPerThread = kai.getVecNumPerThread(m, threadNum, kai.getMr(mAccelType, m));
|
2024-10-22 14:37:28 +08:00
|
|
|
threadNeed = m % vecPerThread == 0 ? m / vecPerThread : (m / vecPerThread + 1);
|
2025-01-07 10:18:20 +08:00
|
|
|
size_t srcStride = vecPerThread * k * elementSize;
|
2024-10-22 14:37:28 +08:00
|
|
|
|
|
|
|
auto BatchDynamicQuant = [=, &kai](int tId) {
|
2025-01-07 10:18:20 +08:00
|
|
|
auto threadSrc = linearLhs + tId * srcStride;
|
|
|
|
auto threadDst = lhsPacked + kai.getLhsQuantedPackedOffset(mAccelType, m, tId * vecPerThread, k, blkSize);
|
2024-10-22 14:37:28 +08:00
|
|
|
int vecNum = (tId == threadNeed - 1) ? (m - vecPerThread * tId) : vecPerThread; //Last threadN may less than vecPerThread.
|
2025-01-07 10:18:20 +08:00
|
|
|
kai.runLhsQuantPack(mAccelType, vecNum, k, blkSize, kai.getMr(mAccelType, m), threadSrc, threadDst);
|
2024-08-16 09:05:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MNN_CONCURRENCY_BEGIN(tId, threadNeed) {
|
2024-10-22 14:37:28 +08:00
|
|
|
BatchDynamicQuant((int)tId);
|
2024-08-16 09:05:35 +08:00
|
|
|
}
|
|
|
|
MNN_CONCURRENCY_END();
|
2024-10-22 14:37:28 +08:00
|
|
|
}
|
|
|
|
|
2025-02-11 14:23:54 +08:00
|
|
|
//Run matmul.
|
2025-04-25 08:27:01 +08:00
|
|
|
if(kai.bSupportSme2() && mAccelType == KleidiAI::AccelType::QI4_SYM_CHNLQT) {
|
2025-02-11 14:23:54 +08:00
|
|
|
//SME prefer running on single thread to obtain better performance/power consumption ratio.
|
|
|
|
threadNum = 1;
|
|
|
|
}
|
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
vecPerThread = kai.getVecNumPerThread(n, threadNum, kai.getNStep(mAccelType));
|
2024-10-22 14:37:28 +08:00
|
|
|
threadNeed = n % vecPerThread == 0 ? n / vecPerThread : (n / vecPerThread + 1);
|
|
|
|
|
|
|
|
auto ThreadFunction = [=, &kai](int tId) {
|
2025-01-07 10:18:20 +08:00
|
|
|
auto threadRhsPacked = rhsPacked + kai.getRhsPackedOffset(mAccelType, tId * vecPerThread, k, blkSize);
|
|
|
|
auto threadDst = linearDst + kai.getDstOffset(0, tId * vecPerThread, n, elementSize);
|
2024-10-22 14:37:28 +08:00
|
|
|
int vecNum = (tId == threadNeed - 1) ? (n - vecPerThread * tId) : vecPerThread; //Last threadN may less than vecPerThread.
|
2025-04-24 08:32:00 +08:00
|
|
|
float scalarMax = bHalf ? FLT16_MAX : FLT_MAX;
|
|
|
|
kai.runMatmul(mAccelType, m, vecNum, k, blkSize, lhsPacked, threadRhsPacked, threadDst, n * elementSize, elementSize, scalarMax, -scalarMax);
|
2024-10-22 14:37:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MNN_CONCURRENCY_BEGIN(tId, threadNeed) {
|
|
|
|
ThreadFunction((int)tId);
|
|
|
|
}
|
|
|
|
MNN_CONCURRENCY_END();
|
2024-08-16 09:05:35 +08:00
|
|
|
|
2025-01-07 10:18:20 +08:00
|
|
|
if(m > 1 && !kai.isLinear()) {
|
|
|
|
if(bHalf) {
|
|
|
|
KleidiAIUtil::transferNCHWToNC4HW4((__fp16 *)linearDst, (__fp16 *)dst, m, n);
|
|
|
|
} else {
|
|
|
|
KleidiAIUtil::transferNCHWToNC4HW4((float *)linearDst, (float *)dst, m, n);
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 14:37:28 +08:00
|
|
|
|
|
|
|
return NO_ERROR;
|
2024-08-16 09:05:35 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
int UNIT, SRC_UNIT, DST_XUNIT;
|
|
|
|
core->MNNGetGemmUnit(&UNIT, &SRC_UNIT, &DST_XUNIT);
|
2023-06-16 09:42:45 +08:00
|
|
|
auto blitProc = core->MNNPackC4Int8ForMatMul_A;
|
2024-07-22 19:51:53 +08:00
|
|
|
const int plane = output->batch() * mIm2ColParamter.oh * mIm2ColParamter.ow;
|
|
|
|
const int batch = input->batch();
|
|
|
|
const int PackUnit = gcore->pack;
|
|
|
|
const int dstZStep = plane * PackUnit;
|
|
|
|
const int ocDiv4 = UP_DIV(output->channel(), PackUnit);
|
|
|
|
const int ocUp4 = ROUND_UP(output->channel(), PackUnit);
|
2025-05-08 12:39:44 +08:00
|
|
|
const auto kernelCountUnit = mIm2ColParamter.kernelCountUnit;
|
|
|
|
const auto unitColBufferSize = kernelCountUnit * DST_XUNIT * SRC_UNIT * sizeof(int8_t);
|
|
|
|
const auto colBufferSize = unitColBufferSize * mIm2ColCount;
|
2024-07-22 19:51:53 +08:00
|
|
|
const int dstBytes = static_cast<CPUBackend*>(backend())->getBytes(backend(), output);
|
2025-05-08 12:39:44 +08:00
|
|
|
const int blockL = kernelCountUnit / mBlockNum; // source depthQuad for each block.
|
2025-04-28 11:38:44 +08:00
|
|
|
const int kxky = mIm2ColParamter.kernelX * mIm2ColParamter.kernelY;
|
2025-05-08 12:39:44 +08:00
|
|
|
const int blocklu = blockL / kxky; // UP_DIV(ic,src_unit) per block
|
2024-07-22 19:51:53 +08:00
|
|
|
float weightBytes = 1.f;
|
2025-05-08 12:39:44 +08:00
|
|
|
int weightStepY = weightBytes * (UNIT * SRC_UNIT);
|
2025-02-12 11:14:19 +08:00
|
|
|
int inputPlane = batch * input->width() * input->height();
|
2024-07-22 19:51:53 +08:00
|
|
|
|
|
|
|
auto im2colPtr = mTempIm2ColBuffer->host<int8_t>();
|
2024-10-14 19:26:28 +08:00
|
|
|
if (SRC_UNIT > PackUnit) {
|
|
|
|
memset(im2colPtr, 0, mTempIm2ColBuffer->size());
|
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
const auto weightDataPtr = mResourceInt8->mWeightInt8->host<int8_t>();
|
2025-05-08 12:39:44 +08:00
|
|
|
auto srcKernelSumPtr = (int8_t*)mTempSrcSum.ptr();
|
|
|
|
auto im2colSrc = input->host<uint8_t>();
|
2024-07-22 19:51:53 +08:00
|
|
|
auto outputDataPtr = output->host<int8_t>();
|
2024-09-12 12:57:57 +08:00
|
|
|
uint8_t* biasPtr = nullptr;
|
|
|
|
int32_t inputZeroPoint = 0;
|
2025-05-08 12:39:44 +08:00
|
|
|
int im2colBytes = mIm2ColBasedInt8 == true ? 1 : gcore->bytes;
|
|
|
|
|
2024-09-12 12:57:57 +08:00
|
|
|
if (nullptr != mMutableResource.get()) {
|
|
|
|
biasPtr = mMutableResource->mBiasFloat->host<uint8_t>();
|
|
|
|
inputZeroPoint = mMutableResource->mInputZeroPoint;
|
2025-02-12 11:14:19 +08:00
|
|
|
if (mBatchQuantInfo.get()) {
|
|
|
|
float scalein = TensorUtils::getQuantInfo(inputs[0])[0];
|
|
|
|
float scaleou = TensorUtils::getQuantInfo(outputs[0])[0];
|
2025-05-08 12:39:44 +08:00
|
|
|
auto scaleX = scalein / scaleou;
|
|
|
|
for (int i = 0; i < DST_XUNIT; ++i) {
|
|
|
|
mBatchQuantInfo->host<float>()[i] = scaleX;
|
2025-02-12 11:14:19 +08:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 12:57:57 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
#ifdef MNN_LOW_MEMORY
|
|
|
|
auto BatchAsyDynamicQuant = [&](uint8_t* floatPtr, int32_t& inputZero, uint8_t* inputDequantScale, int LDiv4, int eCount, int innerSide, int32_t availableThreads, int8_t* dstInt8, uint8_t* inputDequantBias, int tId) {
|
|
|
|
// if mIm2ColBasedInt8=false, input shape: [kernelsize,mBlockNum,blocklu,EP,LP]
|
|
|
|
// if mIm2ColBasedInt8=true, input shape: [ic/pack,EP,pack]
|
|
|
|
auto scalePtr = (float*)inputDequantScale;
|
|
|
|
auto zeroPtr = (float*)inputDequantBias;
|
|
|
|
int scaleCount = mSizeInputBlockQuant;
|
|
|
|
int kernelsize = 1;
|
|
|
|
if (!mIm2ColBasedInt8) {
|
|
|
|
kernelsize = kxky;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
2021-06-11 17:17:13 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
auto minPtr = mTempMaxMinValueBuffer.ptr() + tId * scaleCount * gcore->bytes;
|
|
|
|
auto maxPtr = mTempMaxMinValueBuffer.ptr() + tId * scaleCount * gcore->bytes + (scaleCount / 2) * gcore->bytes;
|
|
|
|
auto qscale = (float*)(mQScaleZero.ptr() + tId * scaleCount * QUANT_INFO_BYTES);
|
|
|
|
auto qbias = (float*)(mQScaleZero.ptr() + tId * scaleCount * QUANT_INFO_BYTES + (scaleCount / 2) * QUANT_INFO_BYTES);
|
|
|
|
|
|
|
|
size_t info[9] = {(size_t)mInputBlockNum, (size_t)eCount, (size_t)innerSide, (size_t)DST_XUNIT, (size_t)SRC_UNIT, (size_t)kernelsize, (size_t)blocklu, 0, 0};
|
|
|
|
if (mIm2ColBasedInt8) {
|
|
|
|
info[6] = LDiv4 / mInputBlockNum;
|
2024-11-18 14:37:45 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mToFuseInputbias2Bias) {
|
|
|
|
info[7] = 1;
|
2024-10-11 11:52:14 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColParamter.padX > 0 || mIm2ColParamter.padY > 0) {
|
|
|
|
info[8] = 1;
|
2025-02-12 11:14:19 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
// scale&bias:float32
|
|
|
|
gcore->MNNAsyQuantInfo(scalePtr, zeroPtr, qscale, qbias, (float*)minPtr, (float*)maxPtr, (float*)floatPtr, info);
|
2024-07-22 19:51:53 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
// quant: float->int8_t
|
|
|
|
if (!mToFuseInputbias2Bias) {
|
|
|
|
gcore->MNNAsyQuantFunc(dstInt8, (float*)floatPtr, qscale, qbias, info);
|
|
|
|
} else {
|
|
|
|
auto sizeDiv4 = UP_DIV(eCount * LDiv4 * innerSide, PackUnit);
|
|
|
|
mQuantFunc((float*)floatPtr, dstInt8, sizeDiv4, qscale, -128, 127, qbias, 0);
|
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mToFuseInputbias2Bias) { // Decode
|
|
|
|
inputZero = qbias[0];
|
|
|
|
auto updatedBiasPtr = (float*)(mBiasBufferFusedInputzero.ptr() + tId * ocUp4 * QUANT_INFO_BYTES);
|
|
|
|
auto matmulBiasPtr = mResourceInt8->mOriginBias->host<float>();
|
|
|
|
auto weightKernelSum = mResourceInt8->mWeightKernelSum->host<float>();
|
|
|
|
auto zero_ = -inputZero * scalePtr[0];
|
|
|
|
gcore->MNNDynamicUpdateConvBiasScale(updatedBiasPtr, matmulBiasPtr, weightKernelSum, &zero_, UP_DIV(ocUp4, 4));
|
|
|
|
biasPtr = (uint8_t*)updatedBiasPtr;
|
|
|
|
auto unitsize = mBatchQuantInfo->length(1) / (2 * QUANT_INFO_BYTES);
|
|
|
|
auto inputScale = scalePtr[0];
|
|
|
|
for (int i = 0; i < unitsize; ++i) {
|
|
|
|
((float*)inputDequantScale)[i] = inputScale;
|
|
|
|
}
|
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
};
|
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
auto BatchSymDynamicQuant = [&](uint8_t* floatPtr, int32_t& inputZero, uint8_t* inputDequantScale, int LU, int EP, int LP, int32_t availableThreads, int8_t* dstInt8, int tId) {
|
|
|
|
auto quantPtr = mQScaleZero.ptr() + tId * mSizeInputBlockQuant * QUANT_INFO_BYTES;
|
|
|
|
auto maxPtr = mTempMaxMinValueBuffer.ptr() + tId * mSizeInputBlockQuant * gcore->bytes;
|
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
// compute sum and absmax
|
2025-02-12 11:14:19 +08:00
|
|
|
int divlu = UP_DIV(LU, availableThreads);
|
2025-05-08 12:39:44 +08:00
|
|
|
MNN_CONCURRENCY_BEGIN (tIdx, ALIMIN(availableThreads, UP_DIV(LU, divlu))) {
|
|
|
|
auto exeLu = ALIMIN(divlu, LU - tIdx * divlu);
|
|
|
|
auto batchMax = reinterpret_cast<float*>(maxPtr + tIdx * EP * gcore->bytes);
|
|
|
|
auto ptr_ = reinterpret_cast<float*>(floatPtr + tIdx * divlu * gcore->bytes * EP * LP);
|
|
|
|
gcore->MNNAbsMax((float*)ptr_, batchMax, exeLu, EP, LP);
|
2025-02-12 11:14:19 +08:00
|
|
|
} MNN_CONCURRENCY_END();
|
|
|
|
|
2024-07-22 19:51:53 +08:00
|
|
|
|
|
|
|
// Compute quant scale
|
2025-02-12 11:14:19 +08:00
|
|
|
gcore->MNNQuantScale((float*)maxPtr, (float*)quantPtr, (float*)inputDequantScale, availableThreads, EP);
|
2024-07-22 19:51:53 +08:00
|
|
|
|
|
|
|
// quant
|
2025-02-12 11:14:19 +08:00
|
|
|
auto scale_ptr = reinterpret_cast<float*>(quantPtr);
|
2025-05-08 12:39:44 +08:00
|
|
|
gcore->MNNDynamicQuant((float*)floatPtr, dstInt8, scale_ptr, LU, EP, LP, nullptr);
|
2025-01-22 14:47:50 +08:00
|
|
|
inputZero = 0;
|
2024-07-22 19:51:53 +08:00
|
|
|
};
|
2025-01-22 14:47:50 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mResourceInt8->mDynamicQuant) {
|
2025-01-22 14:47:50 +08:00
|
|
|
biasPtr = mResourceInt8->mOriginBias->host<uint8_t>();
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColBasedInt8 && mResourceInt8->mDynamicQuant) {
|
|
|
|
int icDiv4 = UP_DIV(input->channel(), PackUnit);
|
2025-01-22 14:47:50 +08:00
|
|
|
if (mUseBatchQuan) {
|
2025-05-08 12:39:44 +08:00
|
|
|
int availthreads = (icDiv4 > mThreadNums && inputPlane > 255 ) ? mThreadNums : 1;
|
|
|
|
if (dynamicOption != 2) {
|
|
|
|
BatchSymDynamicQuant(input->host<uint8_t>(), inputZeroPoint, mBatchQuantInfo->host<uint8_t>(), icDiv4, inputPlane, PackUnit, availthreads, mQuantInput->host<int8_t>(), 0);
|
|
|
|
} else {
|
|
|
|
BatchAsyDynamicQuant(input->host<uint8_t>(), inputZeroPoint, mBatchQuantInfo->host<uint8_t>(), icDiv4, inputPlane, PackUnit, availthreads, mQuantInput->host<int8_t>(), mBatchQuantInfo->host<uint8_t>() + mBatchQuantInfo->stride(0) / 2, 0);
|
2025-04-28 11:38:44 +08:00
|
|
|
}
|
2025-01-22 14:47:50 +08:00
|
|
|
} else {
|
2025-05-08 12:39:44 +08:00
|
|
|
BatchAsyDynamicQuant(input->host<uint8_t>(), inputZeroPoint, mBatchQuantInfo->host<uint8_t>(), icDiv4, inputPlane, PackUnit, 1, mQuantInput->host<int8_t>(), mBatchQuantInfo->host<uint8_t>() + mBatchQuantInfo->stride(0) / 2, 0);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colSrc = mQuantInput->host<uint8_t>();
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
#endif
|
2024-08-24 15:46:21 +08:00
|
|
|
if (mResourceInt8->mActBits == 4) {
|
2024-07-22 19:51:53 +08:00
|
|
|
weightBytes = 0.5;
|
2025-05-08 12:39:44 +08:00
|
|
|
weightStepY /= 2;
|
2023-12-27 17:26:44 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
int blockunit = ocUp4 * 2 * QUANT_INFO_BYTES + blockL * weightStepY * UP_DIV(output->channel(), UNIT);
|
2025-01-22 14:47:50 +08:00
|
|
|
auto inputchannel = input->channel();
|
2024-07-22 19:51:53 +08:00
|
|
|
SumByAxisParams sumParams;
|
2025-05-08 12:39:44 +08:00
|
|
|
sumParams.oneScale = (mUseBatchQuan || dynamicOption == 2) ? 0 : 1;
|
2024-07-22 19:51:53 +08:00
|
|
|
sumParams.SRC_UNIT = SRC_UNIT;
|
|
|
|
sumParams.blockNum = mBlockNum;
|
|
|
|
sumParams.DST_XUNIT = DST_XUNIT;
|
2025-05-08 12:39:44 +08:00
|
|
|
sumParams.unitColBufferSize = unitColBufferSize;
|
|
|
|
sumParams.kernelCountUnitDouble = kernelCountUnit;
|
2025-01-22 14:47:50 +08:00
|
|
|
sumParams.valid = inputchannel % SRC_UNIT;
|
2025-04-28 11:38:44 +08:00
|
|
|
sumParams.kernelxy = kxky;
|
2025-01-22 14:47:50 +08:00
|
|
|
sumParams.LU = UP_DIV(inputchannel, SRC_UNIT);
|
2025-05-08 12:39:44 +08:00
|
|
|
sumParams.inputBlock = (mInputBlockNum > 1) ? 1 : 0;
|
2024-08-24 15:46:21 +08:00
|
|
|
|
2025-01-22 14:47:50 +08:00
|
|
|
auto tileSplitFunction = [&](int tId, int eStartIndex, int eEndIndex, int estep) {
|
2024-07-22 19:51:53 +08:00
|
|
|
auto ocDivThread = ocDiv4;
|
2024-08-24 15:46:21 +08:00
|
|
|
float* reluPtr = mResourceInt8->mReluThreshold.data();
|
2025-02-12 11:14:19 +08:00
|
|
|
float* accumbuff = nullptr;
|
2025-05-08 12:39:44 +08:00
|
|
|
uint8_t* inputScale = nullptr;
|
|
|
|
uint8_t* inputBias = nullptr;
|
|
|
|
uint8_t* ptrInputScale = nullptr;
|
|
|
|
uint8_t* ptrInputBias = nullptr;
|
|
|
|
if (mBatchQuantInfo.get()) {
|
|
|
|
if (mIm2ColBasedInt8) {
|
|
|
|
inputScale = mBatchQuantInfo->host<uint8_t>();
|
|
|
|
ptrInputScale = inputScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dynamicOption == 2 && mUseBatchQuan && mIm2ColBasedInt8) {
|
|
|
|
inputBias = inputScale + mBatchQuantInfo->stride(0) / 2;
|
|
|
|
ptrInputBias = inputBias;
|
|
|
|
}
|
2025-02-12 11:14:19 +08:00
|
|
|
}
|
|
|
|
if (mBlockNum > 1) {
|
|
|
|
accumbuff = reinterpret_cast<float*>(mAccumBuffer->host<int8_t>() + tId * mAccumBuffer->stride(0) * sizeof(int32_t));
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
float* ptrY = nullptr;
|
|
|
|
if ((dstBytes != 1)) {
|
|
|
|
ptrY = mResourceInt8->mWeightKernelSum->host<float>();
|
2025-02-12 11:14:19 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
QuanPostTreatParameters quanParam;
|
|
|
|
quanParam.blockNum = mBlockNum;
|
|
|
|
quanParam.weightKernelSum = ptrY;
|
2024-07-22 19:51:53 +08:00
|
|
|
if (dstBytes != 1) {
|
|
|
|
quanParam.useInt8 = 0;
|
|
|
|
quanParam.fp32minmax = reluPtr;
|
|
|
|
} else {
|
2024-08-24 15:46:21 +08:00
|
|
|
quanParam.maxValue = mMutableResource->mClampMax;
|
2024-07-22 19:51:53 +08:00
|
|
|
if (mResourceInt8->mRelu) {
|
2024-08-24 15:46:21 +08:00
|
|
|
quanParam.minValue = mMutableResource->mOutputZeroPoint;
|
2024-07-22 19:51:53 +08:00
|
|
|
} else {
|
2024-08-24 15:46:21 +08:00
|
|
|
quanParam.minValue = mMutableResource->mClampMin;
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-22 14:47:50 +08:00
|
|
|
auto weightPtrTid = weightDataPtr;
|
2025-05-08 12:39:44 +08:00
|
|
|
quanParam.biasFloat = reinterpret_cast<float*>(biasPtr);
|
|
|
|
auto im2colDstThread = im2colPtr + tId * mTempIm2ColBuffer->stride(0);
|
2023-09-04 10:42:11 +08:00
|
|
|
auto srcPtr = (int8_t const **)(mBlitInfo.ptr() + tId * mBlitInfoStride.first);
|
2023-06-16 09:42:45 +08:00
|
|
|
auto el = (int32_t *)(srcPtr + mBlitInfoStride.second);
|
2025-05-08 12:39:44 +08:00
|
|
|
auto xKernelSumPtrTid = reinterpret_cast<float*>(srcKernelSumPtr + tId * mBlockNum * DST_XUNIT * mIm2ColCount * QUANT_INFO_BYTES);
|
2023-06-16 09:42:45 +08:00
|
|
|
|
2025-01-22 14:47:50 +08:00
|
|
|
int32_t info[5];
|
2023-06-16 09:42:45 +08:00
|
|
|
info[1] = mIm2ColParamter.iw * mIm2ColParamter.ih * batch;
|
2025-05-08 12:39:44 +08:00
|
|
|
info[2] = static_cast<int32_t>(unitColBufferSize);
|
2023-06-16 09:42:45 +08:00
|
|
|
info[3] = mIm2ColParamter.strideX;
|
2024-07-22 19:51:53 +08:00
|
|
|
for (int tIndex = eStartIndex; tIndex < eEndIndex; tIndex += estep) {
|
2023-06-16 09:42:45 +08:00
|
|
|
const int xIndexStart = tIndex * DST_XUNIT * mIm2ColCount;
|
2025-01-22 14:47:50 +08:00
|
|
|
auto outputInTilePtr = outputDataPtr + xIndexStart * PackUnit * dstBytes;
|
2023-06-16 09:42:45 +08:00
|
|
|
int realDstCount = ALIMIN(plane - xIndexStart, DST_XUNIT * mIm2ColCount);
|
2025-05-08 12:39:44 +08:00
|
|
|
ptrInputScale = (mUseBatchQuan && mIm2ColBasedInt8) ? (inputScale + xIndexStart * mInputBlockNum * QUANT_INFO_BYTES) : inputScale;
|
|
|
|
ptrInputBias = (inputBias != nullptr) ? (inputBias + xIndexStart * mInputBlockNum * QUANT_INFO_BYTES) : inputBias;
|
2023-06-16 09:42:45 +08:00
|
|
|
// im2col
|
2025-05-08 12:39:44 +08:00
|
|
|
auto im2colDst = im2colDstThread;
|
2025-01-22 14:47:50 +08:00
|
|
|
auto res = ConvolutionTiledExecutor::turnIm2ColToBlitInfo((const float**)srcPtr, el, xIndexStart, realDstCount, mIm2ColParamter, (uint8_t*)im2colSrc, im2colBytes);
|
2023-06-16 09:42:45 +08:00
|
|
|
int number = res.first;
|
|
|
|
bool needZero = res.second;
|
2025-05-08 12:39:44 +08:00
|
|
|
if (needZero && mIm2ColBasedInt8) {
|
2021-06-11 17:17:13 +08:00
|
|
|
#ifdef MNN_USE_SSE
|
2025-05-08 12:39:44 +08:00
|
|
|
::memset(im2colDst, inputZeroPoint + 128, colBufferSize);
|
2021-09-18 15:52:30 +08:00
|
|
|
#else
|
2025-05-08 12:39:44 +08:00
|
|
|
::memset(im2colDst, inputZeroPoint, colBufferSize);
|
2021-06-11 17:17:13 +08:00
|
|
|
#endif
|
|
|
|
}
|
2023-06-16 09:42:45 +08:00
|
|
|
info[0] = number;
|
2024-07-22 19:51:53 +08:00
|
|
|
info[4] = realDstCount;
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColBasedInt8 && number > 0) {
|
|
|
|
blitProc(im2colDst, srcPtr, info, el);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
#ifdef MNN_LOW_MEMORY
|
|
|
|
if (!mIm2ColBasedInt8) {
|
|
|
|
if (needZero) {
|
|
|
|
::memset(im2colDst, 0, mTempIm2ColBuffer->stride(0));
|
|
|
|
}
|
|
|
|
if (number > 0) {
|
|
|
|
if (SRC_UNIT > PackUnit && !needZero) {
|
|
|
|
memset(im2colDst, 0, mTempIm2ColBuffer->stride(0));
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
info[2] = realDstCount;
|
|
|
|
gcore->MNNGeneralIm2Col((float*)im2colDst, (float const**)srcPtr, info, el, SRC_UNIT, PackUnit); // im2colDst: [lu, realDstCount, lp]
|
|
|
|
}
|
|
|
|
ptrInputScale = mBatchQuantInfo->host<uint8_t>() + tId * mBatchQuantInfo->stride(0);
|
|
|
|
if (dynamicOption == 2) {
|
|
|
|
ptrInputBias = ptrInputScale + mBatchQuantInfo->stride(0) / 2;
|
|
|
|
BatchAsyDynamicQuant((uint8_t*)im2colDst, inputZeroPoint, ptrInputScale, kernelCountUnit, realDstCount, SRC_UNIT, 1, mQuantInput->host<int8_t>() + tId * mQuantInput->stride(0), ptrInputBias, tId);
|
|
|
|
} else if (mUseBatchQuan) {
|
|
|
|
BatchSymDynamicQuant((uint8_t*)im2colDst, inputZeroPoint, ptrInputScale, kernelCountUnit, realDstCount, SRC_UNIT, 1, mQuantInput->host<int8_t>() + tId * mQuantInput->stride(0), tId);
|
|
|
|
} else {
|
|
|
|
auto maxMinPtr = mTempMaxMinValueBuffer.ptr() + tId * 2 * gcore->bytes;
|
|
|
|
ptrInputBias = ptrInputScale + mBatchQuantInfo->stride(0) / 2;
|
|
|
|
BatchAsyDynamicQuant((uint8_t*)im2colDst, inputZeroPoint, ptrInputScale, kernelCountUnit, realDstCount, SRC_UNIT, 1, mQuantInput->host<int8_t>() + tId * mQuantInput->stride(0), ptrInputBias, tId);
|
|
|
|
quanParam.biasFloat = (float*)(mBiasBufferFusedInputzero.ptr() + tId * ocUp4 * QUANT_INFO_BYTES);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDst = mQuantInput->host<int8_t>() + tId * mQuantInput->stride(0);
|
2023-06-16 09:42:45 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mBlockNum > 1 && kxky > 1) {
|
|
|
|
auto eU = UP_DIV(realDstCount, DST_XUNIT); // eU <= mIm2ColCount
|
|
|
|
auto reorderBuffer = mReorderBuffer.ptr() + tId * colBufferSize;
|
2025-04-28 11:38:44 +08:00
|
|
|
for (int k = 0; k < eU; ++k) {
|
|
|
|
int inside = blocklu * SRC_UNIT * ALIMIN(realDstCount - k * DST_XUNIT, DST_XUNIT);
|
2025-05-08 12:39:44 +08:00
|
|
|
auto dstbuffer = reorderBuffer + k * unitColBufferSize;
|
|
|
|
auto srcbuffer = im2colDst + k * unitColBufferSize;
|
2025-04-28 11:38:44 +08:00
|
|
|
for (int i = 0; i < mBlockNum; ++i) {
|
|
|
|
for (int j = 0; j < kxky; ++j) {
|
|
|
|
memcpy(dstbuffer + i * kxky * inside + j * inside, srcbuffer + i * inside + j * mBlockNum * inside, inside);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDst = (int8_t*)reorderBuffer;
|
2025-04-28 11:38:44 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
#endif
|
2024-07-22 19:51:53 +08:00
|
|
|
if (mResourceInt8->mWeightAsymmetricQuant) {
|
2025-02-12 11:14:19 +08:00
|
|
|
MNN_ASSERT(mBatchQuantInfo.get() && mBatchQuantInfo->host<float>());
|
2025-05-08 12:39:44 +08:00
|
|
|
gcore->MNNSumByAxisLForMatmul_A(xKernelSumPtrTid, im2colDst, (float*)ptrInputScale, realDstCount, sumParams);
|
|
|
|
} else {
|
|
|
|
memset(xKernelSumPtrTid, 0, mBlockNum * DST_XUNIT * mIm2ColCount * QUANT_INFO_BYTES);
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
auto ptrX = xKernelSumPtrTid;
|
2025-02-12 11:14:19 +08:00
|
|
|
do {
|
|
|
|
int step = ALIMIN(DST_XUNIT, realDstCount);
|
2025-05-08 12:39:44 +08:00
|
|
|
quanParam.inputScale = (float*)ptrInputScale;
|
|
|
|
quanParam.inputBias = (float*)ptrInputBias;
|
2025-02-12 11:14:19 +08:00
|
|
|
if (mBlockNum > 1) {
|
2025-05-08 12:39:44 +08:00
|
|
|
memset(accumbuff, 0, UNIT * 4 * DST_XUNIT);
|
2025-02-12 11:14:19 +08:00
|
|
|
quanParam.accumBuffer = accumbuff;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
quanParam.srcKernelSum = ptrX;
|
|
|
|
mGemmKernel(outputInTilePtr, im2colDst, weightPtrTid, blockL, dstZStep * dstBytes, ocDivThread, &quanParam, step);
|
|
|
|
|
2025-02-12 11:14:19 +08:00
|
|
|
ptrX += (step * mBlockNum);
|
|
|
|
realDstCount-=step;
|
|
|
|
outputInTilePtr += DST_XUNIT * PackUnit * dstBytes;
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDst += unitColBufferSize;
|
|
|
|
ptrInputScale = mUseBatchQuan ? (ptrInputScale + step * mInputBlockNum * QUANT_INFO_BYTES) : ptrInputScale;
|
|
|
|
ptrInputBias = (ptrInputBias != nullptr) ? (ptrInputBias + step * mInputBlockNum * QUANT_INFO_BYTES) : ptrInputBias;
|
2025-02-12 11:14:19 +08:00
|
|
|
} while(realDstCount > 0);
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
[MNN:Sync] Sync internal github
Commits:
8148ae75c 弗人 bugfix
14cb8ec7f 弗人 [Converter:Bugfix] bugfix for onnx depthwise convtranspose
476fbcd90 雁行 [MNN:Feature] Open AVX cast and bugfix for contentCFG.
5e26b9fd3 雁行 [Test:Feature] Add android test.
37e147b25 雁行 [MNN:Bugfix] Bugfix for floordiv.
144c185f5 tianbu.xsw hangxing fix hiai
b4fd429d6 tianbu.xsw updateCacheFile bugfix -- update cache size
d4ba572a8 雁行 [MNN:Bugfix] Support int8 in AVX2 and some Bugfix.
43061f07e xiaying [MNN:Bugfix] Fix bug for module mode run part of model
398cc5ab6 tianhang.yth refactor demo
736380600 xiaying [Express:Bugfix] Fix memory leak for copy branch
b8dab0a27 tianhang.yth MNNFloat2Int8 sizeQuad=0 crash fix
94b95bfed ghz [BugFix]1.Better method for fast pack valid check
6a921f85e xiaying [Converter:Bugfix] Fix bug for Fuseconsttosubgraph
5f77ae889 tianhang.yth numThread bugfix
a807ef879 tianhang.yth add createSession(configs, runtimeinfo) API, add pymnn demo, pymnn logcat bugfix
ad05409d3 xiaying [MNN:Bugfix] Fix bug for StaticModule's sizecompute overflow, add error print for module mode
9d81b8299 xiaying [MNN:Bugfix] Fix bug for Unique op for output size = 1
03b15e9af xiaying [Test:Feature] Add MatMulBConst Test, Fix bug for single Convert
c944a76ee tianhang.yth add auto backend and getSessionInfo @tianbu
91fa7267b ghz [BugFix]1.fix the error in eP check
bf0041f77 ghz [BugFix]1.Fix the logic error in eP check. 2.Fix the sp align error
693871672 雁行 [CPU:Bugfix] rm adrp instruction for clang compiler bug.
1b8f6b3d8 ghz 1.Fix the wronly use of r13 in arm32 version. 2.Fix the missing callee register save and restore process.
feb7ecc4c 弗人 modify log of python offline quant
040c04811 ghz [BufFix]1.replace platform-related regs. 2.fix the same problem in arm32 version
609f37db8 弗人 add log for python quant, python convert
5511dd30a ghz [BugFix]1.Add testcases in SparseConv to check all functional code branch. 2. Fix the bug in "MNNPackC4ForMatMul_A.S" in arm64, which is caused by the missing check of eReal parameter.
a93ff9280 tianhang.yth add tf.Unique op support
9729ff773 allen.lk [Bugfix] Fix one arm32 instruction syntax that clang works but gcc DOES NOT work. use index instruction instead.
297c1ad14 雁行 [Expr:Bugfix] bugfix for tensor content used by shape compute.
ef8c369e3 弗人 catch exception
07c2dd670 弗人 add dependence to setup, base64 encode url, add time log
177e590c1 弗人 [Python:Feature] add aliyun log for python quant tool
40a7928cf allen.lk [Debug:Sparse] 1.Add group parameter in torchscript converter. 2. Stop split running to avoid memory corruption when check failed in TransformGroupConvolution 3. fix Op split issue in TransformGroupConvolution
3bdea84a1 allen.lk [Debug:Sparse] Fix and warning one kind of segmentfault cause by memory corruption when resize ConvolutionWinograd. Avoid to use some registers as arm restriction.
c3c6fbdbd allen.lk [Debug:Sparse] Fix and warning one kind of segmentfault cause by memory corruption when resize ConvolutionWinograd. Avoid to use some registers as arm restriction.
bc590eee4 雁行 [Converter:Bugfix] bugfix for onnx instancenormalization convert.
d8918593f tianhang.yth add auto backend and getSessionInfo @tianbu
83a198ed7 杭行 update
d0dd3e09b 杭行 update
99540202e xiaying [Converter:Optimize] Opt the tensor convert insert
333d8db82 allen.lk [Debug:Sparse] Fix All platform-register r9 / x18 issue on arm32 and arm64.
db5994672 杭行 merge
6293de7b8 tianbu.xsw fix pymnn updateCacheFile
5c2e11cb1 tianbu.xsw do updateCache in createSession
6e7641ff4 tianbu.xsw do not limit cacheFile for a model
5287a65e4 tianbu.xsw bugfix
52ba53a91 tianbu.xsw revert pymnn api
60284d830 tianbu.xsw bugfix
6d8077490 tianbu.xsw rename updateCacheFile api params
3cb172710 tianhang.yth updateCacheFile API size default value is 0
c5b69aabf tianbu.xsw updateCacheFile python api fix
5d5da7aa5 tianbu.xsw reflector code
5707877a4 雁行 [MNN:Speed] Speedup for softmax in x86 and arm.
2a211825c tianbu.xsw reflector code for updateCacheFile
76db3a835 tianbu.xsw [Cache Feature]: Add updateCacheFile API for increment cache
b06b0fd43 allen.lk [Debug:Sparse] Fix and warning one kind of segmentfault cause by memory corruption when resize ConvolutionWinograd. Avoid to use some registers as arm restriction.
e68bfa495 雁行 [Converter:Feature] Add UUID when model convert.
a9cb935dc xiaying [MNN:Speed] Support c4nhwc for more fastblit
019f40353 xiaying [Converter:Refractor] Reduce memory used by MNNConvert(bert from 5G -> 1G)
d2a6d3d05 xiaying [MNN:Bugfix] Fix bug for identity output not find
604d0801b xiaying [Converter:Bugfix] Fix bug for FuseGeLu
4bada2367 xiaying [MNN:Refractor] SegmentMean rewrite as segment
82070e708 xiaying [MNN:Bugfix] Fix bug for GeometryBinary
e8ea4266e xiaying Fix bug for ShapeTensorConvert compute for dim = 1 error
1f1cf1991 xiaying [Tools:Bugfix] Fix system compability for fastTestOnnx
6f422efe2 xiaying [Tools:Bugfix] Remove color for checkDir for easy to dump
968f7ec88 xiaying [MNN:Speed] Support turn broadcast binary to loop
3e7aaf46f xiaying [MNN:Refractor] Set Convolution1x1Strassen support variable input/output ptr
1f65ab163 xiaying [MNN:Bugfix] Fix bug for mini mnn can't convert model
d65953d47 xiaying [MNN:Bugfix] Fix bug for armv7a - android-14 + ARM82
8b68be45c xiaying [MNN:Feature] Add segment
8a8f264f5 xiaying [Vulkan:Bugfix] Remove unuseful print
025bb0fda xiaying [Converter:Bugfix] Fix bug for oneof don't support
43900251e tianbu.xsw enable setCacheFile python API
ebfb05c74 tianbu.xsw [Metal Feature] support metallib obtain from walle transfer task
9665c0a79 弗人 add check for path in json file
c66fef224 xiaying [Converter:Bugfix] Fix bug for oneof don't support
42f192852 xiaying [MNN:Bugfix] Fix bug for not set output / saveTensor into origin Schedule's outputs
1b95354ff 雁行 [Feature]: Support shape compute for SetDiff1D, and null input for Prod.
83966d043 xiaying [Test:Feature] Add test for static module
42d1be933 xiaying [Converter:Bugfix] Fix bug for mnn convert and static model add more outputs for origin model
9067531c3 xiaying [Converter:Refractor] formatLicence
99558bed9 xiaying [Converter:Bugfix] Count the op for unuseful and controlflow
4f6da0fa7 allen.lk [Feature:GRUMultiOutput] fix multi output dimension type
c6b219bce xiaying [Converter:Feature] Turn torch converter to object
dd4e68a37 xiaying [Converter:Feature] Support dump supported ops
80b6a60a3 xiaying [Converter:Info] If has output name, print output name instead of computed
015278fc3 xiaying [MNN:Refractor] Revert IfModule's debug info
23ac967c4 xiaying Don't transform for multi-input convolution/deconvolution
b02b0d4de xiaying Fix bug for multi-input for conv1d
254d8b1d4 xiaying Fix bug for Conv1dSqueezeMove for multi input convolution 1d
d47d0b9ca xiaying Fix bug for CPURaster's fuse nc4hw4
357c5bd33 xiaying Fix ConvBiasAdd for conv's inputs op > 1
55b1f0c9c xiaying [Converter:Bugfix] Don't transform for multi-input convolution/deconvolution
1902a30f5 xiaying [Converter:Bugfix] Fix bug for Conv1dSqueezeMove for multi input convolution 1d
c23fe617b xiaying [MNN:Bugfix] Fix bug for multi-input for conv1d
8ff018426 xiaying [MNN:Bugfix] Fix bug for CPURaster's fuse nc4hw4
d4e8cd602 xiaying [Converter:Bugfix] Fix ConvBiasAdd for conv's inputs op > 1
846266b42 tianbu.xsw return when program and tune both nullptr
fd67c76a9 xiaying [Converter:Bugfix] DepthwiseConvWeightMerge only valid for tflite
e77a242c4 xiaying [Converter:Feature] Support tflite's half pixel
be054c377 tianbu.xsw [OpenCL Bugfix] do not rewrite cache when binary program is produced
51e65aa35 xiaying [Converter:Feature] Support tflite for fp16 and multi-input convolution
1ccdfdeb5 tianbu.xsw redefine svm macro name
31234d372 tianbu.xsw [OpenCL SVM] add macro for only use wrapper
d739e35da xiaying [MNN:Bugfix] Fix compile bug for grid op
24ab13c79 Joker feat(arm82): add GridSample op support in arm82 backend, AVX(by xiaying)
7b142978e xiaying [AVX512:Speed] Optimize for e <= 8
5f6febe7b tianbu.xsw code refactor
998d91b57 xiaying [Express:Speed] Merge submodule for speed
22c89146f tianhang.yth fix alpha div by zero bug and arm server compile bug
8f829a170 tianbu.xsw [OpenCL Pad] unify conv/deconv pad computing
4a28f603e xiaying [Express:Speed] Shared Const for All Submodule
c74cf28f3 xiaying [MNN:Refractor] Seperate Const init and schedule
2a1eebb7a xiaying [Tools:Bugfix] Fix bug for modelTest.py count size
72f04008c xiaying [MNN:Refractor] Delete unuseful const op
1e735d03c xiaying [Converter:Bugfix] Fix bug for static module gen
4dfadbc6e xiaying [MNN:Refractor] Rewrite const init mode
1fcf0417a xiaying [MNN:Bugfix] Fix bug for deconvolutin multi-input for multi-batch
41d429cfd xiaying [Train:Bugfix] Revert convert NCHW for mnistTrain
f947a5f01 xiaying [Test:Feature] Add testTrain
dad59b6f6 tianbu.xsw move realize code from Backend.hpp to Tensor.cpp
cf4473ad1 xiaying [Train:Bugfix] Support pad for GeometryPoolGrad
91ab13734 xiaying [MNN:Bugfix] Fix compile bug for avx512
742e80f47 xiaying [MNN:Refractor] Opt the logic for checknan judge
12543b841 xiaying [ARM82:Bugfix] Fix compile bug for ios
3a2b0a49f xiaying [ARM82:Speed] Opt Pack / Unpack for armv8
c0f1995cd xiaying [ARM82:Speed] Opt MNNPackC8FP16 and MNNUnpackC8FP16 by asm
e0fc77dcf xiaying [MNN:Speed] Fix bug for DeconvolutionWithStride for C4HW4, open it
584bec578 xiaying [MNN:Bugfix] Fix bug for format set error for onnx
d5bd4148d xiaying [MNN:Bugfix] Fix bug for format set error for onnx
b00265841 xiaying [MNN:Bugfix] Fix bug for SparseConvolutionTiledExecutor
bb09188ac xiaying [Test:Bugfix] Fix bug for run into sparse auto
426d1babd xiaying [MNN:Refractor] Small bugfix for Group convolution and pack
7d0ea1c46 tianbu.xsw [testModel Feature] support testModel.out input resize
4169c54ce xiaying [MNN:Bugfix] Fix bug for checkNAN for origin
412a82222 xiaying [Test:Bugfix] Fix bug for CheckNAN's error of matmul
319b1d425 xiaying [MNN:Bugfix] Fix bug for multi-batch for ConvInt8
050b728a6 xiaying [Test:Bugfix] Use NCHW for ConvInt8Test
7db3423a1 xiaying [OpenCL:Bugfix] Fix bug for opencl::image,opencl::buffer for C4HW4
adcec6a7f xiaying [Vulkan:Bugfix] Fix bug for invalid tensor size limit
d2a7cf4e9 xiaying [Vulkan:Bugfix] Fix bug for onCopyBuffer of nc4hw4
557bebdd3 xiaying [MNN:Bugfix] Fix bug for BF16-ARM32
bbe186649 tianbu.xsw [Update AUTO mode]: fix MNN_FORWARD_AUTO choose priority
6deb23439 xiaying [MNN:Bugfix] Fix bug for GeometryBinary don't care about NC4HW4 same size
b137590e4 xiaying [MNN:Bugfix] Fix bug for GeometryBinary don't care about NC4HW4 same size
7003558ea xiaying [Converter:Bugfix] Fix bug for onnx pad for serveral case
b5f8cae5a xiaying [Converter:Bugfix] Fix bug for onnx pad for serveral case
29b09e125 xiaying [MNN:Bugfix] Fix bug for arm64-bf16
42ce00770 xiaying [MNN:Bugfix] Fix bug for ARM64 - float
a2d89fc18 雁行 [Converter:Feature] Support Binary Unary for Torch.
7f1c0deb1 xiaying [MNN:Bugfix] Fix bug for Raster for Int8
8335a6f18 tianbu.xsw [OpenCL Shared Memory] modify data_format method
b359e031b xiaying [ARM82:Bugfix] Fix bug for arm82 and speed up pack / unpack c8
24bf3fc88 雁行 [Convert:Feature] Support LayerNormFuse without gamma beta.
3e629624b xiaying [MNN:Bugfix] Fix bug for float - armv7a
2b7908ec7 tianbu.xsw modify workItemSize
3cee0d413 xiaying [MNN:Bugfix] test wrong clear
9cbbfb998 xiaying [MNN:Bugfix] fix compile bug for c++ < 14
2d7a44484 xiaying [MNN:Bugfix] fix compile bug for c++ < 14
eb7d0cb53 xiaying [Test:Bugfix] Don't test for NC4HW4 directly
7b40ca8d1 xiaying [MNN:Bugfix] Fix bug for ConvolutionGroup
2694d8a91 xiaying [MNN:Bugfix] Fix bug for CPUGridSample
f89af60f6 xiaying [MNN:Bugfix] Fix compile bug for arm
a151abcdd xiaying [MNN:Bugfix] Fix bug for convert for int8 / int16
b254dbe61 雁行 [MNN:Bugfix] Bugfix for Conv onClone.
d08150631 xiaying [MNN:Bugfix] Fix bug for fast rcnn
e5568a0df xiaying [MNN:Bugfix] Fix bug for CPURaster treat NC4HW4 fast blit
128318933 雁行 [Raster:Bugfix] bugfix for Raster merge onResize.
03caacbea xiaying [MNN:Bugfix] fix bug for CPUDeconvolution and Convolution1x1Strassen for iw != ow
e1e3c245c xiaying [MNN:Bugfix] Fix bug for ConvolutionWinograd
2524cbc6d xiaying [MNN:Bugfix] Fix bug for CPUSoftmax
44ec79b8f xiaying [MNN:Bugfix] Fix bug for CPUConvolutionDepthwise / Scale / DeconvolutionDW
21ae956ce xiaying [MNN:Bugfix] Fix bug for Multi-Batch-TiledExecutor
09a5069c7 xiaying [MNN:Speed] Add offset for src and dst
6776c6784 xiaying [MNN:Bugfix] Fix bug for trainable model
cc83ae30b xiaying [MNN:Bugfix] Fix bug for trainable model
2021-07-29 11:46:59 +08:00
|
|
|
};
|
2025-01-22 14:47:50 +08:00
|
|
|
auto ocSplitFunction = [&](int threads) { // Thread split by OC
|
2025-05-08 12:39:44 +08:00
|
|
|
auto im2colDst = mTempIm2ColBuffer->host<int8_t>();
|
2024-12-02 10:12:08 +08:00
|
|
|
auto srcPtr = (int8_t const **)(mBlitInfo.ptr());
|
|
|
|
auto el = (int32_t *)(srcPtr + mBlitInfoStride.second);
|
2025-05-08 12:39:44 +08:00
|
|
|
auto xKernelSumPtr = reinterpret_cast<float*>(mTempSrcSum.ptr());
|
2024-11-18 14:37:45 +08:00
|
|
|
|
2025-05-08 12:39:44 +08:00
|
|
|
auto eU = UP_DIV(plane, DST_XUNIT);
|
2025-01-22 14:47:50 +08:00
|
|
|
int32_t info[5];
|
2024-12-02 10:12:08 +08:00
|
|
|
info[1] = mIm2ColParamter.iw * mIm2ColParamter.ih * batch;
|
2025-05-08 12:39:44 +08:00
|
|
|
info[2] = static_cast<int32_t>(unitColBufferSize);
|
2024-12-02 10:12:08 +08:00
|
|
|
info[3] = mIm2ColParamter.strideX;
|
|
|
|
|
|
|
|
float* reluPtr = mResourceInt8->mReluThreshold.data();
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mIm2ColBasedInt8) { // im2col
|
2025-01-22 14:47:50 +08:00
|
|
|
auto res = ConvolutionTiledExecutor::turnIm2ColToBlitInfo((const float**)srcPtr, el, 0, plane, mIm2ColParamter, (uint8_t*)im2colSrc, im2colBytes);
|
|
|
|
int number = res.first;
|
|
|
|
bool needZero = res.second;
|
|
|
|
if (needZero) {
|
2024-12-02 10:12:08 +08:00
|
|
|
#ifdef MNN_USE_SSE
|
2025-05-08 12:39:44 +08:00
|
|
|
::memset(im2colDst, inputZeroPoint + 128, mTempIm2ColBuffer->size());
|
2024-12-02 10:12:08 +08:00
|
|
|
#else
|
2025-05-08 12:39:44 +08:00
|
|
|
::memset(im2colDst, inputZeroPoint, mTempIm2ColBuffer->size());
|
2024-12-02 10:12:08 +08:00
|
|
|
#endif
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
info[0] = number;
|
|
|
|
info[4] = plane;
|
|
|
|
if (number > 0) {
|
2025-05-08 12:39:44 +08:00
|
|
|
blitProc(im2colDst, srcPtr, info, el);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
#ifdef MNN_LOW_MEMORY
|
|
|
|
if (false == mIm2ColBasedInt8) {
|
2025-01-22 14:47:50 +08:00
|
|
|
int realDstCount = plane;
|
|
|
|
int start = 0;
|
2025-02-12 11:14:19 +08:00
|
|
|
auto ptrInputscale = mBatchQuantInfo->host<uint8_t>();
|
2025-05-08 12:39:44 +08:00
|
|
|
auto ptrInputbias = ptrInputscale + mBatchQuantInfo->stride(0) / 2;
|
2025-01-22 14:47:50 +08:00
|
|
|
auto int8Ptr = mQuantInput->host<int8_t>();
|
|
|
|
int sizePacked = 0;
|
2025-05-08 12:39:44 +08:00
|
|
|
auto im2colDstTmp = im2colDst;
|
2025-01-22 14:47:50 +08:00
|
|
|
while (realDstCount > 0) {
|
|
|
|
int work = std::min(realDstCount, DST_XUNIT);
|
2025-05-08 12:39:44 +08:00
|
|
|
sizePacked += (work * SRC_UNIT * kernelCountUnit);
|
2025-01-22 14:47:50 +08:00
|
|
|
auto res = ConvolutionTiledExecutor::turnIm2ColToBlitInfo((const float**)srcPtr, el, start, work, mIm2ColParamter, (uint8_t*)im2colSrc, im2colBytes);
|
|
|
|
int number = res.first;
|
|
|
|
bool needZero = res.second;
|
|
|
|
if (needZero) {
|
2025-05-08 12:39:44 +08:00
|
|
|
::memset(im2colDstTmp, 0, unitColBufferSize * gcore->bytes);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
info[0] = number;
|
|
|
|
info[2] = work;
|
|
|
|
if (number > 0) { // im2col
|
2025-05-08 12:39:44 +08:00
|
|
|
gcore->MNNGeneralIm2Col((float*)im2colDstTmp, (float const**)srcPtr, info, el, SRC_UNIT, PackUnit); // im2colDst: [lu, realDstCount, lp]
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mUseBatchQuan || dynamicOption == 2) {
|
|
|
|
if (dynamicOption == 2) {
|
|
|
|
BatchAsyDynamicQuant((uint8_t*)im2colDstTmp, inputZeroPoint, ptrInputscale, kernelCountUnit, work, SRC_UNIT, 1, int8Ptr, ptrInputbias, 0);
|
|
|
|
ptrInputbias += (mInputBlockNum * work * sizeof(int32_t));
|
|
|
|
} else {
|
|
|
|
BatchSymDynamicQuant((uint8_t*)im2colDstTmp, inputZeroPoint, ptrInputscale, kernelCountUnit, work, SRC_UNIT, 1, int8Ptr, 0);
|
|
|
|
}
|
|
|
|
ptrInputscale += (mInputBlockNum * work * sizeof(int32_t));
|
|
|
|
int8Ptr += unitColBufferSize;
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
|
|
|
realDstCount -= work;
|
|
|
|
start += work;
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDstTmp += (unitColBufferSize * gcore->bytes);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (!mUseBatchQuan && dynamicOption != 2) {
|
|
|
|
BatchAsyDynamicQuant((uint8_t*)im2colDst, inputZeroPoint, ptrInputscale, kernelCountUnit, plane, SRC_UNIT, 1, mQuantInput->host<int8_t>(), ptrInputscale + plane * mInputBlockNum* QUANT_INFO_BYTES, 0);
|
2025-01-22 14:47:50 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDst = mQuantInput->host<int8_t>();
|
2024-12-02 10:12:08 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
if (mBlockNum > 1 && kxky > 1) {
|
2025-04-28 11:38:44 +08:00
|
|
|
for (int k = 0; k < eU; ++k) {
|
|
|
|
int inside = blocklu * SRC_UNIT * ALIMIN(DST_XUNIT, plane - k * DST_XUNIT);
|
2025-05-08 12:39:44 +08:00
|
|
|
auto dstbuffer = mReorderBuffer.ptr() + k * unitColBufferSize;
|
|
|
|
auto srcbuffer = im2colDst + k * unitColBufferSize;
|
2025-04-28 11:38:44 +08:00
|
|
|
for (int i = 0; i < mBlockNum; ++i) {
|
|
|
|
for (int j = 0; j < kxky; ++j) {
|
|
|
|
memcpy(dstbuffer + i * kxky * inside + j * inside, srcbuffer + i * inside + j * mBlockNum * inside, inside);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDst = (int8_t*)mReorderBuffer.ptr();
|
2025-04-28 11:38:44 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
#endif
|
2024-12-02 10:12:08 +08:00
|
|
|
if (mResourceInt8->mWeightAsymmetricQuant) {
|
2025-02-12 11:14:19 +08:00
|
|
|
MNN_ASSERT(mBatchQuantInfo.get() && mBatchQuantInfo->host<float>());
|
2025-05-08 12:39:44 +08:00
|
|
|
gcore->MNNSumByAxisLForMatmul_A(xKernelSumPtr, im2colDst, mBatchQuantInfo->host<float>(), plane, sumParams);
|
|
|
|
} else {
|
|
|
|
memset(xKernelSumPtr, 0, mTileCount * mBlockNum * DST_XUNIT * mIm2ColCount * QUANT_INFO_BYTES);
|
2024-12-02 10:12:08 +08:00
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-08-24 15:46:21 +08:00
|
|
|
MNN_CONCURRENCY_BEGIN(tId, threads) {
|
2024-07-22 19:51:53 +08:00
|
|
|
int ocIndex = PackUnit * mDivides[tId];
|
2024-12-02 10:12:08 +08:00
|
|
|
auto ocDivThread = ALIMIN(mDivides[tId + 1] - mDivides[tId], ocDiv4 - mDivides[tId]);
|
2025-05-08 12:39:44 +08:00
|
|
|
|
2024-10-22 15:11:21 +08:00
|
|
|
if (ocIndex < ocUp4) {
|
2025-05-08 12:39:44 +08:00
|
|
|
auto im2colDstThread = im2colDst;
|
|
|
|
float* ptrY = nullptr;
|
|
|
|
if (dstBytes != 1) {
|
|
|
|
ptrY = mResourceInt8->mWeightKernelSum->host<float>() + (ocIndex / UNIT) * UNIT * mInputBlockNum;
|
|
|
|
}
|
2024-12-02 10:12:08 +08:00
|
|
|
QuanPostTreatParameters quanParam;
|
|
|
|
quanParam.blockNum = mBlockNum;
|
2025-05-08 12:39:44 +08:00
|
|
|
quanParam.weightKernelSum = ptrY;
|
|
|
|
quanParam.biasFloat = reinterpret_cast<float*>(biasPtr + ocIndex * 4);
|
2024-12-02 10:12:08 +08:00
|
|
|
if (dstBytes != 1) {
|
|
|
|
quanParam.useInt8 = 0;
|
|
|
|
quanParam.fp32minmax = reluPtr;
|
|
|
|
} else {
|
|
|
|
quanParam.maxValue = mMutableResource->mClampMax;
|
|
|
|
if (mResourceInt8->mRelu) {
|
|
|
|
quanParam.minValue = mMutableResource->mOutputZeroPoint;
|
|
|
|
} else {
|
|
|
|
quanParam.minValue = mMutableResource->mClampMin;
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
uint8_t* inputScale = nullptr; // input scale for batch dynamic quant.
|
|
|
|
uint8_t* inputBias = nullptr;
|
|
|
|
float* accumbuff = nullptr;
|
|
|
|
if (mBatchQuantInfo.get()) {
|
|
|
|
inputScale = mBatchQuantInfo->host<uint8_t>();
|
|
|
|
if (dynamicOption == 2) {
|
|
|
|
inputBias = inputScale + mInputBlockNum * plane * QUANT_INFO_BYTES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mBlockNum > 1) {
|
|
|
|
accumbuff = reinterpret_cast<float*>(mAccumBuffer->host<int8_t>() + tId * mAccumBuffer->stride(0) * sizeof(int32_t));
|
|
|
|
}
|
|
|
|
|
2024-12-02 10:12:08 +08:00
|
|
|
auto outputInTilePtr = outputDataPtr + ocIndex * plane * dstBytes;
|
2025-05-08 12:39:44 +08:00
|
|
|
const auto weightPtrTid = weightDataPtr + static_cast<int32_t>(ocIndex * mBlockNum * blockL * SRC_UNIT * weightBytes + ocIndex * 2 * mBlockNum * QUANT_INFO_BYTES);
|
2024-12-02 10:12:08 +08:00
|
|
|
int realDstCount = plane;
|
2025-05-08 12:39:44 +08:00
|
|
|
auto ptrX = xKernelSumPtr;
|
2025-02-12 11:14:19 +08:00
|
|
|
do {
|
|
|
|
int step = ALIMIN(DST_XUNIT, realDstCount);
|
2025-05-08 12:39:44 +08:00
|
|
|
quanParam.inputScale = (float*)inputScale;
|
|
|
|
quanParam.inputBias = (float*)inputBias;
|
|
|
|
quanParam.srcKernelSum = ptrX;
|
2025-02-12 11:14:19 +08:00
|
|
|
if (mBlockNum > 1) {
|
2025-05-08 12:39:44 +08:00
|
|
|
memset(accumbuff, 0, UNIT * 4 * DST_XUNIT);
|
2025-02-12 11:14:19 +08:00
|
|
|
quanParam.accumBuffer = accumbuff;
|
|
|
|
}
|
2025-05-08 12:39:44 +08:00
|
|
|
mGemmKernel(outputInTilePtr, im2colDstThread, weightPtrTid, blockL, dstZStep * dstBytes, ocDivThread, &quanParam, step);
|
2025-02-12 11:14:19 +08:00
|
|
|
ptrX += (step * mBlockNum);
|
|
|
|
realDstCount-=step;
|
|
|
|
outputInTilePtr += DST_XUNIT * PackUnit * dstBytes;
|
2025-05-08 12:39:44 +08:00
|
|
|
im2colDstThread += unitColBufferSize;
|
|
|
|
inputScale = mUseBatchQuan ? (inputScale + mInputBlockNum * step * QUANT_INFO_BYTES) : inputScale;
|
|
|
|
inputBias = (inputBias != nullptr) ? (inputBias + mInputBlockNum * step * QUANT_INFO_BYTES) : inputBias;
|
2025-02-12 11:14:19 +08:00
|
|
|
} while(realDstCount > 0);
|
2024-08-24 15:46:21 +08:00
|
|
|
}
|
2024-07-22 19:51:53 +08:00
|
|
|
}
|
|
|
|
MNN_CONCURRENCY_END();
|
2024-12-02 10:12:08 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
const int threads = static_cast<CPUBackend*>(backend())->threadNumber();
|
|
|
|
if (!mSplitByOc) {
|
|
|
|
MNN_CONCURRENCY_BEGIN(tId, threads) {
|
2025-01-22 14:47:50 +08:00
|
|
|
if (mDivides[tId + 1] - mDivides[tId] > 0) {
|
|
|
|
tileSplitFunction((int)tId, mDivides[tId], mDivides[tId + 1], 1);
|
|
|
|
}
|
2024-12-02 10:12:08 +08:00
|
|
|
}
|
|
|
|
MNN_CONCURRENCY_END();
|
|
|
|
} else {
|
2025-01-22 14:47:50 +08:00
|
|
|
ocSplitFunction(threads);
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:52:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-31 15:34:08 +08:00
|
|
|
} // namespace MNN
|