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