From 6417a1055f84e6d7175232f0fd9b18e640cf9be2 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: Wed, 29 Aug 2018 17:29:11 +0900 Subject: [PATCH] [enco] Emit setOperandValue calls (#1233) Generated C++ code will include setOperand (ANeuralNetworksModel_setOperandValue) calls with this commit. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/CppGen/Global.cpp | 25 +++++++++++++++++++++ contrib/enco/core/src/CppGen/Global.h | 3 +++ contrib/enco/core/src/CppGen/Subnet.cpp | 39 ++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/contrib/enco/core/src/CppGen/Global.cpp b/contrib/enco/core/src/CppGen/Global.cpp index b991eab..5385899 100644 --- a/contrib/enco/core/src/CppGen/Global.cpp +++ b/contrib/enco/core/src/CppGen/Global.cpp @@ -36,4 +36,29 @@ template <> std::string Global::constant(const std::vector &values) return name; } +std::string Global::constant(const uint8_t *base, uint32_t size) +{ + auto name = pp::fmt("g_", _count++); + + std::stringstream ss; + + ss << "const uint8_t " << name << "[" << size << "] = { "; + + if (size > 0) + { + ss << "0x" << std::hex << (base[0] + 0); + + for (uint32_t n = 1; n < size; ++n) + { + ss << ", 0x" << std::hex << (base[n] + 0); + } + } + + ss << " };"; + + _content.append(ss.str()); + + return name; +} + } // namespace enco diff --git a/contrib/enco/core/src/CppGen/Global.h b/contrib/enco/core/src/CppGen/Global.h index 5dbdf52..6161b06 100644 --- a/contrib/enco/core/src/CppGen/Global.h +++ b/contrib/enco/core/src/CppGen/Global.h @@ -21,6 +21,9 @@ public: // @brief Create a global constant array variable of type T template std::string constant(const std::vector &values); + // @brief Create a global constant array variable of byte (uint8_t) type + std::string constant(const uint8_t *base, uint32_t size); + public: const pp::MultiLineText &content(void) const { return _content; } diff --git a/contrib/enco/core/src/CppGen/Subnet.cpp b/contrib/enco/core/src/CppGen/Subnet.cpp index c3629f1..46fa56c 100644 --- a/contrib/enco/core/src/CppGen/Subnet.cpp +++ b/contrib/enco/core/src/CppGen/Subnet.cpp @@ -184,6 +184,33 @@ private: nncc::core::ADT::tensor::Shape _shape; }; +/** + * @brief Code fragment that calls ANeuralNetworksModel_setOperandValue + */ +class WeightDecl final : public CodeFragment +{ +public: + WeightDecl(const std::string &model, const ann::OperandID &id, const std::string &base, + const std::string &size) + : _model{model}, _id{id}, _base{base}, _size{size} + { + // DO NOTHING + } + +public: + void dump(pp::LinearDocument *doc) const override + { + doc->append("ANeuralNetworksModel_setOperandValue(", _model, ", ", _id.value(), ", ", _base, + ", ", _size, ");"); + } + +private: + std::string _model; + ann::OperandID _id; + std::string _base; + std::string _size; +}; + } // namespace namespace enco @@ -215,7 +242,17 @@ std::unique_ptr SubnetStructBuilder::build(const ANNBinder *binder res->ctor() << TensorOperandDecl{mname, tensor->dtype(), tensor->shape()}; } - // TODO Invoke setOperand + if (info->weight()) + { + auto base = info->weight()->base(); + auto size = info->weight()->size(); + auto gvar = _global->constant(base, size); + + auto base_exp = pp::fmt("reinterpret_cast(&", gvar, ")"); + auto size_exp = pp::fmt(size); + + res->ctor() << WeightDecl{mname, id, base_exp, size_exp}; + } }); // TODO Invoke addOperation -- 2.7.4