forked from unifyai/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
552 lines (457 loc) · 11 KB
/
Copy path__init__.py
File metadata and controls
552 lines (457 loc) · 11 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
552
# global
import warnings
warnings.filterwarnings("ignore", module="^(?!.*ivy).*$")
# class placeholders
class Container:
pass
class NativeArray:
pass
class NativeVariable:
pass
class Array:
pass
class Variable:
pass
class FrameworkStr(str):
def __new__(cls, fw_str):
assert fw_str in ["jax", "tensorflow", "torch", "mxnet", "numpy"]
return str.__new__(cls, fw_str)
class Framework:
pass
class NativeDevice:
pass
class NativeDtype:
pass
class Device(str):
def __new__(cls, dev_str):
assert dev_str[0:3] in ["gpu", "tpu", "cpu"]
if dev_str != "cpu":
assert dev_str[3] == ":"
assert dev_str[4:].isnumeric()
return str.__new__(cls, dev_str)
class Dtype(str):
def __new__(cls, dtype_str):
assert "int" in dtype_str or "float" in dtype_str or "bool" in dtype_str
return str.__new__(cls, dtype_str)
class IntDtype(Dtype):
def __new__(cls, dtype_str):
assert "int" in dtype_str
return str.__new__(cls, dtype_str)
class FloatDtype(Dtype):
def __new__(cls, dtype_str):
assert "float" in dtype_str
return str.__new__(cls, dtype_str)
class Node(str):
# ToDo: add formatting checks once multi-node is supported
pass
array_significant_figures_stack = list()
array_decimal_values_stack = list()
warning_level_stack = list()
warn_to_regex = {"all": "!.*", "ivy_only": "^(?!.*ivy).*$", "none": ".*"}
# global constants
_MIN_DENOMINATOR = 1e-12
_MIN_BASE = 1e-5
# local
import threading
from .array import Array, Variable, add_ivy_array_instance_methods
from .array.conversions import *
from .container import (
ContainerBase,
Container,
MultiDevContainer,
add_ivy_container_instance_methods,
)
from .backend_handler import (
current_backend,
get_backend,
set_backend,
unset_backend,
backend_stack,
choose_random_backend,
try_import_ivy_jax,
try_import_ivy_tf,
try_import_ivy_torch,
try_import_ivy_mxnet,
try_import_ivy_numpy,
clear_backend_stack,
)
from . import backend_handler, func_wrapper
from . import functional
from .functional import *
from . import stateful
from .stateful import *
from . import verbosity
from .inspection import fn_array_spec, add_array_specs
add_array_specs()
# add instance methods to Ivy Array and Container
from ivy.functional.ivy import (
activations,
creation,
data_type,
device,
elementwise,
general,
gradients,
layers,
linear_algebra,
losses,
manipulation,
norms,
random,
searching,
set,
sorting,
statistical,
utility,
)
add_ivy_array_instance_methods(
Array,
[
activations,
creation,
data_type,
device,
elementwise,
general,
gradients,
layers,
linear_algebra,
losses,
manipulation,
norms,
random,
searching,
set,
sorting,
statistical,
utility,
],
)
add_ivy_container_instance_methods(
Container,
[
activations,
creation,
data_type,
device,
elementwise,
general,
gradients,
layers,
linear_algebra,
losses,
manipulation,
norms,
random,
searching,
set,
sorting,
statistical,
utility,
],
)
add_ivy_container_instance_methods(
Container,
[
activations,
creation,
data_type,
device,
elementwise,
general,
gradients,
layers,
linear_algebra,
losses,
manipulation,
norms,
random,
searching,
set,
sorting,
statistical,
utility,
],
static=True,
)
# data types
int8 = IntDtype("int8")
int16 = IntDtype("int16")
int32 = IntDtype("int32")
int64 = IntDtype("int64")
uint8 = IntDtype("uint8")
uint16 = IntDtype("uint16")
uint32 = IntDtype("uint32")
uint64 = IntDtype("uint64")
bfloat16 = FloatDtype("bfloat16")
float16 = FloatDtype("float16")
float32 = FloatDtype("float32")
float64 = FloatDtype("float64")
# noinspection PyShadowingBuiltins
bool = Dtype("bool")
# native data types
native_int8 = IntDtype("int8")
native_int16 = IntDtype("int16")
native_int32 = IntDtype("int32")
native_int64 = IntDtype("int64")
native_uint8 = IntDtype("uint8")
native_uint16 = IntDtype("uint16")
native_uint32 = IntDtype("uint32")
native_uint64 = IntDtype("uint64")
native_bfloat16 = FloatDtype("bfloat16")
native_float16 = FloatDtype("float16")
native_float32 = FloatDtype("float32")
native_float64 = FloatDtype("float64")
native_bool = Dtype("bool")
# all
all_dtypes = (
int8,
int16,
int32,
int64,
uint8,
uint16,
uint32,
uint64,
bfloat16,
float16,
float32,
float64,
bool,
)
all_numeric_dtypes = (
int8,
int16,
int32,
int64,
uint8,
uint16,
uint32,
uint64,
bfloat16,
float16,
float32,
float64,
)
all_int_dtypes = (
int8,
int16,
int32,
int64,
uint8,
uint16,
uint32,
uint64,
)
all_float_dtypes = (
bfloat16,
float16,
float32,
float64,
)
# valid data types
valid_dtypes = all_dtypes
valid_numeric_dtypes = all_numeric_dtypes
valid_int_dtypes = all_int_dtypes
valid_float_dtypes = all_float_dtypes
# invalid data types
invalid_dtypes = ()
invalid_numeric_dtypes = ()
invalid_int_dtypes = ()
invalid_float_dtypes = ()
# data type promotion
promotion_table = {
(int8, int8): int8,
(int8, int16): int16,
(int8, int32): int32,
(int8, int64): int64,
(int16, int8): int16,
(int16, int16): int16,
(int16, int32): int32,
(int16, int64): int64,
(int32, int8): int32,
(int32, int16): int32,
(int32, int32): int32,
(int32, int64): int64,
(int64, int8): int64,
(int64, int16): int64,
(int64, int32): int64,
(int64, int64): int64,
(uint8, uint8): uint8,
(uint8, uint16): uint16,
(uint8, uint32): uint32,
(uint8, uint64): uint64,
(uint16, uint8): uint16,
(uint16, uint16): uint16,
(uint16, uint32): uint32,
(uint16, uint64): uint64,
(uint32, uint8): uint32,
(uint32, uint16): uint32,
(uint32, uint32): uint32,
(uint32, uint64): uint64,
(uint64, uint8): uint64,
(uint64, uint16): uint64,
(uint64, uint32): uint64,
(uint64, uint64): uint64,
(int8, uint8): int16,
(int8, uint16): int32,
(int8, uint32): int64,
(int16, uint8): int16,
(int16, uint16): int32,
(int16, uint32): int64,
(int32, uint8): int32,
(int32, uint16): int32,
(int32, uint32): int64,
(int64, uint8): int64,
(int64, uint16): int64,
(int64, uint32): int64,
(uint8, int8): int16,
(uint16, int8): int32,
(uint32, int8): int64,
(uint8, int16): int16,
(uint16, int16): int32,
(uint32, int16): int64,
(uint8, int32): int32,
(uint16, int32): int32,
(uint32, int32): int64,
(uint8, int64): int64,
(uint16, int64): int64,
(uint32, int64): int64,
(float16, float16): float16,
(float16, float32): float32,
(float16, float64): float64,
(float32, float16): float32,
(float32, float32): float32,
(float32, float64): float64,
(float64, float16): float64,
(float64, float32): float64,
(float64, float64): float64,
(bool, bool): bool,
}
locks = {"backend_setter": threading.Lock()}
backend = "none"
if "IVY_BACKEND" in os.environ:
ivy.set_backend(os.environ["IVY_BACKEND"])
# Array Significant Figures #
def _assert_array_significant_figures_formatting(sig_figs):
assert isinstance(sig_figs, int)
assert sig_figs > 0
def _sf(x, sig_fig=3):
if isinstance(x, np.bool_):
return x
f = float(
np.format_float_positional(
x, precision=sig_fig, unique=False, fractional=False, trim="k"
)
)
if "uint" in type(x).__name__:
f = np.uint(f)
elif "int" in type(x).__name__:
f = int(f)
x = f
return x
vec_sig_fig = np.vectorize(_sf)
vec_sig_fig.__name__ = "vec_sig_fig"
def array_significant_figures(sig_figs=None):
"""Summary.
Parameters
----------
sig_figs
optional int, number of significant figures to be shown when printing
Returns
-------
ret
"""
if ivy.exists(sig_figs):
_assert_array_significant_figures_formatting(sig_figs)
return sig_figs
global array_significant_figures_stack
if not array_significant_figures_stack:
ret = 3
else:
ret = array_significant_figures_stack[-1]
return ret
def set_array_significant_figures(sig_figs):
"""Summary.
Parameters
----------
sig_figs
optional int, number of significant figures to be shown when printing
"""
_assert_array_significant_figures_formatting(sig_figs)
global array_significant_figures_stack
array_significant_figures_stack.append(sig_figs)
def unset_array_significant_figures():
""""""
global array_significant_figures_stack
if array_significant_figures_stack:
array_significant_figures_stack.pop(-1)
# Decimal Values #
def _assert_array_decimal_values_formatting(dec_vals):
assert isinstance(dec_vals, int)
assert dec_vals >= 0
def array_decimal_values(dec_vals=None):
"""Summary.
Parameters
----------
dec_vals
optional int, number of decimal values to be shown when printing
Returns
-------
ret
"""
if ivy.exists(dec_vals):
_assert_array_decimal_values_formatting(dec_vals)
return dec_vals
global array_decimal_values_stack
if not array_decimal_values_stack:
ret = None
else:
ret = array_decimal_values_stack[-1]
return ret
def set_array_decimal_values(dec_vals):
"""Summary.
Parameters
----------
dec_vals
optional int, number of significant figures to be shown when printing
"""
_assert_array_decimal_values_formatting(dec_vals)
global array_decimal_values_stack
array_decimal_values_stack.append(dec_vals)
def unset_array_decimal_values():
""""""
global array_decimal_values_stack
if array_decimal_values_stack:
array_decimal_values_stack.pop(-1)
def warning_level():
"""Summary.
Returns
-------
ret
current warning level, default is "ivy_only"
"""
global warning_level_stack
if not warning_level_stack:
ret = "ivy_only"
else:
ret = warning_level_stack[-1]
return ret
def set_warning_level(warn_level):
"""Summary.
Parameters
----------
warn_level
string for the warning level to be set, one of "none", "ivy_only", "all"
"""
global warning_level_stack
warning_level_stack.append(warn_level)
def unset_warning_level():
""""""
global warning_level_stack
if warning_level_stack:
warning_level_stack.pop(-1)
def warn(warning_message, stacklevel=0):
warn_level = warning_level()
warnings.filterwarnings("ignore", module=warn_to_regex[warn_level])
warnings.warn(warning_message, stacklevel=stacklevel)