[tflchef] tlchef for FullyConnected (#2852)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Tue, 15 Jan 2019 06:15:05 +0000 (15:15 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 15 Jan 2019 06:15:05 +0000 (15:15 +0900)
* [tflchef] tlchef for FullyConnected

This commit adds code to convert recipe for FullyConnected to tflite file.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* alphabetical order

* remove comment about alphabetical order

contrib/tflchef/core/src/Op/FullyConnected.cpp [new file with mode: 0644]
contrib/tflchef/core/src/Op/FullyConnected.h [new file with mode: 0644]
contrib/tflchef/core/src/OpChef.def
contrib/tflchef/core/src/OpChefs.h
contrib/tflchef/proto/tflchef.proto
contrib/tflchef/tests/fullyconnected/test.recipe [new file with mode: 0644]

diff --git a/contrib/tflchef/core/src/Op/FullyConnected.cpp b/contrib/tflchef/core/src/Op/FullyConnected.cpp
new file mode 100644 (file)
index 0000000..4526991
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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 "FullyConnected.h"
+#include "Convert.h"
+
+#include <cassert>
+
+flatbuffers::Offset<void> FullyConnectedChef::value(flatbuffers::FlatBufferBuilder &fbb) const
+{
+  auto &operation = (*_operation);
+
+  assert(operation.has_fullyconnected_options());
+
+  auto tflite_activation = as_tflite_activation(operation.fullyconnected_options().activation());
+
+  tflite::FullyConnectedOptionsBuilder fc_options_builder{fbb};
+  fc_options_builder.add_fused_activation_function(tflite_activation);
+
+  return fc_options_builder.Finish().Union();
+}
+
+std::unique_ptr<OpChef> FullyConnectedChefFactory::create(const tflchef::Operation *operation) const
+{
+  return std::unique_ptr<OpChef>{new FullyConnectedChef{operation}};
+}
diff --git a/contrib/tflchef/core/src/Op/FullyConnected.h b/contrib/tflchef/core/src/Op/FullyConnected.h
new file mode 100644 (file)
index 0000000..ea71012
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+#ifndef __OP_FULLYCONNECTED_H__
+#define __OP_FULLYCONNECTED_H__
+
+#include "OpChef.h"
+
+class FullyConnectedChef final : public OpChef
+{
+public:
+  explicit FullyConnectedChef(const tflchef::Operation *operation) : _operation{operation}
+  {
+    // DO NOTHING
+  }
+
+public:
+  tflite::BuiltinOperator code(void) const override
+  {
+    return tflite::BuiltinOperator_FULLY_CONNECTED;
+  }
+
+  tflite::BuiltinOptions type(void) const override
+  {
+    return tflite::BuiltinOptions_FullyConnectedOptions;
+  }
+
+  flatbuffers::Offset<void> value(flatbuffers::FlatBufferBuilder &fbb) const override;
+
+private:
+  const tflchef::Operation *_operation;
+};
+
+struct FullyConnectedChefFactory final : public OpChefFactory
+{
+  std::unique_ptr<OpChef> create(const tflchef::Operation *operation) const override;
+};
+
+#endif // __OP_FULLYCONNECTED_H__
index 3310e45..0b9ea73 100644 (file)
@@ -9,6 +9,7 @@ OP_CHEF(Concatenation, ConcatenationChefFactory)
 OP_CHEF(Conv2D, Conv2DChefFactory)
 OP_CHEF(DepthwiseConv2D, DepthwiseConv2DChefFactory)
 OP_CHEF(Div, DivChefFactory)
+OP_CHEF(FullyConnected, FullyConnectedChefFactory)
 OP_CHEF(MaxPool2D, MaxPool2DChefFactory)
 OP_CHEF(ReLU, ReLUChefFactory)
 OP_CHEF(ReLU6, ReLU6ChefFactory)
index 4e42163..933697a 100644 (file)
@@ -22,6 +22,7 @@
 #include "Op/Conv2D.h"
 #include "Op/DepthwiseConv2D.h"
 #include "Op/Div.h"
+#include "Op/FullyConnected.h"
 #include "Op/MaxPool2D.h"
 #include "Op/ReLU.h"
 #include "Op/ReLU6.h"
index 512a350..dc63f0c 100644 (file)
@@ -88,6 +88,10 @@ message DivOptions {
   optional Activation activation = 1 [default = NONE];
 }
 
+message FullyConnectedOptions {
+  optional Activation activation = 1 [default = NONE];
+}
+
 message Operation {
   optional string type = 1;
   repeated string input = 2;
@@ -101,6 +105,7 @@ message Operation {
   optional DepthwiseConv2DOptions depthwiseconv2d_options = 105;
   optional SubOptions sub_options = 106;
   optional DivOptions div_options = 107;
+  optional FullyConnectedOptions fullyconnected_options = 108;
 }
 
 message ModelRecipe {
diff --git a/contrib/tflchef/tests/fullyconnected/test.recipe b/contrib/tflchef/tests/fullyconnected/test.recipe
new file mode 100644 (file)
index 0000000..dca4c09
--- /dev/null
@@ -0,0 +1,34 @@
+operand {
+  name: "in"
+  type: FLOAT32
+  shape { dim: 1 dim: 64 }
+}
+operand {
+  name: "weight"
+  type: FLOAT32
+  shape { dim: 8 dim: 64 }
+}
+operand {
+  name: "bias"
+  type: FLOAT32
+  shape { dim: 8 }
+}
+operand {
+  name: "out"
+  type: FLOAT32
+  shape { dim: 1 dim: 8 }
+}
+operation {
+  type: "FullyConnected"
+  fullyconnected_options {
+    activation: NONE
+  }
+  input: "in"
+  input: "weight"
+  input: "bias"
+  output: "out"
+}
+input: "in"
+input: "weight"
+input: "bias"
+output: "out"