[enco] Emit Bag's initial data as global data (#1940)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 23 Oct 2018 01:57:39 +0000 (10:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 23 Oct 2018 01:57:39 +0000 (10:57 +0900)
With this commit, global data that global data generation pass emits
includes bag's weight values (in addition to weight values for ANN
operands).

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/Transforms/GlobalDataGeneration.cpp
contrib/enco/core/src/Transforms/GlobalDataGeneration.h

index f0c4dc8..f45b63a 100644 (file)
@@ -83,6 +83,8 @@ namespace
 {
 
 std::map<const ann::Operand *, enco::GlobalOffset> data_offset_ctx;
+std::map<const coco::Bag *, enco::GlobalOffset> bag_data_offset_ctx;
+
 std::map<const coco::Arg *, enco::GlobalOffset> name_offset_ctx;
 std::map<const coco::Arg *, enco::GlobalOffset> dims_offset_ctx;
 
@@ -93,6 +95,12 @@ namespace enco
 
 GlobalOffset GlobalData::data_offset(const ann::Operand *o) { return data_offset_ctx.at(o); }
 
+GlobalOffset GlobalData::data_offset(const coco::Bag *bag)
+{
+  assert(bag_data_offset_ctx.find(bag) != bag_data_offset_ctx.end());
+  return bag_data_offset_ctx.at(bag);
+}
+
 GlobalOffset GlobalData::name_offset(const coco::Input *in) { return name_offset_ctx.at(in); }
 GlobalOffset GlobalData::dims_offset(const coco::Input *in) { return dims_offset_ctx.at(in); }
 
@@ -102,10 +110,39 @@ GlobalOffset GlobalData::dims_offset(const coco::Output *out) { return dims_offs
 void generate_global_data(std::ostream &os, enco::Code *code)
 {
   auto m = code->module();
+  auto d = code->data();
+
   auto ann_ctx = enco::SubnetManager::context(m);
 
   auto global = make_unique<Global>(os);
 
+  //
+  // Emit Bag's weight
+  //
+  for (uint32_t n = 0; n < m->entity()->bag()->size(); ++n)
+  {
+    auto bag = m->entity()->bag()->at(n);
+
+    if (!d->allocated(bag))
+    {
+      // Skip if the weight value does not exist for a given bag
+      continue;
+    }
+
+    // NOTE The current implementation assumes that all the values are of float(fp32) type
+    // TODO Support non-float values
+    auto span = d->f32()->weight(bag);
+
+    assert(span.data() != nullptr);
+    assert(span.size() > 0);
+
+    auto const base = reinterpret_cast<const uint8_t *>(span.data());
+    uint32_t const size = span.size() * sizeof(float);
+
+    assert(bag_data_offset_ctx.find(bag) == bag_data_offset_ctx.end());
+    bag_data_offset_ctx[bag] = global->constant(base, size);
+  }
+
   for (uint32_t n = 0; n < ann_ctx->count(); ++n)
   {
     auto binder = ann_ctx->nth(n);
index fd7b382..4334314 100644 (file)
@@ -29,6 +29,13 @@ using GlobalOffset = uint32_t;
 struct GlobalData
 {
   static GlobalOffset data_offset(const ann::Operand *);
+  /**
+   * @brief Return the weight offset of a given bag
+   *
+   * @note The behavior of "data_offset" is undefined if a bag has no weight.
+   */
+  static GlobalOffset data_offset(const coco::Bag *);
+
   static GlobalOffset name_offset(const coco::Input *);
   static GlobalOffset dims_offset(const coco::Input *);
   static GlobalOffset name_offset(const coco::Output *);