forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Converter:Bugfix] Support Onnx::TopK for dynamic shape
- Loading branch information
xiaying
committed
Dec 10, 2021
1 parent
a14ef5e
commit b3c5fee
Showing
7 changed files
with
156 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// GeometryTopK.cpp | ||
// MNN | ||
// | ||
// Created by MNN on 2020/06/09. | ||
// Copyright © 2018, Alibaba Group Holding Limited | ||
// | ||
|
||
#include <numeric> | ||
#include "geometry/GeometryComputer.hpp" | ||
#include "geometry/GeometryComputerUtils.hpp" | ||
#include "core/OpCommonUtils.hpp" | ||
namespace MNN { | ||
class GeometryTopK : 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 { | ||
if (outputs.size() != 2 || inputs.size() < 2 || inputs.size() > 3) { | ||
MNN_ERROR("TopK should have 2 output and 2~3 input, get %lu in and %lu out\n", inputs.size(), outputs.size()); | ||
return false; | ||
} | ||
if (inputs.size() == 2) { | ||
SharedPtr<Command> cmdP(new Command); | ||
auto& cmd = *cmdP; | ||
cmd.op = op; | ||
cmd.inputs = std::move(inputs); | ||
cmd.outputs = std::move(outputs); | ||
res.command.emplace_back(std::move(cmdP)); | ||
return true; | ||
} | ||
if (inputs[1]->host<int32_t>() == nullptr || inputs[2]->host<int32_t>() == nullptr) { | ||
MNN_ERROR("Invalid k or axis\n"); | ||
return false; | ||
} | ||
int k = inputs[1]->host<int32_t>()[0], axis = inputs[2]->host<int32_t>()[0]; | ||
auto shape = inputs[0]->shape(); | ||
int outside = std::accumulate(shape.begin(), shape.begin() + axis, 1, [](int a, int b) { return a * b; }); | ||
int inside = std::accumulate(shape.begin() + axis + 1, shape.end(), 1, [](int a, int b) { return a * b; }); | ||
if (axis < 0) { | ||
axis = axis + shape.size(); | ||
} | ||
std::shared_ptr<Tensor> transInput, transVal, transInd; | ||
{ // transpose TopK's axis to last axis | ||
transInput.reset(Tensor::createDevice({outside * inside, shape[axis]}, inputs[0]->getType(), inputs[0]->getDimensionType())); | ||
auto des = TensorUtils::getDescribe(transInput.get()); | ||
des->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL; | ||
Tensor::InsideDescribe::Region reg; | ||
reg.origin = inputs[0]; | ||
reg.src.stride[0] = reg.dst.stride[0] = inside * shape[axis]; | ||
reg.src.stride[2] = inside; | ||
reg.dst.stride[1] = shape[axis]; | ||
reg.size[0] = outside; | ||
reg.size[1] = inside; | ||
reg.size[2] = shape[axis]; | ||
des->regions.assign({reg}); | ||
res.extras.emplace_back(transInput); | ||
} | ||
{ // transpose TopK's axis from last axis | ||
transVal.reset(Tensor::createDevice({outside * inside, k}, outputs[0]->getType(), outputs[0]->getDimensionType())); | ||
transInd.reset(Tensor::createDevice({outside * inside, k}, outputs[1]->getType(), outputs[1]->getDimensionType())); | ||
Tensor::InsideDescribe::Region reg; | ||
reg.src.stride[0] = reg.dst.stride[0] = inside * k; | ||
reg.src.stride[2] = k; | ||
reg.dst.stride[1] = inside; | ||
reg.size[0] = outside; | ||
reg.size[1] = k; | ||
reg.size[2] = inside; | ||
auto des = TensorUtils::getDescribe(outputs[0]); | ||
des->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL; | ||
reg.origin = transVal.get(); | ||
des->regions.assign({reg}); | ||
res.extras.emplace_back(transVal); | ||
des = TensorUtils::getDescribe(outputs[1]); | ||
des->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL; | ||
reg.origin = transInd.get(); | ||
des->regions.assign({reg}); | ||
res.extras.emplace_back(transInd); | ||
} | ||
{ // do TopK on last axis | ||
SharedPtr<Command> cmdP(new Command); | ||
auto& cmd = *cmdP; | ||
cmd.op = op; | ||
cmd.inputs.assign({transInput.get(), inputs[1]}); | ||
cmd.outputs.assign({transVal.get(), transInd.get()}); | ||
res.command.emplace_back(std::move(cmdP)); | ||
} | ||
return true; | ||
} | ||
}; | ||
static void _create() { | ||
std::shared_ptr<GeometryComputer> comp(new GeometryTopK); | ||
GeometryComputer::registerGeometryComputer(comp, {OpType_TopKV2}); | ||
} | ||
|
||
REGISTER_GEOMETRY(GeometryTopK, _create); | ||
|
||
} // namespace MNN |
Oops, something went wrong.