-
Notifications
You must be signed in to change notification settings - Fork 4
/
scalar.go
291 lines (265 loc) · 6.5 KB
/
scalar.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package gguf_parser
import (
"errors"
"strconv"
"strings"
)
const (
_Ki = 1 << ((iota + 1) * 10)
_Mi
_Gi
_Ti
_Pi
)
const (
_K = 1e3
_M = 1e6
_G = 1e9
_T = 1e12
_P = 1e15
)
const (
_Thousand = 1e3
_Million = 1e6
_Billion = 1e9
_Trillion = 1e12
_Quadrillion = 1e15
)
type (
// SizeScalar is the scalar for size.
SizeScalar uint64
// FLOPSScalar is the scalar for FLOPS.
FLOPSScalar uint64
// BytesPerSecondScalar is the scalar for bytes per second (Bps).
BytesPerSecondScalar uint64
)
var (
// _GeneralBaseUnitMatrix is the base unit matrix for bytes.
_GeneralBaseUnitMatrix = []struct {
Base float64
Unit string
}{
{_Pi, "Pi"},
{_P, "P"},
{_Ti, "Ti"},
{_T, "T"},
{_Gi, "Gi"},
{_G, "G"},
{_Mi, "Mi"},
{_M, "M"},
{_Ki, "Ki"},
{_K, "K"},
}
// _SizeBaseUnitMatrix is the base unit matrix for size.
_SizeBaseUnitMatrix = []struct {
Base float64
Unit string
}{
{_Pi, "P"},
{_Ti, "T"},
{_Gi, "G"},
{_Mi, "M"},
{_Ki, "K"},
}
// _NumberBaseUnitMatrix is the base unit matrix for numbers.
_NumberBaseUnitMatrix = []struct {
Base float64
Unit string
}{
{_Quadrillion, "Q"},
{_Trillion, "T"},
{_Billion, "B"},
{_Million, "M"},
{_Thousand, "K"},
}
)
// ParseSizeScalar parses the SizeScalar from the string.
func ParseSizeScalar(s string) (_ SizeScalar, err error) {
if s == "" {
return 0, errors.New("invalid SizeScalar")
}
b := float64(1)
for i := range _SizeBaseUnitMatrix {
if strings.HasSuffix(s, _SizeBaseUnitMatrix[i].Unit) {
b = _SizeBaseUnitMatrix[i].Base
s = strings.TrimSuffix(s, _SizeBaseUnitMatrix[i].Unit)
break
}
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return 0, err
}
return SizeScalar(f * b), nil
}
func (s SizeScalar) String() string {
if s == 0 {
return "0"
}
b, u := float64(1), ""
for i := range _SizeBaseUnitMatrix {
if float64(s) >= _SizeBaseUnitMatrix[i].Base {
b = _SizeBaseUnitMatrix[i].Base
u = _SizeBaseUnitMatrix[i].Unit
break
}
}
f := strconv.FormatFloat(float64(s)/b, 'f', 2, 64)
return strings.TrimSuffix(f, ".00") + " " + u
}
// ParseFLOPSScalar parses the FLOPSScalar from the string.
func ParseFLOPSScalar(s string) (_ FLOPSScalar, err error) {
if s == "" {
return 0, errors.New("invalid FLOPSScalar")
}
s = strings.TrimSuffix(s, "FLOPS")
b := float64(1)
for i := range _GeneralBaseUnitMatrix {
if strings.HasSuffix(s, _GeneralBaseUnitMatrix[i].Unit) {
b = _GeneralBaseUnitMatrix[i].Base
s = strings.TrimSuffix(s, _GeneralBaseUnitMatrix[i].Unit)
break
}
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return 0, err
}
return FLOPSScalar(f * b), nil
}
func (s FLOPSScalar) String() string {
if s == 0 {
return "0 FLOPS"
}
b, u := float64(1), ""
for i := range _GeneralBaseUnitMatrix {
if float64(s) >= _GeneralBaseUnitMatrix[i].Base {
b = _GeneralBaseUnitMatrix[i].Base
u = _GeneralBaseUnitMatrix[i].Unit
break
}
}
f := strconv.FormatFloat(float64(s)/b, 'f', 2, 64)
return strings.TrimSuffix(f, ".00") + " " + u + "FLOPS"
}
// ParseBytesPerSecondScalar parses the BytesPerSecondScalar from the string.
func ParseBytesPerSecondScalar(s string) (_ BytesPerSecondScalar, err error) {
if s == "" {
return 0, errors.New("invalid BytesPerSecondScalar")
}
b := float64(1)
o := float64(1)
switch {
case strings.HasSuffix(s, "Bps") || strings.HasSuffix(s, "B/s"):
s = strings.TrimSuffix(strings.TrimSuffix(s, "Bps"), "B/s")
case strings.HasSuffix(s, "bps") || strings.HasSuffix(s, "b/s"):
s = strings.TrimSuffix(strings.TrimSuffix(s, "bps"), "b/s")
o = 8
}
for i := range _GeneralBaseUnitMatrix {
if strings.HasSuffix(s, _GeneralBaseUnitMatrix[i].Unit) {
b = _GeneralBaseUnitMatrix[i].Base
s = strings.TrimSuffix(s, _GeneralBaseUnitMatrix[i].Unit)
break
}
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return 0, err
}
return BytesPerSecondScalar(f * b / o), nil
}
func (s BytesPerSecondScalar) String() string {
if s == 0 {
return "0 Bps"
}
b, u := float64(1), ""
for i := range _GeneralBaseUnitMatrix {
if float64(s) >= _GeneralBaseUnitMatrix[i].Base {
b = _GeneralBaseUnitMatrix[i].Base
u = _GeneralBaseUnitMatrix[i].Unit
break
}
}
f := strconv.FormatFloat(float64(s)/b, 'f', 2, 64)
return strings.TrimSuffix(f, ".00") + " " + u + "Bps"
}
type (
// GGUFBytesScalar is the scalar for bytes.
GGUFBytesScalar uint64
// GGUFParametersScalar is the scalar for parameters.
GGUFParametersScalar uint64
// GGUFBitsPerWeightScalar is the scalar for bits per weight.
GGUFBitsPerWeightScalar float64
// GGUFTokensPerSecondScalar is the scalar for tokens per second.
GGUFTokensPerSecondScalar float64
)
// ParseGGUFBytesScalar parses the GGUFBytesScalar from the string.
func ParseGGUFBytesScalar(s string) (_ GGUFBytesScalar, err error) {
if s == "" {
return 0, errors.New("invalid GGUFBytesScalar")
}
s = strings.TrimSuffix(s, "B")
b := float64(1)
for i := range _GeneralBaseUnitMatrix {
if strings.HasSuffix(s, _GeneralBaseUnitMatrix[i].Unit) {
b = _GeneralBaseUnitMatrix[i].Base
s = strings.TrimSuffix(s, _GeneralBaseUnitMatrix[i].Unit)
break
}
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return 0, err
}
return GGUFBytesScalar(f * b), nil
}
// GGUFBytesScalarStringInMiBytes is the flag to show the GGUFBytesScalar string in MiB.
var GGUFBytesScalarStringInMiBytes bool
func (s GGUFBytesScalar) String() string {
if s == 0 {
return "0 B"
}
b, u := float64(1), ""
if GGUFBytesScalarStringInMiBytes {
b = _Mi
u = "Mi"
} else {
for i := range _GeneralBaseUnitMatrix {
if float64(s) >= _GeneralBaseUnitMatrix[i].Base {
b = _GeneralBaseUnitMatrix[i].Base
u = _GeneralBaseUnitMatrix[i].Unit
break
}
}
}
f := strconv.FormatFloat(float64(s)/b, 'f', 2, 64)
return strings.TrimSuffix(f, ".00") + " " + u + "B"
}
func (s GGUFParametersScalar) String() string {
if s == 0 {
return "0"
}
b, u := float64(1), ""
for i := range _NumberBaseUnitMatrix {
if float64(s) >= _NumberBaseUnitMatrix[i].Base {
b = _NumberBaseUnitMatrix[i].Base
u = _NumberBaseUnitMatrix[i].Unit
break
}
}
f := strconv.FormatFloat(float64(s)/b, 'f', 2, 64)
return strings.TrimSuffix(f, ".00") + " " + u
}
func (s GGUFBitsPerWeightScalar) String() string {
if s <= 0 {
return "0 bpw"
}
return strconv.FormatFloat(float64(s), 'f', 2, 64) + " bpw"
}
func (s GGUFTokensPerSecondScalar) String() string {
if s <= 0 {
return "0 tps"
}
return strconv.FormatFloat(float64(s), 'f', 2, 64) + " tps"
}