[enco] frontend/caffe: separate relu (#2562)
author박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Fri, 7 Dec 2018 08:29:26 +0000 (17:29 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 7 Dec 2018 08:29:26 +0000 (17:29 +0900)
This will move ReLU layer handler to separate file

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
contrib/enco/frontend/caffe/src/Frontend.cpp
contrib/enco/frontend/caffe/src/GraphBuilderRegistry.cpp
contrib/enco/frontend/caffe/src/Op/ReLU.cpp [new file with mode: 0644]
contrib/enco/frontend/caffe/src/Op/ReLU.h [new file with mode: 0644]

index 6a34dd0..9b53b3c 100644 (file)
@@ -130,51 +130,6 @@ enco::Bundle Frontend::load(void) const
       graph_builder->build(layer, &opbuilder_context);
     }
     // TODO move type handlers to separate builder
-    else if (layer.type() == "ReLU")
-    {
-      assert(layer.bottom().size() == 1);
-      assert(layer.top().size() == 1);
-
-      // PReLU is not supported, yet
-      // TODO Support PReLU
-      assert(!layer.has_relu_param());
-
-      // NOTE The current implementation treats ReLU as Feature op
-      // TODO Support ReLU over general tensor
-      const auto ifm_name = layer.bottom(0);
-      const auto ifm_shape = shape_ctx.at(ifm_name);
-      auto ifm_bag = bag_ctx.at(ifm_name);
-      auto ifm_obj = module->entity()->object()->create<coco::FeatureObject>();
-
-      ifm_obj->bag(ifm_bag);
-      ifm_obj->layout(coco::FeatureLayouts::BCHW::create(as_feature_shape(ifm_shape)));
-
-      const auto ofm_name = layer.top(0);
-      const auto ofm_shape = ifm_shape;
-      auto ofm_bag = module->entity()->bag()->create(num_elements(ofm_shape));
-      auto ofm_obj = module->entity()->object()->create<coco::FeatureObject>();
-
-      ofm_obj->bag(ofm_bag);
-      ofm_obj->layout(coco::FeatureLayouts::BCHW::create(as_feature_shape(ofm_shape)));
-
-      // Create a Load Op
-      auto load = op_builder(module).load(ifm_obj).pop();
-
-      // Create a ReLU op
-      auto op = module->entity()->op()->create<coco::ReLU>();
-
-      op->arg(load);
-
-      // Create a Eval instruction
-      auto ins = instr_builder(module).eval(ofm_obj, op);
-
-      // Append the instruction to the block
-      blk->instr()->append(ins);
-
-      // Update bag and shape context
-      bag_ctx[ofm_name] = ofm_bag;
-      shape_ctx[ofm_name] = ofm_shape;
-    }
     else if (layer.type() == "Concat")
     {
       assert(layer.bottom().size() > 0);
index c2fee6b..ccff739 100644 (file)
@@ -19,6 +19,7 @@
 #include "Op/Convolution.h"
 #include "Op/Input.h"
 #include "Op/Pooling.h"
+#include "Op/ReLU.h"
 
 #include <stdex/Memory.h>
 
@@ -33,6 +34,7 @@ GraphBuilderRegistry::GraphBuilderRegistry()
   _builder_map["Convolution"] = make_unique<ConvolutionBuilder>();
   _builder_map["Input"] = make_unique<InputBuilder>();
   _builder_map["Pooling"] = make_unique<PoolingBuilder>();
+  _builder_map["ReLU"] = make_unique<ReLUBuilder>();
 }
 
 } // namespace caffeimport
diff --git a/contrib/enco/frontend/caffe/src/Op/ReLU.cpp b/contrib/enco/frontend/caffe/src/Op/ReLU.cpp
new file mode 100644 (file)
index 0000000..61e206d
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * 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 "ReLU.h"
+#include "IRBuilder.h"
+
+#include <coco/IR/FeatureLayouts.h>
+
+#include <morph/caffe.h>
+
+#include <cassert>
+
+using namespace nncc::core::ADT;
+using namespace morph::caffe;
+
+namespace caffeimport
+{
+
+void ReLUBuilder::build(const ::caffe::LayerParameter &layer, GraphBuilderContext *context) const
+{
+  coco::Module *module = context->module();
+  coco::Block *blk = context->block();
+  std::map<std::string, tensor::Shape> &shape_ctx = context->shape_ctx();
+  std::map<std::string, coco::Bag *> &bag_ctx = context->bag_ctx();
+
+  assert(layer.bottom().size() == 1);
+  assert(layer.top().size() == 1);
+
+  // PReLU is not supported, yet
+  // TODO Support PReLU
+  assert(!layer.has_relu_param());
+
+  // NOTE The current implementation treats ReLU as Feature op
+  // TODO Support ReLU over general tensor
+  const auto ifm_name = layer.bottom(0);
+  const auto ifm_shape = shape_ctx.at(ifm_name);
+  auto ifm_bag = bag_ctx.at(ifm_name);
+  auto ifm_obj = module->entity()->object()->create<coco::FeatureObject>();
+
+  ifm_obj->bag(ifm_bag);
+  ifm_obj->layout(coco::FeatureLayouts::BCHW::create(as_feature_shape(ifm_shape)));
+
+  const auto ofm_name = layer.top(0);
+  const auto ofm_shape = ifm_shape;
+  auto ofm_bag = module->entity()->bag()->create(num_elements(ofm_shape));
+  auto ofm_obj = module->entity()->object()->create<coco::FeatureObject>();
+
+  ofm_obj->bag(ofm_bag);
+  ofm_obj->layout(coco::FeatureLayouts::BCHW::create(as_feature_shape(ofm_shape)));
+
+  // Create a Load Op
+  auto load = op_builder(module).load(ifm_obj).pop();
+
+  // Create a ReLU op
+  auto op = module->entity()->op()->create<coco::ReLU>();
+
+  op->arg(load);
+
+  // Create a Eval instruction
+  auto ins = instr_builder(module).eval(ofm_obj, op);
+
+  // Append the instruction to the block
+  blk->instr()->append(ins);
+
+  // Update bag and shape context
+  bag_ctx[ofm_name] = ofm_bag;
+  shape_ctx[ofm_name] = ofm_shape;
+}
+
+} // namespace caffeimport
diff --git a/contrib/enco/frontend/caffe/src/Op/ReLU.h b/contrib/enco/frontend/caffe/src/Op/ReLU.h
new file mode 100644 (file)
index 0000000..94836fd
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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 __RELU_BUILDER_H__
+#define __RELU_BUILDER_H__
+
+#include "GraphBuilder.h"
+
+#include "Context.h"
+
+namespace caffeimport
+{
+
+class ReLUBuilder final : public GraphBuilder
+{
+public:
+  void build(const ::caffe::LayerParameter &layer, GraphBuilderContext *context) const override;
+};
+
+} // namespace caffeimport
+
+#endif // __RELU_BUILDER_H__