[loco] Introduce DepthwiseConv2D Layer (#6662)
author채성우/On-Device Lab(SR)/Engineer/삼성전자 <sw4670.chae@samsung.com>
Mon, 19 Aug 2019 07:55:40 +0000 (16:55 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 19 Aug 2019 07:55:40 +0000 (16:55 +0900)
This commit introduce DepthwiseConv2D layer and related GraphTestcase.

Related: #6569

Signed-off-by: seongwoo <sw4670.chae@samsung.com>
compiler/loco/src/Service/GraphBuilder.h
compiler/loco/src/Service/GraphTestcase.h

index ac9749e..2afc516 100644 (file)
@@ -385,6 +385,36 @@ struct DepthwiseFilterEncodeLayer final
   }
 };
 
+struct DepthwiseConv2DLayer final
+{
+  class Return
+  {
+  public:
+    Return(loco::DepthwiseConv2D *node) : _node{node}
+    {
+      // DO NOTHING
+    }
+
+  public:
+    loco::DepthwiseConv2D *node(void) { return _node; }
+
+  private:
+    loco::DepthwiseConv2D *_node;
+  };
+
+  std::unique_ptr<Return> operator()(GraphBuilder::Context *ctx)
+  {
+    auto depthwiseconv2d_node = ctx->graph()->nodes()->create<loco::DepthwiseConv2D>();
+
+    depthwiseconv2d_node->ker(ctx->stack()->pop());
+    depthwiseconv2d_node->ifm(ctx->stack()->pop());
+
+    ctx->stack()->push(depthwiseconv2d_node);
+
+    return stdex::make_unique<Return>(depthwiseconv2d_node);
+  }
+};
+
 struct FixedReshapeLayer final
 {
   class Return
index e515746..323f24a 100644 (file)
@@ -15,11 +15,45 @@ enum class GraphCode
   Relu,
   FeatureCodec,
   AvgPool2D,
+  DepthwiseConv2D,
   MaxPool2D,
   TensorConcat,
   FixedReshape,
 };
 
+namespace
+{
+
+template <loco::Domain D> loco::Permutation<D> make_NHWC_perm(void);
+
+template <> loco::Permutation<loco::Domain::Feature> make_NHWC_perm(void)
+{
+  loco::Permutation<loco::Domain::Feature> perm;
+
+  perm[loco::FeatureAxis::Count] = 0;
+  perm[loco::FeatureAxis::Height] = 1;
+  perm[loco::FeatureAxis::Width] = 2;
+  perm[loco::FeatureAxis::Depth] = 3;
+
+  return perm;
+}
+
+template <loco::Domain D> loco::Permutation<D> make_HWCM_perm(void);
+
+template <> loco::Permutation<loco::Domain::DepthwiseFilter> make_HWCM_perm(void)
+{
+  loco::Permutation<loco::Domain::DepthwiseFilter> perm;
+
+  perm[loco::DepthwiseFilterAxis::Height] = 0;
+  perm[loco::DepthwiseFilterAxis::Width] = 1;
+  perm[loco::DepthwiseFilterAxis::Depth] = 2;
+  perm[loco::DepthwiseFilterAxis::Multiplier] = 3;
+
+  return perm;
+}
+
+} // namespace
+
 template <GraphCode Code> class GraphTestcase;
 
 template <> class GraphTestcase<GraphCode::Identity> final
@@ -190,6 +224,49 @@ private:
   std::unique_ptr<loco::Graph> _graph;
 };
 
+template <> class GraphTestcase<GraphCode::DepthwiseConv2D> final
+{
+public:
+  GraphTestcase()
+  {
+    using namespace loco;
+
+    _graph = make_graph();
+
+    auto graph_builder = make_graph_builder(_graph.get());
+
+    Permutation<Domain::Feature> perm = make_NHWC_perm<Domain::Feature>();
+    Permutation<Domain::DepthwiseFilter> filter_perm = make_HWCM_perm<Domain::DepthwiseFilter>();
+
+    pull_node = graph_builder->push<InputLayer>()->name("input")->node();
+    encode_node = graph_builder->push<FeatureEncodeLayer>()->perm(perm)->node();
+
+    const_node = graph_builder->push<ConstGenLayer>()->node();
+
+    filter_encode_node =
+        graph_builder->push<DepthwiseFilterEncodeLayer>()->perm(filter_perm)->node();
+
+    depthwiseconv2d_node = graph_builder->push<DepthwiseConv2DLayer>()->node();
+
+    decode_node = graph_builder->push<FeatureDecodeLayer>()->perm(perm)->node();
+    push_node = graph_builder->push<OutputLayer>()->name("output")->node();
+  }
+
+public:
+  loco::Graph *graph() { return _graph.get(); }
+
+  loco::Pull *pull_node = nullptr;
+  loco::FeatureEncode *encode_node = nullptr;
+  loco::ConstGen *const_node = nullptr;
+  loco::DepthwiseFilterEncode *filter_encode_node = nullptr;
+  loco::DepthwiseConv2D *depthwiseconv2d_node = nullptr;
+  loco::FeatureDecode *decode_node = nullptr;
+  loco::Push *push_node = nullptr;
+
+private:
+  std::unique_ptr<loco::Graph> _graph;
+};
+
 template <> class GraphTestcase<GraphCode::MaxPool2D> final
 {
 public:
@@ -319,22 +396,4 @@ private:
   std::unique_ptr<loco::Graph> _graph;
 };
 
-namespace
-{
-
-template <loco::Domain D> loco::Permutation<D> make_NHWC_perm(void);
-
-template <> loco::Permutation<loco::Domain::Feature> make_NHWC_perm(void)
-{
-  loco::Permutation<loco::Domain::Feature> perm;
-
-  perm[loco::FeatureAxis::Count] = 0;
-  perm[loco::FeatureAxis::Height] = 1;
-  perm[loco::FeatureAxis::Width] = 2;
-  perm[loco::FeatureAxis::Depth] = 3;
-
-  return perm;
-}
-
-} // namespace
 #endif // __GRAPH_TESTCASE_H__