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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
|
import test_ndarray_ext as t
import test_jax_ext as tj
import test_tensorflow_ext as tt
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
import tensorflow.config
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")
try:
import cupy as cp
def needs_cupy(x):
return x
except:
needs_cupy = pytest.mark.skip(reason="CuPy 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_is_valid(a)
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_))
assert not t.get_is_valid(None)
assert t.get_size(None) == 0
assert t.get_nbytes(None) == 0
assert t.get_itemsize(None) == 0
def test02_docstr():
assert t.pass_uint32.__doc__ == "pass_uint32(array: ndarray[dtype=uint32]) -> None"
assert t.get_shape.__doc__ == "get_shape(array: ndarray[writable=False]) -> list"
assert t.pass_float32.__doc__ == "pass_float32(array: ndarray[dtype=float32]) -> None"
assert t.pass_complex64.__doc__ == "pass_complex64(array: ndarray[dtype=complex64]) -> 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, shape=(*, *, 4), order='C']) -> 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_cf64 = np.array([1+1j], dtype=np.complex64)
a_bool = np.array([1], dtype=np.bool_)
t.pass_uint32(a_u32)
t.pass_float32(a_f32)
t.pass_complex64(a_cf64)
t.pass_complex64_const(a_cf64)
t.pass_bool(a_bool)
a_f32_const = a_f32.copy()
a_f32_const.flags.writeable = False
t.pass_float32_const(a_f32_const)
a_cf64_const = a_cf64.copy()
a_cf64_const.flags.writeable = False
t.pass_complex64_const(a_cf64_const)
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_complex64(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 test05_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 test06_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 test07_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 test08_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 test09_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 test10_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 test11_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 test12_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 test13_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 test14_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 test15_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 test16_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
a = None
with pytest.raises(TypeError) as excinfo:
b = t.passthrough(a)
assert 'incompatible function arguments' in str(excinfo.value)
b = t.passthrough_arg_none(a)
assert a is b
@needs_numpy
def test17_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 test18_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_jax
def test19_return_jax():
collect()
dc = tj.destruct_count()
x = tj.ret_jax()
assert x.shape == (2, 4)
assert jnp.all(x == jnp.array([[1,2,3,4], [5,6,7,8]], dtype=jnp.float32))
del x
collect()
assert tj.destruct_count() - dc == 1
@needs_tensorflow
def test20_return_tensorflow():
collect()
dc = tt.destruct_count()
x = tt.ret_tensorflow()
assert x.get_shape().as_list() == [2, 4]
assert tf.math.reduce_all(
x == tf.constant([[1,2,3,4], [5,6,7,8]], dtype=tf.float32))
del x
collect()
assert tt.destruct_count() - dc == 1
@needs_numpy
def test21_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 test22_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 test23_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 test24_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 test25_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 test26_return_ro():
x = t.ret_numpy_const_ref()
y = t.ret_numpy_const_ref_f()
assert t.ret_numpy_const_ref.__doc__ == 'ret_numpy_const_ref() -> numpy.ndarray[dtype=float32, shape=(2, 4), order=\'C\', writable=False]'
assert t.ret_numpy_const_ref_f.__doc__ == 'ret_numpy_const_ref_f() -> numpy.ndarray[dtype=float32, shape=(2, 4), order=\'F\', writable=False]'
assert x.shape == (2, 4)
assert y.shape == (2, 4)
assert np.all(x == [[1, 2, 3, 4], [5, 6, 7, 8]])
assert np.all(y == [[1, 3, 5, 7], [2, 4, 6, 8]])
with pytest.raises(ValueError) as excinfo:
x[0,0] =1
assert 'read-only' in str(excinfo.value)
with pytest.raises(ValueError) as excinfo:
y[0,0] =1
assert 'read-only' in str(excinfo.value)
@needs_numpy
def test27_check_numpy():
assert t.check(np.zeros(1))
@needs_torch
def test28_check_torch():
assert t.check(torch.zeros((1)))
@needs_tensorflow
def test29_check_tensorflow():
assert t.check(tf.zeros((1)))
@needs_jax
def test30_check_jax():
assert t.check(jnp.zeros((1)))
@needs_numpy
def test31_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 test32_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_export(): reference_internal policy cannot be applied (ndarray already has an owner)'
assert msg in str(excinfo.value)
@needs_numpy
def test33_force_contig_numpy():
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 test34_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 test35_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)
# 3
x1 = np.array([[1+2j, 3+4j], [5+6j, 7+8j]], dtype=np.complex64)
x2 = x1 * 2
t.fill_view_1(x1.view(np.float32))
assert np.allclose(x1, x2)
x2 = x1 * (-1+2j)
t.fill_view_5(x1)
assert np.allclose(x1, x2)
x2 = -x2;
t.fill_view_6(x1)
assert np.allclose(x1, x2)
@needs_numpy
def test36_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 test37_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
@needs_numpy
def test38_complex_decompose():
x1 = np.array([1 + 2j, 3 + 4j, 5 + 6j], dtype=np.complex64)
assert np.all(x1.real == np.array([1, 3, 5], dtype=np.float32))
assert np.all(x1.imag == np.array([2, 4, 6], dtype=np.float32))
@needs_numpy
@pytest.mark.parametrize("variant", [1, 2])
def test_uint32_complex_do_not_convert(variant):
if variant == 1:
arg = 1
else:
arg = np.uint32(1)
data = np.array([1.0 + 2.0j, 3.0 + 4.0j])
t.set_item(data, arg)
data2 = np.array([123, 3.0 + 4.0j])
assert np.all(data == data2)
@needs_numpy
def test40_check_generic():
class DLPackWrapper:
def __init__(self, o):
self.o = o
def __dlpack__(self):
return self.o.__dlpack__()
arr = DLPackWrapper(np.zeros((1)))
assert t.check(arr)
@needs_numpy
def test41_noninteger_stride():
a = np.array([[1, 2, 3, 4, 0, 0], [5, 6, 7, 8, 0, 0]], dtype=np.float32)
s = a[:, 0:4] # slice
t.pass_float32(s)
assert t.get_stride(s, 0) == 6;
assert t.get_stride(s, 1) == 1;
try:
v = s.view(np.complex64)
except:
pytest.skip('your version of numpy is too old')
t.pass_complex64(v)
assert t.get_stride(v, 0) == 3;
assert t.get_stride(v, 1) == 1;
a = np.array([[1, 2, 3, 4, 0], [5, 6, 7, 8, 0]], dtype=np.float32)
s = a[:, 0:4] # slice
t.pass_float32(s)
assert t.get_stride(s, 0) == 5;
assert t.get_stride(s, 1) == 1;
v = s.view(np.complex64)
with pytest.raises(TypeError) as excinfo:
t.pass_complex64(v)
assert 'incompatible function arguments' in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
t.get_stride(v, 0);
assert 'incompatible function arguments' in str(excinfo.value)
@needs_numpy
def test42_const_qualifiers_numpy():
a = np.array([0, 0, 0, 3.14159, 0], dtype=np.float64)
assert t.check_rw_by_value(a);
assert a[1] == 1.414214;
assert t.check_rw_by_value_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_value_ro(a);
assert t.check_ro_by_value_const_float64(a);
a.setflags(write=False)
assert t.check_ro_by_value_ro(a);
assert t.check_ro_by_value_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
a = np.array([0, 0, 0, 3.14159, 0], dtype=np.float64)
assert t.check_rw_by_const_ref(a);
assert a[1] == 1.414214;
assert t.check_rw_by_const_ref_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_const_ref_ro(a);
assert t.check_ro_by_const_ref_const_float64(a);
a.setflags(write=False)
assert t.check_ro_by_const_ref_ro(a);
assert t.check_ro_by_const_ref_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
a = np.array([0, 0, 0, 3.14159, 0], dtype=np.float64)
assert t.check_rw_by_rvalue_ref(a);
assert a[1] == 1.414214;
assert t.check_rw_by_rvalue_ref_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_rvalue_ref_ro(a);
assert t.check_ro_by_rvalue_ref_const_float64(a);
a.setflags(write=False)
assert t.check_ro_by_rvalue_ref_ro(a);
assert t.check_ro_by_rvalue_ref_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
@needs_torch
def test43_const_qualifiers_pytorch():
a = torch.tensor([0, 0, 0, 3.14159, 0], dtype=torch.float64)
assert t.check_rw_by_value(a);
assert a[1] == 1.414214;
assert t.check_rw_by_value_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_value_ro(a);
assert t.check_ro_by_value_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
a = torch.tensor([0, 0, 0, 3.14159, 0], dtype=torch.float64)
assert t.check_rw_by_const_ref(a);
assert a[1] == 1.414214;
assert t.check_rw_by_const_ref_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_const_ref_ro(a);
assert t.check_ro_by_const_ref_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
a = torch.tensor([0, 0, 0, 3.14159, 0], dtype=torch.float64)
assert t.check_rw_by_rvalue_ref(a);
assert a[1] == 1.414214;
assert t.check_rw_by_rvalue_ref_float64(a);
assert a[2] == 2.718282;
assert a[4] == 16.0;
assert t.check_ro_by_rvalue_ref_ro(a);
assert t.check_ro_by_rvalue_ref_const_float64(a);
assert a[0] == 0.0;
assert a[3] == 3.14159;
@needs_cupy
@pytest.mark.filterwarnings
def test44_constrain_order_cupy():
try:
c = cp.zeros((3, 5))
c.__dlpack__()
except:
pytest.skip('cupy is missing')
f = cp.asarray(c, order="F")
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(cp.zeros((3, 5))) == 'cuda'
@needs_cupy
def test45_implicit_conversion_cupy():
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
c = cp.zeros((3, 5))
except:
pytest.skip('cupy is missing')
t.implicit(cp.zeros((2, 2), dtype=cp.int32))
t.implicit(cp.zeros((2, 2, 10), dtype=cp.float32)[:, :, 4])
t.implicit(cp.zeros((2, 2, 10), dtype=cp.int32)[:, :, 4])
t.implicit(cp.zeros((2, 2, 10), dtype=cp.bool_)[:, :, 4])
with pytest.raises(TypeError) as excinfo:
t.noimplicit(cp.zeros((2, 2), dtype=cp.int32))
with pytest.raises(TypeError) as excinfo:
t.noimplicit(cp.zeros((2, 2), dtype=cp.uint8))
@needs_numpy
def test46_implicit_conversion_contiguous_complex():
# Test fix for issue #709
import numpy as np
c_f32 = np.random.rand(10, 10)
c_c64 = c_f32.astype(np.complex64)
assert c_f32.flags['C_CONTIGUOUS']
assert c_c64.flags['C_CONTIGUOUS']
def test_conv(x):
y = t.test_implicit_conversion(x)
assert np.all(x == y)
assert y.flags['C_CONTIGUOUS']
test_conv(c_f32)
test_conv(c_c64)
nc_f32 = c_f32.T
nc_c64 = c_c64.T
assert not nc_f32.flags['C_CONTIGUOUS']
assert not nc_c64.flags['C_CONTIGUOUS']
test_conv(nc_f32)
test_conv(nc_c64)
@needs_numpy
def test_47_ret_infer():
import numpy as np
assert np.all(t.ret_infer_c() == [[1, 2, 3, 4], [5, 6, 7, 8]])
assert np.all(t.ret_infer_f() == [[1, 3, 5, 7], [2, 4, 6, 8]])
@needs_numpy
def test48_test_matrix4f():
a = t.Matrix4f()
ad = a.data()
bd = a.data()
for i in range(16):
ad[i%4, i//4] = i
del a, ad
for i in range(16):
assert bd[i%4, i//4] == i
@needs_numpy
def test49_test_matrix4f_ref():
assert t.Matrix4f.data_ref.__doc__.replace('data_ref', 'data') == t.Matrix4f.data.__doc__
a = t.Matrix4f()
ad = a.data_ref()
bd = a.data_ref()
for i in range(16):
ad[i%4, i//4] = i
del a, ad
for i in range(16):
assert bd[i%4, i//4] == i
@needs_numpy
def test50_test_matrix4f_copy():
assert t.Matrix4f.data_ref.__doc__.replace('data_ref', 'data') == t.Matrix4f.data.__doc__
a = t.Matrix4f()
ad = a.data_ref()
for i in range(16):
ad[i%4, i//4] = i
bd = a.data_copy()
for i in range(16):
ad[i%4, i//4] = 0
del a, ad
for i in range(16):
assert bd[i%4, i//4] == i
@needs_numpy
def test51_return_from_stack():
import numpy as np
assert np.all(t.ret_from_stack_1() == [1,2,3])
assert np.all(t.ret_from_stack_2() == [1,2,3])
@needs_numpy
def test52_accept_np_both_true_contig():
import numpy as np
a = np.zeros((2, 1), dtype=np.float32)
assert a.flags['C_CONTIGUOUS'] and a.flags['F_CONTIGUOUS']
t.accept_np_both_true_contig_a(a)
t.accept_np_both_true_contig_c(a)
t.accept_np_both_true_contig_f(a)
@needs_numpy
def test53_issue_930():
import numpy as np
wrapper = t.Wrapper(np.ones(3, dtype=np.float32))
assert wrapper.value[0] == 1
|