forked from wjakob/nanobind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ndarray.py
603 lines (487 loc) · 16.8 KB
/
test_ndarray.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import test_ndarray_ext as t
import pytest
import warnings
import importlib
from common import collect
try:
import numpy as np
def needs_numpy(x):
return x
except:
needs_numpy = pytest.mark.skip(reason="NumPy is required")
try:
import torch
def needs_torch(x):
return x
except:
needs_torch = pytest.mark.skip(reason="PyTorch is required")
try:
import tensorflow as tf
def needs_tensorflow(x):
return x
except:
needs_tensorflow = pytest.mark.skip(reason="TensorFlow is required")
try:
import jax.numpy as jnp
def needs_jax(x):
return x
except:
needs_jax = pytest.mark.skip(reason="JAX is required")
@needs_numpy
def test01_metadata():
a = np.zeros(shape=())
assert t.get_shape(a) == []
if hasattr(a, '__dlpack__'):
b = a.__dlpack__()
assert t.get_shape(b) == []
else:
b = None
with pytest.raises(TypeError) as excinfo:
# Capsule can only be consumed once
assert t.get_shape(b) == []
assert 'incompatible function arguments' in str(excinfo.value)
a = np.zeros(shape=(3, 4, 5), dtype=np.float64)
assert t.get_shape(a) == [3, 4, 5]
assert t.get_size(a) == 60
assert t.get_nbytes(a) == 60*8
assert t.get_itemsize(a) == 8
assert t.check_shape_ptr(a)
assert t.check_stride_ptr(a)
if hasattr(a, '__dlpack__'):
assert t.get_shape(a.__dlpack__()) == [3, 4, 5]
assert not t.check_float(np.array([1], dtype=np.bool_)) and \
not t.check_float(np.array([1], dtype=np.uint32)) and \
t.check_float(np.array([1], dtype=np.float32))
assert not t.check_bool(np.array([1], dtype=np.uint32)) and \
not t.check_bool(np.array([1], dtype=np.float32)) and \
t.check_bool(np.array([1], dtype=np.bool_))
def test02_docstr():
assert t.get_shape.__doc__ == "get_shape(array: ndarray[writable=False]) -> list"
assert t.pass_uint32.__doc__ == "pass_uint32(array: ndarray[dtype=uint32]) -> None"
assert t.pass_float32.__doc__ == "pass_float32(array: ndarray[dtype=float32]) -> None"
assert t.pass_bool.__doc__ == "pass_bool(array: ndarray[dtype=bool]) -> None"
assert t.pass_float32_shaped.__doc__ == "pass_float32_shaped(array: ndarray[dtype=float32, shape=(3, *, 4)]) -> None"
assert t.pass_float32_shaped_ordered.__doc__ == "pass_float32_shaped_ordered(array: ndarray[dtype=float32, order='C', shape=(*, *, 4)]) -> None"
assert t.check_device.__doc__ == ("check_device(arg: ndarray[device='cpu'], /) -> str\n"
"check_device(arg: ndarray[device='cuda'], /) -> str")
@needs_numpy
def test03_constrain_dtype():
a_u32 = np.array([1], dtype=np.uint32)
a_f32 = np.array([1], dtype=np.float32)
a_bool = np.array([1], dtype=np.bool_)
t.pass_uint32(a_u32)
t.pass_float32(a_f32)
t.pass_bool(a_bool)
with pytest.raises(TypeError) as excinfo:
t.pass_uint32(a_f32)
assert 'incompatible function arguments' in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
t.pass_float32(a_u32)
assert 'incompatible function arguments' in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
t.pass_bool(a_u32)
assert 'incompatible function arguments' in str(excinfo.value)
@needs_numpy
def test04_constrain_shape():
t.pass_float32_shaped(np.zeros((3, 0, 4), dtype=np.float32))
t.pass_float32_shaped(np.zeros((3, 5, 4), dtype=np.float32))
with pytest.raises(TypeError) as excinfo:
t.pass_float32_shaped(np.zeros((3, 5), dtype=np.float32))
with pytest.raises(TypeError) as excinfo:
t.pass_float32_shaped(np.zeros((2, 5, 4), dtype=np.float32))
with pytest.raises(TypeError) as excinfo:
t.pass_float32_shaped(np.zeros((3, 5, 6), dtype=np.float32))
with pytest.raises(TypeError) as excinfo:
t.pass_float32_shaped(np.zeros((3, 5, 4, 6), dtype=np.float32))
@needs_numpy
def test04_constrain_order():
assert t.check_order(np.zeros((3, 5, 4, 6), order='C')) == 'C'
assert t.check_order(np.zeros((3, 5, 4, 6), order='F')) == 'F'
assert t.check_order(np.zeros((3, 5, 4, 6), order='C')[:, 2, :, :]) == '?'
assert t.check_order(np.zeros((3, 5, 4, 6), order='F')[:, 2, :, :]) == '?'
@needs_jax
def test05_constrain_order_jax():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = jnp.zeros((3, 5))
except:
pytest.skip('jax is missing')
z = jnp.zeros((3, 5, 4, 6))
assert t.check_order(z) == 'C'
@needs_torch
@pytest.mark.filterwarnings
def test06_constrain_order_pytorch():
try:
c = torch.zeros(3, 5)
c.__dlpack__()
except:
pytest.skip('pytorch is missing')
f = c.t().contiguous().t()
assert t.check_order(c) == 'C'
assert t.check_order(f) == 'F'
assert t.check_order(c[:, 2:5]) == '?'
assert t.check_order(f[1:3, :]) == '?'
assert t.check_device(torch.zeros(3, 5)) == 'cpu'
if torch.cuda.is_available():
assert t.check_device(torch.zeros(3, 5, device='cuda')) == 'cuda'
@needs_tensorflow
def test07_constrain_order_tensorflow():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = tf.zeros((3, 5))
except:
pytest.skip('tensorflow is missing')
assert t.check_order(c) == 'C'
@needs_numpy
def test08_write_from_cpp():
x = np.zeros(10, dtype=np.float32)
t.initialize(x)
assert np.all(x == np.arange(10, dtype=np.float32))
x = np.zeros((10, 3), dtype=np.float32)
t.initialize(x)
assert np.all(x == np.arange(30, dtype=np.float32).reshape(10, 3))
@needs_numpy
def test09_implicit_conversion():
t.implicit(np.zeros((2, 2), dtype=np.uint32))
t.implicit(np.zeros((2, 2, 10), dtype=np.float32)[:, :, 4])
t.implicit(np.zeros((2, 2, 10), dtype=np.uint32)[:, :, 4])
t.implicit(np.zeros((2, 2, 10), dtype=np.bool_)[:, :, 4])
with pytest.raises(TypeError) as excinfo:
t.noimplicit(np.zeros((2, 2), dtype=np.bool_))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(np.zeros((2, 2), dtype=np.uint32))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(np.zeros((2, 2, 10), dtype=np.float32)[:, :, 4])
@needs_torch
def test10_implicit_conversion_pytorch():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = torch.zeros(3, 5)
c.__dlpack__()
except:
pytest.skip('pytorch is missing')
t.implicit(torch.zeros(2, 2, dtype=torch.int32))
t.implicit(torch.zeros(2, 2, 10, dtype=torch.float32)[:, :, 4])
t.implicit(torch.zeros(2, 2, 10, dtype=torch.int32)[:, :, 4])
with pytest.raises(TypeError) as excinfo:
t.noimplicit(torch.zeros(2, 2, dtype=torch.int32))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(torch.zeros(2, 2, 10, dtype=torch.float32)[:, :, 4])
@needs_tensorflow
def test11_implicit_conversion_tensorflow():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = tf.zeros((3, 5))
except:
pytest.skip('tensorflow is missing')
t.implicit(tf.zeros((2, 2), dtype=tf.int32))
t.implicit(tf.zeros((2, 2, 10), dtype=tf.float32)[:, :, 4])
t.implicit(tf.zeros((2, 2, 10), dtype=tf.int32)[:, :, 4])
t.implicit(tf.zeros((2, 2, 10), dtype=tf.bool)[:, :, 4])
with pytest.raises(TypeError) as excinfo:
t.noimplicit(tf.zeros((2, 2), dtype=tf.int32))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(tf.zeros((2, 2), dtype=tf.bool))
@needs_jax
def test12_implicit_conversion_jax():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = jnp.zeros((3, 5))
except:
pytest.skip('jax is missing')
t.implicit(jnp.zeros((2, 2), dtype=jnp.int32))
t.implicit(jnp.zeros((2, 2, 10), dtype=jnp.float32)[:, :, 4])
t.implicit(jnp.zeros((2, 2, 10), dtype=jnp.int32)[:, :, 4])
t.implicit(jnp.zeros((2, 2, 10), dtype=jnp.bool_)[:, :, 4])
with pytest.raises(TypeError) as excinfo:
t.noimplicit(jnp.zeros((2, 2), dtype=jnp.int32))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(jnp.zeros((2, 2), dtype=jnp.uint8))
def test13_destroy_capsule():
collect()
dc = t.destruct_count()
a = t.return_dlpack()
assert dc == t.destruct_count()
del a
collect()
assert t.destruct_count() - dc == 1
@needs_numpy
def test14_consume_numpy():
collect()
class wrapper:
def __init__(self, value):
self.value = value
def __dlpack__(self):
return self.value
dc = t.destruct_count()
a = t.return_dlpack()
if hasattr(np, '_from_dlpack'):
x = np._from_dlpack(wrapper(a))
elif hasattr(np, 'from_dlpack'):
x = np.from_dlpack(wrapper(a))
else:
pytest.skip('your version of numpy is too old')
del a
collect()
assert x.shape == (2, 4)
assert np.all(x == [[1, 2, 3, 4], [5, 6, 7, 8]])
assert dc == t.destruct_count()
del x
collect()
assert t.destruct_count() - dc == 1
@needs_numpy
def test15_passthrough():
a = t.ret_numpy()
b = t.passthrough(a)
assert a is b
a = np.array([1,2,3])
b = t.passthrough(a)
assert a is b
@needs_numpy
def test16_return_numpy():
collect()
dc = t.destruct_count()
x = t.ret_numpy()
assert x.shape == (2, 4)
assert np.all(x == [[1, 2, 3, 4], [5, 6, 7, 8]])
del x
collect()
assert t.destruct_count() - dc == 1
@needs_torch
def test17_return_pytorch():
try:
c = torch.zeros(3, 5)
except:
pytest.skip('pytorch is missing')
collect()
dc = t.destruct_count()
x = t.ret_pytorch()
assert x.shape == (2, 4)
assert torch.all(x == torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]))
del x
collect()
assert t.destruct_count() - dc == 1
@needs_numpy
def test18_return_array_scalar():
collect()
dc = t.destruct_count()
x = t.ret_array_scalar()
assert np.array_equal(x, np.array(1))
del x
collect()
assert t.destruct_count() - dc == 1
# See PR #162
@needs_torch
def test19_single_and_empty_dimension_pytorch():
a = torch.ones((1,100,1025), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((100,1,1025), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((0,100,1025), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((100,0,1025), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((100,1025,0), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((100,0,0), dtype=torch.float32)
t.noop_3d_c_contig(a)
a = torch.ones((0,0,0), dtype=torch.float32)
t.noop_3d_c_contig(a)
# See PR #162
@needs_numpy
def test20_single_and_empty_dimension_numpy():
a = np.ones((1,100,1025), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((100,1,1025), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((0,100,1025), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((100,0,1025), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((100,1025,0), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((100,0,0), dtype=np.float32)
t.noop_3d_c_contig(a)
a = np.ones((0,0,0), dtype=np.float32)
t.noop_3d_c_contig(a)
# See PR #162
@needs_torch
def test21_single_and_empty_dimension_fortran_order_pytorch():
# This idiom creates a pytorch 2D tensor in column major (aka, 'F') ordering
a = torch.ones((0,100), dtype=torch.float32).t().contiguous().t()
t.noop_2d_f_contig(a)
a = torch.ones((100,0), dtype=torch.float32).t().contiguous().t()
t.noop_2d_f_contig(a)
a = torch.ones((1,100), dtype=torch.float32).t().contiguous().t()
t.noop_2d_f_contig(a)
a = torch.ones((100,1), dtype=torch.float32).t().contiguous().t()
t.noop_2d_f_contig(a)
@needs_numpy
def test22_ro_array():
a = np.array([1, 2], dtype=np.float32)
assert t.accept_ro(a) == 1
assert t.accept_rw(a) == 1
a.setflags(write=False)
assert t.accept_ro(a) == 1
with pytest.raises(TypeError) as excinfo:
t.accept_rw(a)
assert 'incompatible function arguments' in str(excinfo.value)
@needs_numpy
def test22_return_ro():
x = t.ret_numpy_const_ref()
assert t.ret_numpy_const.__doc__ == 'ret_numpy_const() -> numpy.ndarray[dtype=float32, writable=False, shape=(2, 4)]'
assert x.shape == (2, 4)
assert np.all(x == [[1, 2, 3, 4], [5, 6, 7, 8]])
with pytest.raises(ValueError) as excinfo:
x[0,0] =1
assert 'read-only' in str(excinfo.value)
@needs_numpy
def test23_check_numpy():
assert t.check(np.zeros(1))
@needs_torch
def test24_check_torch():
assert t.check(torch.zeros((1)))
@needs_tensorflow
def test25_check_tensorflow():
assert t.check(tf.zeros((1)))
@needs_jax
def test26_check_jax():
assert t.check(jnp.zeros((1)))
@needs_numpy
def test27_rv_policy():
def p(a):
return a.__array_interface__['data']
x1 = t.ret_numpy_const_ref()
x2 = t.ret_numpy_const_ref()
y1 = t.ret_numpy_const()
y2 = t.ret_numpy_const()
z1 = t.passthrough(y1)
z2 = t.passthrough(y2)
q1 = t.passthrough_copy(y1)
q2 = t.passthrough_copy(y2)
assert p(x1) == p(x2)
assert p(y1) != p(y2)
assert z1 is y1
assert z2 is y2
assert q1 is not y1
assert q2 is not y2
assert p(q1) != p(y1)
assert p(q2) != p(y2)
@needs_numpy
def test28_reference_internal():
collect()
dc = t.destruct_count()
c = t.Cls()
v1_a = c.f1()
v1_b = c.f1()
v2_a = c.f2()
v2_b = c.f2()
del c
assert np.all(v1_a == np.arange(10, dtype=np.float32))
assert np.all(v1_b == np.arange(10, dtype=np.float32))
v1_a += 1
v1_b += 2
assert np.all(v1_a == np.arange(10, dtype=np.float32) + 1)
assert np.all(v1_b == np.arange(10, dtype=np.float32) + 2)
del v1_a
del v1_b
assert np.all(v2_a == np.arange(10, dtype=np.float32))
assert np.all(v2_b == np.arange(10, dtype=np.float32))
v2_a += 1
v2_b += 2
assert np.all(v2_a == np.arange(10, dtype=np.float32) + 3)
assert np.all(v2_b == np.arange(10, dtype=np.float32) + 3)
del v2_a
collect()
assert t.destruct_count() == dc
del v2_b
collect()
dc += 1
assert t.destruct_count() == dc
for i in range(2):
c2 = t.Cls()
if i == 0:
v3_a = c2.f1_ri()
v3_b = c2.f1_ri()
else:
v3_a = c2.f2_ri()
v3_b = c2.f2_ri()
del c2
assert np.all(v3_a == np.arange(10, dtype=np.float32))
assert np.all(v3_b == np.arange(10, dtype=np.float32))
v3_a += 1
v3_b += 2
assert np.all(v3_a == np.arange(10, dtype=np.float32) + 3)
assert np.all(v3_b == np.arange(10, dtype=np.float32) + 3)
del v3_a
collect()
assert t.destruct_count() == dc
del v3_b
collect()
dc += 1
assert t.destruct_count() == dc
c3 = t.Cls()
c3_t = (c3,)
with pytest.raises(RuntimeError) as excinfo:
c3.f3_ri(c3_t)
msg = 'nanobind::detail::ndarray_wrap(): reference_internal policy cannot be applied (ndarray already has an owner)'
assert msg in str(excinfo.value)
@needs_numpy
def test29_force_contig_pytorch():
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = t.make_contig(a)
assert b is a
a = a.T
b = t.make_contig(a)
assert b is not a
assert np.all(b == a)
@needs_torch
@pytest.mark.filterwarnings
def test30_force_contig_pytorch():
a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = t.make_contig(a)
assert b is a
a = a.T
b = t.make_contig(a)
assert b is not a
assert torch.all(b == a)
@needs_numpy
def test31_view():
# 1
x1 = np.array([[1,2],[3,4]], dtype=np.float32)
x2 = np.array([[1,2],[3,4]], dtype=np.float64)
assert np.allclose(x1, x2)
t.fill_view_1(x1)
assert np.allclose(x1, x2*2)
t.fill_view_1(x2)
assert np.allclose(x1, x2*2)
#2
x1 = np.zeros((3, 4), dtype=np.float32, order='C')
x2 = np.zeros((3, 4), dtype=np.float32, order='F')
t.fill_view_2(x1)
t.fill_view_2(x2)
x3 = np.zeros((3, 4), dtype=np.float32, order='C')
t.fill_view_3(x3)
x4 = np.zeros((3, 4), dtype=np.float32, order='F')
t.fill_view_4(x4)
assert np.all(x1 == x2) and np.all(x2 == x3) and np.all(x3 == x4)
@needs_numpy
def test32_half():
if not hasattr(t, 'ret_numpy_half'):
pytest.skip('half precision test is missing')
x = t.ret_numpy_half()
assert x.dtype == np.float16
assert x.shape == (2, 4)
assert np.all(x == [[1, 2, 3, 4], [5, 6, 7, 8]])
@needs_numpy
def test33_cast():
a = t.cast(False)
b = t.cast(True)
assert a.ndim == 0 and b.ndim == 0
assert a.dtype == np.int32 and b.dtype == np.float32
assert a == 1 and b == 1