Imported Upstream version 1.18.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       QuantizeDequantizeWeights,
51       QuantizeWithMinMax,
52       Requantize,
53       FoldAddV2,
54       FoldCast,
55       FoldDepthwiseConv2D,
56       FoldDequantize,
57       FoldSparseToDense,
58       ForceQuantParam,
59       ForwardReshapeToUnaryOp,
60       SparsifyTensorPass,
61       FusePreActivationBatchNorm,
62       MakeBatchNormGammaPositive,
63       FuseActivationFunction,
64       ShuffleWeightTo16x1Float32,
65       RemoveRedundantTranspose,
66       ReplaceMulAddWithDepthwiseConv,
67       ReplaceSubWithAdd,
68       SubstitutePackToReshape,
69       SubstitutePadV2ToPad,
70       SubstituteSplitVToSplit,
71       SubstituteSqueezeToReshape,
72       ExpandBroadcastConst,
73       ConvertNCHWToNHWC,
74       RemoveUnnecessarySlice,
75       RemoveUnnecessaryStridedSlice,
76       RemoveUnnecessarySplit,
77       RemoveUnnecessaryReshape,
78       TransformMinMaxToRelu6Pass,
79       TransformMinReluToRelu6Pass,
80       SubstituteStridedSliceToReshape,
81       SubstituteTransposeToReshape,
82       RemoveRedundantReshape,
83       RemoveFakeQuant,
84       RemoveQuantDequantSeq,
85     };
86
87     enum AlgorithmParameters
88     {
89       // quantize
90       Quantize_input_model_dtype,
91       Quantize_output_model_dtype,
92       Quantize_granularity, // layer-wise or channel-wise
93       Quantize_tensor_names,
94       Quantize_scales,
95       Quantize_zero_points,
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       Quantize_input_dtype = Quantize_input_model_dtype,   // TODO Remove this
109       Quantize_output_dtype = Quantize_output_model_dtype, // TODO Remove this
110     };
111
112     virtual ~Options() = default;
113
114     virtual void enable(Algorithm) = 0;
115     virtual bool query(Algorithm) = 0;
116     virtual void param(AlgorithmParameters, const std::string &) = 0;
117     virtual const std::string param(AlgorithmParameters) const = 0;
118     virtual void params(AlgorithmParameters, std::vector<std::string> &) = 0;
119     virtual std::vector<std::string> params(AlgorithmParameters) const = 0;
120   };
121
122 public:
123   // TODO maybe caller can provide Options as ctor parameters
124   Options *options(void);
125
126 public:
127   void optimize(luci::Module *) const;
128
129   void optimize(loco::Graph *) const;
130
131   void quantize(loco::Graph *) const;
132
133   void sparsify(loco::Graph *) const;
134
135 private:
136   std::unique_ptr<Options> _options;
137 };
138
139 } // namespace luci
140
141 #endif // __LUCI_CIRCLE_OPTIMIZER_H__