2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// ShapeExpandDims.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/01/10.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
#include "shape/SizeComputer.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
class ExpandDimsComputer : public SizeComputer {
|
|
|
|
public:
|
|
|
|
virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs,
|
|
|
|
const std::vector<Tensor*>& outputs) const override {
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
const int inputSize = (int)inputs.size();
|
2019-09-01 19:25:26 +08:00
|
|
|
MNN_ASSERT(2 == inputSize || 1 == inputSize);
|
2019-04-17 10:49:11 +08:00
|
|
|
MNN_ASSERT(1 == outputs.size());
|
|
|
|
|
|
|
|
auto input = inputs[0];
|
|
|
|
auto output = outputs[0];
|
2019-09-01 19:25:26 +08:00
|
|
|
|
|
|
|
// default -1
|
|
|
|
int dim = -1;
|
|
|
|
if (inputSize == 2) {
|
|
|
|
// read dim from the second input
|
|
|
|
auto dims = inputs[1];
|
|
|
|
dim = dims->host<int32_t>()[0];
|
|
|
|
} else {
|
|
|
|
// get dim from expand_dims parameter(axis)
|
|
|
|
auto param = op->main_as_ExpandDims();
|
|
|
|
dim = param->axis();
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:36:47 +08:00
|
|
|
if (dim < 0) {
|
2019-04-17 10:49:11 +08:00
|
|
|
dim = input->dimensions() + 1 + dim;
|
|
|
|
}
|
2020-11-12 10:26:35 +08:00
|
|
|
output->buffer().type = input->buffer().type;
|
|
|
|
int outputShapeDims = 0;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < input->buffer().dimensions; i++) {
|
|
|
|
if (i == dim) {
|
2020-11-12 10:26:35 +08:00
|
|
|
output->buffer().dim[outputShapeDims++].extent = 1;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
2020-11-12 10:26:35 +08:00
|
|
|
output->buffer().dim[outputShapeDims++].extent = input->buffer().dim[i].extent;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
if (dim == input->buffer().dimensions) {
|
2020-11-12 10:26:35 +08:00
|
|
|
output->buffer().dim[outputShapeDims++].extent = 1;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
2020-11-12 10:26:35 +08:00
|
|
|
output->buffer().dimensions = outputShapeDims;
|
2019-08-22 20:13:46 +08:00
|
|
|
TensorUtils::getDescribe(output)->dimensionFormat = TensorUtils::getDescribe(input)->dimensionFormat;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-22 20:13:46 +08:00
|
|
|
REGISTER_SHAPE_INPUTS(ExpandDimsComputer, OpType_ExpandDims, {1});
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
} // namespace MNN
|