From e66da6afc59678a4a0e6bfd5a1dd694e45618933 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 30 Aug 2018 10:52:04 +0900 Subject: [PATCH] [enco] Emit addOperation calls (#1240) With this commit, C++ code generated by enco includes addOperation calls. Note that the current implementation supports only CONV_2D operation. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/CppGen/Subnet.cpp | 70 ++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/contrib/enco/core/src/CppGen/Subnet.cpp b/contrib/enco/core/src/CppGen/Subnet.cpp index 46fa56c..876b78e 100644 --- a/contrib/enco/core/src/CppGen/Subnet.cpp +++ b/contrib/enco/core/src/CppGen/Subnet.cpp @@ -12,6 +12,15 @@ using nncc::foundation::make_unique; #define S(content) #content +namespace ann +{ +static std::ostream &operator<<(std::ostream &os, const ann::OperandID &id) +{ + os << id.value(); + return os; +} +} // namespace ann + namespace { template void concat(std::ostream &os, const std::string &sep, It beg, It end) @@ -211,6 +220,58 @@ private: std::string _size; }; +/** + * @brief Code fragment that calls ANeuralNetworksModel_addOperation + */ +class OperationDecl final : public CodeFragment +{ +public: + OperationDecl(const std::string &model, const ann::Operation *op) : _model{model}, _op{op} + { + // DO NOTHING + } + +private: + static std::string opcode(const ann::Operation::Code &code) + { + switch (code) + { + case ann::Operation::Code::CONV_2D: + return "ANEURALNETWORKS_CONV_2D"; + default: + throw std::invalid_argument{"code"}; + }; + } + +public: + void dump(pp::LinearDocument *doc) const override + { + const auto in_count = _op->inputs().size(); + auto in_beg = _op->inputs().begin(); + auto in_end = _op->inputs().end(); + + const auto out_count = _op->outputs().size(); + auto out_beg = _op->outputs().begin(); + auto out_end = _op->outputs().end(); + + auto op = opcode(_op->code()); + + doc->append("{"); + doc->indent(); + doc->append("uint32_t inputs[", in_count, "] = { ", concat(", ", in_beg, in_end), " };"); + doc->append("uint32_t outputs[", out_count, "] = { ", concat(", ", out_beg, out_end), " };"); + doc->append(); + doc->append("ANeuralNetworksModel_addOperation(", _model, ", ", op, ", ", in_count, + ", inputs, ", out_count, ", outputs);"); + doc->unindent(); + doc->append("}"); + } + +private: + std::string _model; + const ann::Operation *_op; +}; + } // namespace namespace enco @@ -255,7 +316,14 @@ std::unique_ptr SubnetStructBuilder::build(const ANNBinder *binder } }); - // TODO Invoke addOperation + for (auto n = 0; n < binder->module()->operation()->count(); ++n) + { + auto op = binder->module()->operation()->at(n); + res->ctor() << OperationDecl{mname, op}; + } + + // TODO Emit ANeuralNetworksModel_identifyInputsAndOutputs call + // TODO Emit ANeuralNetworksModel_finish call res->ctor()->append(S(assert("NYI");)); // Create compilation -- 2.7.4