MNN/source/shape/ShapeConcat.cpp

66 lines
2.3 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// ShapeConcat.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 ConcatSizeComputer : public SizeComputer {
virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs,
const std::vector<Tensor*>& outputs) const override {
MNN_ASSERT(1 == outputs.size());
2024-12-19 16:20:00 +08:00
// MNN_ASSERT(inputs.size() >= 2);
auto& ob = outputs[0]->buffer();
int basicAxis = 0;
if (op->type() == OpType_Concat) {
basicAxis = op->main_as_Axis()->axis();
} else if (op->type() == OpType_QuantizedConcat) {
basicAxis = op->main_as_QuantizedConcat()->axis();
}
int axis = basicAxis;
2019-04-17 10:49:11 +08:00
// Concat-inputs may have scalar which should be delete
for (const auto& input : inputs) {
2020-11-05 16:41:56 +08:00
auto inputDimensions = input->buffer().dimensions;
2022-06-10 10:39:50 +08:00
// Tensor might be zeros size, but some dims may not be zero. should concat as usual.
2020-11-05 16:41:56 +08:00
::memcpy(ob.dim, input->buffer().dim, sizeof(halide_dimension_t) * inputDimensions);
ob.dimensions = inputDimensions;
ob.type = input->buffer().type;
if (axis < 0) {
axis = inputDimensions + axis;
2019-04-17 10:49:11 +08:00
}
2020-11-05 16:41:56 +08:00
break;
2019-04-17 10:49:11 +08:00
}
2022-06-10 10:39:50 +08:00
2019-04-17 10:49:11 +08:00
int sum = 0;
for (auto t : inputs) {
sum += t->buffer().dim[axis].extent;
2019-12-27 22:16:57 +08:00
ob.type = t->buffer().type;
2019-04-17 10:49:11 +08:00
for (int i = 0; i < t->dimensions(); ++i) {
if (axis == i) {
continue;
}
if (t->length(i) != outputs[0]->length(i)) {
2019-12-27 22:16:57 +08:00
auto name = op->name() ? op->name()->c_str() : "";
MNN_PRINT("Error for concat size of op [ %s ], the %d input not match output\n", name, i);
2019-04-17 10:49:11 +08:00
return false;
}
}
}
ob.dim[axis].extent = sum;
TensorUtils::getDescribe(outputs[0])->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat;
2019-04-17 10:49:11 +08:00
return true;
}
};
REGISTER_SHAPE(ConcatSizeComputer, OpType_Concat);
REGISTER_SHAPE(ConcatSizeComputer, OpType_QuantizedConcat);
2019-04-17 10:49:11 +08:00
} // namespace MNN