Imported Upstream version 1.12.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       FuseAddWithTConv,
38       FuseBatchNormWithTConv,
39       FuseBCQ,
40       FuseInstanceNorm,
41       ResolveCustomOpAdd,
42       ResolveCustomOpBatchMatMul,
43       ResolveCustomOpMatMul,
44       QuantizeDequantizeWeights,
45       QuantizeWithMinMax,
46       Requantize,
47       FoldDequantize,
48       SparsifyTensorPass,
49       FusePreActivationBatchNorm,
50       MakeBatchNormGammaPositive,
51       FuseActivationFunction,
52       ShuffleWeightTo16x1Float32,
53       RemoveRedundantTranspose,
54       ReplaceMulAddWithDepthwiseConv,
55       SubstitutePackToReshape,
56     };
57
58     enum AlgorithmParameters
59     {
60       // quantize
61       Quantize_input_dtype,
62       Quantize_output_dtype,
63       Quantize_granularity, // layer-wise or channel-wise
64
65       // sparsify
66       Sparsify_tensor_name,
67       Sparsify_traversal_order,
68       Sparsify_format,
69       Sparsify_block_size,
70       Sparsify_block_map,
71     };
72
73     virtual ~Options() = default;
74
75     virtual void enable(Algorithm) = 0;
76     virtual bool query(Algorithm) = 0;
77     virtual void param(AlgorithmParameters, const std::string &) = 0;
78     virtual const std::string param(AlgorithmParameters) const = 0;
79   };
80
81 public:
82   // TODO maybe caller can provide Options as ctor parameters
83   Options *options(void);
84
85 public:
86   void optimize(luci::Module *) const;
87
88   void optimize(loco::Graph *) const;
89
90   void quantize(loco::Graph *) const;
91
92   void sparsify(loco::Graph *) const;
93
94 private:
95   std::unique_ptr<Options> _options;
96 };
97
98 } // namespace luci
99
100 #endif // __LUCI_CIRCLE_OPTIMIZER_H__