[tflchef] Support Div operation (#2755)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Wed, 2 Jan 2019 00:08:17 +0000 (09:08 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 2 Jan 2019 00:08:17 +0000 (09:08 +0900)
This commit will let tflchef cook Div operation

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
contrib/tflchef/core/src/Op/Div.cpp [new file with mode: 0644]
contrib/tflchef/core/src/Op/Div.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/div/test.recipe [new file with mode: 0644]

diff --git a/contrib/tflchef/core/src/Op/Div.cpp b/contrib/tflchef/core/src/Op/Div.cpp
new file mode 100644 (file)
index 0000000..98eead8
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2018 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 "Div.h"
+#include "Convert.h"
+
+#include <cassert>
+
+flatbuffers::Offset<void> DivChef::value(flatbuffers::FlatBufferBuilder &fbb) const
+{
+  auto &operation = (*_operation);
+
+  assert(operation.has_div_options());
+
+  auto tflite_activation = as_tflite_activation(operation.div_options().activation());
+
+  tflite::DivOptionsBuilder div_options_builder{fbb};
+  div_options_builder.add_fused_activation_function(tflite_activation);
+
+  return div_options_builder.Finish().Union();
+}
+
+std::unique_ptr<OpChef> DivChefFactory::create(const tflchef::Operation *operation) const
+{
+  return std::unique_ptr<OpChef>{new DivChef{operation}};
+}
diff --git a/contrib/tflchef/core/src/Op/Div.h b/contrib/tflchef/core/src/Op/Div.h
new file mode 100644 (file)
index 0000000..0d1063c
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018 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_DIV_H__
+#define __OP_DIV_H__
+
+#include "OpChef.h"
+
+class DivChef final : public OpChef
+{
+public:
+  explicit DivChef(const tflchef::Operation *operation) : _operation{operation}
+  {
+    // DO NOTHING
+  }
+
+public:
+  tflite::BuiltinOperator code(void) const override { return tflite::BuiltinOperator_DIV; }
+
+  tflite::BuiltinOptions type(void) const override { return tflite::BuiltinOptions_DivOptions; }
+
+  flatbuffers::Offset<void> value(flatbuffers::FlatBufferBuilder &fbb) const override;
+
+private:
+  const tflchef::Operation *_operation;
+};
+
+struct DivChefFactory final : public OpChefFactory
+{
+  std::unique_ptr<OpChef> create(const tflchef::Operation *operation) const override;
+};
+
+#endif // __OP_DIV_H__
index 7f29efa..3310e45 100644 (file)
@@ -8,6 +8,7 @@ OP_CHEF(AveragePool2D, AveragePool2DChefFactory)
 OP_CHEF(Concatenation, ConcatenationChefFactory)
 OP_CHEF(Conv2D, Conv2DChefFactory)
 OP_CHEF(DepthwiseConv2D, DepthwiseConv2DChefFactory)
+OP_CHEF(Div, DivChefFactory)
 OP_CHEF(MaxPool2D, MaxPool2DChefFactory)
 OP_CHEF(ReLU, ReLUChefFactory)
 OP_CHEF(ReLU6, ReLU6ChefFactory)
index 2319a6f..4e42163 100644 (file)
@@ -21,6 +21,7 @@
 #include "Op/Concatenation.h"
 #include "Op/Conv2D.h"
 #include "Op/DepthwiseConv2D.h"
+#include "Op/Div.h"
 #include "Op/MaxPool2D.h"
 #include "Op/ReLU.h"
 #include "Op/ReLU6.h"
index 3452fdc..512a350 100644 (file)
@@ -84,6 +84,10 @@ message SubOptions {
   optional Activation activation = 1 [default = NONE];
 }
 
+message DivOptions {
+  optional Activation activation = 1 [default = NONE];
+}
+
 message Operation {
   optional string type = 1;
   repeated string input = 2;
@@ -96,6 +100,7 @@ message Operation {
   optional ReshapeOptions reshape_options = 104;
   optional DepthwiseConv2DOptions depthwiseconv2d_options = 105;
   optional SubOptions sub_options = 106;
+  optional DivOptions div_options = 107;
 }
 
 message ModelRecipe {
diff --git a/contrib/tflchef/tests/div/test.recipe b/contrib/tflchef/tests/div/test.recipe
new file mode 100644 (file)
index 0000000..4fb76d4
--- /dev/null
@@ -0,0 +1,27 @@
+operand {
+  name: "ifm1"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+}
+operand {
+  name: "ifm2"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+}
+operand {
+  name: "ofm"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+}
+operation {
+  type: "Div"
+  input: "ifm1"
+  input: "ifm2"
+  output: "ofm"
+  div_options {
+    activation: NONE
+  }
+}
+input: "ifm1"
+input: "ifm2"
+output: "ofm"