2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// ShapeWhere.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 {
|
2020-11-05 16:41:56 +08:00
|
|
|
#define MNN_WHERE_OLD_VERSION
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
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;
|
2019-08-22 20:13:46 +08:00
|
|
|
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-11-05 16:41:56 +08:00
|
|
|
#ifdef MNN_WHERE_OLD_VERSION
|
|
|
|
return true;
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
REGISTER_SHAPE(WhereSizeComputer, OpType_Where);
|
2019-04-17 10:49:11 +08:00
|
|
|
} // namespace MNN
|