MNN/source/backend/cpu/CPUConvolution.cpp

88 lines
2.8 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// CPUConvolution.cpp
// MNN
//
// Created by MNN on 2018/07/15.
// Copyright © 2018, Alibaba Group Holding Limited
//
2019-12-27 22:16:57 +08:00
#include "backend/cpu/CPUConvolution.hpp"
2019-04-17 10:49:11 +08:00
#include <math.h>
2019-12-27 22:16:57 +08:00
#include "backend/cpu/compute/CommonOptFunction.h"
#include "core/Macro.h"
2020-07-04 01:21:30 +08:00
#include <limits>
2019-12-27 22:16:57 +08:00
#include "backend/cpu/compute/ConvolutionFloatFactory.h"
//#define MNN_OPEN_TIME_TRACE
2019-12-27 22:16:57 +08:00
#include <MNN/AutoTime.hpp>
#include "core/ConvolutionCommon.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
CPUConvolution::CPUConvolution(const Convolution2DCommon *convOp, Backend *b) : MNN::Execution(b), mCommon(convOp) {
mPostFunction = getPostFunction();
}
2020-07-04 01:21:30 +08:00
std::vector<float> CPUConvolution::getPostParameters() const {
std::vector<float> postParameters = {
1.0f,
1.0f,
-std::numeric_limits<float>().max(),
std::numeric_limits<float>().max(),
};
if (mCommon->relu()) {
postParameters[2] = 0.0f;
}
if (mCommon->relu6()) {
postParameters[2] = 0.0f;
postParameters[3] = 6.0f;
}
return postParameters;
}
2019-04-17 10:49:11 +08:00
int CPUConvolution::reorderWeightSize(int depth, int outputCount, int kernelSize, int unit) {
int unit2 = unit * unit;
return UP_DIV(outputCount, unit) * UP_DIV(depth, unit) * kernelSize * unit2;
}
void CPUConvolution::reorderWeight(float *dest, const float *source, int depth, int outputCount, int kernelSize,
float *cache) {
auto alignDepth = ALIGN_UP4(depth);
2019-04-17 10:49:11 +08:00
for (int b = 0; b < outputCount; ++b) {
auto dst = cache + b * alignDepth * kernelSize;
auto src = source + b * depth * kernelSize;
MNNPackC4(dst, src, kernelSize, depth);
2019-04-17 10:49:11 +08:00
}
MNNPackC4(dest, cache, kernelSize * ALIGN_UP4(depth), outputCount);
2020-04-10 14:44:01 +08:00
auto count = UP_DIV(depth, 4) * kernelSize * UP_DIV(outputCount, 4);
MNNReorder4x4ByPlatform(dest, count);
2019-04-17 10:49:11 +08:00
}
ErrorCode CPUConvolution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
auto input = inputs[0];
auto output = outputs[0];
auto pad = ConvolutionCommon::convolutionPad(input, output, mCommon);
mPadY = pad.second;
mPadX = pad.first;
2019-04-17 10:49:11 +08:00
return NO_ERROR;
}
CPUConvolution::POSTFUNCTION CPUConvolution::getPostFunction() const {
if (mCommon->relu()) {
return MNNAddBiasRelu;
}
if (mCommon->relu6()) {
return MNNAddBiasRelu6;
}
return MNNAddBias;
}
class ConvolutionFactory : public CPUBackend::Creator {
public:
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
return ConvolutionFloatFactory::create(inputs, outputs, op, backend);
}
};
REGISTER_CPU_OP_CREATOR(ConvolutionFactory, OpType_Convolution);
} // namespace MNN