[enco] Set weight (kernel/bias) data for ANN subnet (#1242)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 30 Aug 2018 05:28:31 +0000 (14:28 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 30 Aug 2018 05:28:31 +0000 (14:28 +0900)
This commit implements weight initializer for android NN subnet inside
Split pass.

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

index b0142f3..b46d8c3 100644 (file)
@@ -134,6 +134,8 @@ void ANNGroupBuilder::build(void) const
 
 #include <stdexcept>
 
+#include <nncc/core/ADT/kernel/NHWCLayout.h>
+
 namespace
 {
 
@@ -153,13 +155,52 @@ public:
       auto ifm = _binder->addOperand<float>(unit->ifm());
       auto ker = _binder->addOperand<float>(conv->ker());
 
-      // TODO Fill kernel data
+      // Fill kernel data
+      {
+        auto obj = conv->ker();
+        auto shape = obj->shape();
+        auto len = nncc::core::ADT::kernel::num_elements(shape);
+
+        auto ovl = _data->f32()->read(obj);
+        assert(ovl != nullptr);
+
+        // Flatten?
+        std::vector<float> values;
+        values.resize(len);
+
+        for (uint32_t n = 0; n < shape.count(); ++n)
+        {
+          for (uint32_t ch = 0; ch < shape.depth(); ++ch)
+          {
+            for (uint32_t row = 0; row < shape.height(); ++row)
+            {
+              for (uint32_t col = 0; col < shape.width(); ++col)
+              {
+                const static nncc::core::ADT::kernel::NHWCLayout l{};
+                const auto offset = static_cast<uint32_t>(l.offset(shape, n, ch, row, col));
+
+                values.at(offset) = ovl->at(n, ch, row, col);
+              }
+            }
+          }
+        }
+
+        _binder->setOperand(ker, values.begin(), values.end());
+      }
 
       // Conv2D in coco IR has no bias, but bias is mandatory in Android NN API
       auto bias =
           _binder->addOperand<float>(nncc::core::ADT::tensor::Shape{conv->ker()->shape().count()});
 
-      // TODO Fill bias data
+      // Fill bias data
+      {
+        auto length = conv->ker()->shape().count();
+
+        std::vector<float> values;
+        values.resize(length, 0.0f);
+
+        _binder->setOperand(bias, values.begin(), values.end());
+      }
 
       auto left = _binder->addOperand<int32_t>();
       _binder->setOperand(left, 0);