2021-12-07 10:57:55 +08:00
|
|
|
// Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
|
|
|
|
#include "archive/value_archive.h"
|
2022-06-01 14:10:43 +08:00
|
|
|
#include "core/graph.h"
|
2021-12-07 10:57:55 +08:00
|
|
|
#include "core/operator.h"
|
|
|
|
|
|
|
|
namespace mmdeploy::graph {
|
|
|
|
|
2022-06-01 14:10:43 +08:00
|
|
|
class FlattenNode : public Node {
|
|
|
|
public:
|
|
|
|
Sender<Value> Process(Sender<Value> input) override {
|
|
|
|
return Then(std::move(input), [&](Value args) {
|
|
|
|
Value rets = Value::kArray;
|
|
|
|
std::vector<int> idxs;
|
|
|
|
idxs.reserve(inputs().size());
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
auto [ret, idx] = Flatten(arg).value();
|
|
|
|
if (idxs.empty()) {
|
|
|
|
idxs = std::move(idx);
|
|
|
|
} else if (idx != idxs) {
|
|
|
|
MMDEPLOY_ERROR("args does not have same structure");
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
rets.push_back(std::move(ret));
|
2021-12-07 10:57:55 +08:00
|
|
|
}
|
2022-06-01 14:10:43 +08:00
|
|
|
rets.push_back(to_value(idxs));
|
|
|
|
return rets;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2021-12-07 10:57:55 +08:00
|
|
|
|
|
|
|
class FlattenCreator : public Creator<Node> {
|
|
|
|
public:
|
|
|
|
const char* GetName() const override { return "Flatten"; }
|
|
|
|
int GetVersion() const override { return 0; }
|
2022-06-01 14:10:43 +08:00
|
|
|
std::unique_ptr<Node> Create(const Value& config) override {
|
|
|
|
auto inst = std::make_unique<FlattenNode>();
|
|
|
|
NodeParser::Parse(config, *inst).value();
|
|
|
|
return inst;
|
2021-12-07 10:57:55 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_MODULE(Node, FlattenCreator);
|
|
|
|
|
|
|
|
} // namespace mmdeploy::graph
|