ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CircleDepthwiseConv2D.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
20
21#include <oops/UserExn.h>
22
23namespace luci
24{
25
27{
28 // Circle DepthwiseConv2D may not have a bias but we won't support this
29 if (args.op.inputs.size() != 3 && args.op.inputs.size() != 2)
30 return false;
31
32 if (args.op.outputs.size() != 1)
33 return false;
34
35 const auto tensors = args.reader.tensors();
36
37 // input shape
38 const auto input = tensors.at(args.op.inputs.at(0));
39 assert(input != nullptr);
40 const auto input_shape = wrap(input->shape());
41
42 // input shape must be rank 4
43 if (input_shape.size() != 4)
44 return false;
45
46 // filter shape
47 const auto filter = tensors.at(args.op.inputs.at(1));
48 assert(filter != nullptr);
49 const auto filter_shape = wrap(filter->shape());
50
51 // filter shape must be rank 4
52 if (filter_shape.size() != 4)
53 return false;
54
55 // multiplier
56 const auto *options = args.op.builtin_options.AsDepthwiseConv2DOptions();
57 const auto &multiplier = options->depth_multiplier;
58
59 // filter represents as [1, H, W, C*M] where M is multiplier.
60 if (filter_shape.at(3) != input_shape.at(3) * multiplier)
61 return false;
62
63 return true;
64}
65
66CircleNode *CircleDepthwiseConv2DGraphBuilder::build_node(const circle::OperatorT &op,
67 const std::vector<CircleNode *> &inputs,
68 loco::Graph *graph) const
69{
70 auto *node = graph->nodes()->create<CircleDepthwiseConv2D>();
71 node->input(inputs.at(0));
72 node->filter(inputs.at(1));
73 if (inputs.size() != 3)
74 throw oops::UserExn("DepthwiseConv2d without bias is unsupported");
75 node->bias(inputs.at(2));
76
77 const auto *options = op.builtin_options.AsDepthwiseConv2DOptions();
78 node->padding(luci_padding(options->padding));
79 node->stride()->w(options->stride_w);
80 node->stride()->h(options->stride_h);
81 node->depthMultiplier(options->depth_multiplier);
82 node->fusedActivationFunction(luci_actfunc(options->fused_activation_function));
83 node->dilation()->w(options->dilation_w_factor);
84 node->dilation()->h(options->dilation_h_factor);
85
86 return node;
87}
88
89} // namespace luci
A neural network graph.
Definition Graph.h:161
bool validate(const ValidateArgs &args) const final
DEPTHWISE_CONV_2D in Circle.
loco::Node * input(void) const
Exception to user.
Definition UserExn.h:42
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)
Padding luci_padding(const circle::Padding padding)
FusedActFunc luci_actfunc(const circle::ActivationFunctionType type)