-
Notifications
You must be signed in to change notification settings - Fork 104
/
quantizedfloat.go
247 lines (201 loc) · 5.59 KB
/
quantizedfloat.go
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// decoding the quantized float is a bit to messy to do in one function, so
// all the decoder steps are located in this file
package manta
import (
"math"
)
// Quantized float flags
const qff_rounddown uint32 = (1 << 0)
const qff_roundup uint32 = (1 << 1)
const qff_encode_zero uint32 = (1 << 2)
const qff_encode_integers uint32 = (1 << 3)
// Quantized-decoder struct containing the computed properties
type quantizedFloatDecoder struct {
Low float32 // Gets recomputed for round up / down
High float32
HighLowMul float32
DecMul float32
Offset float32
Bitcount uint32 // Gets recomputed for qff_encode_int
Flags uint32
NoScale bool // Whether to decodes this as a noscale
}
// Validates / recomputes decoder flags
func (qfd *quantizedFloatDecoder) validateFlags() {
// Check that we have some flags set
if qfd.Flags == 0 {
return
}
// Discard zero flag when encoding min / max set to 0
if (qfd.Low == 0.0 && (qfd.Flags&qff_rounddown) != 0) || (qfd.High == 0.0 && (qfd.Flags&qff_roundup) != 0) {
qfd.Flags &= ^qff_encode_zero
}
// If min / max is zero when encoding zero, switch to round up / round down instead
if qfd.Low == 0.0 && (qfd.Flags&qff_encode_zero) != 0 {
qfd.Flags |= qff_rounddown
qfd.Flags &= ^qff_encode_zero
}
if qfd.High == 0.0 && (qfd.Flags&qff_encode_zero) != 0 {
qfd.Flags |= qff_roundup
qfd.Flags &= ^qff_encode_zero
}
// Check if the range spans zero
if qfd.Low > 0.0 || qfd.High < 0.0 {
qfd.Flags &= ^qff_encode_zero
}
// If we are left with encode zero, only leave integer flag
if (qfd.Flags & qff_encode_integers) != 0 {
qfd.Flags &= ^(qff_roundup | qff_rounddown | qff_encode_zero)
}
// Verify that we don;t have roundup / rounddown set
if qfd.Flags&(qff_rounddown|qff_roundup) == (qff_rounddown | qff_roundup) {
_panicf("Roundup / Rounddown are mutually exclusive")
}
}
// Assign multipliers
func (qfd *quantizedFloatDecoder) assignMultipliers(steps uint32) {
qfd.HighLowMul = 0.0
Range := qfd.High - qfd.Low
High := uint32(0)
if qfd.Bitcount == 32 {
High = 0xFFFFFFFE
} else {
High = (1 << qfd.Bitcount) - 1
}
HighMul := float32(0.0)
if math.Abs(float64(Range)) <= 0.0 {
HighMul = float32(High)
} else {
HighMul = float32(High) / Range
}
// Adjust precision
if (HighMul*Range > float32(High)) || (float64(HighMul*Range) > float64(High)) {
multipliers := []float32{0.9999, 0.99, 0.9, 0.8, 0.7}
for _, mult := range multipliers {
HighMul = float32(High) / Range * mult
if (HighMul*Range > float32(High)) || (float64(HighMul*Range) > float64(High)) {
continue
}
break
}
}
qfd.HighLowMul = HighMul
qfd.DecMul = 1.0 / float32(steps-1)
if qfd.HighLowMul == 0.0 {
_panicf("Error computing high / low multiplier")
}
}
// Quantize a float
func (qfd *quantizedFloatDecoder) quantize(val float32) float32 {
if val < qfd.Low {
if (qfd.Flags & qff_roundup) == 0 {
_panicf("Field tried to quantize an out of range value")
}
return qfd.Low
} else if val > qfd.High {
if (qfd.Flags & qff_rounddown) == 0 {
_panicf("Field tried to quantize an out of range value")
}
return qfd.High
}
i := uint32((val - qfd.Low) * qfd.HighLowMul)
return qfd.Low + (qfd.High-qfd.Low)*(float32(i)*qfd.DecMul)
}
// Actual float decoding
func (qfd *quantizedFloatDecoder) decode(r *reader) float32 {
if (qfd.Flags&qff_rounddown) != 0 && r.readBoolean() {
return qfd.Low
}
if (qfd.Flags&qff_roundup) != 0 && r.readBoolean() {
return qfd.High
}
if (qfd.Flags&qff_encode_zero) != 0 && r.readBoolean() {
return 0.0
}
return qfd.Low + (qfd.High-qfd.Low)*float32(r.readBits(qfd.Bitcount))*qfd.DecMul
}
// Creates a new quantized float decoder based on given field
func newQuantizedFloatDecoder(bitCount, flags *int32, lowValue, highValue *float32) *quantizedFloatDecoder {
qfd := &quantizedFloatDecoder{}
// Set common properties
if *bitCount == 0 || *bitCount >= 32 {
qfd.NoScale = true
qfd.Bitcount = 32
return qfd
} else {
qfd.NoScale = false
qfd.Bitcount = uint32(*bitCount)
qfd.Offset = 0.0
if lowValue != nil {
qfd.Low = *lowValue
} else {
qfd.Low = 0.0
}
if highValue != nil {
qfd.High = *highValue
} else {
qfd.High = 1.0
}
}
if flags != nil {
qfd.Flags = uint32(*flags)
} else {
qfd.Flags = 0
}
// Validate flags
qfd.validateFlags()
// Handle Round Up, Round Down
steps := (1 << uint(qfd.Bitcount))
Range := float32(0)
if (qfd.Flags & qff_rounddown) != 0 {
Range = qfd.High - qfd.Low
qfd.Offset = (Range / float32(steps))
qfd.High -= qfd.Offset
} else if (qfd.Flags & qff_roundup) != 0 {
Range = qfd.High - qfd.Low
qfd.Offset = (Range / float32(steps))
qfd.Low += qfd.Offset
}
// Handle integer encoding flag
if (qfd.Flags & qff_encode_integers) != 0 {
delta := qfd.High - qfd.Low
if delta < 1 {
delta = 1
}
deltaLog2 := math.Ceil(math.Log2(float64(delta)))
Range2 := (1 << uint(deltaLog2))
bc := qfd.Bitcount
for 1 == 1 {
if (1 << uint(bc)) > Range2 {
break
} else {
bc++
}
}
if bc > qfd.Bitcount {
qfd.Bitcount = bc
steps = (1 << uint(qfd.Bitcount))
}
qfd.Offset = float32(Range2) / float32(steps)
qfd.High = qfd.Low + float32(Range2) - qfd.Offset
}
// Assign multipliers
qfd.assignMultipliers(uint32(steps))
// Remove unessecary flags
if (qfd.Flags & qff_rounddown) != 0 {
if qfd.quantize(qfd.Low) == qfd.Low {
qfd.Flags &= ^qff_rounddown
}
}
if (qfd.Flags & qff_roundup) != 0 {
if qfd.quantize(qfd.High) == qfd.High {
qfd.Flags &= ^qff_roundup
}
}
if (qfd.Flags & qff_encode_zero) != 0 {
if qfd.quantize(0.0) == 0.0 {
qfd.Flags &= ^qff_encode_zero
}
}
return qfd
}