[enco] Introduce Session (#1697)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 1 Oct 2018 23:54:49 +0000 (08:54 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 1 Oct 2018 23:54:49 +0000 (08:54 +0900)
* [enco] Introduce Session

This commit introduces Session-related API in enco compiler framework.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Correct return value for make_session

contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Session.cpp [new file with mode: 0644]
contrib/enco/core/src/Session.h [new file with mode: 0644]
contrib/enco/core/src/Transforms/Split.cpp

index 645b048..8755b31 100644 (file)
@@ -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 (file)
index 0000000..f930faa
--- /dev/null
@@ -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 <nncc/foundation/Memory.h>
+
+#include <map>
+#include <memory>
+
+using nncc::foundation::make_unique;
+
+namespace
+{
+
+std::map<enco::SessionID, std::unique_ptr<enco::Code>> sess_to_code;
+std::map<const coco::Module *, enco::SessionID> module_to_sess;
+std::map<const coco::Data *, enco::SessionID> 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<Code>(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 (file)
index 0000000..b6d502f
--- /dev/null
@@ -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__
index bfdd0bd..a48959b 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "Split.h"
 #include "Usage.h"
+#include "Session.h"
 
 #include <coco/IR.h>
 
@@ -32,7 +33,8 @@ using Appender = std::function<void(ANNBinder *binder)>;
 class ANNOpBuilder : public coco::Instr::Visitor<Appender>
 {
 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<float>(unit->ifm());
       auto ker = _binder->addOperand<float>(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