• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Gemmlowp Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // output_stages.h: public definitions of the output stages that can
16 // be assembled into an output pipeline, to control how internal
17 // 32-bit accumulators are transformed to obtain the final uint8
18 // result matrix entries.
19 
20 #ifndef GEMMLOWP_PUBLIC_OUTPUT_STAGES_H_
21 #define GEMMLOWP_PUBLIC_OUTPUT_STAGES_H_
22 
23 #include <tuple>
24 
25 #include "../internal/common.h"
26 
27 namespace gemmlowp {
28 
29 // This output stage takes int32 values and returns still int32 values,
30 // but "quantized down" to the uint8 scale; in other words, its output
31 // is typically what one would then clamp to [0..255] and cast to uint8
32 // (see OutputStageSaturatingCastToUint8).
33 //
34 // This "quantization down" process depends on 3 parameters,
35 //   result_offset, result_mult_int, result_shift,
36 // and the result is:
37 //   ((input + result_offset) * result_mult_int + rounding) >> result_shift
38 // where
39 //   rounding = (result_shift < 1) ? 0 : (1 << (result_shift - 1));
40 struct OutputStageQuantizeDownInt32ToUint8Scale {
41   std::int32_t result_offset;
42   std::int32_t result_mult_int;
43   std::int32_t result_shift;
44 };
45 
46 // This output stage takes int32 values and returns still int32 values,
47 // but "quantized down" to the uint8 scale; in other words, its output
48 // is typically what one would then clamp to [0..255] and cast to uint8
49 // (see OutputStageSaturatingCastToUint8).
50 //
51 // This "quantization down" process depends on 3 parameters,
52 //   result_offset, result_mult_int, result_shift,
53 // and the result is:
54 //   ((input + result_offset) * result_mult_int + rounding) >> result_shift
55 // where
56 //   rounding = (result_shift < 1) ? 0 : (1 << (result_shift - 1));
57 //
58 // Difference from OutputStageQuantizeDownInt32ToUint8Scale here is that each
59 // row or column of the output (depending on tShape) has its own result_offset
60 // and result_mult_int numbers.
61 template <VectorShape tShape>
62 struct OutputStageQuantizeDownInt32ToUint8ScalePC {
63   VectorMap<const std::int32_t, tShape> result_offset;
64   VectorMap<const std::int32_t, tShape> result_mult_int;
65   std::int32_t result_shift;
66 };
67 
68 // This output stage takes int32 values and returns still int32 values,
69 // but "quantized down" to the uint8 scale; in other words, its output
70 // is typically what one would then clamp to [0..255] and cast to uint8
71 // (see OutputStageSaturatingCastToUint8).
72 //
73 // This "quantization down" process depends on 3 parameters,
74 //   result_offset, result_fixedpoint_multiplier, result_shift,
75 // and the result is:
76 //   ((FixedPointMul(input, result_fixedpoint_multiplier) +
77 //   rounding) >> result_shift) + result_offset_after_shift
78 // where
79 //   rounding = (result_shift < 1) ? 0 : (1 << (result_shift - 1));
80 // and where FixedPointMul(x, y) is the nearest integer to the following
81 // mathematical expression, evaluated without overflow or intermediate
82 // rounding:
83 //   (x * y) / 2^31
84 // In practice, it is expected that FixedPointMul will be implemented
85 // using hardware "rounding doubling int32 multiply high" instructions,
86 // such as VQRDMULH on ARM. See in fixedpoint.h the generic function,
87 // SaturatingRoundingDoublingHighMul.
88 //
89 // Notice that the other difference from
90 // OutputStageQuantizeDownInt32ToUint8Scale is that the result offset
91 // is applied after the multiplier and shift, not before. This ensures
92 // that no matter what the multiplier and shift are, the result offset
93 // is effectively integral: offsetting the final result by an integer.
94 // The motivation for this is to faithfully support quantization schemes
95 // where the formula linking quantized values to the real mathematical
96 // values that they represent, is of the form
97 //
98 //   real_value = scale * (quantized_value - zero_point)
99 //
100 // where scale is a real number (represented in quantized form by
101 // result_fixedpoint_multiplier and result_shift) and zero_point
102 // is an integer telling which quantized value correspond to the
103 // real value 0, and is represented here by (the opposite of)
104 // result_offset_after_shift.
105 // The motivation for such a quantization scheme, designed to
106 // ensure that 0 is always a representable value, is that in
107 // many applications, we need to 0-pad arrays and that can only be
108 // done for quantized arrays if 0 is a representable value in
109 // quantized form. In particular, convolution-like operations
110 // are often implemented using 0-padding, or "im2col"-like
111 // expansions that implicitly rely on 0-padding. If 0 were not
112 // a representable value, such operations would have to pad
113 // using a nonzero value, introducing bias in the computation.
114 struct OutputStageQuantizeDownInt32ToUint8ScaleByFixedPoint {
115   std::int32_t result_fixedpoint_multiplier;
116   std::int32_t result_shift;
117   std::int32_t result_offset_after_shift;
118 };
119 
120 // This output stage takes int32 values that are expected to be already
121 // on the final uint8 scale, but not necessarily in the [0..255] range.
122 // It clamps them to the [0..255] range and returns them casted to uint8.
123 struct OutputStageSaturatingCastToUint8 {};
124 
125 // This output stage depends on a "bias vector" that should contain int32
126 // entries, and be either a row-vector of the same number of columns as the
127 // result matrix, or a column-vector of the same number of rows as the
128 // result matrix. This output stage takes int32 values and adds to them
129 // the corresponding entry of the bias vector (broadcasted in the other
130 // direction to fit the matrix's shape), outputting int32 values.
131 template <typename VectorType>
132 struct OutputStageBiasAddition {
133   VectorType bias_vector;
134 };
135 
136 // This output stage clamps value between the specified min and max bounds.
137 // It can be used to implement "rectified linear unit" activation functions
138 // in neural networks.
139 struct OutputStageClamp {
140   std::int32_t min;
141   std::int32_t max;
142 };
143 
144 struct OutputStageTanh {
145   std::int32_t real_zero_as_int32;
146   std::int32_t real_amplitude_as_int32;
147 };
148 
149 // An output pipeline is just a std::tuple of output stages.
150 // This function generates a standard output pipeline consisting of two stages:
151 // OutputStageQuantizeDownInt32ToUint8Scale, OutputStageSaturatingCastToUint8.
152 inline std::tuple<OutputStageQuantizeDownInt32ToUint8Scale,
153                   OutputStageSaturatingCastToUint8>
MakeStandardOutputPipeline(std::int32_t result_offset,std::int32_t result_mult_int,std::int32_t result_shift)154 MakeStandardOutputPipeline(std::int32_t result_offset,
155                            std::int32_t result_mult_int,
156                            std::int32_t result_shift) {
157   OutputStageQuantizeDownInt32ToUint8Scale quantize_down_stage;
158   quantize_down_stage.result_offset = result_offset;
159   quantize_down_stage.result_mult_int = result_mult_int;
160   quantize_down_stage.result_shift = result_shift;
161   OutputStageSaturatingCastToUint8 saturating_cast_stage;
162   return std::make_tuple(quantize_down_stage, saturating_cast_stage);
163 }
164 
165 // An output pipeline is just a std::tuple of output stages.
166 // This function generates a standard output pipeline consisting of two stages:
167 // OutputStageQuantizeDownInt32ToUint8ScalePC, OutputStageSaturatingCastToUint8.
168 template <VectorShape tShape>
169 inline std::tuple<OutputStageQuantizeDownInt32ToUint8ScalePC<tShape>,
170                   OutputStageSaturatingCastToUint8>
MakeStandardOutputPipeline(const VectorMap<const std::int32_t,tShape> & result_offset,const VectorMap<const std::int32_t,tShape> & result_mult_int,std::int32_t result_shift)171 MakeStandardOutputPipeline(
172     const VectorMap<const std::int32_t, tShape>& result_offset,
173     const VectorMap<const std::int32_t, tShape>& result_mult_int,
174     std::int32_t result_shift) {
175   OutputStageQuantizeDownInt32ToUint8ScalePC<tShape> quantize_down_stage;
176   quantize_down_stage.result_offset = result_offset;
177   quantize_down_stage.result_mult_int = result_mult_int;
178   quantize_down_stage.result_shift = result_shift;
179   OutputStageSaturatingCastToUint8 saturating_cast_stage;
180   return std::make_tuple(quantize_down_stage, saturating_cast_stage);
181 }
182 
183 }  // namespace gemmlowp
184 
185 #endif  // GEMMLOWP_PUBLIC_OUTPUT_STAGES_H_
186