|
| 1 | +import pytest |
| 2 | +import unittest |
| 3 | + |
| 4 | +import dpnp |
| 5 | +import numpy |
| 6 | + |
| 7 | +from dpnp.random import RandomState |
| 8 | +from numpy.testing import (assert_allclose, assert_raises, assert_array_almost_equal) |
| 9 | + |
| 10 | + |
| 11 | +class TestSeed: |
| 12 | + def test_scalar(self): |
| 13 | + seed = 28041997 |
| 14 | + size = (3, 2, 4) |
| 15 | + rs = RandomState(seed) |
| 16 | + a1 = dpnp.asnumpy(rs.uniform(size=size)) |
| 17 | + rs = RandomState(seed) |
| 18 | + a2 = dpnp.asnumpy(rs.uniform(size=size)) |
| 19 | + assert_allclose(a1, a2, rtol=1e-07, atol=0) |
| 20 | + |
| 21 | + @pytest.mark.parametrize("seed", |
| 22 | + [range(3), |
| 23 | + numpy.arange(3, dtype=numpy.int32), |
| 24 | + dpnp.arange(3, dtype=numpy.int32), |
| 25 | + [0], [4294967295], [2, 7, 15], (1,), (85, 6, 17)], |
| 26 | + ids=['range(2)', |
| 27 | + 'numpy.arange(2)', |
| 28 | + 'dpnp.arange(2)', |
| 29 | + '[0]', '[4294967295]', '[2, 7, 15]', '(1,)', '(85, 6, 17)']) |
| 30 | + def test_array_range(self, seed): |
| 31 | + size = 15 |
| 32 | + a1 = dpnp.asnumpy(RandomState(seed).uniform(size=size)) |
| 33 | + a2 = dpnp.asnumpy(RandomState(seed).uniform(size=size)) |
| 34 | + assert_allclose(a1, a2, rtol=1e-07, atol=0) |
| 35 | + |
| 36 | + @pytest.mark.parametrize("seed", |
| 37 | + [0.5, -1.5, [-0.3], (1.7, 3), |
| 38 | + 'text', |
| 39 | + numpy.arange(0, 1, 0.5), |
| 40 | + dpnp.arange(3), |
| 41 | + dpnp.arange(3, dtype=numpy.float32)], |
| 42 | + ids=['0.5', '-1.5', '[-0.3]', '(1.7, 3)', |
| 43 | + 'text', |
| 44 | + 'numpy.arange(0, 1, 0.5)', |
| 45 | + 'dpnp.arange(3)', |
| 46 | + 'dpnp.arange(3, dtype=numpy.float32)']) |
| 47 | + def test_invalid_type(self, seed): |
| 48 | + # seed must be an unsigned 32-bit integer |
| 49 | + assert_raises(TypeError, RandomState, seed) |
| 50 | + |
| 51 | + @pytest.mark.parametrize("seed", |
| 52 | + [-1, [-3, 7], (17, 3, -5), [4, 3, 2, 1], (7, 6, 5, 1), |
| 53 | + range(-1, -11, -1), |
| 54 | + numpy.arange(4, dtype=numpy.int32), |
| 55 | + dpnp.arange(-3, 3, dtype=numpy.int32), |
| 56 | + numpy.iinfo(numpy.uint32).max + 1, |
| 57 | + (1, 7, numpy.iinfo(numpy.uint32).max + 1)], |
| 58 | + ids=['-1', '[-3, 7]', '(17, 3, -5)', '[4, 3, 2, 1]', '(7, 6, 5, 1)', |
| 59 | + 'range(-1, -11, -1)', |
| 60 | + 'numpy.arange(4, dtype=numpy.int32)', |
| 61 | + 'dpnp.arange(-3, 3, dtype=numpy.int32)', |
| 62 | + 'numpy.iinfo(numpy.uint32).max + 1', |
| 63 | + '(1, 7, numpy.iinfo(numpy.uint32).max + 1)']) |
| 64 | + def test_invalid_value(self, seed): |
| 65 | + # seed must be an unsigned 32-bit integer |
| 66 | + assert_raises(ValueError, RandomState, seed) |
| 67 | + |
| 68 | + @pytest.mark.parametrize("seed", |
| 69 | + [[], (), |
| 70 | + [[1, 2, 3]], |
| 71 | + [[1, 2, 3], [4, 5, 6]], |
| 72 | + numpy.array([], dtype=numpy.int64), |
| 73 | + dpnp.array([], dtype=numpy.int64)], |
| 74 | + ids=['[]', '()', |
| 75 | + '[[1, 2, 3]]', |
| 76 | + '[[1, 2, 3], [4, 5, 6]]', |
| 77 | + 'numpy.array([], dtype=numpy.int64)', |
| 78 | + 'dpnp.array([], dtype=numpy.int64)']) |
| 79 | + def test_invalid_shape(self, seed): |
| 80 | + # seed must be an unsigned or 1-D array |
| 81 | + assert_raises(ValueError, RandomState, seed) |
| 82 | + |
| 83 | +class TestUniform: |
| 84 | + @pytest.mark.parametrize("dtype", |
| 85 | + [dpnp.float32, dpnp.float64, numpy.float32, numpy.float64], |
| 86 | + ids=['dpnp.float32', 'dpnp.float64', 'numpy.float32', 'numpy.float64']) |
| 87 | + @pytest.mark.parametrize("usm_type", |
| 88 | + ["host", "device", "shared"], |
| 89 | + ids=['host', 'device', 'shared']) |
| 90 | + def test_uniform(self, dtype, usm_type): |
| 91 | + seed = 28041997 |
| 92 | + actual = dpnp.asnumpy(RandomState(seed).uniform(low=1.23, high=10.54, size=(3, 2), dtype=dtype, usm_type=usm_type)) |
| 93 | + desired = numpy.array([[3.700744485249743, 8.390019132522866], |
| 94 | + [2.60340195777826, 4.473366308724508], |
| 95 | + [1.773701806552708, 4.193498786306009]]) |
| 96 | + assert_array_almost_equal(actual, desired, decimal=6) |
| 97 | + |
| 98 | + @pytest.mark.parametrize("high", |
| 99 | + [dpnp.array([3]), numpy.array([3])], |
| 100 | + ids=['dpnp.array([3])', 'numpy.array([3])']) |
| 101 | + @pytest.mark.parametrize("low", |
| 102 | + [[2], dpnp.array([2]), numpy.array([2])], |
| 103 | + ids=['[2]', 'dpnp.array([2])', 'numpy.array([2])']) |
| 104 | + def test_fallback(self, low, high): |
| 105 | + seed = 15 |
| 106 | + # dpnp accepts only scalar as low and/or high, in other case it will be a fallback to numpy |
| 107 | + actual = dpnp.asnumpy(RandomState(seed).uniform(low=low, high=high, size=(3, 2, 5))) |
| 108 | + desired = numpy.random.RandomState(seed).uniform(low=low, high=high, size=(3, 2, 5)) |
| 109 | + assert_array_almost_equal(actual, desired, decimal=15) |
| 110 | + |
| 111 | + @pytest.mark.parametrize("dtype", |
| 112 | + [dpnp.float16, numpy.integer, dpnp.int, dpnp.bool, numpy.int64, dpnp.int32], |
| 113 | + ids=['dpnp.float16', 'numpy.integer', 'dpnp.int', 'dpnp.bool', 'numpy.int64', 'dpnp.int32']) |
| 114 | + def test_invalid_dtype(self, dtype): |
| 115 | + # dtype must be float32 or float64 |
| 116 | + assert_raises(TypeError, RandomState().uniform, dtype=dtype) |
0 commit comments