forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfused_rowwise_8bit_conversion_ops.h
195 lines (165 loc) · 6.48 KB
/
fused_rowwise_8bit_conversion_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef CAFFE2_OPERATORS_FUSED_ROWWISE_8BIT_CONVERSION_OPS_H_
#define CAFFE2_OPERATORS_FUSED_ROWWISE_8BIT_CONVERSION_OPS_H_
#include "caffe2/core/context.h"
#include "caffe2/core/export_caffe2_op_to_c10.h"
#include <c10/util/irange.h>
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/operators/reducer_functors.h"
#include "caffe2/perfkernels/fused_nbit_rowwise_conversion.h"
#include "caffe2/utils/math.h"
C10_DECLARE_EXPORT_CAFFE2_OP_TO_C10(Fused8BitRowwiseQuantizedToFloat);
namespace caffe2 {
#define IS_LITTLE_ENDIAN \
[] { \
const int32_t kValue = 1; \
return reinterpret_cast<const std::uint8_t*>(&kValue)[0] == 1; \
}()
template <
typename T,
typename Tsb, // Type for Scale and Bias
void (*convert)(float* dst, const T* src, size_t N),
bool HAS_CONVERT,
class Context>
class FloatToFused8BitRowwiseQuantizedOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_SIMPLE_CTOR_DTOR(FloatToFused8BitRowwiseQuantizedOp)
bool RunOnDevice() override {
CAFFE_ENFORCE(IS_LITTLE_ENDIAN, "Unsupported endianness");
const auto& input = Input(DATA_FLOAT);
CAFFE_ENFORCE_GT(input.dim(), 0, "Input's dimension must be at least 1");
const auto input_rows = input.size_to_dim(input.dim() - 1);
const auto input_columns = input.size(input.dim() - 1);
// The "fused" representation stores the scale and bias with the row-wise
// quantized data in one tensor. Since we quantize with 8 bits (1 byte) and
// represent the scale and bias with 32-bit floats, we'll use the last 8
// bytes of each row for scale (4 bytes) and bias (4 bytes).
// | ... int8 data ... | scale | bias |
// | number_of_columns | sizeof(Tsb)| sizeof(Tsb)|
auto output_dimensions = input.sizes().vec();
output_dimensions[input.dim() - 1] =
input_columns + 2 * static_cast<std::int64_t>(sizeof(Tsb));
auto* output = Output(
DATA_FUSED_SCALE_BIAS_INT8,
output_dimensions,
at::dtype<std::uint8_t>());
const auto* input_data = input.template data<T>();
auto* output_data = output->template mutable_data<std::uint8_t>();
const auto output_columns = output->size(output->dim() - 1);
bool is_float = std::is_same<T, float>::value;
bool out_sb_half = std::is_same<Tsb, at::Half>::value;
if (!HAS_CONVERT) {
CAFFE_ENFORCE(is_float, "convert can be nullptr only if T is float");
if (out_sb_half) {
FloatToFusedNBitRowwiseQuantizedSBHalf(
8,
reinterpret_cast<const float*>(input_data),
input_rows,
input_columns,
output_data);
} else {
FloatToFused8BitRowwiseQuantized(
reinterpret_cast<const float*>(input_data),
input_rows,
input_columns,
output_data);
}
} else {
bool is_half = std::is_same<T, at::Half>::value;
CAFFE_ENFORCE(is_half);
vector<float> tmp(input_columns);
// NOLINTNEXTLINE(clang-diagnostic-sign-compare)
for (const auto row : c10::irange(input_rows)) {
convert(tmp.data(), input_data + row * input_columns, input_columns);
if (out_sb_half) {
FloatToFusedNBitRowwiseQuantizedSBHalf(
8,
tmp.data(),
1,
input_columns,
output_data + row * output_columns);
} else {
FloatToFused8BitRowwiseQuantized(
tmp.data(), 1, input_columns, output_data + row * output_columns);
}
}
}
return true;
}
private:
INPUT_TAGS(DATA_FLOAT);
OUTPUT_TAGS(DATA_FUSED_SCALE_BIAS_INT8);
};
template <
typename T,
typename Tsb,
void (*convert)(T* dst, const float* src, size_t N),
bool HAS_CONVERT,
class Context>
class Fused8BitRowwiseQuantizedToFloatOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_SIMPLE_CTOR_DTOR(Fused8BitRowwiseQuantizedToFloatOp)
bool RunOnDevice() override {
CAFFE_ENFORCE(IS_LITTLE_ENDIAN, "Unsupported endianness");
const auto& input = Input(DATA_FUSED_SCALE_BIAS_INT8);
CAFFE_ENFORCE_GT(input.dim(), 0, "Input's dimension must be at least 1");
const auto input_rows = input.size_to_dim(input.dim() - 1);
const auto input_columns = input.size(input.dim() - 1);
// The last 2*sizeof(Tsb) bytes per row are the scale and the bias.
// The rest of input_columns is the number of values in the original row.
auto output_dimensions = input.sizes().vec();
output_dimensions[input.dim() - 1] =
input_columns - 2 * static_cast<std::int64_t>(sizeof(Tsb));
auto* output = Output(DATA_FLOAT, output_dimensions, at::dtype<T>());
const auto output_columns = output->size(output->dim() - 1);
const auto* input_data = input.template data<std::uint8_t>();
T* output_data = output->template mutable_data<T>();
bool is_float = std::is_same<T, float>::value;
bool in_sb_half = std::is_same<Tsb, at::Half>::value;
if (!HAS_CONVERT) {
CAFFE_ENFORCE(is_float, "convert can be nullptr only if T is float");
if (in_sb_half) {
FusedNBitRowwiseQuantizedSBHalfToFloat(
8,
input_data,
input_rows,
input_columns,
reinterpret_cast<float*>(output_data));
} else {
Fused8BitRowwiseQuantizedToFloat(
input_data,
input_rows,
input_columns,
reinterpret_cast<float*>(output_data));
}
} else {
bool is_half = std::is_same<T, at::Half>::value;
CAFFE_ENFORCE(is_half);
vector<float> tmp(input_columns);
// NOLINTNEXTLINE(clang-diagnostic-sign-compare)
for (const auto row : c10::irange(input_rows)) {
if (in_sb_half) {
FusedNBitRowwiseQuantizedSBHalfToFloat(
8,
input_data + row * input_columns,
1,
input_columns,
tmp.data());
} else {
Fused8BitRowwiseQuantizedToFloat(
input_data + row * input_columns, 1, input_columns, tmp.data());
}
convert(output_data + row * output_columns, tmp.data(), output_columns);
}
}
return true;
}
private:
INPUT_TAGS(DATA_FUSED_SCALE_BIAS_INT8);
OUTPUT_TAGS(DATA_FLOAT);
};
#undef IS_LITTLE_ENDIAN
} // namespace caffe2
#endif // CAFFE2_OPERATORS_FUSED_ROWWISE_8BIT_CONVERSION_OPS_H_