test_randint_op.py 8.6 KB
Newer Older
S
silingtong123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

F
From00 已提交
15 16
import os
import paddle
S
silingtong123 已提交
17 18 19
import unittest
import numpy as np
from op_test import OpTest
20
from paddle.fluid import core
F
From00 已提交
21
from paddle.fluid.framework import _test_eager_guard
22
from paddle.static import program_guard, Program
23 24

paddle.enable_static()
S
silingtong123 已提交
25 26 27


def output_hist(out):
28
    hist, _ = np.histogram(out, range=(-10, 10))
S
silingtong123 已提交
29 30 31 32 33 34 35
    hist = hist.astype("float32")
    hist /= float(out.size)
    prob = 0.1 * np.ones((10))
    return hist, prob


class TestRandintOp(OpTest):
36

S
silingtong123 已提交
37 38 39 40 41 42 43
    def setUp(self):
        self.op_type = "randint"
        self.inputs = {}
        self.init_attrs()
        self.outputs = {"Out": np.zeros((10000, 784)).astype("float32")}

    def init_attrs(self):
44
        self.attrs = {"shape": [10000, 784], "low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
45 46 47 48 49 50 51
        self.output_hist = output_hist

    def test_check_output(self):
        self.check_output_customized(self.verify_output)

    def verify_output(self, outs):
        hist, prob = self.output_hist(np.array(outs[0]))
52
        np.testing.assert_allclose(hist, prob, rtol=0, atol=0.001)
S
silingtong123 已提交
53

F
From00 已提交
54 55 56 57
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
58 59

class TestRandintOpError(unittest.TestCase):
60

S
silingtong123 已提交
61
    def test_errors(self):
62 63 64 65
        with program_guard(Program(), Program()):
            self.assertRaises(TypeError, paddle.randint, 5, shape=np.array([2]))
            self.assertRaises(TypeError, paddle.randint, 5, dtype='float32')
            self.assertRaises(ValueError, paddle.randint, 5, 5)
66
            self.assertRaises(ValueError, paddle.randint, -5)
67 68 69
            self.assertRaises(TypeError, paddle.randint, 5, shape=['2'])
            shape_tensor = paddle.static.data('X', [1])
            self.assertRaises(TypeError, paddle.randint, 5, shape=shape_tensor)
70 71 72 73
            self.assertRaises(TypeError,
                              paddle.randint,
                              5,
                              shape=[shape_tensor])
S
silingtong123 已提交
74

F
From00 已提交
75 76 77 78
    def test_errors_eager(self):
        with _test_eager_guard():
            self.test_errors()

S
silingtong123 已提交
79 80

class TestRandintOp_attr_tensorlist(OpTest):
81

S
silingtong123 已提交
82 83 84 85 86 87 88 89 90 91 92 93
    def setUp(self):
        self.op_type = "randint"
        self.new_shape = (10000, 784)
        shape_tensor = []
        for index, ele in enumerate(self.new_shape):
            shape_tensor.append(("x" + str(index), np.ones(
                (1)).astype("int64") * ele))
        self.inputs = {'ShapeTensorList': shape_tensor}
        self.init_attrs()
        self.outputs = {"Out": np.zeros((10000, 784)).astype("int32")}

    def init_attrs(self):
94
        self.attrs = {"low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
95 96 97 98 99 100 101
        self.output_hist = output_hist

    def test_check_output(self):
        self.check_output_customized(self.verify_output)

    def verify_output(self, outs):
        hist, prob = self.output_hist(np.array(outs[0]))
102
        np.testing.assert_allclose(hist, prob, rtol=0, atol=0.001)
S
silingtong123 已提交
103

F
From00 已提交
104 105 106 107
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
108 109

class TestRandint_attr_tensor(OpTest):
110

S
silingtong123 已提交
111 112 113 114 115 116 117
    def setUp(self):
        self.op_type = "randint"
        self.inputs = {"ShapeTensor": np.array([10000, 784]).astype("int64")}
        self.init_attrs()
        self.outputs = {"Out": np.zeros((10000, 784)).astype("int64")}

    def init_attrs(self):
118
        self.attrs = {"low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
119 120 121 122 123 124 125
        self.output_hist = output_hist

    def test_check_output(self):
        self.check_output_customized(self.verify_output)

    def verify_output(self, outs):
        hist, prob = self.output_hist(np.array(outs[0]))
126
        np.testing.assert_allclose(hist, prob, rtol=0, atol=0.001)
S
silingtong123 已提交
127

F
From00 已提交
128 129 130 131
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
132 133 134

# Test python API
class TestRandintAPI(unittest.TestCase):
135

S
silingtong123 已提交
136
    def test_api(self):
137
        with program_guard(Program(), Program()):
S
silingtong123 已提交
138
            # results are from [0, 5).
139
            out1 = paddle.randint(5)
S
silingtong123 已提交
140
            # shape is a list and dtype is 'int32'
141 142 143 144
            out2 = paddle.randint(low=-100,
                                  high=100,
                                  shape=[64, 64],
                                  dtype='int32')
S
silingtong123 已提交
145
            # shape is a tuple and dtype is 'int64'
146 147 148 149
            out3 = paddle.randint(low=-100,
                                  high=100,
                                  shape=(32, 32, 3),
                                  dtype='int64')
S
silingtong123 已提交
150
            # shape is a tensorlist and dtype is 'float32'
151 152
            dim_1 = paddle.fluid.layers.fill_constant([1], "int64", 32)
            dim_2 = paddle.fluid.layers.fill_constant([1], "int32", 50)
153 154 155 156
            out4 = paddle.randint(low=-100,
                                  high=100,
                                  shape=[dim_1, 5, dim_2],
                                  dtype='int32')
S
silingtong123 已提交
157
            # shape is a tensor and dtype is 'float64'
158 159 160 161 162 163 164 165 166 167
            var_shape = paddle.static.data(name='var_shape',
                                           shape=[2],
                                           dtype="int64")
            out5 = paddle.randint(low=1,
                                  high=1000,
                                  shape=var_shape,
                                  dtype='int64')

            place = paddle.CUDAPlace(
                0) if core.is_compiled_with_cuda() else paddle.CPUPlace()
168
            exe = paddle.static.Executor(place)
S
silingtong123 已提交
169 170
            outs = exe.run(
                feed={'var_shape': np.array([100, 100]).astype('int64')},
171
                fetch_list=[out1, out2, out3, out4, out5])
S
silingtong123 已提交
172

F
From00 已提交
173 174 175 176
    def test_api_eager(self):
        with _test_eager_guard():
            self.test_api()

S
silingtong123 已提交
177

178
class TestRandintImperative(unittest.TestCase):
179

180
    def test_api(self):
181
        paddle.disable_static()
F
From00 已提交
182 183 184 185 186 187 188 189 190 191

        self.run_test_case()

        with _test_eager_guard():
            self.run_test_case()

        paddle.enable_static()

    def run_test_case(self):
        n = 10
192 193 194 195 196 197
        x1 = paddle.randint(n, shape=[10], dtype="int32")
        x2 = paddle.tensor.randint(n)
        x3 = paddle.tensor.random.randint(n)
        for i in [x1, x2, x3]:
            for j in i.numpy().tolist():
                self.assertTrue((j >= 0 and j < n))
S
silingtong123 已提交
198 199


200
class TestRandomValue(unittest.TestCase):
201

202 203 204 205 206 207
    def test_fixed_random_number(self):
        # Test GPU Fixed random number, which is generated by 'curandStatePhilox4_32_10_t'
        if not paddle.is_compiled_with_cuda():
            return

        # Different GPU generatte different random value. Only test V100 here.
208
        if "V100" not in paddle.device.cuda.get_device_name():
209 210 211 212
            return

        print("Test Fixed Random number on GPU------>")
        paddle.disable_static()
F
From00 已提交
213 214 215 216 217 218 219 220 221

        self.run_test_case()

        with _test_eager_guard():
            self.run_test_case()

        paddle.enable_static()

    def run_test_case(self):
222 223 224
        paddle.set_device('gpu')
        paddle.seed(100)

225 226
        x = paddle.randint(-10000, 10000, [32, 3, 1024, 1024],
                           dtype='int32').numpy()
227 228 229
        self.assertTrue(x.mean(), -0.7517569760481516)
        self.assertTrue(x.std(), 5773.696619107639)
        expect = [2535, 2109, 5916, -5011, -261]
230
        np.testing.assert_array_equal(x[10, 0, 100, 100:105], expect)
231
        expect = [3465, 7206, -8660, -9628, -6574]
232
        np.testing.assert_array_equal(x[20, 1, 600, 600:605], expect)
233
        expect = [881, 1560, 1100, 9664, 1669]
234
        np.testing.assert_array_equal(x[30, 2, 1000, 1000:1005], expect)
235

236 237
        x = paddle.randint(-10000, 10000, [32, 3, 1024, 1024],
                           dtype='int64').numpy()
238 239 240
        self.assertTrue(x.mean(), -1.461287518342336)
        self.assertTrue(x.std(), 5773.023477548159)
        expect = [7213, -9597, 754, 8129, -1158]
241
        np.testing.assert_array_equal(x[10, 0, 100, 100:105], expect)
242
        expect = [-7159, 8054, 7675, 6980, 8506]
243
        np.testing.assert_array_equal(x[20, 1, 600, 600:605], expect)
244
        expect = [3581, 3420, -8027, -5237, -2436]
245
        np.testing.assert_array_equal(x[30, 2, 1000, 1000:1005], expect)
246 247


S
silingtong123 已提交
248 249
if __name__ == "__main__":
    unittest.main()