|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | + |
| 4 | +import numpy |
| 5 | +import pytest |
| 6 | + |
| 7 | +import dpnp as cupy |
| 8 | +from tests.third_party.cupy import testing |
| 9 | + |
| 10 | + |
| 11 | +class TestSize(unittest.TestCase): |
| 12 | + # def tearDown(self): |
| 13 | + # # Free huge memory for slow test |
| 14 | + # cupy.get_default_memory_pool().free_all_blocks() |
| 15 | + |
| 16 | + @testing.for_all_dtypes() |
| 17 | + @testing.numpy_cupy_equal() |
| 18 | + def test_size(self, xp, dtype): |
| 19 | + a = xp.ndarray((2, 3), dtype=dtype) |
| 20 | + return xp.size(a) |
| 21 | + |
| 22 | + @testing.for_all_dtypes() |
| 23 | + @testing.numpy_cupy_equal() |
| 24 | + def test_size_axis(self, xp, dtype): |
| 25 | + a = xp.ndarray((2, 3), dtype=dtype) |
| 26 | + return xp.size(a, axis=1) |
| 27 | + |
| 28 | + @testing.for_all_dtypes() |
| 29 | + def test_size_axis_error(self, dtype): |
| 30 | + for xp in (numpy, cupy): |
| 31 | + a = xp.ndarray((2, 3), dtype=dtype) |
| 32 | + with pytest.raises(IndexError): |
| 33 | + return xp.size(a, axis=3) |
| 34 | + |
| 35 | + @testing.numpy_cupy_equal() |
| 36 | + @testing.slow |
| 37 | + def test_size_huge(self, xp): |
| 38 | + a = xp.ndarray(2**32, "b") # 4 GiB |
| 39 | + return xp.size(a) |
| 40 | + |
| 41 | + |
| 42 | +_orders = { |
| 43 | + order_arg: order_expect |
| 44 | + for order_expect, order_args in [ |
| 45 | + ("C", ["C", "c", "CONTIGUOUS", "", None]), |
| 46 | + ("F", ["F", "f", "FORTRAN"]), |
| 47 | + ] |
| 48 | + for order_arg in order_args |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.skip("no cupy._core submodule") |
| 53 | +class TestOrder(unittest.TestCase): |
| 54 | + @testing.for_orders(_orders.keys()) |
| 55 | + def test_ndarray(self, order): |
| 56 | + order_expect = _orders[order] |
| 57 | + a = core.ndarray((2, 3), order=order) |
| 58 | + expect_c = order_expect == "C" |
| 59 | + expect_f = order_expect == "F" |
| 60 | + assert a.flags.c_contiguous == expect_c |
| 61 | + assert a.flags.f_contiguous == expect_f |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.skip("min_scalar_type() is not supported") |
| 65 | +class TestMinScalarType: |
| 66 | + def test_scalar(self): |
| 67 | + for v in (-129, -128, 0, 1.2, numpy.inf): |
| 68 | + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) |
| 69 | + |
| 70 | + @testing.for_all_dtypes() |
| 71 | + def test_numpy_scalar(self, dtype): |
| 72 | + sc = dtype(1) |
| 73 | + for v in (sc, [sc, sc]): |
| 74 | + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) |
| 75 | + |
| 76 | + @testing.for_all_dtypes() |
| 77 | + def test_cupy_scalar(self, dtype): |
| 78 | + sc = cupy.array(-1).astype(dtype) |
| 79 | + for v in (sc, [sc, sc]): |
| 80 | + assert cupy.min_scalar_type(v) is sc.dtype |
| 81 | + |
| 82 | + @testing.for_all_dtypes() |
| 83 | + def test_numpy_ndarray(self, dtype): |
| 84 | + arr = numpy.array([[-1, 1]]).astype(dtype) |
| 85 | + for v in (arr, (arr, arr)): |
| 86 | + assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v) |
| 87 | + |
| 88 | + @testing.for_all_dtypes() |
| 89 | + def test_cupy_ndarray(self, dtype): |
| 90 | + arr = cupy.array([[-1, 1]]).astype(dtype) |
| 91 | + for v in (arr, (arr, arr)): |
| 92 | + assert cupy.min_scalar_type(v) is arr.dtype |
| 93 | + |
| 94 | + |
| 95 | +@testing.parameterize( |
| 96 | + *testing.product( |
| 97 | + { |
| 98 | + "cxx": (None, "--std=c++11"), |
| 99 | + } |
| 100 | + ) |
| 101 | +) |
| 102 | +@pytest.mark.skip("compiling cupy headers are not supported") |
| 103 | +class TestCuPyHeaders(unittest.TestCase): |
| 104 | + def setUp(self): |
| 105 | + self.temporary_cache_dir_context = test_raw.use_temporary_cache_dir() |
| 106 | + self.cache_dir = self.temporary_cache_dir_context.__enter__() |
| 107 | + self.header = "\n".join( |
| 108 | + ["#include <" + h + ">" for h in core._cupy_header_list] |
| 109 | + ) |
| 110 | + |
| 111 | + def tearDown(self): |
| 112 | + self.temporary_cache_dir_context.__exit__(*sys.exc_info()) |
| 113 | + |
| 114 | + def test_compiling_core_header(self): |
| 115 | + code = r""" |
| 116 | + extern "C" __global__ void _test_ker_() { } |
| 117 | + """ |
| 118 | + code = self.header + code |
| 119 | + options = () if self.cxx is None else (self.cxx,) |
| 120 | + ker = cupy.RawKernel( |
| 121 | + code, "_test_ker_", options=options, backend="nvrtc" |
| 122 | + ) |
| 123 | + ker((1,), (1,), ()) |
| 124 | + cupy.cuda.Device().synchronize() |
0 commit comments