-
Notifications
You must be signed in to change notification settings - Fork 2
/
dtype.go
454 lines (418 loc) · 13.5 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright 2023 Jan Pfeifer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shapes
import (
"github.com/gomlx/gomlx/types/exceptions"
"github.com/pkg/errors"
"math"
"reflect"
"strconv"
"unsafe"
)
// DType indicates the type of the unit element of a Tensor (or its representation in
// a computation graph). It enumerates the known data types. So far only
// Bool, Uint8 (U8), Int32 (I32), Int64 (I64), Uint64 (U64), Float32 (F32) and Float64 (F64) are supported.
//
// The values of DType must match "tensorflow/compiler/xla/xla_data.pb.h", hence it needs
// to be an int32.
//
// See example in package shapes documentation.
type DType int32
//go:generate enumer -type=DType -yaml -json -text -values
// DType constants must match `tensorflow/compiler/xla/xla_data.proto`.
const (
InvalidDType DType = iota
Bool // Bool, but also known as PRED in `xla_data.proto`.
Int8 // S8
Int16 // S16
Int32 // S32
Int64 // S64
UInt8 // U8
UInt16 // U16
UInt32 // U32
UInt64 // U64
Float16 // F16
Float32 // F32
Float64 // F64
BFloat16 DType = 16 // BF16
Complex64 DType = 15 // C64
Complex128 DType = 18 // C128
Tuple DType = 13
OpaqueType DType = 14
Token DType = 17
)
// PRED type is an alias to Bool, used in `tensorflow/compiler/xla/xla_data.proto`.
const PRED = Bool
// Aliases:
const (
U8 = UInt8
U32 = UInt32
U64 = UInt64
I32 = Int32
I64 = Int64
F16 = Float16
F32 = Float32
F64 = Float64
C64 = Complex64
C128 = Complex128
)
// IsFloat returns whether dtype is a supported float -- float types not yet supported will return false.
// It returns false for complex numbers.
func (dtype DType) IsFloat() bool {
return dtype == Float32 || dtype == Float64
}
// IsComplex returns whether dtype is a supported complex number type.
func (dtype DType) IsComplex() bool {
return dtype == Complex64 || dtype == Complex128
}
// RealDType returns the real component of complex dtypes.
// For float dtypes, it returns itself.
//
// It returns InvalidDType for other non-(complex or float) dtypes.
func (dtype DType) RealDType() DType {
if dtype.IsFloat() {
return dtype
}
switch dtype {
case Complex64:
return Float32
case Complex128:
return Float64
}
// RealDType is not defined for other dtypes.
return InvalidDType
}
// IsInt returns whether dtype is a supported integer type -- float types not yet supported will return false.
func (dtype DType) IsInt() bool {
return dtype == Int64 || dtype == Int32 || dtype == UInt8 || dtype == UInt32 || dtype == UInt64
}
func (dtype DType) IsSupported() bool {
return dtype == Bool || dtype == Float32 || dtype == Float64 ||
dtype == Int64 || dtype == Int32 || dtype == UInt8 || dtype == UInt32 || dtype == UInt64 ||
dtype == Complex64 || dtype == Complex128
}
// GoStr converts dtype to the corresponding Go type and convert that to string.
// Notice the names are different from the Dtype (so `Int64` dtype is simply `int` in Go).
func (dtype DType) GoStr() string {
t := TypeForDType(dtype)
return t.Name()
}
// Generated by `cmd/constraints_generator`:
// Supported lists the Go types that are supported by the graph package. Used as a Generics constraint.
// See also Number.
//
// Notice Go's `int` type is not portable, since it may translate to dtypes Int32 or Int64 depending
// on the platform.
//
// Generated by `cmd/constraints_generator`.
type Supported interface {
bool | float32 | float64 | int | int32 | int64 | uint8 | uint32 | uint64 | complex64 | complex128
}
// Number represents the Go numeric types that are supported by graph package.
// Used as a Generics constraint.
// Notice that "int" becomes int64 in the implementation.
// Since it needs a 1:1 mapping, it doesn't support the native (Go) int64 type.
// It includes complex numbers.
type Number interface {
float32 | float64 | int | int32 | int64 | uint8 | uint32 | uint64 | complex64 | complex128
}
// NumberNotComplex represents the Go numeric types that are supported by graph package except the complex numbers.
// Used as a Generics constraint.
// See Number for details.
type NumberNotComplex interface {
float32 | float64 | int | int32 | int64 | uint8 | uint32 | uint64
}
// GoFloat represent a continuous Go numeric type, supported by GoMLX.
type GoFloat interface {
float32 | float64
}
// Generated by `cmd/constraints_generator`:
// MultiDimensionSlice lists the Go types a Tensor can be converted to/from. There are no recursions in
// generics' constraint definitions, so we enumerate up to 7 levels of slices. Feel free to add
// more if needed, the implementation will work with any arbitrary number.
type MultiDimensionSlice interface {
bool | float32 | float64 | int | int32 | int64 | uint8 | uint32 | uint64 | complex64 | complex128 |
[]bool | []float32 | []float64 | []int | []int32 | []int64 | []uint8 | []uint32 | []uint64 | []complex64 | []complex128 |
[][]bool | [][]float32 | [][]float64 | [][]int | [][]int32 | [][]int64 | [][]uint8 | [][]uint32 | [][]uint64 | [][]complex64 | [][]complex128 |
[][][]bool | [][][]float32 | [][][]float64 | [][][]int | [][][]int32 | [][][]int64 | [][][]uint8 | [][][]uint32 | [][][]uint64 | [][][]complex64 | [][][]complex128 |
[][][][]bool | [][][][]float32 | [][][][]float64 | [][][][]int | [][][][]int32 | [][][][]int64 | [][][][]uint8 | [][][][]uint32 | [][][][]uint64 | [][][][]complex64 | [][][][]complex128 |
[][][][][]bool | [][][][][]float32 | [][][][][]float64 | [][][][][]int | [][][][][]int32 | [][][][][]int64 | [][][][][]uint8 | [][][][][]uint32 | [][][][][]uint64 | [][][][][]complex64 | [][][][][]complex128 |
[][][][][][]bool | [][][][][][]float32 | [][][][][][]float64 | [][][][][][]int | [][][][][][]int32 | [][][][][][]int64 | [][][][][][]uint8 | [][][][][][]uint32 | [][][][][][]uint64 | [][][][][][]complex64 | [][][][][][]complex128
}
func DTypeGeneric[T Supported]() DType {
var t T
switch (any(t)).(type) {
case float32:
return Float32
case float64:
return Float64
case int:
switch strconv.IntSize {
case 32:
return Int32
case 64:
return Int64
default:
exceptions.Panicf("Cannot use int of %d bits with GoMLX -- try using int32 or int64", strconv.IntSize)
}
case int64:
return Int64
case int32:
return Int32
case bool:
return Bool
case uint8:
return UInt8
case uint32:
return UInt32
case uint64:
return UInt64
case complex64:
return Complex64
case complex128:
return Complex128
}
return InvalidDType
}
// ConvertTo converts any scalar (typically returned by `tensor.Local.Value()`) of the
// supported dtypes to `T`.
// Returns 0 if value is not a scalar or not a supported number (e.g: bool).
// It doesn't work for if T (the output type) is a complex number.
// If value is a complex number, it converts by taking the real part of the number and
// discarding the imaginary part.
func ConvertTo[T NumberNotComplex](value any) T {
switch v := value.(type) {
case float32:
return T(v)
case float64:
return T(v)
case int:
return T(v)
case int64:
return T(v)
case int32:
return T(v)
case uint8:
return T(v)
case uint32:
return T(v)
case uint64:
return T(v)
case complex64:
return T(real(v))
case complex128:
return T(real(v))
}
return T(0)
}
func DTypeForType(t reflect.Type) DType {
switch t.Kind() {
case reflect.Int:
switch strconv.IntSize {
case 32:
return Int32
case 64:
return Int64
default:
exceptions.Panicf("cannot use int of %d bits with GoMLX -- try using int32 or int64", strconv.IntSize)
}
case reflect.Int64:
return Int64
case reflect.Int32:
return Int32
case reflect.Float32:
return Float32
case reflect.Float64:
return Float64
case reflect.Bool:
return Bool
case reflect.Uint8:
return UInt8
case reflect.Uint32:
return UInt32
case reflect.Uint64:
return UInt64
case reflect.Complex64:
return Complex64
case reflect.Complex128:
return Complex128
}
return InvalidDType
}
// UnsafeSliceForDType creates a slice of the corresponding dtype
// and casts it to any.
// It uses unsafe.Slice.
// Set `len` to the number of `DType` elements (not the number of bytes).
func UnsafeSliceForDType(dtype DType, unsafePtr unsafe.Pointer, len int) any {
switch dtype {
case Int64:
return unsafe.Slice((*int64)(unsafePtr), len)
case Int32:
return unsafe.Slice((*int32)(unsafePtr), len)
case Float32:
return unsafe.Slice((*float32)(unsafePtr), len)
case Float64:
return unsafe.Slice((*float64)(unsafePtr), len)
case Bool:
return unsafe.Slice((*bool)(unsafePtr), len)
case UInt8:
return unsafe.Slice((*uint8)(unsafePtr), len)
case UInt32:
return unsafe.Slice((*uint32)(unsafePtr), len)
case UInt64:
return unsafe.Slice((*uint64)(unsafePtr), len)
case Complex64:
return unsafe.Slice((*complex64)(unsafePtr), len)
case Complex128:
return unsafe.Slice((*complex128)(unsafePtr), len)
}
return nil
}
// TypeForDType returns the Go `reflect.Type` corresponding to the tensor DType.
func TypeForDType(dtype DType) reflect.Type {
switch dtype {
case Int64:
return reflect.TypeOf(int64(0))
case Int32:
return reflect.TypeOf(int32(0))
case Float32:
return float32Type
case Float64:
return float64Type
case Bool:
return reflect.TypeOf(true)
case UInt8:
return reflect.TypeOf(uint8(0))
case UInt32:
return reflect.TypeOf(uint32(0))
case UInt64:
return reflect.TypeOf(uint64(0))
case Complex64:
return reflect.TypeOf(complex64(0))
case Complex128:
return reflect.TypeOf(complex128(0))
}
return reflect.TypeOf(nil)
}
// Type returns the Go `reflect.Type` corresponding to the tensor DType.
func (dtype DType) Type() reflect.Type {
return TypeForDType(dtype)
}
// Memory returns the number of bytes in Go used to store the given DType.
func (dtype DType) Memory() int64 {
t := TypeForDType(dtype)
return int64(t.Size())
}
// Pre-generate constant reflect.TypeOf for convenience.
var (
float32Type = reflect.TypeOf(float32(0))
float64Type = reflect.TypeOf(float64(0))
)
// CastAsDType casts a numeric value to the corresponding for the DType.
// If the value is a slice it will convert to a newly allocated slice of
// the given DType.
//
// It doesn't work for complex numbers.
func CastAsDType(value any, dtype DType) any {
typeOf := reflect.TypeOf(value)
valueOf := reflect.ValueOf(value)
newTypeOf := typeForSliceDType(typeOf, dtype)
if typeOf.Kind() != reflect.Slice && typeOf.Kind() != reflect.Array {
// Scalar value.
if dtype == Bool {
return !valueOf.IsZero()
}
if dtype == Complex64 {
r := valueOf.Convert(float32Type).Interface().(float32)
return complex(r, float32(0))
}
if dtype == Complex128 {
r := valueOf.Convert(float64Type).Interface().(float64)
return complex(r, float64(0))
}
// TODO: if adding support for non-native Go types (e.g: B16), we need
// to write our own conversion here.
return valueOf.Convert(newTypeOf).Interface()
}
newValueOf := reflect.MakeSlice(newTypeOf, valueOf.Len(), valueOf.Len())
for ii := 0; ii < valueOf.Len(); ii++ {
elem := CastAsDType(valueOf.Index(ii).Interface(), dtype)
newValueOf.Index(ii).Set(reflect.ValueOf(elem))
}
return newValueOf.Interface()
}
// typeForSliceDType recursively converts a type that is a (multi-dimension-) slice
// of some type, to a `reflect.Type` of a (multi-dimension-) slice of `dtype`.
// Arrays are converted to slices.
func typeForSliceDType(valueType reflect.Type, dtype DType) reflect.Type {
if valueType.Kind() != reflect.Slice && valueType.Kind() != reflect.Array {
// Base case for recursion, simply return the `reflect.Type` for the DType.
return TypeForDType(dtype)
}
subType := typeForSliceDType(valueType.Elem(), dtype)
return reflect.SliceOf(subType) // Return a slice of the recursively converted type.
}
// LowestValueForDType converted to the corresponding Go type.
// For float values it will return negative infinite.
// There is no lowest value for complex numbers, since they are not ordered.
func LowestValueForDType(dtype DType) any {
switch dtype {
case Int64:
return int64(math.MinInt64)
case Int32:
return int32(math.MinInt32)
case Float32:
return float32(math.Inf(-1))
case Float64:
return math.Inf(-1)
case Bool:
return false
case UInt8:
return uint8(0)
case UInt32:
return uint32(0)
case UInt64:
return uint64(0)
default:
exceptions.Panicf("LowestValueForDType not defined for dtype %s", dtype)
}
return 0 // Never reaches here.
}
// SmallestNonZeroValueForDType is the smallest non-zero value dtypes.
// Only useful for float types.
// The return value is converted to the corresponding Go type.
// There is no smallest non-zero value for complex numbers, since they are not ordered.
func SmallestNonZeroValueForDType(dtype DType) any {
switch dtype {
case Int64:
return int64(1)
case Int32:
return int32(1)
case Float32:
return float32(math.SmallestNonzeroFloat32)
case Float64:
return math.SmallestNonzeroFloat64
case Bool:
return true
case UInt8:
return uint8(1)
case UInt32:
return uint32(1)
case UInt64:
return uint64(1)
}
panic(errors.Errorf("SmallestNonZeroValueForDType not defined for dtype %s", dtype))
}