Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / include / luci / CircleOptimizer.h
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
17 #ifndef __LUCI_CIRCLE_OPTIMIZER_H__
18 #define __LUCI_CIRCLE_OPTIMIZER_H__
19
20 #include <loco.h>
21
22 #include <luci/IR/Module.h>
23
24 #include <string>
25 #include <vector>
26
27 namespace luci
28 {
29
30 class CircleOptimizer final
31 {
32 public:
33   struct Options
34   {
35     enum Algorithm
36     {
37       FuseAddWithFullyConnected,
38       FuseAddWithTConv,
39       FuseBatchNormWithConv,
40       FuseBatchNormWithDwConv,
41       FuseBatchNormWithTConv,
42       FuseBCQ,
43       FuseInstanceNorm,
44       FuseMeanWithMean,
45       FuseTransposeWithMean,
46       ResolveCustomOpAdd,
47       ResolveCustomOpBatchMatMul,
48       ResolveCustomOpMatMul,
49       ResolveCustomOpMaxPoolWithArgmax,
50       ResolveCustomOpSplitV,
51       FoldAddV2,
52       FoldCast,
53       FoldDensify,
54       FoldDepthwiseConv2D,
55       FoldFullyConnected,
56       FoldDequantize,
57       FoldGather,
58       FoldSparseToDense,
59       ForwardReshapeToUnaryOp,
60       ForwardTransposeOp,
61       SparsifyTensorPass,
62       FusePreActivationBatchNorm,
63       MakeBatchNormGammaPositive,
64       FuseActivationFunction,
65       FusePRelu,
66       FuseGelu,
67       ShuffleWeightTo16x1Float32,
68       RemoveRedundantTranspose,
69       ReplaceMulAddWithDepthwiseConv,
70       ReplaceNonConstFCWithBatchMatMul,
71       ReplaceSubWithAdd,
72       SubstitutePackToReshape,
73       SubstitutePadV2ToPad,
74       SubstituteSplitVToSplit,
75       SubstituteSqueezeToReshape,
76       ExpandBroadcastConst,
77       ConvertNCHWToNHWC,
78       RemoveUnnecessarySlice,
79       RemoveUnnecessaryStridedSlice,
80       RemoveUnnecessarySplit,
81       RemoveUnnecessaryReshape,
82       TransformMinMaxToRelu6Pass,
83       TransformMinReluToRelu6Pass,
84       DecomposeHardSwishPass,
85       SubstituteStridedSliceToReshape,
86       SubstituteTransposeToReshape,
87       RemoveRedundantQuantize,
88       RemoveRedundantReshape,
89       RemoveFakeQuant,
90       RemoveQuantDequantSeq,
91       RemoveDuplicateConst,
92       UnrollUnidirSeqLSTM,
93     };
94
95     enum AlgorithmParameters
96     {
97       // sparsify
98       Sparsify_tensor_name,
99       Sparsify_traversal_order,
100       Sparsify_format,
101       Sparsify_block_size,
102       Sparsify_block_map,
103
104       // convert NCHW to NHWC
105       NCHW_to_NHWC_input_shape,
106       NCHW_to_NHWC_output_shape,
107     };
108
109     virtual ~Options() = default;
110
111     virtual void enable(Algorithm) = 0;
112     virtual bool query(Algorithm) = 0;
113     virtual void param(AlgorithmParameters, const std::string &) = 0;
114     virtual const std::string param(AlgorithmParameters) const = 0;
115   };
116
117 public:
118   // TODO maybe caller can provide Options as ctor parameters
119   Options *options(void);
120
121 public:
122   void optimize(luci::Module *) const;
123
124   void optimize(loco::Graph *) const;
125
126   void sparsify(loco::Graph *) const;
127
128 private:
129   std::unique_ptr<Options> _options;
130 };
131
132 } // namespace luci
133
134 #endif // __LUCI_CIRCLE_OPTIMIZER_H__