-
Notifications
You must be signed in to change notification settings - Fork 44
/
dtype.go
423 lines (377 loc) · 9.79 KB
/
dtype.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package gotch
import (
"fmt"
"log"
"reflect"
)
// CInt is equal to C type int. Go type is int32
type CInt = int32
// DType represents different kind of element that a tensor can hold.
// Ref. https://github.com/pytorch/pytorch/blob/a290cbf32b0c282aa60fa521ca5c6cd19c7f779f/c10/core/ScalarType.h
type DType int
const (
Invalid DType = -1
Uint8 DType = 0
Int8 DType = 1
Int16 DType = 2
Int DType = 3
Int64 DType = 4
Half DType = 5
Float DType = 6
Double DType = 7
ComplexHalf DType = 8
ComplexFloat DType = 9
ComplexDouble DType = 10
Bool DType = 11
QInt8 DType = 12
QUInt8 DType = 13
QInt32 DType = 14
BFloat16 DType = 15
// ---not implemented ---
QUInt4x2 DType = 16
QUInt2x4 DType = 17
Bits1x8 DType = 18
Bits2x4 DType = 19
Bits4x2 DType = 20
Bits8 DType = 21
Bits16 DType = 22
)
var dtype2CKind = map[DType]CInt{
Uint8: 0,
Int8: 1,
Int16: 2,
Int: 3,
Int64: 4,
Half: 5,
Float: 6,
Double: 7,
ComplexHalf: 8,
ComplexFloat: 9,
ComplexDouble: 10,
Bool: 11,
QInt8: 12,
QUInt8: 13,
QInt32: 14,
BFloat16: 15,
// ---not implemented ---
QUInt4x2: 16,
QUInt2x4: 17,
Bits1x8: 18,
Bits2x4: 19,
Bits4x2: 20,
Bits8: 21,
Bits16: 22,
}
func (dt DType) CKind() CInt {
if cint, ok := dtype2CKind[dt]; ok {
return cint
}
if Debug {
log.Printf("WARNING: dt.CKind() failed: no corresponding CKind to this DType %v\n", dt)
}
return -1 // invalid
}
// Back compat
func (dt DType) CInt() CInt {
return dt.CKind()
}
var ckind2DType map[CInt]DType = map[CInt]DType{
0: Uint8,
1: Int8,
2: Int16,
3: Int,
4: Int64,
5: Half,
6: Float,
7: Double,
8: ComplexHalf,
9: ComplexFloat,
10: ComplexDouble,
11: Bool,
12: QInt8,
13: QUInt8,
14: QInt32,
15: BFloat16,
// ---not implemented ---
16: QUInt4x2,
17: QUInt2x4,
18: Bits1x8,
19: Bits2x4,
20: Bits4x2,
21: Bits8,
22: Bits16,
}
func CKind2DType(ckind int32) DType {
if dtype, ok := ckind2DType[ckind]; ok {
return dtype
}
if Debug {
log.Printf("WARNING: CKind2DType() failed: no corresponding DType to input CInt %v\n", ckind)
}
return -1 // invalid
}
var dtypeSize map[DType]uint = map[DType]uint{
Uint8: 1,
Int8: 1,
Int16: 2,
Int: 4,
Int64: 8,
Half: 2,
Float: 4,
Double: 8,
ComplexHalf: 4,
ComplexFloat: 8,
ComplexDouble: 16,
Bool: 1,
QInt8: 1,
QUInt8: 1,
QInt32: 4,
BFloat16: 2,
QUInt4x2: 2,
QUInt2x4: 1,
// ---not implemented ---
Bits1x8: 1,
Bits2x4: 1,
Bits4x2: 1,
Bits8: 1,
Bits16: 2,
}
// Size returns dtype size in Bytes.
func (dt DType) Size() uint {
return dtypeSize[dt]
}
type DTypeDevice struct {
DType DType
Device Device
}
var (
FloatCPU DTypeDevice = DTypeDevice{Float, CPU}
DoubleCPU DTypeDevice = DTypeDevice{Double, CPU}
Int64CPU DTypeDevice = DTypeDevice{Int64, CPU}
FloatCUDA DTypeDevice = DTypeDevice{Float, CudaBuilder(0)}
DoubleCUDA DTypeDevice = DTypeDevice{Double, CudaBuilder(0)}
Int64CUDA DTypeDevice = DTypeDevice{Int64, CudaBuilder(0)}
)
var dtype2GoKind map[DType]reflect.Kind = map[DType]reflect.Kind{
Uint8: reflect.Uint8,
Int8: reflect.Int8,
Int16: reflect.Int16,
Int: reflect.Int32,
Int64: reflect.Int64,
Half: reflect.Uint16, // <- just uint16
Float: reflect.Float32,
Double: reflect.Float64,
ComplexHalf: reflect.Invalid, // no equivalent in Go. Would it be reflect.Float64?
ComplexFloat: reflect.Complex64,
ComplexDouble: reflect.Complex128,
Bool: reflect.Bool,
QInt8: reflect.Int8,
QUInt8: reflect.Uint8,
QInt32: reflect.Int32,
BFloat16: reflect.Uint16, // <- just uint16
// ---not implemented ---
QUInt4x2: reflect.Invalid,
QUInt2x4: reflect.Invalid,
Bits1x8: reflect.Invalid,
Bits2x4: reflect.Invalid,
Bits4x2: reflect.Invalid,
Bits8: reflect.Invalid,
Bits16: reflect.Invalid,
}
func (dt DType) GoKind() reflect.Kind {
if kind, ok := dtype2GoKind[dt]; ok && kind != reflect.Invalid {
return kind
}
if Debug {
log.Printf("WARNING: DType.GoKind() failed: no corresponding Go reflect.Kind to given DType %v\n", dt)
}
return reflect.Invalid
}
const (
// NOTE. reflect.Kind 0-26
QUInt8Kind reflect.Kind = 27
QInt8Kind reflect.Kind = 28
QInt32Kind reflect.Kind = 29
Float16Kind reflect.Kind = 30
BFloat16Kind reflect.Kind = 31
QUInt4x2Kind reflect.Kind = 32
QUInt2x4Kind reflect.Kind = 33
Bits1x8Kind reflect.Kind = 34
Bits2x4Kind reflect.Kind = 35
Bits4x2Kind reflect.Kind = 36
Bits8Kind reflect.Kind = 37
Bits16Kind reflect.Kind = 38
ComplexHalfKind reflect.Kind = 39
)
var goKind2DType map[reflect.Kind]DType = map[reflect.Kind]DType{
reflect.Uint8: Uint8,
reflect.Int8: Int8,
reflect.Int16: Int16,
reflect.Int32: Int,
reflect.Int64: Int64,
reflect.Float32: Float,
reflect.Float64: Double,
reflect.Complex64: ComplexFloat,
reflect.Complex128: ComplexDouble,
reflect.Bool: Bool,
reflect.Uint16: Half,
// Added Kinds
QUInt8Kind: QUInt8,
QInt8Kind: QInt8,
QInt32Kind: QInt32,
// Float16Kind: Half,
BFloat16Kind: BFloat16,
QUInt4x2Kind: QUInt4x2,
QUInt2x4Kind: QUInt2x4,
Bits1x8Kind: Bits1x8,
Bits2x4Kind: Bits2x4,
Bits4x2Kind: Bits4x2,
Bits8Kind: Bits8,
Bits16Kind: Bits16,
ComplexHalfKind: ComplexHalf,
}
type DTypeOptions struct {
HalfDTypePref DType
Quantized bool
}
type DTypeOpt func(*DTypeOptions)
func DefaultDTypeOptions() *DTypeOptions {
return &DTypeOptions{
HalfDTypePref: Half,
Quantized: false,
}
}
func HalfDTypePref(v DType) DTypeOpt {
if v != Half && v != BFloat16 {
if Debug {
log.Printf("WARNING: HalfDTypePref(): Ignoring invalid HalfDTypePref. HalfDTypePref either 'gotch.Half' or 'gotch.BFloat16'. Got %v\n", v)
}
}
return func(o *DTypeOptions) {
o.HalfDTypePref = v
}
}
func WithQuantized(v bool) DTypeOpt {
return func(o *DTypeOptions) {
o.Quantized = v
}
}
func GoKind2DType(kind reflect.Kind, opts ...DTypeOpt) (DType, error) {
o := DefaultDTypeOptions()
for _, opt := range opts {
opt(o)
}
switch {
case kind == reflect.Uint16 && o.HalfDTypePref == Half:
return Half, nil
case kind == reflect.Uint16 && o.HalfDTypePref == BFloat16:
return BFloat16, nil
case kind == reflect.Int8 && o.Quantized:
return QInt8, nil
case kind == reflect.Uint8 && o.Quantized:
return QUInt8, nil
case kind == reflect.Int32 && o.Quantized:
return QInt32, nil
default:
dtype, ok := goKind2DType[kind]
if !ok {
err := fmt.Errorf("GoKind2DType() failed: no corresponding DType to given Go reflect.Kind %v\n", kind)
return Invalid, err
}
return dtype, nil
}
}
var dtype2GoType map[DType]reflect.Type = map[DType]reflect.Type{
Uint8: reflect.TypeOf(uint8(0)),
Int8: reflect.TypeOf(int8(0)),
Int16: reflect.TypeOf(int16(0)),
Int: reflect.TypeOf(int(0)),
Int64: reflect.TypeOf(int64(0)),
Half: reflect.TypeOf(uint16(0)), // <- just uint16
Float: reflect.TypeOf(float32(0)),
Double: reflect.TypeOf(float64(0)),
// ComplexHalf: reflect.Invalid, // no equivalent in Go. Would it be reflect.Float64?
ComplexFloat: reflect.TypeOf(complex64(0)),
ComplexDouble: reflect.TypeOf(complex128(0)),
Bool: reflect.TypeOf(true),
QInt8: reflect.TypeOf(int8(0)),
QUInt8: reflect.TypeOf(uint8(0)),
QInt32: reflect.TypeOf(int32(0)),
BFloat16: reflect.TypeOf(uint16(0)), // <- just uint16
// ---not implemented ---
QUInt4x2: reflect.TypeOf(int8(0)),
QUInt2x4: reflect.TypeOf(uint8(0)),
Bits1x8: reflect.TypeOf(uint8(0)),
Bits2x4: reflect.TypeOf(uint8(0)),
Bits4x2: reflect.TypeOf(uint8(0)),
Bits8: reflect.TypeOf(uint8(0)),
Bits16: reflect.TypeOf(uint16(0)),
}
func (dt DType) GoType() (reflect.Type, error) {
typ, ok := dtype2GoType[dt]
if !ok {
err := fmt.Errorf("DType.GoType() failed: no corresponding Go type to given DType %v\n", typ.String())
return nil, err
}
return typ, nil
}
var dtypeNames map[DType]string = map[DType]string{
Uint8: "Uint8",
Int8: "Int8",
Int16: "Int16",
Int: "Int",
Int64: "Int64",
Half: "Half", // <- just uint16
Float: "Float",
Double: "Double",
// ComplexHalf: reflect.Invalid, // no equivalent in Go. Would it be reflect.Float64?
ComplexFloat: "ComplexFloat",
ComplexDouble: "ComplexDouble",
Bool: "Bool",
QInt8: "QInt8",
QUInt8: "QUInt8",
QInt32: "QInt32",
BFloat16: "BFloat16", // <- just uint16
// ---not implemented ---
QUInt4x2: "QUInt4x2",
QUInt2x4: "QUInt2x4",
Bits1x8: "Bits1x8",
Bits2x4: "Bits2x4",
Bits4x2: "Bits4x2",
Bits8: "Bits8",
Bits16: "Bits16",
}
func (dt DType) String() string {
return dtypeNames[dt]
}
func DTypeFromData(data interface{}) (DType, error) {
dataKind := reflect.TypeOf(data).Kind()
// Data is a slice/array
if dataKind == reflect.Slice || dataKind == reflect.Array {
elementKind := reflect.TypeOf(data).Elem().Kind()
return GoKind2DType(elementKind)
}
// single element
return GoKind2DType(dataKind)
}
// IsFloatDType returns whether dtype is floating point data type.
func IsFloatDType(dtype DType) bool {
switch dtype {
case Double, Float, Half, BFloat16:
return true
default:
return false
}
}
// Default DType:
// ==============
var DefaultDType DType = Float
// SetDefaultDType set DefaultDType to new value and return the previous one.
func SetDefaultDType(dtype DType) DType {
odtype := DefaultDType
DefaultDType = dtype
if Debug {
log.Printf("INFO: gotch 'DefaultDType' has changed to %v. Remember to change back to previous default to avoid unexpected outcome.\n", dtype)
}
return odtype
}