MNN/source/shape/ShapeWhere.cpp

71 lines
2.1 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
//
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
template <typename T>
int _count(Tensor* t) {
const T* ptr = t->host<T>();
int count = 0;
for (int i = 0; i < t->elementSize(); i++) {
count += (ptr[i] > 0);
}
return count;
}
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();
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>();
2021-02-07 10:45:07 +08:00
auto param = op->main_as_Extra();
if (param == nullptr) {
// support old version
return true;
}
2022-02-18 11:30:27 +08:00
// For zeroshape input
if (nullptr == inputs[0]->host<void>()) {
2022-02-18 11:30:27 +08:00
ob.dimensions = 1;
ob.dim[0].extent = 0;
2020-07-04 01:21:30 +08:00
return true;
}
int count = 0;
if (ib.type == halide_type_of<float>()) {
count = _count<float>(inputs[0]);
} else if (ib.type == halide_type_of<int32_t>()) {
count = _count<int32_t>(inputs[0]);
} else if (ib.type == halide_type_of<uint8_t>()) {
count = _count<uint8_t>(inputs[0]);
} else {
return false;
2020-07-04 01:21:30 +08:00
}
if (count > 0) {
ob.dim[0].extent = count;
2022-02-18 11:30:27 +08:00
} else {
ob.dimensions = 1;
ob.dim[0].extent = 0;
2020-07-04 01:21:30 +08:00
}
2019-04-17 10:49:11 +08:00
return true;
}
};
REGISTER_SHAPE_INPUTS(WhereSizeComputer, OpType_Where, std::vector<int>{0});
2019-04-17 10:49:11 +08:00
} // namespace MNN