1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
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 GLOW_QUANTIZATION_QUANTIZATION_H
18#define GLOW_QUANTIZATION_QUANTIZATION_H
19
20#include "glow/Graph/Graph.h"
21#include "glow/Quantization/Base/Base.h"
22
23#include <string>
24#include <tuple>
25#include <vector>
26
27namespace glow {
28
29class Backend;
30
31namespace quantization {
32
33/// Generate NodeProfilingInfo for all the required nodes from function \p F
34/// using the profiling information stored in \p bindings obtained after
35/// running the network in profiling mode. During the profiling phase all the
36/// nodes are lowered. The map \p loweredMap maps the vector of NodeOutputNames
37/// obtained after lowering a NodeValue to the NodeOutputName of the lowered
38/// NodeValue. This map is used to replicate the profiling information for the
39/// unlowered NodeValues such that during the actual quantization, depending
40/// on the backend decision to lower some nodes or not, a profile will be found
41/// for any NodeValue.
42std::vector<NodeProfilingInfo>
43generateNodeProfilingInfos(PlaceholderBindings &bindings, const Function *F,
44 const LoweredInfoMap &loweredMap = {});
45
46/// Generate NodeQuantizationInfo for all the required nodes from function \p F
47/// using the profiling information and the parameters from \p quantConfig. The
48/// map \p loweredMap is the lowering map obtained during the quantization phase
49/// and is used to find lowering patterns for the bias operands.
50std::vector<NodeQuantizationInfo>
51generateNodeQuantizationInfos(Function *F,
52 const QuantizationConfiguration &quantConfig,
53 const LoweredInfoMap &loweredMap = {});
54
55/// Quantizes the function \p F into an unoptimized partially quantized function
56/// based on configuration from \p quantConfig. This method converts to integer
57/// as many nodes as permitted by the backend \p B. \p loweredMap contains info
58/// about what nodes were lowered from what, to be used during quantization.
59/// \p doNotQuantizeKinds lists kinds to not quantize, even if a profile was
60/// gathered for them and the backend supports the quantized operation.
61void quantizeFunction(Function *F, const QuantizationConfiguration &quantConfig,
62 const Backend &B, const LoweredInfoMap &loweredMap = {},
63 const KindSet &doNotQuantizeKinds = {});
64
65/// Support quantized Log \p LN inside \p F by replacing it with an
66/// IntLookupTable. \returns final node in the chain implementing the quantized
67/// Log via the IntLookupTable.
68Node *replaceQuantizedLogWithLookupTable(Function &F, const LogNode &LN,
69 Schema schema = Asymmetric);
70
71/// Support quantized Exp \p EN inside \p F by replacing it with an
72/// IntLookupTable. \returns final node in the chain implementing the quantized
73/// Exp via the IntLookupTable.
74Node *replaceQuantizedExpWithLookupTable(Function &F, const ExpNode &EN,
75 Schema schema = Asymmetric);
76
77/// Support quantized Tanh \p TN inside \p F by replacing it with an
78/// IntLookupTable. \returns final node in the chain implementing the quantized
79/// Tanh via the IntLookupTable.
80Node *replaceQuantizedTanhWithLookupTable(Function &F, const TanhNode &TN,
81 Schema schema = Asymmetric);
82
83/// Support quantized Sigmoid \p SN inside \p F by replacing it with an
84/// IntLookupTable. \returns final node in the chain implementing the quantized
85/// Sigmoid via the IntLookupTable.
86Node *replaceQuantizedSigmoidWithLookupTable(Function &F, const SigmoidNode &SN,
87 Schema schema = Asymmetric);
88
89} // namespace quantization
90} // namespace glow
91
92#endif // GLOW_QUANTIZATION_QUANTIZATION_H
93