-
Notifications
You must be signed in to change notification settings - Fork 75.3k
Expand file tree
/
Copy pathflexible_dtypes.py
More file actions
551 lines (513 loc) · 18 KB
/
flexible_dtypes.py
File metadata and controls
551 lines (513 loc) · 18 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# Copyright 2023 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# =============================================================================
"""Auto dtype conversion semantics for TF."""
import numpy as np
from tensorflow.python.framework import composite_tensor
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import indexed_slices
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import weak_tensor
from tensorflow.python.ops import variables
from tensorflow.python.util import nest
# PromoMode Enum that denotes safe and all mode.
PromoMode = ops.PromoMode
# Namings are similar to third_party/py/jax/_src/dtypes.py.
_b8 = (dtypes.bool, False)
_u8 = (dtypes.uint8, False)
_u16 = (dtypes.uint16, False)
_u32 = (dtypes.uint32, False)
_u64 = (dtypes.uint64, False)
_i8 = (dtypes.int8, False)
_i16 = (dtypes.int16, False)
_i32 = (dtypes.int32, False)
_i64 = (dtypes.int64, False)
_bf16 = (dtypes.bfloat16, False)
_f16 = (dtypes.float16, False)
_f32 = (dtypes.float32, False)
_f64 = (dtypes.float64, False)
_c64 = (dtypes.complex64, False)
_c128 = (dtypes.complex128, False)
# Weak dtypes
_i32w = (dtypes.int32, True)
_i64w = (dtypes.int64, True)
_f32w = (dtypes.float32, True)
_f64w = (dtypes.float64, True)
_c128w = (dtypes.complex128, True)
# String
_str = (dtypes.string, False)
_all_dtypes = [
_b8,
_u8,
_u16,
_u32,
_u64,
_i8,
_i16,
_i32,
_i64,
_bf16,
_f16,
_f32,
_f64,
_c64,
_c128,
_i32w,
_i64w,
_f32w,
_f64w,
_c128w,
]
# Python numbers
_pi = int # pylint: disable=invalid-name
_pf = float # pylint: disable=invalid-name
_pc = complex # pylint: disable=invalid-name
# Mappings between types_pb2.DataType values and numpy.dtypes.
_NP_TO_TF = dtypes._NP_TO_TF # pylint: disable=protected-access
# OP(arg1, arg2) => (res, weak_type, safety level)
# If promotion mode is SAFE, results corresponding to ALL will be disallowed.
# This map only contains one-way dtype promotion results and is used to generate
# _BINARY_DTYPE_RES_FULL.
_BINARY_DTYPE_RES_HALF = {
_b8: {
_b8: (_b8, PromoMode.SAFE),
_u8: (_u8, PromoMode.SAFE),
_u16: (_u16, PromoMode.SAFE),
_u32: (_u32, PromoMode.SAFE),
_u64: (_u64, PromoMode.SAFE),
_i8: (_i8, PromoMode.SAFE),
_i16: (_i16, PromoMode.SAFE),
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.SAFE),
_f16: (_f16, PromoMode.SAFE),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_i32w, PromoMode.SAFE),
_i64w: (_i64w, PromoMode.SAFE),
_f32w: (_f32w, PromoMode.SAFE),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_u8: {
_u8: (_u8, PromoMode.SAFE),
_u16: (_u16, PromoMode.SAFE),
_u32: (_u32, PromoMode.SAFE),
_u64: (_u64, PromoMode.SAFE),
_i8: (_i16, PromoMode.ALL),
_i16: (_i16, PromoMode.SAFE),
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.SAFE),
_f16: (_f16, PromoMode.SAFE),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_u8, PromoMode.SAFE),
_i64w: (_u8, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_u16: {
_u16: (_u16, PromoMode.SAFE),
_u32: (_u32, PromoMode.SAFE),
_u64: (_u64, PromoMode.SAFE),
_i8: (_i32, PromoMode.ALL),
_i16: (_i32, PromoMode.ALL),
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_u16, PromoMode.SAFE),
_i64w: (_u16, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_u32: {
_u32: (_u32, PromoMode.SAFE),
_u64: (_u64, PromoMode.SAFE),
_i8: (_i64, PromoMode.ALL),
_i16: (_i64, PromoMode.ALL),
_i32: (_i64, PromoMode.ALL),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.ALL),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.ALL),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_u32, PromoMode.SAFE),
_i64w: (_u32, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_u64: {
_u64: (_u64, PromoMode.SAFE),
_i8: (_f64w, PromoMode.ALL),
_i16: (_f64w, PromoMode.ALL),
_i32: (_f64w, PromoMode.ALL),
_i64: (_f64w, PromoMode.ALL),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.ALL),
_f64: (_f64, PromoMode.ALL),
_c64: (_c64, PromoMode.ALL),
_c128: (_c128, PromoMode.ALL),
_i32w: (_u64, PromoMode.SAFE),
_i64w: (_u64, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.ALL),
_c128w: (_c128w, PromoMode.ALL),
},
_i8: {
_i8: (_i8, PromoMode.SAFE),
_i16: (_i16, PromoMode.SAFE),
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.SAFE),
_f16: (_f16, PromoMode.SAFE),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_i8, PromoMode.SAFE),
_i64w: (_i8, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_i16: {
_i16: (_i16, PromoMode.SAFE),
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_i16, PromoMode.SAFE),
_i64w: (_i16, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_i32: {
_i32: (_i32, PromoMode.SAFE),
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.ALL),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.ALL),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_i32, PromoMode.SAFE),
_i64w: (_i32, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_i64: {
_i64: (_i64, PromoMode.SAFE),
_bf16: (_bf16, PromoMode.ALL),
_f16: (_f16, PromoMode.ALL),
_f32: (_f32, PromoMode.ALL),
_f64: (_f64, PromoMode.ALL),
_c64: (_c64, PromoMode.ALL),
_c128: (_c128, PromoMode.ALL),
_i32w: (_i64, PromoMode.SAFE),
_i64w: (_i64, PromoMode.SAFE),
_f32w: (_f64w, PromoMode.ALL),
_f64w: (_f64w, PromoMode.ALL),
_c128w: (_c128w, PromoMode.ALL),
},
_bf16: {
_bf16: (_bf16, PromoMode.SAFE),
_f16: (_f32, PromoMode.ALL),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_bf16, PromoMode.SAFE),
_i64w: (_bf16, PromoMode.SAFE),
_f32w: (_bf16, PromoMode.SAFE),
_f64w: (_bf16, PromoMode.SAFE),
_c128w: (_c64, PromoMode.ALL),
},
_f16: {
_f16: (_f16, PromoMode.SAFE),
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_f16, PromoMode.SAFE),
_i64w: (_f16, PromoMode.SAFE),
_f32w: (_f16, PromoMode.SAFE),
_f64w: (_f16, PromoMode.SAFE),
_c128w: (_c64, PromoMode.ALL),
},
_f32: {
_f32: (_f32, PromoMode.SAFE),
_f64: (_f64, PromoMode.SAFE),
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_f32, PromoMode.SAFE),
_i64w: (_f32, PromoMode.SAFE),
_f32w: (_f32, PromoMode.SAFE),
_f64w: (_f32, PromoMode.SAFE),
_c128w: (_c64, PromoMode.ALL),
},
_f64: {
_f64: (_f64, PromoMode.SAFE),
_c64: (_c128, PromoMode.ALL),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_f64, PromoMode.SAFE),
_i64w: (_f64, PromoMode.SAFE),
_f32w: (_f64, PromoMode.SAFE),
_f64w: (_f64, PromoMode.SAFE),
_c128w: (_c128, PromoMode.SAFE),
},
_c64: {
_c64: (_c64, PromoMode.SAFE),
_c128: (_c128, PromoMode.SAFE),
_i32w: (_c64, PromoMode.SAFE),
_i64w: (_c64, PromoMode.SAFE),
_f32w: (_c64, PromoMode.SAFE),
_f64w: (_c64, PromoMode.SAFE),
_c128w: (_c64, PromoMode.SAFE),
},
_c128: {
_c128: (_c128, PromoMode.SAFE),
_i32w: (_c128, PromoMode.SAFE),
_i64w: (_c128, PromoMode.SAFE),
_f32w: (_c128, PromoMode.SAFE),
_f64w: (_c128, PromoMode.SAFE),
_c128w: (_c128, PromoMode.SAFE),
},
_i32w: {
_i32w: (_i32w, PromoMode.SAFE),
_i64w: (_i64w, PromoMode.SAFE),
_f32w: (_f32w, PromoMode.SAFE),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_i64w: {
_i64w: (_i64w, PromoMode.SAFE),
_f32w: (_f32w, PromoMode.SAFE),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_f32w: {
_f32w: (_f32w, PromoMode.SAFE),
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_f64w: {
_f64w: (_f64w, PromoMode.SAFE),
_c128w: (_c128w, PromoMode.SAFE),
},
_c128w: {
_c128w: (_c128w, PromoMode.SAFE),
},
}
# A full promotion table that contains two-way mappings.
# This table can be directly used for NumPy types as well, because
# e.g. `np.int32` == `tf.int32`.
_BINARY_DTYPE_RES_FULL = {}
def _initialize():
"""Generate the rest of the promotion table from the one-way promotion table.
Returns: None
"""
for dtype1 in _all_dtypes:
_BINARY_DTYPE_RES_FULL[dtype1] = {}
for dtype2 in _all_dtypes:
try:
res = _BINARY_DTYPE_RES_HALF[dtype1][dtype2]
except KeyError:
res = _BINARY_DTYPE_RES_HALF[dtype2][dtype1]
_BINARY_DTYPE_RES_FULL[dtype1][dtype2] = res
# We do not support any conversions between string and others dtypes.
_BINARY_DTYPE_RES_FULL[_str] = {_str: (_str, PromoMode.SAFE)}
_initialize()
# The name np.string_ and np.unicode_ were available as aliases to np.bytes_ and
# np.str_; however, they are removed in numpy 2.0. See
# https://numpy.org/devdocs/numpy_2_0_migration_guide.html#changes-to-namespaces.
_all_str_dtypes = (
np.dtype('object_'),
np.dtype('bytes_'),
np.dtype('str_'),
dtypes.string,
)
def _is_acceptable_input_type(x):
"""Determines if x is an acceptable input type for auto dtype conversion semantics."""
# List of composite types that are supported by the auto dtype conversion
# semantics.
supported_composite_types = (
indexed_slices.IndexedSlices,
weak_tensor.WeakTensor,
variables.Variable,
)
return isinstance(x, supported_composite_types) or not isinstance(
x, composite_tensor.CompositeTensor
)
def _get_dtype_and_weakness(x):
"""Returns a TF type and weak type information from x.
Args:
x: an input scalar, array or a NumPy/TF/Python dtype.
Raises:
OverflowError: if Python int x is too large to convert to int32.
NotImplementedError: when x is an unsupported input type.
Returns:
TF type and weak type information inferred from x in the form of
(dtype, bool).
"""
if isinstance(x, weak_tensor.WeakTensor):
return (x.dtype, True)
if isinstance(x, dtypes.DType):
return (x, False)
# TODO(b/286585200): Add support for `AutoCastVariable` in Keras.
tf_dtype = getattr(x, 'dtype', None)
if isinstance(tf_dtype, dtypes.DType):
return (tf_dtype, False)
# `isinstance(tf_dtype, np.dtype)` handles classes that implement `dtype`
# using `np.dtype` (e.g. `xla_extension.Array`).
# This condition is put before e.g. python int/float because
# `isinstance(np.float64(1), float)` returns True.
if isinstance(x, (np.ndarray, np.generic)) or isinstance(tf_dtype, np.dtype):
# Use `dtypes.as_dtype(x.dtype)` because in `as_dtype`, the input will be
# compared against a list of types including TF Dtypes which are protobufs.
infer_dtype = dtypes.as_dtype(tf_dtype)
return (infer_dtype, False)
if isinstance(x, (bytes, str)) or tf_dtype in _all_str_dtypes:
return _str
try:
if x in _NP_TO_TF:
return (_NP_TO_TF[x], False)
except TypeError:
pass
# bool type check must happen before int type check because
# isinstance(True, int) == True (https://peps.python.org/pep-0285/).
if isinstance(x, bool) or x == bool:
return _b8
# TODO(b/286585058): Update implementation depending on whether Python
# scalars are inferred to 32 bit or 64 bit.
if isinstance(x, _pi):
if x < np.iinfo(np.int32).min or x > np.iinfo(np.int32).max:
raise OverflowError(f'Python int {x} too large to convert to np.int32')
return _i32w
if x == int:
return _i32w
if isinstance(x, _pf) or x == float:
return _f32w
if isinstance(x, _pc) or x == complex:
return _c128w
if isinstance(x, tensor_shape.TensorShape):
# Since TensorShape is always integer value, return int32.
return _i32
# Only support NumPy dtype objects with corresponding TF types.
if isinstance(x, np.dtype):
try:
np_dtype = dtypes.as_dtype(x)
return (np_dtype, False)
except TypeError as exc:
raise NotImplementedError(
f'Auto dtype conversion semantics does not support {x}. Try using a'
' NumPy built-in dtype objects or cast them explicitly.'
) from exc
raise NotImplementedError(
f'Auto dtype conversion semantics does not support {type(x)} type.'
)
def _result_type_impl(*arrays_and_dtypes):
"""Internal implementation of jnp_style_result_type.
Args:
*arrays_and_dtypes: A list of Tensors, Variables, NumPy arrays or python
numbers.
Returns:
The result promotion type from all the inputs.
Raises:
TypeError: when the promotion between the input dtypes is disabled in the
current mode
NotImplementedError:
(1) When arrays_and_dtypes contains an unsupported input type (e.g.
RaggedTensor).
(2) When there isn't a possible promotion for the input dtypes.
"""
promo_safety_mode = ops.get_dtype_conversion_mode()
# Drop None inputs and check if input type is supported.
valid_arrays_and_dtypes = []
for inp in arrays_and_dtypes:
if inp is not None:
if _is_acceptable_input_type(inp):
valid_arrays_and_dtypes.append(inp)
else:
raise NotImplementedError(
'Auto dtype conversion semantics does not support'
f' {type(inp)} type.'
)
dtypes_and_is_weak = [
_get_dtype_and_weakness(x) for x in nest.flatten(valid_arrays_and_dtypes)
]
# If there are no valid inputs, return f32.
if not dtypes_and_is_weak:
# If dtypes_and_is_weak is an empty list, return weakly-typed f32.
dtypes_and_is_weak = [(dtypes.float32, True)]
res = dtypes_and_is_weak[0]
for arg in dtypes_and_is_weak[1:]:
# Use `base_dtype` in case of `ref` types (for e.g. ref_variables).
res = (res[0].base_dtype, res[1])
arg = (arg[0].base_dtype, arg[1])
try:
res_next, allowed_mode = _BINARY_DTYPE_RES_FULL[res][arg]
except KeyError as exc:
# Throw NotImplementedError When there isn't a possible promotion for the
# input dtypes. We will proceed with the default system promotion if
# NotImplementedError is thrown.
raise NotImplementedError(
f'Implicit Conversion between {res[0]} and {arg[0]} is '
'not allowed. Please convert the input manually if you '
'need to.'
) from exc
if allowed_mode.value > promo_safety_mode.value:
raise TypeError(
f'In promotion mode {promo_safety_mode}, implicit dtype '
f'promotion between ({res[0]}, weak={res[1]}) and '
f'({arg[0]}, weak={arg[1]}) is disallowed. '
'You need to explicitly specify the dtype in your op, '
'or relax your dtype promotion rules (such as from SAFE '
'mode to ALL mode).'
)
res = res_next
return res
def result_type(*arrays_and_dtypes):
"""Determine the result promotion dtype using the JNP-like promotion system.
Args:
*arrays_and_dtypes: A list of Tensors, Variables, NumPy arrays or python
numbers.
Returns:
The result promotion type from all the inputs.
"""
# Make sure to catch NotImplementedError when using this method to account for
# inputs that are not supported yet.
return _result_type_impl(*arrays_and_dtypes)