[enco/tfl/frontend] Function to copy tensor value into coco::Data (#2511)
author윤현식/동작제어Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Fri, 7 Dec 2018 01:48:21 +0000 (10:48 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 7 Dec 2018 01:48:21 +0000 (10:48 +0900)
* [enco/tfl/frontend] Function to copy unfilled tensor value into coco::Data

This commit adds a function to copy unfilled tensor value into coco::Data.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* pr fix: added if with continue, assert, int32 message

contrib/enco/frontend/tflite/src/Frontend.cpp
contrib/enco/frontend/tflite/src/TensorBags.h

index 5fe345d..c64f181 100644 (file)
@@ -23,6 +23,8 @@
 #include <nncc/core/ADT/tensor/LexicalLayout.h>
 #include <nncc/core/ADT/tensor/Shape.h>
 
+#include <iostream>
+
 using namespace nncc::core::ADT;
 
 namespace tflimport
@@ -76,6 +78,48 @@ void set_module_outputs(coco::Module *m, TensorContext &ctx, TensorBags &bags,
   }
 }
 
+/**
+ * @brief Copy values of tfl tensors into coco::Data if the data was not copied
+ */
+void copy_tensors(GraphBuilderContext *ctx)
+{
+  auto d = ctx->d();
+
+  // for each bag, check if bag is not allocated but tflite tensor has values
+  for (auto &iter : ctx->bags())
+  {
+    auto tfl_tensor_id = iter.first;
+    auto bag = iter.second;
+
+    auto tfl_buffer = ctx->buffer().tensor_buffer<float>(ctx->graph(), tfl_tensor_id);
+
+    // TODO remove this line when support int32 is ready
+    if (ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_INT32)
+    {
+      std::cout << "*** INT32 COPYING IS NOT SUPPORTED ***" << std::endl;
+      continue;
+    }
+
+    assert(ctx->tensor().type(tfl_tensor_id) == tflite::TensorType::TensorType_FLOAT32);
+
+    auto span = d->f32()->weight(bag); // TODO support other type
+
+    if (!(span.data() == nullptr && span.size() == 0)) // already allocated
+      continue;
+
+    if (tfl_buffer.ptr == nullptr || tfl_buffer.len == 0) // no data to copy
+      continue;
+
+    d->f32()->allocate(bag);
+
+    auto ifm_span = d->f32()->weight(bag);
+    for (uint32_t idx = 0; idx < tfl_buffer.len; ++idx)
+    {
+      ifm_span[idx] = tfl_buffer.ptr[idx];
+    }
+  }
+}
+
 } // namespace tflimport
 
 Frontend::Frontend(std::unique_ptr<RawModel> &&raw) : _raw{std::move(raw)}
@@ -139,6 +183,9 @@ enco::Bundle Frontend::load(void) const
       std::string opcodename = opcode_context.opcode_name(op);
       throw std::runtime_error{"Not supported: " + opcodename};
     }
+
+    // copying unfilled tensor value
+    copy_tensors(&opbuilder_context);
   }
 
   // Create "Bundle"
index 84bec0b..29558b8 100644 (file)
@@ -51,6 +51,11 @@ public:
 
   coco::Bag *bag(int32_t tensor_id) { return _bag_ctx[tensor_id]; }
 
+public:
+  std::map<uint32_t, coco::Bag *>::iterator begin() { return _bag_ctx.begin(); }
+
+  std::map<uint32_t, coco::Bag *>::iterator end() { return _bag_ctx.end(); }
+
 private:
   std::map<uint32_t, coco::Bag *> _bag_ctx;
 };