MNN/source/shape/ShapeWhere.cpp

47 lines
1.5 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// ShapeWhere.cpp
// MNN
//
// Created by MNN on 2019/01/10.
// Copyright © 2018, Alibaba Group Holding Limited
//
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 WhereSizeComputer : public SizeComputer {
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& ib = inputs[0]->buffer();
auto& ob = outputs[0]->buffer();
MNN_ASSERT(ib.type.code == halide_type_int);
ob.dimensions = 2;
ob.dim[0].extent = inputs[0]->elementSize();
ob.dim[1].extent = ib.dimensions;
TensorUtils::getDescribe(outputs[0])->dimensionFormat = TensorUtils::getDescribe(inputs[0])->dimensionFormat;
2019-07-04 19:38:23 +08:00
outputs[0]->buffer().type = halide_type_of<int32_t>();
2020-07-04 01:21:30 +08:00
const int32_t* inputData = inputs[0]->host<int32_t>();
// For compability
if (nullptr == inputData) {
return true;
}
std::vector<int32_t> trueVec;
for (int i = 0; i < ob.dim[0].extent; i++) {
if (inputData[i] > 0) {
trueVec.push_back(i);
}
}
if (trueVec.size() > 0) {
ob.dim[0].extent = (int)trueVec.size();
}
2019-04-17 10:49:11 +08:00
return true;
}
};
REGISTER_SHAPE_INPUTS(WhereSizeComputer, OpType_Where, {0});
2019-04-17 10:49:11 +08:00
} // namespace MNN