From 7561c6c753bd8bbf20667928e5a493fbb4f6dccc 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: Tue, 28 Aug 2018 16:22:26 +0900 Subject: [PATCH] [enco] Generate Subnet declaration (#1203) This commit implements SubnetStructBuilder which generates a helper struct for accessing android NN sub-network. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/CppCode.cpp | 77 +++++++++++++++++++++++++++++++++ contrib/enco/core/src/CppGen/Subnet.cpp | 56 ++++++++++++++++++++++++ contrib/enco/core/src/CppGen/Subnet.h | 41 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 contrib/enco/core/src/CppGen/Subnet.cpp create mode 100644 contrib/enco/core/src/CppGen/Subnet.h diff --git a/contrib/enco/core/src/CppCode.cpp b/contrib/enco/core/src/CppCode.cpp index dd38763..3cbe8c9 100644 --- a/contrib/enco/core/src/CppCode.cpp +++ b/contrib/enco/core/src/CppCode.cpp @@ -4,18 +4,25 @@ #include "CppGen/MemoryContext.h" #include "CppGen/Host.h" +#include "CppGen/Subnet.h" #include "Dims.h" #include #include +#include #include #include namespace { +struct SubnetInfo +{ + std::string struct_name; +}; + struct InvokeFunction { pp::LinearDocument body; @@ -32,6 +39,70 @@ void CppCode::dump(std::ostream &os) const Global global; InvokeFunction invoke; + pp::LinearDocument internal; + + // Record the subnet information + std::map subnet_ctx; + + /** + * Create a struct for each android NN network of the following form: + * + * struct [Name] + * { + * ... + * + * [Name]() // constructor + * { + * ... + * } + * + * ~[Name]() // destructor + * { + * ... + * } + * }; + * + */ + for (uint32_t n = 0; n < _code->ann()->count(); ++n) + { + const SubnetStructBuilder builder{&global}; + + auto subnet_binder = _code->ann()->nth(n); + auto subnet_struct_name = pp::fmt("Subnet_", subnet_ctx.size()); + auto subnet_struct_content = builder.build(subnet_binder); + + // Emit C++ declaration + internal.append("struct ", subnet_struct_name); + internal.append("{"); + internal.indent(); + + internal.append(subnet_struct_content->def()); + + internal.append(subnet_struct_name, "()"); + internal.append("{"); + internal.indent(); + internal.append(subnet_struct_content->ctor()); + internal.unindent(); + internal.append("}"); + + internal.append("~", subnet_struct_name, "()"); + internal.append("{"); + internal.indent(); + internal.append(subnet_struct_content->dtor()); + internal.unindent(); + internal.append("}"); + + internal.unindent(); + internal.append("};"); + + // Update subnet context + SubnetInfo subnet_info; + + subnet_info.struct_name = subnet_struct_name; + + assert(subnet_ctx.find(subnet_binder) == subnet_ctx.end()); + subnet_ctx[subnet_binder] = subnet_info; + } MemoryContext mem; @@ -171,6 +242,12 @@ void CppCode::dump(std::ostream &os) const source.append(); source.append(global.content()); source.append(); + + source.append("namespace"); + source.append("{"); + source.append(internal); + source.append("} // namespace"); + source.append(); source.append(net_def); source.append(); source.append(net_ctor); diff --git a/contrib/enco/core/src/CppGen/Subnet.cpp b/contrib/enco/core/src/CppGen/Subnet.cpp new file mode 100644 index 0000000..327b2bc --- /dev/null +++ b/contrib/enco/core/src/CppGen/Subnet.cpp @@ -0,0 +1,56 @@ +#include "CppGen/Subnet.h" + +#include + +#include + +using nncc::foundation::make_unique; + +#define S(content) #content + +namespace +{ + +class SubnetStructImpl final : public enco::SubnetStruct +{ +public: + SubnetStructImpl() : _dtor{pp::LinearDocument::Direction::Reverse} + { + // DO NOTHING + } + +public: + const pp::MultiLineText &def(void) const override { return _def; } + pp::LinearDocument *def(void) { return &_def; } + +public: + const pp::MultiLineText &ctor(void) const override { return _ctor; } + pp::LinearDocument *ctor(void) { return &_ctor; } + +public: + const pp::MultiLineText &dtor(void) const override { return _dtor; } + pp::LinearDocument *dtor(void) { return &_dtor; } + +private: + pp::LinearDocument _def; + pp::LinearDocument _ctor; + pp::LinearDocument _dtor; +}; + +} // namespace + +namespace enco +{ + +std::unique_ptr SubnetStructBuilder::build(const ANNBinder *binder) const +{ + auto res = make_unique(); + + // TODO Fill constructor and destructor + res->ctor()->append(S(assert("NYI"))); + res->dtor()->append(S(assert("NYI"))); + + return std::move(res); +} + +} // namespace enco diff --git a/contrib/enco/core/src/CppGen/Subnet.h b/contrib/enco/core/src/CppGen/Subnet.h new file mode 100644 index 0000000..36d66db --- /dev/null +++ b/contrib/enco/core/src/CppGen/Subnet.h @@ -0,0 +1,41 @@ +#ifndef __ENCO_CPP_GEN_SUBNET_H__ +#define __ENCO_CPP_GEN_SUBNET_H__ + +#include "ANN/Binder.h" +#include "CppGen/Global.h" + +#include + +namespace enco +{ + +/** + * @brief A C++ struct that provides Android NN model & compilation + */ +struct SubnetStruct +{ + virtual ~SubnetStruct() = default; + + virtual const pp::MultiLineText &def(void) const = 0; + virtual const pp::MultiLineText &ctor(void) const = 0; + virtual const pp::MultiLineText &dtor(void) const = 0; +}; + +class SubnetStructBuilder +{ +public: + SubnetStructBuilder(enco::Global *global) : _global{global} + { + // DO NOTHING + } + +public: + std::unique_ptr build(const ANNBinder *binder) const; + +private: + enco::Global *_global; +}; + +} // namespace enco + +#endif // __ENCO_CPP_GEN_SUBNET_H__ -- 2.7.4