|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +import numpy |
| 6 | +import pytest |
| 7 | + |
| 8 | +import dpnp as cupy |
| 9 | +import dpnp.special |
| 10 | +from dpnp.tests.third_party.cupy import testing |
| 11 | + |
| 12 | + |
| 13 | +def _boundary_inputs(boundary, rtol, atol): |
| 14 | + left = boundary * (1 - numpy.copysign(rtol, boundary)) - atol |
| 15 | + right = boundary * (1 + numpy.copysign(rtol, boundary)) + atol |
| 16 | + return [left, boundary, right] |
| 17 | + |
| 18 | + |
| 19 | +@testing.with_requires("scipy") |
| 20 | +class _TestBase: |
| 21 | + |
| 22 | + # @testing.with_requires('scipy>=1.16.0') |
| 23 | + def test_erf(self): |
| 24 | + self.check_unary("erf") |
| 25 | + |
| 26 | + @pytest.mark.skip("erfc() is not supported yet") |
| 27 | + @testing.with_requires("scipy>=1.16.0") |
| 28 | + def test_erfc(self): |
| 29 | + self.check_unary("erfc") |
| 30 | + |
| 31 | + @pytest.mark.skip("erfcx() is not supported yet") |
| 32 | + @testing.with_requires("scipy>=1.16.0") |
| 33 | + def test_erfcx(self): |
| 34 | + self.check_unary("erfcx") |
| 35 | + |
| 36 | + @pytest.mark.skip("erfinv() is not supported yet") |
| 37 | + def test_erfinv(self): |
| 38 | + self.check_unary("erfinv") |
| 39 | + self.check_unary_random("erfinv", scale=2, offset=-1) |
| 40 | + self.check_unary_boundary("erfinv", boundary=-1) |
| 41 | + self.check_unary_boundary("erfinv", boundary=1) |
| 42 | + |
| 43 | + @pytest.mark.skip("erfcinv() is not supported yet") |
| 44 | + def test_erfcinv(self): |
| 45 | + self.check_unary("erfcinv") |
| 46 | + self.check_unary_random("erfcinv", scale=2, offset=0) |
| 47 | + self.check_unary_boundary("erfcinv", boundary=0) |
| 48 | + self.check_unary_boundary("erfcinv", boundary=2) |
| 49 | + |
| 50 | + |
| 51 | +@testing.with_requires("scipy") |
| 52 | +class TestSpecial(unittest.TestCase, _TestBase): |
| 53 | + |
| 54 | + @testing.for_dtypes(["e", "f", "d"]) |
| 55 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 56 | + def check_unary(self, name, xp, scp, dtype): |
| 57 | + import scipy.special |
| 58 | + |
| 59 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 60 | + return getattr(scp.special, name)(a) |
| 61 | + |
| 62 | + @testing.for_dtypes(["f", "d"]) |
| 63 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 64 | + def check_unary_random(self, name, xp, scp, dtype, scale, offset): |
| 65 | + import scipy.special |
| 66 | + |
| 67 | + a = testing.shaped_random((2, 3), xp, dtype, scale=scale) + offset |
| 68 | + return getattr(scp.special, name)(a) |
| 69 | + |
| 70 | + @testing.for_dtypes(["f", "d"]) |
| 71 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 72 | + def check_unary_boundary(self, name, xp, scp, dtype, boundary): |
| 73 | + import scipy.special |
| 74 | + |
| 75 | + a = _boundary_inputs(boundary, 1.0 / 1024, 1.0 / 1024) |
| 76 | + a = xp.array(a, dtype=dtype) |
| 77 | + return getattr(scp.special, name)(a) |
| 78 | + |
| 79 | + @pytest.mark.skip("erfinv() is not supported yet") |
| 80 | + @testing.with_requires("scipy>=1.4.0") |
| 81 | + @testing.for_dtypes(["f", "d"]) |
| 82 | + def test_erfinv_behavior(self, dtype): |
| 83 | + a = cupy.empty((1,), dtype=dtype) |
| 84 | + |
| 85 | + a[:] = 1.0 + 1e-6 |
| 86 | + a = cupyx.scipy.special.erfinv(a) |
| 87 | + assert cupy.isnan(a) |
| 88 | + a[:] = -1.0 - 1e-6 |
| 89 | + a = cupyx.scipy.special.erfinv(a) |
| 90 | + assert cupy.isnan(a) |
| 91 | + a[:] = 1.0 |
| 92 | + a = cupyx.scipy.special.erfinv(a) |
| 93 | + assert numpy.isposinf(cupy.asnumpy(a)) |
| 94 | + a[:] = -1.0 |
| 95 | + a = cupyx.scipy.special.erfinv(a) |
| 96 | + assert numpy.isneginf(cupy.asnumpy(a)) |
| 97 | + |
| 98 | + @pytest.mark.skip("erfcinv() is not supported yet") |
| 99 | + @testing.with_requires("scipy>=1.4.0") |
| 100 | + @testing.for_dtypes(["f", "d"]) |
| 101 | + def test_erfcinv_behavior(self, dtype): |
| 102 | + a = cupy.empty((1,), dtype=dtype) |
| 103 | + |
| 104 | + a[:] = 2.0 + 1e-6 |
| 105 | + a = cupyx.scipy.special.erfcinv(a) |
| 106 | + assert cupy.isnan(a) |
| 107 | + a[:] = 0.0 - 1e-6 |
| 108 | + a = cupyx.scipy.special.erfcinv(a) |
| 109 | + assert cupy.isnan(a) |
| 110 | + a[:] = 0.0 |
| 111 | + a = cupyx.scipy.special.erfcinv(a) |
| 112 | + assert numpy.isposinf(cupy.asnumpy(a)) |
| 113 | + a[:] = 2.0 |
| 114 | + a = cupyx.scipy.special.erfcinv(a) |
| 115 | + assert numpy.isneginf(cupy.asnumpy(a)) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.skip("fuse() is not supported yet") |
| 119 | +@testing.with_requires("scipy") |
| 120 | +class TestFusionSpecial(unittest.TestCase, _TestBase): |
| 121 | + |
| 122 | + @testing.for_dtypes(["e", "f", "d"]) |
| 123 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 124 | + def check_unary(self, name, xp, scp, dtype): |
| 125 | + import scipy.special |
| 126 | + |
| 127 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 128 | + |
| 129 | + @cupy.fuse() |
| 130 | + def f(x): |
| 131 | + return getattr(scp.special, name)(x) |
| 132 | + |
| 133 | + return f(a) |
| 134 | + |
| 135 | + @testing.for_dtypes(["f", "d"]) |
| 136 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 137 | + def check_unary_random(self, name, xp, scp, dtype, scale, offset): |
| 138 | + import scipy.special |
| 139 | + |
| 140 | + a = testing.shaped_random((2, 3), xp, dtype, scale=scale) + offset |
| 141 | + |
| 142 | + @cupy.fuse() |
| 143 | + def f(x): |
| 144 | + return getattr(scp.special, name)(x) |
| 145 | + |
| 146 | + return f(a) |
| 147 | + |
| 148 | + @testing.for_dtypes(["f", "d"]) |
| 149 | + @testing.numpy_cupy_allclose(atol=1e-5, scipy_name="scp") |
| 150 | + def check_unary_boundary(self, name, xp, scp, dtype, boundary): |
| 151 | + import scipy.special |
| 152 | + |
| 153 | + a = _boundary_inputs(boundary, 1.0 / 1024, 1.0 / 1024) |
| 154 | + a = xp.array(a, dtype=dtype) |
| 155 | + |
| 156 | + @cupy.fuse() |
| 157 | + def f(x): |
| 158 | + return getattr(scp.special, name)(x) |
| 159 | + |
| 160 | + return f(a) |
0 commit comments