forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_udf.py
More file actions
532 lines (417 loc) · 13.5 KB
/
Copy pathtest_udf.py
File metadata and controls
532 lines (417 loc) · 13.5 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
import functools
import math
import numpy as np
import pytest
import iarray as ia
from iarray import udf
def cmp_udf_np(
f,
start_stop,
shape,
chunks,
blocks,
dtype,
cparams,
input_factory=ia.linspace,
user_params=None,
f_np=None,
np_dtype=None,
):
"""Helper function that compares UDF against numpy.
Parameters:
f : The User-Defined-Function.
start_stop : A list of tuples defining the input arrays. Each tuple
has 2 elements with the start and stop arguments that
define each input array.
shape : Shape of the iarrays.
chunks : Chunks shape for iarrays.
blocks : Blocks shape for iarrays.
dtype : Data type.
cparams : Configuration parameters for ironArray.
input_factory: function used to generate the input arrays, by default linspace
user_params : User parameters (scalars)
f_np : An equivalent function for NumPy (for incompatible UDFs).
Function results must not depend on chunks/blocks, otherwise the
comparison with numpy will fail.
"""
assert type(start_stop) is list
if f_np is None:
f_np = f.py_function
cfg = ia.Config(chunks=chunks, blocks=blocks)
if input_factory == ia.linspace:
inputs = [
input_factory(
start,
stop,
num=int(np.prod(shape)),
shape=shape,
cfg=cfg,
dtype=dtype,
np_dtype=None,
**cparams,
)
for start, stop in start_stop
]
else:
inputs = [
input_factory(start, stop, shape=shape, cfg=cfg, dtype=dtype, np_dtype=None, **cparams)
for start, stop in start_stop
]
expr = ia.expr_from_udf(
f, inputs, user_params, shape=shape, dtype=dtype, np_dtype=np_dtype, cfg=cfg, **cparams
)
out = expr.eval()
out_dtype = dtype if np_dtype is None else np.dtype(np_dtype)
num = functools.reduce(lambda x, y: x * y, shape)
out_ref = np.zeros(num, dtype=out_dtype).reshape(shape)
args = [x.data for x in inputs]
if user_params is not None:
args += user_params
f_np(out_ref, *args)
if out_dtype in [np.float64, np.float32]:
ia.cmp_arrays(out, out_ref)
else:
if type(out) is ia.IArray:
out = ia.iarray2numpy(out)
if type(out_ref) is ia.IArray:
out_ref = ia.iarray2numpy(out_ref)
np.testing.assert_array_equal(out, out_ref)
def cmp_udf_np_strict(f, start, stop, shape, partitions, dtype, cparams):
"""Same as cmp_udf_np but the comparison is done strictly. This is to say:
numpy arrays are evaluated chunk by chunk, this way it works even when the
function accesses elements other than the current element.
Contraints:
- Input is always 1 linspace array, defined by start and stop
- Only works for 1 dimension arrays
"""
chunks, blocks = partitions
assert len(chunks) == 1
assert len(blocks) == 1
cfg = ia.Config(chunks=chunks, blocks=blocks)
x = ia.linspace(start, stop, int(np.prod(shape)), shape=shape, cfg=cfg, dtype=dtype, **cparams)
assert x.cfg.dtype == dtype
expr = ia.expr_from_udf(f, [x], cfg=cfg, **cparams)
out = expr.eval()
num = functools.reduce(lambda x, y: x * y, shape)
x_ref = np.linspace(start, stop, num, dtype=dtype).reshape(shape)
out_ref = np.zeros(num, dtype=dtype).reshape(shape)
indices = range(0, num, blocks[0])
for out_ref_slice, x_ref_slice in zip(
np.array_split(out_ref, indices), np.array_split(x_ref, indices)
):
f.py_function(out_ref_slice, x_ref_slice)
ia.cmp_arrays(out, out_ref)
@udf.jit()
def f_1dim(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[0]
for i in range(n):
if i % 3 == 0:
out[i] = 0.0
elif x[i] > 1.0 or x[i] <= 3.0 and i % 2 == 0:
out[i] = (math.sin(x[i]) + 1.35) * (x[i] + 4.45) * (x[i] + 8.5)
else:
out[i] = (math.sin(x[i]) - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return 0
@udf.jit
def f_fabs_copysign(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[0]
for i in range(n):
out[i] = math.fabs(x[i]) + math.copysign(x[i], -1.0)
return 0
@udf.jit
def f_while(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = x.shape[0]
i = 0
while i < n:
out[i] = (math.sin(x[i]) - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
i = i + 1
return 0
@udf.jit
def f_1dim_int(out: udf.Array(udf.int64, 1), x: udf.Array(udf.int64, 1)):
n = out.shape[0]
for i in range(n):
if i % 3 == 0:
out[i] = 0
elif x[i] > 1 or x[i] <= 3 and i % 2 == 0:
out[i] = (x[i] + 4) * (x[i] + 8)
else:
out[i] = (x[i] - 4) * (x[i] - 8)
return 0
@udf.jit
def f_1dim_f32(out: udf.Array(udf.float32, 1), x: udf.Array(udf.float32, 1)):
n = out.shape[0]
for i in range(n):
if i % 3 == 0:
out[i] = 0.0
elif x[i] > 1.0 or x[i] <= 3.0 and i % 2 == 0:
out[i] = (math.sin(x[i]) + 1.35) * (x[i] + 4.45) * (x[i] + 8.5)
else:
out[i] = (math.sin(x[i]) - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return 0
@udf.jit
def f_math(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[0]
for i in range(n):
if x[i] > 0.0:
out[i] = (
math.log(x[i])
+ math.log10(x[i])
+ math.sqrt(x[i])
+ math.floor(x[i])
+ math.ceil(x[i])
+ math.fabs(x[i])
)
else:
out[i] = x[i]
return 0
@udf.jit
def f_math_int(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[0]
for i in range(n):
out[i] = x[i] * math.cos(1)
return 0
@udf.jit
def f_avg(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = x.shape[0]
for i in range(n):
value = x[i]
value += x[i - 1] if i > 0 else x[i]
value += x[i + 1] if i < n - 1 else x[i]
out[i] = value / 3
return 0
@udf.jit
def f_power(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
for i in range(out.shape[0]):
out[i] = 2.71828 ** x[i]
return 0
@udf.jit
def f_unary_float(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
for i in range(out.shape[0]):
out[i] = -x[i]
return 0
@udf.jit
def f_unary_int(out: udf.Array(udf.int64, 1), x: udf.Array(udf.int64, 1)):
n = out.shape[0]
for i in range(out.shape[0]):
out[i] = -x[i]
return 0
@udf.jit
def f_idx_const(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[0]
for i in range(n):
out[0] = x[i]
return 0
@udf.jit
def f_idx_var(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
var = 0
n = out.shape[0]
for i in range(n):
out[var] = x[i]
return 0
@pytest.mark.parametrize(
"f, dtype",
[
(f_1dim, np.float64),
(f_fabs_copysign, np.float64),
(f_while, np.float64),
(f_1dim_int, np.int64),
(f_1dim_f32, np.float32),
(f_math, np.float64),
(f_math_int, np.float64),
(f_avg, np.float64),
# Power
(f_power, np.float64),
# Unary operator
(f_unary_float, np.float64),
(f_unary_int, np.int64),
# https://github.com/inaos/iron-array/issues/502
(f_idx_const, np.float64),
(f_idx_var, np.float64),
],
)
def test_1dim(f, dtype):
shape = [10 * 1000]
chunks = [3 * 1000]
blocks = [3 * 100]
cparams = dict(nthreads=16)
start, stop = 0, 100 # to avoid overflows, don't use a too large stop here
cmp_udf_np_strict(f, start, stop, shape, (chunks, blocks), dtype, cparams)
@pytest.mark.parametrize("f", [f_1dim])
def test_partition_mismatch(f):
shape = [10 * 1000]
chunks = [3 * 1000]
blocks = [3 * 100]
dtype = np.float64
cparams = dict(nthreads=16)
start, stop = 0, 10
# For the test function to return the same output as the Python function
# the partition size must be multiple of 3. This is just an example of
# how the result is not always the same as in the Python function.
blocks = [4 * 100]
with pytest.raises(AssertionError):
cmp_udf_np(f, [(start, stop)], shape, chunks, blocks, dtype, cparams)
@udf.jit
def f_2dim(out: udf.Array(udf.float64, 2), x: udf.Array(udf.float64, 2)):
n = x.shape[0]
m = x.shape[1]
for i in range(n):
for j in range(m):
out[i, j] = (math.sin(x[i, j]) - 1.35) * (x[i, j] - 4.45) * (x[i, j] - 8.5)
return 0
@pytest.mark.parametrize("f", [f_2dim])
def test_2dim(f):
shape = [40, 80] # [400, 800]
chunks = [6, 20] # [60, 200]
blocks = [4, 2] # [11, 200]
dtype = np.float64
cparams = dict()
start, stop = 0, 10
cmp_udf_np(f, [(start, stop)], shape, chunks, blocks, dtype, cparams)
@udf.jit
def f_ifexp(out: udf.Array(udf.float64, 2)):
n = out.shape[0]
m = out.shape[1]
start_n = out.window_start[0]
start_m = out.window_start[1]
for i in range(n):
for j in range(m):
out[i, j] = 1.0 if i + start_n == j + start_m else 0.0
return 0
# NumPy counterpart of the above
def f_ifexp_np(out):
n = out.shape[0]
m = out.shape[1]
for i in range(n):
for j in range(m):
out[i, j] = 1.0 if i == j else 0.0
return 0
@pytest.mark.parametrize("f, f_np", [(f_ifexp, f_ifexp_np)])
def test_ifexp(f, f_np):
shape = [400, 800]
chunkshape = [60, 200]
blockshape = [11, 200]
dtype = np.float64
cparams = dict()
cmp_udf_np(f, [], shape, chunkshape, blockshape, dtype, cparams, f_np=f_np)
@udf.jit
def f_ifexp2(out: udf.Array(udf.float64, 2)):
n = out.shape[0]
m = out.shape[1]
if UDFJIT:
start_n = out.window_start[0]
start_m = out.window_start[1]
for i in range(n):
for j in range(m):
if UDFJIT:
out[i, j] = 1.0 if i + start_n == j + start_m else 0.0
else:
out[i, j] = 1.0 if i == j else 0.0
return 0
@pytest.mark.parametrize("f", [f_ifexp2])
def test_ifexp2(f):
shape = [400, 800]
chunkshape = [60, 200]
blockshape = [11, 200]
dtype = np.float64
cparams = dict()
cmp_udf_np(f, [], shape, chunkshape, blockshape, dtype, cparams)
@udf.jit
def f_error_bug(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
n = out.shape[1]
for i in range(n):
out[i] = (math.sin(x[i]) - 1.35) * (x[i] - 4.45) * (x[i] - 8.5)
return 0
@udf.jit
def f_error_user(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
return 1
@pytest.mark.parametrize("f", [f_error_bug, f_error_user])
def test_error(f):
shape = [20 * 1000]
chunks = [4 * 1000]
blocks = [1 * 1000]
dtype = np.float64
cparams = dict(nthreads=1)
start, stop = 0, 10
cfg = ia.Config(chunks=chunks, blocks=blocks)
x = ia.linspace(start, stop, int(np.prod(shape)), shape=shape, cfg=cfg, dtype=dtype, **cparams)
expr = f.create_expr([x], cfg=cfg, **cparams)
with pytest.raises(ia.IArrayError):
expr.eval()
def f_unsupported_function(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
math.isinf(5.0)
return 0
def f_bad_argument_count(out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1)):
math.pow(5)
return 0
def test_function_call_errors():
with pytest.raises(TypeError):
udf.jit(f_unsupported_function)
with pytest.raises(TypeError):
udf.jit(f_bad_argument_count)
@udf.jit
def f_math2(
out: udf.Array(udf.float64, 1), x: udf.Array(udf.float64, 1), y: udf.Array(udf.float64, 1)
):
n = out.shape[0]
for i in range(n):
out[i] = math.pow(x[i], y[i]) + math.atan2(x[i], y[i])
return 0
@pytest.mark.parametrize("f", [f_math2])
def test_math2(f):
shape = [10 * 1000]
chunks = [3 * 1000]
blocks = [3 * 100]
dtype = np.float64
cparams = dict(nthreads=16)
start, stop = 0, 10
cmp_udf_np(f, [(start, stop), (start, stop)], shape, chunks, blocks, dtype, cparams)
@udf.jit
def f_user_params(
out: udf.Array(udf.float64, 1),
x: udf.Array(udf.float64, 1),
a: udf.float64,
b: udf.float64,
divide: udf.bool,
):
n = out.shape[0]
for i in range(n):
if divide:
out[i] = x[i] / a + b
else:
out[i] = x[i] * a + b
return 0
@pytest.mark.parametrize("f", [f_user_params])
def test_user_params(f):
shape = [10 * 1000]
chunks = [3 * 1000]
blocks = [3 * 100]
dtype = np.float64
cparams = dict(nthreads=16)
start, stop = 0, 10
user_params = [2.5, 1, True]
cmp_udf_np(f, [(start, stop)], shape, chunks, blocks, dtype, cparams, user_params=user_params)
@udf.jit
def f_idx_int(out: udf.Array(udf.int64, 1), x: udf.Array(udf.int64, 1)):
n = out.shape[0]
for i in range(n):
out[i] = x[i]
return 0
@pytest.mark.parametrize("f", [f_idx_int])
def test_idx_var_datetime(f):
shape = [10 * 1000]
chunks = [3 * 1000]
blocks = [3 * 100]
dtype = np.int64
cparams = dict(nthreads=16)
start, stop = 0, 10 * 1000
cmp_udf_np(
f,
[(start, stop)],
shape,
chunks,
blocks,
dtype,
cparams,
input_factory=ia.arange,
np_dtype="m8[Y]",
)