Caffe2 - C++ API
A deep learning, cross platform ML framework
lengths_reducer_rowwise_8bit_ops.cc
1 #include "caffe2/operators/lengths_reducer_rowwise_8bit_ops.h"
2 #include "caffe2/core/registry.h"
3 
4 namespace caffe2 {
5 
6 REGISTER_CPU_OPERATOR(
7  Rowwise8BitQuantizedToFloat,
8  Rowwise8BitQuantizedToFloatOp<CPUContext>);
9 REGISTER_CPU_OPERATOR(
10  FloatToRowwiseQuantized8Bits,
11  FloatToRowwiseQuantized8BitsOp<CPUContext>);
12 
13 REGISTER_CPU_OPERATOR(
14  SparseLengthsSum8BitsRowwise,
15  SparseLengths8BitsRowwiseOp<CPUContext>);
16 
17 REGISTER_CPU_OPERATOR(
18  SparseLengthsWeightedSum8BitsRowwise,
19  SparseLengths8BitsRowwiseOp<CPUContext, 1>);
20 
21 REGISTER_CPU_OPERATOR(
22  SparseLengthsMean8BitsRowwise,
23  SparseLengths8BitsRowwiseOp<CPUContext, 0, 1>);
24 
25 REGISTER_CPU_OPERATOR(
26  SparseLengthsWeightedMean8BitsRowwise,
27  SparseLengths8BitsRowwiseOp<CPUContext, 1, 1>);
28 
29 OPERATOR_SCHEMA(SparseLengthsSum8BitsRowwise)
30  .NumInputs(4)
31  .NumOutputs(1)
32  .SetDoc(R"DOC(
33 Variation of SparseLengthsSum operator, where DATA is
34 stored using 8bits. DATA was quantized with 8Bit row-wise
35 quantization (see doc to FloatToRowwiseQuantized8Bits operator). To
36 restore DATA from 8Bit, we use additional input that stores scales
37 and biases.
38 )DOC")
39  .Input(
40  0,
41  "DATA",
42  "uint8 tensor obtained with "
43  "operator FloatToRowwiseQuantized8Bits")
44  .Input(
45  1,
46  "INDICES",
47  "Integer vector containing indices of the first "
48  "dimension of DATA for the slices that are being aggregated")
49  .Input(
50  2,
51  "LENGTHS",
52  "Vector with the same sum of elements as the first dimension of DATA")
53  .Input(
54  3,
55  "scale_bias",
56  "Matrix of floats, each row r_i of which stores a pair "
57  "s_i, b_i -- scale and bias for i-th row")
58 
59  .Output(0, "output", "output");
60 
61 OPERATOR_SCHEMA(SparseLengthsWeightedSum8BitsRowwise)
62  .NumInputs(5)
63  .NumOutputs(1)
64  .SetDoc(R"DOC(
65 Variation of SparseLengthsWeightedSum operator, where
66 DATA is stored using 8bits. DATA was quantized with 8Bit row-wise
67 quantization (see doc to FloatToRowwiseQuantized8Bits operator). To
68 restore DATA from 8Bit, we use additional input that stores scales
69 and biases.
70 )DOC")
71  .Input(
72  0,
73  "DATA",
74  "uint8 tensor obtained with "
75  "operator FloatToRowwiseQuantized8Bits")
76  .Input(
77  1,
78  "SCALARS",
79  "Scalar multipliers for the input slices. Must "
80  "be a vector with the length matching the length of INDICES")
81  .Input(
82  2,
83  "INDICES",
84  "Integer vector containing indices of the first "
85  "dimension of DATA for the slices that are being aggregated")
86  .Input(
87  3,
88  "LENGTHS",
89  "Vector with the same sum of elements as the first dimension of DATA")
90  .Input(
91  4,
92  "scale_bias",
93  "Matrix of floats, each row r_i of which stores a pair "
94  "s_i, b_i -- scale and bias for i-th row")
95  .Output(0, "output", "output");
96 
97 OPERATOR_SCHEMA(SparseLengthsMean8BitsRowwise)
98  .NumInputs(4)
99  .NumOutputs(1)
100  .SetDoc(R"DOC(
101 Variation of SparseLengthsMean operator, where DATA is
102 stored using 8bits. DATA was quantized with 8Bit row-wise
103 quantization (see doc to FloatToRowwiseQuantized8Bits operator). To
104 restore DATA from 8Bit, we use additional input that stores scales
105 and biases.
106 )DOC")
107  .Input(
108  0,
109  "DATA",
110  "uint8 tensor obtained with "
111  "operator FloatToRowwiseQuantized8Bits")
112  .Input(
113  1,
114  "INDICES",
115  "Integer vector containing indices of the first "
116  "dimension of DATA for the slices that are being aggregated")
117  .Input(
118  2,
119  "LENGTHS",
120  "Vector with the same sum of elements as the first dimension of DATA")
121  .Input(
122  3,
123  "scale_bias",
124  "Matrix of floats, each row r_i of which stores a pair "
125  "s_i, b_i -- scale and bias for i-th row")
126 
127  .Output(0, "output", "output");
128 
129 OPERATOR_SCHEMA(SparseLengthsWeightedMean8BitsRowwise)
130  .NumInputs(5)
131  .NumOutputs(1)
132  .SetDoc(R"DOC(
133 Variation of SparseLengthsWeightedMean operator, where
134 DATA is stored using 8bits. DATA was quantized with 8Bit row-wise
135 quantization (see doc to FloatToRowwiseQuantized8Bits operator). To
136 restore DATA from 8Bit, we use additional input that stores scales
137 and biases.
138 )DOC")
139  .Input(
140  0,
141  "DATA",
142  "uint8 tensor obtained with "
143  "operator FloatToRowwiseQuantized8Bits")
144  .Input(
145  1,
146  "SCALARS",
147  "Scalar multipliers for the input slices. Must "
148  "be a vector with the length matching the length of INDICES")
149  .Input(
150  2,
151  "INDICES",
152  "Integer vector containing indices of the first "
153  "dimension of DATA for the slices that are being aggregated")
154  .Input(
155  3,
156  "LENGTHS",
157  "Vector with the same sum of elements as the first dimension of DATA")
158  .Input(
159  4,
160  "scale_bias",
161  "Matrix of floats, each row r_i of which stores a pair "
162  "s_i, b_i -- scale and bias for i-th row")
163  .Output(0, "output", "output");
164 
165 OPERATOR_SCHEMA(FloatToRowwiseQuantized8Bits)
166  .NumInputs(1)
167  .NumOutputs(2)
168  .SetDoc(R"DOC(
169 This operator applies 8Bit row-wise quantization to
170 input tensor and returns quantized tensor. Row wise quantization of
171 input tensor is the following process. We take tensor of size
172 (m_1, m_2,...,m_n), n >= 2, reshape it into matrix of size
173 (m_1, m_2 x... x m_n) and apply row-wise quantization. After this,
174 we compute scale_i= (min_i - max_i) / 255 and bias_i = min_i for
175 i-th row r_i of reshaped matrix, where min_i and max_i -- minimum
176 and maximum elements of i-th row, and quantize each element r_{ij} as
177 0 <= round(r_ij - bias_i) / scale_i) < 256. Instead of input tensor
178 we obtain uint8 tensor and auxiliary information as scale and bias to
179 restore input tensor (with losses).
180 )DOC")
181  .Input(0, "input", "input")
182  .Output(0, "quantized_input", "quantized_input")
183  .Output(
184  1,
185  "scale_bias",
186  "Matrix of floats, each row r_i of which stores a pair "
187  "s_i, b_i");
188 
189 OPERATOR_SCHEMA(Rowwise8BitQuantizedToFloat)
190  .NumInputs(2)
191  .NumOutputs(1)
192  .SetDoc(R"DOC(
193 Given uint8 tensor, quantized using 8bit row-wise
194 quantization, and auxiliary scales and biases, this operator
195 restores float tensor in the following way. We take input 8bits tensor
196 of size (m_1, m_2, ..., m_n), n >= 2, reshape it into matrix of size
197 (m_1, m_2 x... x m_n). We compute element r_{ij} of output matrix as
198 r_{ij} * s_i + b_i and after this we reshape this output matrix into
199 output tensor of size (m_1, m_2, ..., m_n).
200 )DOC")
201  .Input(0, "quantized_input", "quantized_input")
202  .Input(
203  1,
204  "scale_bias",
205  "Matrix of floats, each row r_i of which stores a pair "
206  "s_i, b_i -- scale and bias for i-th row")
207  .Output(1, "output", "output");
208 
209 NO_GRADIENT(Rowwise8BitQuantizedToFloat);
210 NO_GRADIENT(FloatToRowwiseQuantized8Bits);
211 NO_GRADIENT(SparseLengthsSum8BitsRowwise);
212 NO_GRADIENT(SparseLengthsWeightedSum8BitsRowwise);
213 NO_GRADIENT(SparseLengthsMean8BitsRowwise);
214 NO_GRADIENT(SparseLengthsWeightedMean8BitsRowwise);
215 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...