MNN/source/backend/cpu/compute/ConvolutionTiledExecutor.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// ConvolutionTiledExecutor.cpp
// MNN
//
// Created by MNN on 2018/07/16.
// Copyright © 2018, Alibaba Group Holding Limited
//
2020-11-05 16:41:56 +08:00
#include "ConvolutionTiledExecutor.hpp"
2019-12-27 22:16:57 +08:00
#include <MNN/AutoTime.hpp>
#include "backend/cpu/CPUBackend.hpp"
2020-11-05 16:41:56 +08:00
#include "CommonOptFunction.h"
2019-12-27 22:16:57 +08:00
#include "core/Concurrency.h"
2020-11-05 16:41:56 +08:00
#include "ConvOpt.h"
2019-12-27 22:16:57 +08:00
#include "core/Macro.h"
#include "core/TensorUtils.hpp"
2020-11-05 16:41:56 +08:00
#include "math/Vec.hpp"
2021-04-08 15:34:23 +08:00
#include "core/BufferAllocator.hpp"
#include "core/MemoryFormater.h"
2019-04-17 10:49:11 +08:00
2020-11-05 16:41:56 +08:00
using Vec4 = MNN::Math::Vec<float, 4>;
2019-04-17 10:49:11 +08:00
namespace MNN {
void ConvolutionTiledExecutor::initWeight(const float *source, float* cache, int depth, int outputCount, int kernelSize, const CoreFunctions* function) {
2020-07-04 01:21:30 +08:00
// Swap k, ic
2020-11-05 16:41:56 +08:00
int dims[4] = {
depth,
kernelSize,
kernelSize,
depth
};
2020-07-04 01:21:30 +08:00
for (int o=0; o<outputCount; ++o) {
auto dO = cache + o * depth * kernelSize;
2020-07-04 01:21:30 +08:00
auto sO = source + o * depth * kernelSize;
2020-11-05 16:41:56 +08:00
MNNTranspose32Bit((int32_t*)dO, (const int32_t*)sO, &dims[0]);
2020-07-04 01:21:30 +08:00
}
2021-04-08 15:34:23 +08:00
if (function->bytes < 4) {
// Lowp
function->MNNFp32ToLowp((float*)cache, (int16_t*)cache, outputCount * kernelSize * depth);
}
2020-07-04 01:21:30 +08:00
}
ConvolutionTiledExecutor::ConvolutionTiledExecutor(Backend* b, const float* bias, size_t biasSize)
: MNN::Execution(b) {
2021-01-06 16:29:37 +08:00
mResource.reset(new CPUConvolution::Resource);
mResource->backend = b;
2021-04-08 15:34:23 +08:00
mValid = mResource->copyBiasAlign(bias, biasSize);
2019-04-17 10:49:11 +08:00
if (!mValid) {
return;
}
}
ConvolutionTiledExecutor::ConvolutionTiledExecutor(std::shared_ptr<CPUConvolution::Resource> res, Backend* b) : mResource(res), Execution(b) {
}
2019-04-17 10:49:11 +08:00
ConvolutionTiledExecutor::~ConvolutionTiledExecutor() {
2021-01-06 16:29:37 +08:00
// Do nothing
}
bool ConvolutionTiledExecutor::onClone(Backend* bn, const Op* op, Execution** dst) {
if (!mValid) {
return false;
2019-04-17 10:49:11 +08:00
}
2021-01-06 16:29:37 +08:00
if (nullptr == dst) {
return true;
2019-04-17 10:49:11 +08:00
}
*dst = new ConvolutionTiledExecutor(mResource, bn);
2021-01-06 16:29:37 +08:00
return true;
2019-04-17 10:49:11 +08:00
}
2021-01-06 16:29:37 +08:00
ErrorCode ConvolutionTiledImpl::onResize(const std::vector<Tensor*>& inputs,
const std::vector<Tensor*>& outputs) {
2019-04-17 10:49:11 +08:00
return NO_ERROR;
}
ErrorCode ConvolutionTiledImpl::onExecute(const std::vector<Tensor*>& inputs,
const std::vector<Tensor*>& outputs) {
2020-07-23 10:35:12 +08:00
MNN_CONCURRENCY_BEGIN(tId, mFunction.first) {
mFunction.second((int)tId);
2019-04-17 10:49:11 +08:00
}
2020-07-23 10:35:12 +08:00
MNN_CONCURRENCY_END();
2019-04-17 10:49:11 +08:00
return NO_ERROR;
}
2019-04-17 10:49:11 +08:00
} // namespace MNN