2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// ShapePool.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/01/10.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
|
|
|
#include "core/SizeComputer.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
class PoolSizeComputer : public SizeComputer {
|
|
|
|
public:
|
|
|
|
virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs,
|
|
|
|
const std::vector<Tensor*>& outputs) const override {
|
|
|
|
MNN_ASSERT(1 == inputs.size());
|
|
|
|
MNN_ASSERT(1 == outputs.size());
|
|
|
|
|
|
|
|
auto input = inputs[0];
|
|
|
|
auto output = outputs[0];
|
|
|
|
|
|
|
|
::memcpy(output->buffer().dim, input->buffer().dim, input->buffer().dimensions * sizeof(halide_dimension_t));
|
2020-02-26 17:44:28 +08:00
|
|
|
output->buffer().dimensions = input->dimensions();
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
auto layer = op->main_as_Pool();
|
|
|
|
int outw = 1;
|
|
|
|
int outh = 1;
|
|
|
|
if (!layer->isGlobal()) {
|
2019-06-24 11:32:41 +08:00
|
|
|
// when given explicit pad value in tensorflow mode pool, size compute will fast failed to help find problem
|
|
|
|
if ((layer->padType() == PoolPadType_VALID || layer->padType() == PoolPadType_SAME) && (layer->padX() != 0 || layer->padY() != 0)) {
|
|
|
|
MNN_PRINT("tensorflow mode pool should not have explict pad value\n");
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
int w = input->width();
|
|
|
|
int h = input->height();
|
2020-02-26 09:57:17 +08:00
|
|
|
if (nullptr != layer->pads()) {
|
|
|
|
w = w + layer->pads()->data()[1] + layer->pads()->data()[3];
|
|
|
|
h = h + layer->pads()->data()[0] + layer->pads()->data()[2];
|
|
|
|
} else {
|
2019-04-17 10:49:11 +08:00
|
|
|
w += layer->padX() * 2;
|
|
|
|
h += layer->padY() * 2;
|
2020-02-26 09:57:17 +08:00
|
|
|
}
|
|
|
|
int kernelWidth = std::min(layer->kernelX(), input->width());
|
|
|
|
int kernelHeight = std::min(layer->kernelY(), input->height());
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
if (layer->padType() == PoolPadType_SAME) { // Tensorflow padding mode SAME
|
|
|
|
outw = ceil((float)w / (float)layer->strideX());
|
|
|
|
outh = ceil((float)h / (float)layer->strideY());
|
|
|
|
} else if (layer->padType() == PoolPadType_VALID) { // Tensorflow padding mode VALID
|
2020-02-26 09:57:17 +08:00
|
|
|
outw = ceil((float)(w - kernelWidth + 1) / (float)layer->strideX());
|
|
|
|
outh = ceil((float)(h - kernelHeight + 1) / (float)layer->strideY());
|
2019-04-17 10:49:11 +08:00
|
|
|
} else {
|
2019-06-05 10:45:59 +08:00
|
|
|
if (layer->ceilModel()) {
|
2020-02-26 09:57:17 +08:00
|
|
|
outw = UP_DIV(w - kernelWidth, layer->strideX()) + 1;
|
|
|
|
outh = UP_DIV(h - kernelHeight, layer->strideY()) + 1;
|
2019-06-05 10:45:59 +08:00
|
|
|
} else {
|
2020-02-26 09:57:17 +08:00
|
|
|
outw = floor((w - kernelWidth) / layer->strideX() + 1);
|
|
|
|
outh = floor((h - kernelHeight) / layer->strideY() + 1);
|
2019-06-05 10:45:59 +08:00
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outw <= 0 || outh <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-26 09:57:17 +08:00
|
|
|
if (TensorUtils::getDescribe(inputs[0])->dimensionFormat != MNN_DATA_FORMAT_NC4HW4) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
output->buffer().dim[3].extent = outw;
|
|
|
|
output->buffer().dim[2].extent = outh;
|
2019-08-22 20:13:46 +08:00
|
|
|
TensorUtils::getDescribe(outputs[0])->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat;
|
2019-07-11 13:56:52 +08:00
|
|
|
output->buffer().type = input->buffer().type;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual float onComputeFlops(const MNN::Op* op, const std::vector<Tensor*>& inputs,
|
|
|
|
const std::vector<Tensor*>& outputs) const override {
|
|
|
|
auto size = (float)outputs[0]->elementSize() / 1024.0f / 1024.0f;
|
|
|
|
auto layer = op->main_as_Pool();
|
|
|
|
return size * layer->kernelX() * layer->kernelY();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_SHAPE(PoolSizeComputer, OpType_Pooling);
|
2019-07-11 13:56:52 +08:00
|
|
|
REGISTER_SHAPE(PoolSizeComputer, OpType_PoolInt8);
|
2019-04-17 10:49:11 +08:00
|
|
|
} // namespace MNN
|