db5bdb5014e17af96fc823180a2c736e6f9e7069
[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 <string>
23 #include <vector>
24
25 namespace luci
26 {
27
28 class CircleOptimizer final
29 {
30 public:
31   struct Options
32   {
33     enum Algorithm
34     {
35       FuseAddWithTConv,
36       FuseBatchNormWithTConv,
37       FuseBCQ,
38       FuseInstanceNorm,
39       ResolveCustomOpAdd,
40       ResolveCustomOpBatchMatMul,
41       ResolveCustomOpMatMul,
42       QuantizeDequantizeWeights,
43       QuantizeWithMinMax,
44       Requantize,
45       FoldDequantize,
46       SparsifyTensorPass,
47       FusePreActivationBatchNorm,
48       MakeBatchNormGammaPositive,
49       FuseActivationFunction,
50     };
51
52     enum AlgorithmParameters
53     {
54       // quantize
55       Quantize_input_dtype,
56       Quantize_output_dtype,
57       Quantize_granularity, // layer-wise or channel-wise
58
59       // sparsify
60       Sparsify_tensor_name,
61       Sparsify_traversal_order,
62       Sparsify_format,
63       Sparsify_block_size,
64       Sparsify_block_map,
65     };
66
67     virtual ~Options() = default;
68
69     virtual void enable(Algorithm) = 0;
70     virtual bool query(Algorithm) = 0;
71     virtual void param(AlgorithmParameters, const std::string &) = 0;
72     virtual const std::string param(AlgorithmParameters) const = 0;
73   };
74
75 public:
76   // TODO maybe caller can provide Options as ctor parameters
77   Options *options(void);
78
79 public:
80   void optimize(loco::Graph *) const;
81
82   void quantize(loco::Graph *) const;
83
84   void sparsify(loco::Graph *) const;
85
86 private:
87   std::unique_ptr<Options> _options;
88 };
89
90 } // namespace luci
91
92 #endif // __LUCI_CIRCLE_OPTIMIZER_H__