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