forked from ivy-llc/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiversion_backend_test.py
536 lines (472 loc) · 18.3 KB
/
multiversion_backend_test.py
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
import ast
import inspect
from ivy_tests.test_ivy.helpers.function_testing import _is_frontend_array
from ivy_tests import config
# from ivy_tests.test_ivy.helpers.structs import FrontendMethodData
import sys
import jsonpickle
import importlib
from ivy_tests.test_ivy.helpers.testing_helpers import (
_import_fn,
_get_supported_devices_dtypes,
_import_method,
_get_method_supported_devices_dtypes,
)
# import paddle_bfloat
def _lstrip_lines(source: str) -> str:
source = source.lstrip().split("\n")
# If the first line is a decorator
if source[0][0] == "@":
# If the second line is a function definition
if source[1].lstrip()[0:3] == "def":
# Work out how many whitespace chars to remove
num_chars_to_remove = source[1].find("d")
# The first string needs no changes
for i in range(1, len(source)):
source[i] = source[i][num_chars_to_remove:]
source = "\n".join(source)
return source
def _get_functions_from_string(func_names, module):
ret = set()
# We only care about the functions in the ivy or the same module
for func_name in func_names.keys():
if hasattr(ivy, func_name) and callable(getattr(ivy, func_name, None)):
ret.add(getattr(ivy, func_name))
elif hasattr(module, func_name) and callable(getattr(ivy, func_name, None)):
ret.add(getattr(module, func_name))
elif callable(getattr(func_names[func_name], func_name, None)):
ret.add(getattr(func_names[func_name], func_name))
return ret
def _get_function_list(func):
tree = ast.parse(_lstrip_lines(inspect.getsource(func)))
names = {}
# Extract all the call names
for node in ast.walk(tree):
if isinstance(node, ast.Call):
nodef = node.func
if isinstance(nodef, ast.Name):
names[nodef.id] = getattr(
func,
"__self__",
getattr(
importlib.import_module(func.__module__),
func.__qualname__.split(".")[0],
None,
),
)
elif isinstance(nodef, ast.Attribute):
if (
hasattr(nodef, "value")
and hasattr(nodef.value, "id")
and nodef.value.id not in ["ivy", "self"]
):
continue
names[nodef.attr] = getattr(
func,
"__self__",
getattr(
importlib.import_module(func.__module__),
func.__qualname__.split(".")[0],
None,
),
)
return names
def get_ret_and_flattened_np_array(fn, *args, **kwargs):
"""
Runs func with args and kwargs, and returns the result along with its flattened
version.
"""
ret = fn(*args, **kwargs)
def map_fn(x):
if _is_frontend_array(x):
return x.ivy_array
if isinstance(x, ivy.functional.frontends.numpy.ndarray):
return x.ivy_array
return x
ret = ivy.nested_map(ret, map_fn, include_derived={tuple: True})
return ret, flatten_and_to_np(ret=ret)
def gradient_unsupported_dtypes(*, fn):
visited = set()
to_visit = [fn]
out, res = {}, {}
while to_visit:
fn = to_visit.pop()
if fn in visited:
continue
visited.add(fn)
unsupported_grads = (
fn.unsupported_gradients if hasattr(fn, "unsupported_gradients") else {}
)
for k, v in unsupported_grads.items():
if k not in out:
out[k] = []
out[k].extend(v)
# skip if it's not a function
if not (inspect.isfunction(fn) or inspect.ismethod(fn)):
continue
fl = _get_function_list(fn)
res = _get_functions_from_string(fl, __import__(fn.__module__))
to_visit.extend(res)
return out
def flatten_and_to_np(*, ret):
# flatten the return
ret_flat = flatten(ret=ret)
return [ivy.to_numpy(x) for x in ret_flat]
def flatten(*, ret):
"""Returns a flattened numpy version of the arrays in ret."""
if not isinstance(ret, tuple):
ret = (ret,)
ret_idxs = ivy.nested_argwhere(ret, ivy.is_ivy_array)
# no ivy array in the returned values, which means it returned scalar
if len(ret_idxs) == 0:
ret_idxs = ivy.nested_argwhere(ret, ivy.isscalar)
ret_flat = ivy.multi_index_nest(ret, ret_idxs)
temp = []
for x in ret_flat:
temp.append(ivy.asarray(x, dtype=ivy.Dtype(str(numpy.asarray(x).dtype))))
ret_flat = temp
else:
ret_flat = ivy.multi_index_nest(ret, ret_idxs)
return ret_flat
def as_cont(*, x):
"""Returns x as an Ivy Container, containing x at all its leaves."""
return ivy.Container({"a": x, "b": {"c": x, "d": x}})
def create_args_kwargs(
*,
args_np,
arg_np_vals,
args_idxs,
kwargs_np,
kwarg_np_vals,
kwargs_idxs,
input_dtypes,
test_flags,
):
"""Creates arguments and keyword-arguments for the function to test.
Parameters
----------
args_np
A dictionary of arguments in Numpy.
kwargs_np
A dictionary of keyword-arguments in Numpy.
input_dtypes
data-types of the input arguments and keyword-arguments.
Returns
-------
Arguments, Keyword-arguments, number of arguments, and indexes on arguments and
keyword-arguments.
"""
def _apply_flags(args_to_iterate):
ret = []
for i, entry in enumerate(args_to_iterate):
x = ivy.array(entry, dtype=input_dtypes[i])
if test_flags.as_variable[i]:
x = _variable(x)
if test_flags.native_arrays[i]:
x = ivy.to_native(x)
if test_flags.container[i]:
x = as_cont(x=x)
ret.append(x)
return ret
# create args
args = ivy.copy_nest(args_np, to_mutable=False)
ivy.set_nest_at_indices(args, args_idxs, _apply_flags(arg_np_vals))
# create kwargs
kwargs = ivy.copy_nest(kwargs_np, to_mutable=False)
ivy.set_nest_at_indices(kwargs, kwargs_idxs, _apply_flags(kwarg_np_vals))
return args, kwargs, len(arg_np_vals), args_idxs, kwargs_idxs
def _get_fn_dtypes(framework, fn_tree, type, device=None, kind="valid"):
if type == "1":
callable_fn, fn_name, fn_mod = _import_fn(fn_tree)
supported_device_dtypes = _get_supported_devices_dtypes(fn_name, fn_mod)
return supported_device_dtypes[framework][device][kind]
else:
method_tree = fn_tree
callable_method, method_name, _, class_name, method_mod = _import_method(
method_tree
)
supported_device_dtypes = _get_method_supported_devices_dtypes(
method_name, method_mod, class_name
)
return supported_device_dtypes[framework][device][kind]
def _get_type_dict(framework, fn_tree, type, device=None, kind="valid"):
if kind == "valid":
return framework.valid_dtypes
elif kind == "numeric":
return framework.valid_numeric_dtypes
elif kind == "integer":
return framework.valid_int_dtypes
elif kind == "float":
return framework.valid_float_dtypes
elif kind == "unsigned":
return framework.valid_int_dtypes
elif kind == "signed_integer":
return tuple(
set(framework.valid_int_dtypes).difference(framework.valid_uint_dtypes)
)
elif kind == "complex":
return framework.valid_complex_dtypes
elif kind == "real_and_complex":
return tuple(
set(framework.valid_numeric_dtypes).union(framework.valid_complex_dtypes)
)
elif kind == "float_and_complex":
return tuple(
set(framework.valid_float_dtypes).union(framework.valid_complex_dtypes)
)
elif kind == "bool":
return tuple(
set(framework.valid_dtypes).difference(framework.valid_numeric_dtypes)
)
else:
raise RuntimeError("{} is an unknown kind!".format(kind))
def dtype_handler(framework, type):
global temp_store
temp_store = []
z = input()
retrieval_fn = globals()[z]
z = input()
kind = z
z = input()
device = z
z = input()
fn_tree = z
if retrieval_fn.__name__ == "_get_type_dict":
framework = importlib.import_module("ivy.functional.backends." + framework)
dtypes = retrieval_fn(framework, fn_tree, type, device, kind)
dtypes = jsonpickle.dumps(dtypes)
print(dtypes)
def make_json_pickable(s):
s = s.replace("builtins.bfloat16", "ivy.bfloat16")
# s = s.replace("jax._src.device_array.reconstruct_device_array", "jax.numpy.array")
return s
temp_store = [] # for ins_gt
def check_unsupported_dtype(*, fn, input_dtypes, all_as_kwargs_np):
"""Checks whether a function does not support the input data types or the output
data type.
Parameters
----------
fn
The function to check.
input_dtypes
data-types of the input arguments and keyword-arguments.
all_as_kwargs_np
All arguments in Numpy Format, to check for the presence of dtype argument.
Returns
-------
True if the function does not support the given input or output data types, False
otherwise.
"""
test_unsupported = False
unsupported_dtypes_fn = ivy.function_unsupported_dtypes(fn)
supported_dtypes_fn = ivy.function_supported_dtypes(fn)
if unsupported_dtypes_fn:
for d in input_dtypes:
if d in unsupported_dtypes_fn:
test_unsupported = True
break
if (
"dtype" in all_as_kwargs_np
and all_as_kwargs_np["dtype"] in unsupported_dtypes_fn
):
test_unsupported = True
if supported_dtypes_fn and not test_unsupported:
for d in input_dtypes:
if d not in supported_dtypes_fn:
test_unsupported = True
break
if (
"dtype" in all_as_kwargs_np
and all_as_kwargs_np["dtype"] not in supported_dtypes_fn
):
test_unsupported = True
return test_unsupported
def gradient_test():
def grad_fn(all_args):
args, kwargs, i = all_args
ret = (
ivy.__dict__[fn](*args, **kwargs)
if isinstance(fn, str)
else fn[i](*args, **kwargs)
)
return ivy.nested_map(ret, ivy.mean, include_derived=True)
args_np = jsonpickle.loads(make_json_pickable(input()))
arg_np_vals = jsonpickle.loads(make_json_pickable(input()))
args_idxs = jsonpickle.loads(make_json_pickable(input()))
kwargs_np = jsonpickle.loads(make_json_pickable(input()))
kwargs_idxs = jsonpickle.loads(make_json_pickable(input()))
kwarg_np_vals = jsonpickle.loads(make_json_pickable(input()))
input_dtypes = jsonpickle.loads(input())
test_flags = jsonpickle.loads(make_json_pickable(input()))
fn = jsonpickle.loads(make_json_pickable(input()))
all_as_kwargs_np = jsonpickle.loads(make_json_pickable(input()))
grad_fnn = jsonpickle.loads(make_json_pickable(input())) # noqa: F841
ret_grad_idxs = jsonpickle.loads(make_json_pickable(input()))
global temp_store
if not temp_store and isinstance(fn, list):
raise Exception("trouble for ins_gt")
elif isinstance(fn, list):
fn[1] = temp_store[-1][0].__getattribute__(temp_store[-1][1])
test_unsupported = check_unsupported_dtype(
fn=ivy.__dict__[fn] if isinstance(fn, str) else fn[1],
input_dtypes=input_dtypes,
all_as_kwargs_np=all_as_kwargs_np,
)
if test_unsupported:
print(jsonpickle.dumps(None))
return
args, kwargs, _, args_idxs, kwargs_idxs = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwarg_np_vals=kwarg_np_vals,
kwargs_idxs=kwargs_idxs,
input_dtypes=input_dtypes,
test_flags=test_flags,
)
_, grads_from_gt = ivy.execute_with_gradients(
grad_fn,
[args, kwargs, 1],
xs_grad_idxs=xs_grad_idxs, # noqa: F821
ret_grad_idxs=ret_grad_idxs,
)
grads_np_from_gt_flat = flatten_and_to_np(ret=grads_from_gt)
print(jsonpickle.dumps(grads_np_from_gt_flat))
def method_test():
args_np_constructor = jsonpickle.loads(make_json_pickable(input()))
con_arg_np_vals = jsonpickle.loads(make_json_pickable(input()))
con_args_idxs = jsonpickle.loads(make_json_pickable(input()))
kwargs_np_constructor = jsonpickle.loads(make_json_pickable(input()))
con_kwarg_np_vals = jsonpickle.loads(make_json_pickable(input()))
con_kwargs_idxs = jsonpickle.loads(make_json_pickable(input()))
init_input_dtypes = jsonpickle.loads(make_json_pickable(input()))
init_flags = jsonpickle.loads(make_json_pickable(input()))
args_gt_constructor, kwargs_gt_constructor, _, _, _ = create_args_kwargs(
args_np=args_np_constructor,
arg_np_vals=con_arg_np_vals,
args_idxs=con_args_idxs,
kwargs_np=kwargs_np_constructor,
kwargs_idxs=con_kwargs_idxs,
kwarg_np_vals=con_kwarg_np_vals,
input_dtypes=init_input_dtypes,
test_flags=init_flags,
)
args_np = jsonpickle.loads(make_json_pickable(input()))
arg_np_vals = jsonpickle.loads(make_json_pickable(input()))
args_idxs = jsonpickle.loads(make_json_pickable(input()))
kwargs_np = jsonpickle.loads(make_json_pickable(input()))
kwargs_idxs = jsonpickle.loads(make_json_pickable(input()))
kwarg_np_vals = jsonpickle.loads(make_json_pickable(input()))
input_dtypes = jsonpickle.loads(make_json_pickable(input()))
method_flags = jsonpickle.loads(make_json_pickable(input()))
class_name = jsonpickle.loads(make_json_pickable(input()))
method_name = jsonpickle.loads(make_json_pickable(input()))
method_input_dtypes = jsonpickle.loads(make_json_pickable(input()))
v_np = jsonpickle.loads(make_json_pickable(input()))
args_gt_method, kwargs_gt_method, _, _, _ = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwargs_idxs=kwargs_idxs,
kwarg_np_vals=kwarg_np_vals,
input_dtypes=input_dtypes,
test_flags=method_flags,
)
ins_gt = ivy.__dict__[class_name](*args_gt_constructor, **kwargs_gt_constructor)
temp_store.append([ins_gt, method_name])
# ToDo : remove this when the handle_method can properly compute unsupported dtypes
if any(
dtype in ivy.function_unsupported_dtypes(ins_gt.__getattribute__(method_name))
for dtype in method_input_dtypes
):
return
if isinstance(ins_gt, ivy.Module):
v_gt = v_np.cont_map(
lambda x, kc: ivy.asarray(x) if isinstance(x, numpy.ndarray) else x
)
kwargs_gt_method = dict(**kwargs_gt_method, v=v_gt)
ret_from_gt, ret_np_from_gt_flat = get_ret_and_flattened_np_array(
ins_gt.__getattribute__(method_name), *args_gt_method, **kwargs_gt_method
)
fw_list2 = gradient_unsupported_dtypes(fn=ins_gt.__getattribute__(method_name))
for k, v in fw_list2.items():
if k not in fw_list:
fw_list[k] = []
fw_list[k].extend(v)
print(jsonpickle.dumps([ret_np_from_gt_flat, fw_list2]))
if __name__ == "__main__":
from ivy.functional.ivy.gradients import _variable
arg_lis = sys.argv
fw_lis = []
for i in arg_lis[1:]:
if i.split("/")[0] == "jax":
fw_lis.append(i.split("/")[0] + "/" + i.split("/")[1])
fw_lis.append(i.split("/")[2] + "/" + i.split("/")[3])
else:
fw_lis.append(i)
config.allow_global_framework_imports(fw=fw_lis)
j = 1
import ivy
ivy.set_backend(arg_lis[2].split("/")[0])
import numpy
while j:
try:
z = input()
if z == "1" or z == "1a":
dtype_handler(arg_lis[2].split("/")[0], z)
continue
if z == "2":
gradient_test()
continue
if z == "3":
method_test()
continue
args_np = jsonpickle.loads(make_json_pickable(z))
arg_np_vals = jsonpickle.loads(make_json_pickable(input()))
args_idxs = jsonpickle.loads(make_json_pickable(input()))
kwargs_np = jsonpickle.loads(make_json_pickable(input()))
kwargs_idxs = jsonpickle.loads(make_json_pickable(input()))
kwarg_np_vals = jsonpickle.loads(make_json_pickable(input()))
input_dtypes = jsonpickle.loads(make_json_pickable(input()))
test_flags = jsonpickle.loads(make_json_pickable(input()))
fn_name = jsonpickle.loads(make_json_pickable(input()))
with_out = test_flags.with_out
args, kwargs, _, _, _ = create_args_kwargs(
args_np=args_np,
arg_np_vals=arg_np_vals,
args_idxs=args_idxs,
kwargs_np=kwargs_np,
kwargs_idxs=kwargs_idxs,
kwarg_np_vals=kwarg_np_vals,
input_dtypes=input_dtypes,
test_flags=test_flags,
)
ret_from_gt, ret_np_from_gt_flat = get_ret_and_flattened_np_array(
ivy.__dict__[fn_name], *args, **kwargs
)
if with_out:
test_ret_from_gt = (
ret_from_gt[getattr(ivy.__dict__[fn_name], "out_index")]
if hasattr(ivy.__dict__[fn_name], "out_index")
else ret_from_gt
)
out_from_gt = ivy.nested_map(
test_ret_from_gt,
ivy.zeros_like,
to_mutable=True,
include_derived=True,
)
ret_from_gt, ret_np_from_gt_flat = get_ret_and_flattened_np_array(
ivy.__dict__[fn_name], *args, **kwargs, out=out_from_gt
)
fw_list = gradient_unsupported_dtypes(fn=ivy.__dict__[fn_name])
ground_output = jsonpickle.dumps(
[ivy.to_numpy(ret_from_gt), ret_np_from_gt_flat, fw_list]
)
print(ground_output)
except EOFError:
continue
except Exception as e:
raise e