MNN/source/backend/cpu/CPUOneHot.cpp

73 lines
2.3 KiB
C++
Raw Normal View History

2019-12-27 22:16:57 +08:00
//
// CPUOneHot.cpp
// MNN
//
// Created by MNN on 2019/11/29.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "backend/cpu/CPUOneHot.hpp"
#include "backend/cpu/CPUBackend.hpp"
namespace MNN {
template <typename T>
void OneHotImpl(int depth, int outerSize, int innerSize, const int* indices, const Tensor* onValueTensor,
const Tensor* offValueTensor, Tensor* outputTensor) {
const T onValue = onValueTensor->host<T>()[0];
const T offValue = offValueTensor->host<T>()[0];
T* outputPtr = outputTensor->host<T>();
for (int i = 0; i < outerSize; ++i) {
for (int j = 0; j < depth; ++j) {
for (int k = 0; k < innerSize; ++k) {
*outputPtr = indices[i * innerSize + k] == j ? onValue : offValue;
outputPtr++;
}
}
}
}
ErrorCode CPUOneHot::onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
auto indices = inputs[0];
auto depthTensor = inputs[1];
auto onValueTensor = inputs[2];
auto offValueTensor = inputs[3];
if (mAxis == -1) {
mAxis = indices->dimensions();
}
int outerSize = 1;
for (int i = 0; i < mAxis; ++i) {
outerSize *= indices->length(i);
}
const int depth = depthTensor->host<int>()[0];
const int innerSize = indices->elementSize() / outerSize;
const auto indicesPtr = indices->host<int>();
auto dataType = onValueTensor->getType();
auto offDataType = offValueTensor->getType();
MNN_ASSERT(dataType == offDataType);
if (dataType == halide_type_of<float>()) {
OneHotImpl<float>(depth, outerSize, innerSize, indicesPtr, onValueTensor, offValueTensor, outputs[0]);
} else if (dataType == halide_type_of<int>()) {
OneHotImpl<int>(depth, outerSize, innerSize, indicesPtr, onValueTensor, offValueTensor, outputs[0]);
} else {
return NOT_SUPPORT;
}
return NO_ERROR;
}
class CPUOneHotCreator : public CPUBackend::Creator {
public:
virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
const MNN::Op* op, Backend* backend) const override {
return new CPUOneHot(backend, op->main_as_OneHotParam()->axis());
}
};
REGISTER_CPU_OP_CREATOR(CPUOneHotCreator, OpType_OneHot);
} // namespace MNN