2020-11-05 16:41:56 +08:00
|
|
|
//
|
|
|
|
// GeometryPermute.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2020/04/03.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2023-05-18 19:11:50 +08:00
|
|
|
#include <algorithm>
|
2020-11-05 16:41:56 +08:00
|
|
|
#include "geometry/GeometryComputer.hpp"
|
|
|
|
#include "core/TensorUtils.hpp"
|
|
|
|
namespace MNN {
|
|
|
|
class GeometryPermute : public GeometryComputer {
|
|
|
|
public:
|
|
|
|
virtual bool onCompute(const Op* op, const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
Context& context, CommandBuffer& res) const override {
|
|
|
|
auto input = inputs[0];
|
|
|
|
auto output = outputs[0];
|
|
|
|
auto inputDes = TensorUtils::getDescribe(input);
|
|
|
|
auto outputDes = TensorUtils::getDescribe(output);
|
|
|
|
auto inputSlice = inputDes->regions;
|
|
|
|
MNN_ASSERT(input->dimensions() >= 1);
|
|
|
|
MNN_ASSERT(output->dimensions() == input->dimensions());
|
|
|
|
auto originTensor = input;
|
2021-07-29 20:51:03 +08:00
|
|
|
int shape[MNN_MAX_TENSOR_DIM];
|
2020-11-05 16:41:56 +08:00
|
|
|
if (op->type() == OpType_Permute) {
|
|
|
|
auto shapeValue = op->main_as_Permute()->dims();
|
2022-12-30 15:18:58 +08:00
|
|
|
if (nullptr != shapeValue) {
|
|
|
|
for (int i = 0; i < input->buffer().dimensions; ++i) {
|
|
|
|
shape[i] = shapeValue->data()[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < input->buffer().dimensions; ++i) {
|
|
|
|
shape[i] = input->buffer().dimensions - i - 1;
|
|
|
|
}
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
|
|
|
} else if (op->type() == OpType_Transpose) {
|
|
|
|
auto shapeValue = inputs[1]->host<int32_t>();
|
2021-07-29 20:51:03 +08:00
|
|
|
for (int i = 0; i < input->buffer().dimensions; ++i) {
|
2020-11-05 16:41:56 +08:00
|
|
|
shape[i] = shapeValue[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MNN_ASSERT(false);
|
|
|
|
}
|
2021-07-29 20:51:03 +08:00
|
|
|
int inputShape[MNN_MAX_TENSOR_DIM];
|
|
|
|
int inputStrides[MNN_MAX_TENSOR_DIM];
|
|
|
|
int inputShapeSize = 0;
|
|
|
|
int preAxis = -2;
|
|
|
|
for (int i=0; i<input->buffer().dimensions; ++i) {
|
|
|
|
auto axis = shape[i];
|
|
|
|
auto len = input->length(axis);
|
|
|
|
if (1 == len) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (axis - preAxis == 1) {
|
2023-05-18 19:11:50 +08:00
|
|
|
// Fuse dimension if possible
|
2021-07-29 20:51:03 +08:00
|
|
|
inputShape[inputShapeSize - 1] *= len;
|
|
|
|
} else {
|
|
|
|
if (preAxis >= 0) {
|
|
|
|
// Compute last stride
|
|
|
|
int stride = 1;
|
|
|
|
for (int v=preAxis+1; v < input->buffer().dimensions; ++v) {
|
|
|
|
stride *= input->length(v);
|
|
|
|
}
|
|
|
|
inputStrides[inputShapeSize - 1] = stride;
|
|
|
|
}
|
|
|
|
inputShapeSize+=1;
|
|
|
|
inputShape[inputShapeSize - 1] = len;
|
|
|
|
}
|
|
|
|
preAxis = shape[i];
|
|
|
|
}
|
|
|
|
if (preAxis >= 0) {
|
|
|
|
// Compute last stride
|
2020-11-05 16:41:56 +08:00
|
|
|
int stride = 1;
|
2021-07-29 20:51:03 +08:00
|
|
|
for (int v=preAxis+1; v < input->buffer().dimensions; ++v) {
|
|
|
|
stride *= input->length(v);
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
2021-07-29 20:51:03 +08:00
|
|
|
inputStrides[inputShapeSize - 1] = stride;
|
|
|
|
}
|
|
|
|
if (0 == inputShapeSize) {
|
|
|
|
outputDes->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL;
|
|
|
|
outputDes->regions = {TensorUtils::makeFullSlice(input)};
|
|
|
|
return true;
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
2021-07-29 20:51:03 +08:00
|
|
|
int outputStrides[MNN_MAX_TENSOR_DIM];
|
|
|
|
{
|
|
|
|
int stride = 1;
|
|
|
|
for (int i=inputShapeSize-1; i>=0; --i) {
|
|
|
|
outputStrides[i] = stride;
|
|
|
|
stride *= inputShape[i];
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 19:11:50 +08:00
|
|
|
// Sort inputShapeSize from small to large
|
|
|
|
if (inputShapeSize > 3) {
|
|
|
|
for (int i=0; i<inputShapeSize; ++i) {
|
|
|
|
for (int j=i+1; j<inputShapeSize; ++j) {
|
|
|
|
if (inputShape[i] > inputShape[j]) {
|
|
|
|
std::swap(inputShape[i], inputShape[j]);
|
|
|
|
std::swap(inputStrides[i], inputStrides[j]);
|
|
|
|
std::swap(outputStrides[i], outputStrides[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 16:41:56 +08:00
|
|
|
// Compute inside, outside, axis
|
|
|
|
int inside = 1;
|
|
|
|
int insideStride = 0;
|
|
|
|
int outside = 1;
|
|
|
|
int outsideStride = 0;
|
|
|
|
int axis = 1;
|
|
|
|
int axisStride = 0;
|
|
|
|
int breakAxis = -1;
|
|
|
|
int remainSize = 1;
|
2023-05-18 19:11:50 +08:00
|
|
|
int outputInsideStride = 0;
|
|
|
|
int outputAxisStride = 0;
|
|
|
|
int outputOutsideStride = 0;
|
2020-11-05 16:41:56 +08:00
|
|
|
{
|
2021-07-29 20:51:03 +08:00
|
|
|
if (inputShapeSize >= 1) {
|
|
|
|
inside = inputShape[inputShapeSize-1];
|
|
|
|
insideStride = inputStrides[inputShapeSize-1];
|
2023-05-18 19:11:50 +08:00
|
|
|
outputInsideStride = outputStrides[inputShapeSize-1];
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
2021-07-29 20:51:03 +08:00
|
|
|
if (inputShapeSize >= 2) {
|
|
|
|
axis = inputShape[inputShapeSize-2];
|
|
|
|
axisStride = inputStrides[inputShapeSize-2];
|
2023-05-18 19:11:50 +08:00
|
|
|
outputAxisStride = outputStrides[inputShapeSize-2];
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
2021-07-29 20:51:03 +08:00
|
|
|
if (inputShapeSize >= 3) {
|
|
|
|
outside = inputShape[inputShapeSize-3];
|
|
|
|
outsideStride = inputStrides[inputShapeSize-3];
|
2023-05-18 19:11:50 +08:00
|
|
|
outputOutsideStride = outputStrides[inputShapeSize-3];
|
2021-07-29 20:51:03 +08:00
|
|
|
breakAxis = inputShapeSize - 3;
|
|
|
|
for (int i = 0; i < inputShapeSize - 3; ++i) {
|
|
|
|
remainSize *= inputShape[i];
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputDes->regions.resize(remainSize);
|
|
|
|
outputDes->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL;
|
2021-11-30 10:10:53 +08:00
|
|
|
int32_t mod[MNN_MAX_TENSOR_DIM];
|
2020-11-05 16:41:56 +08:00
|
|
|
for (int i = 0; i < breakAxis; ++i) {
|
|
|
|
int value = 1;
|
|
|
|
for (int j = i + 1; j < breakAxis; ++j) {
|
2021-07-29 20:51:03 +08:00
|
|
|
value *= inputShape[j];
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
|
|
|
mod[i] = value;
|
|
|
|
}
|
|
|
|
for (int indice = 0; indice < remainSize; ++indice) {
|
|
|
|
int value = indice;
|
|
|
|
int inputOffset = 0;
|
2023-05-18 19:11:50 +08:00
|
|
|
int outputOffset = 0;
|
2020-11-05 16:41:56 +08:00
|
|
|
for (int i = 0; i < breakAxis; ++i) {
|
|
|
|
auto coordinate = value / mod[i];
|
2021-07-29 20:51:03 +08:00
|
|
|
inputOffset += coordinate * inputStrides[i];
|
2023-05-18 19:11:50 +08:00
|
|
|
outputOffset += coordinate * outputStrides[i];
|
2020-11-05 16:41:56 +08:00
|
|
|
value = value % mod[i];
|
|
|
|
}
|
|
|
|
Tensor::InsideDescribe::Region& slice = outputDes->regions[indice];
|
2023-05-18 19:11:50 +08:00
|
|
|
slice.src.offset = inputOffset;
|
|
|
|
slice.src.stride[0] = outsideStride;
|
2020-11-05 16:41:56 +08:00
|
|
|
slice.size[0] = outside;
|
2023-05-18 19:11:50 +08:00
|
|
|
slice.src.stride[1] = axisStride;
|
2020-11-05 16:41:56 +08:00
|
|
|
slice.size[1] = axis;
|
2023-05-18 19:11:50 +08:00
|
|
|
slice.src.stride[2] = insideStride;
|
2020-11-05 16:41:56 +08:00
|
|
|
slice.size[2] = inside;
|
|
|
|
slice.origin = originTensor;
|
2023-05-18 19:11:50 +08:00
|
|
|
slice.dst.offset = outputOffset;
|
|
|
|
slice.dst.stride[0] = outputOutsideStride;
|
|
|
|
slice.dst.stride[1] = outputAxisStride;
|
|
|
|
slice.dst.stride[2] = outputInsideStride;
|
2020-11-05 16:41:56 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static void _create() {
|
|
|
|
std::shared_ptr<GeometryComputer> comp(new GeometryPermute);
|
|
|
|
GeometryComputer::registerGeometryComputer(comp, {OpType_Transpose, OpType_Permute});
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_GEOMETRY(GeometryPermute, _create);
|
|
|
|
}; // namespace MNN
|