[locomotiv] Session frees node annotations on destruction (#3441)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Mon, 13 May 2019 07:46:07 +0000 (16:46 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 13 May 2019 07:46:07 +0000 (16:46 +0900)
* [locomotiv] Session frees node annotations on destruction

This commit introduces destructor of Session. Previously node
annotations set by Session, like NodeDataImpl, remain even after
Session closed. They were not memory leak by definition, as they can
be accessed through another Session. However node annotations maybe
preserved until program ends, so it is like memory leak. This commit
would prevent that situation.

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
* Delete unused using Index

* Session is final class

contrib/locomotiv/include/locomotiv/Session.h
contrib/locomotiv/src/Session.cpp
contrib/locomotiv/src/Session.internal.test.cpp [new file with mode: 0644]

index 6bca285..4fb913d 100644 (file)
@@ -29,7 +29,7 @@ namespace locomotiv
 /**
  * @brief Session for loco graph inference
  */
-class Session
+class Session final
 {
 public:
   Session() = delete;
@@ -38,6 +38,9 @@ public:
     // DO NOTHING
   }
 
+  /// @brief Free all node annotations of the graph assigned by this Session
+  ~Session();
+
   /**
    * @brief Set graph input at specific index by NodeData.
    *
index 22ba44c..8bccbda 100644 (file)
 namespace locomotiv
 {
 
+Session::~Session()
+{
+  for (uint32_t i = 0; i < _graph->nodes()->size(); ++i)
+  {
+    auto node = _graph->nodes()->at(i);
+    erase_annot_data(node);
+  }
+}
+
 bool Session::set_input(uint32_t index, std::unique_ptr<NodeData> &&data)
 {
   // Check whether already annotated
diff --git a/contrib/locomotiv/src/Session.internal.test.cpp b/contrib/locomotiv/src/Session.internal.test.cpp
new file mode 100644 (file)
index 0000000..ed7d08d
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2019 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 "locomotiv/Session.h"
+#include "locomotiv/NodeData.h"
+
+// This flie is internal test because it includes this local header
+#include "NodeDataImpl.h"
+
+#include <loco.h>
+#include <nncc/core/ADT/tensor/Shape.h>
+#include <nncc/core/ADT/tensor/Buffer.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::LexicalLayout;
+using nncc::core::ADT::tensor::make_buffer;
+
+TEST(Session, dtor)
+{
+  auto g = loco::make_graph();
+
+  // Pull node
+  auto pull = g->nodes()->create<loco::Pull>();
+  pull->dtype(loco::DataType::FLOAT32);
+  pull->rank(1);
+  pull->dim(0) = loco::make_dimension(1);
+
+  // Input
+  auto input = g->inputs()->create();
+  input->node(pull);
+
+  {
+    locomotiv::Session s(g.get());
+
+    auto buf = make_buffer<float, LexicalLayout>(Shape{1});
+    auto data = locomotiv::make_data(buf);
+
+    s.set_input(0, std::move(data));
+
+    auto data_annotated = locomotiv::annot_data(input->node());
+    ASSERT_NE(data_annotated, nullptr);
+  }
+
+  auto data_annotated = locomotiv::annot_data(input->node());
+  ASSERT_EQ(data_annotated, nullptr);
+}