From cf8ed1a7bf818c546ab2c3f866d4acbe0daa847f 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, 2 Oct 2018 08:54:49 +0900 Subject: [PATCH] [enco] Introduce Session (#1697) * [enco] Introduce Session This commit introduces Session-related API in enco compiler framework. Signed-off-by: Jonghyun Park * Correct return value for make_session --- contrib/enco/core/src/Backend.cpp | 4 +++ contrib/enco/core/src/Session.cpp | 58 ++++++++++++++++++++++++++++++ contrib/enco/core/src/Session.h | 45 +++++++++++++++++++++++ contrib/enco/core/src/Transforms/Split.cpp | 15 ++++---- 4 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 contrib/enco/core/src/Session.cpp create mode 100644 contrib/enco/core/src/Session.h diff --git a/contrib/enco/core/src/Backend.cpp b/contrib/enco/core/src/Backend.cpp index 645b048..8755b31 100644 --- a/contrib/enco/core/src/Backend.cpp +++ b/contrib/enco/core/src/Backend.cpp @@ -16,6 +16,8 @@ #include "enco/Backend.h" +#include "Session.h" + #include "Code.h" #include "CppCode.h" @@ -76,6 +78,8 @@ private: void BackendImpl::compile(coco::Module *m, coco::Data *d) { + make_session(m, d); + Code code{m, d}; // As explained below, the current implementation does not work if there is a pair of input/ouput diff --git a/contrib/enco/core/src/Session.cpp b/contrib/enco/core/src/Session.cpp new file mode 100644 index 0000000..f930faa --- /dev/null +++ b/contrib/enco/core/src/Session.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Session.h" + +#include + +#include +#include + +using nncc::foundation::make_unique; + +namespace +{ + +std::map> sess_to_code; +std::map module_to_sess; +std::map data_to_sess; + +} // namespace + +namespace enco +{ + +SessionID make_session(coco::Module *m, coco::Data *d) +{ + static uint32_t sess = 0; + SessionID curr{sess++}; + + sess_to_code[curr] = make_unique(m, d); + module_to_sess[m] = curr; + data_to_sess[d] = curr; + + return curr; +} + +SessionID session(const coco::Module *m) { return module_to_sess.at(m); } +SessionID session(const coco::Data *d) { return data_to_sess.at(d); } + +coco::Module *module(const SessionID &sess) { return sess_to_code.at(sess)->module(); } +coco::Data *data(const SessionID &sess) { return sess_to_code.at(sess)->data(); } + +Code *code(const SessionID &sess) { return sess_to_code.at(sess).get(); } + +} // namespace enco diff --git a/contrib/enco/core/src/Session.h b/contrib/enco/core/src/Session.h new file mode 100644 index 0000000..b6d502f --- /dev/null +++ b/contrib/enco/core/src/Session.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ENCO_SESSION_H__ +#define __ENCO_SESSION_H__ + +#include "Code.h" + +namespace enco +{ + +// TODO Rewrite this definition +using SessionID = uint32_t; + +SessionID make_session(coco::Module *m, coco::Data *d); + +SessionID session(const coco::Module *m); +SessionID session(const coco::Data *d); + +coco::Module *module(const SessionID &); +coco::Data *data(const SessionID &); + +static inline coco::Module *module(const coco::Data *d) { return module(session(d)); } +static inline coco::Data *data(const coco::Module *m) { return data(session(m)); } + +// WARN This API is introduced just for backward compatibility +// Do NOT use this anymore as it will be removed +Code *code(const SessionID &); + +} // namespace enco + +#endif // __ENCO_SESSION_H__ diff --git a/contrib/enco/core/src/Transforms/Split.cpp b/contrib/enco/core/src/Transforms/Split.cpp index bfdd0bd..a48959b 100644 --- a/contrib/enco/core/src/Transforms/Split.cpp +++ b/contrib/enco/core/src/Transforms/Split.cpp @@ -16,6 +16,7 @@ #include "Split.h" #include "Usage.h" +#include "Session.h" #include @@ -32,7 +33,8 @@ using Appender = std::function; class ANNOpBuilder : public coco::Instr::Visitor { public: - ANNOpBuilder(coco::Data *data) : _data{data} + // TODO Revise this constructor + ANNOpBuilder(coco::Data *data) { // DO NOTHING } @@ -77,10 +79,10 @@ public: private: Appender conv2d(const coco::UnitF *unit) const { - auto _data = this->_data; assert(unit->op()->asConv2D()); // TODO Rename "_binder" - return [unit, _data](ANNBinder *_binder) { + return [unit](ANNBinder *_binder) { + auto _data = enco::data(unit->module()); auto conv = unit->op()->asConv2D(); auto ifm = _binder->addOperand(unit->ifm()); auto ker = _binder->addOperand(conv->ker()); @@ -159,10 +161,10 @@ private: { using namespace nncc::core::ADT; - auto data = this->_data; assert(unit->op()->asConv2D()); - return [unit, data](ANNBinder *binder) { + return [unit](ANNBinder *binder) { + auto data = enco::data(unit->module()); auto conv = unit->op()->asConv2D(); assert(unit->ifm()->shape().depth() == conv->group()); @@ -396,9 +398,6 @@ private: }; } - -private: - coco::Data *const _data; }; namespace -- 2.7.4