test_randint_op.py 8.7 KB
Newer Older
S
silingtong123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.

from __future__ import print_function

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

paddle.enable_static()
S
silingtong123 已提交
27 28 29


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


class TestRandintOp(OpTest):
38

S
silingtong123 已提交
39 40 41 42 43 44 45
    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):
46
        self.attrs = {"shape": [10000, 784], "low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
47 48 49 50 51 52 53
        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]))
54 55
        self.assertTrue(np.allclose(hist, prob, rtol=0, atol=0.001),
                        "hist: " + str(hist))
S
silingtong123 已提交
56

F
From00 已提交
57 58 59 60
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
61 62

class TestRandintOpError(unittest.TestCase):
63

S
silingtong123 已提交
64
    def test_errors(self):
65 66 67 68
        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)
69
            self.assertRaises(ValueError, paddle.randint, -5)
70 71 72
            self.assertRaises(TypeError, paddle.randint, 5, shape=['2'])
            shape_tensor = paddle.static.data('X', [1])
            self.assertRaises(TypeError, paddle.randint, 5, shape=shape_tensor)
73 74 75 76
            self.assertRaises(TypeError,
                              paddle.randint,
                              5,
                              shape=[shape_tensor])
S
silingtong123 已提交
77

F
From00 已提交
78 79 80 81
    def test_errors_eager(self):
        with _test_eager_guard():
            self.test_errors()

S
silingtong123 已提交
82 83

class TestRandintOp_attr_tensorlist(OpTest):
84

S
silingtong123 已提交
85 86 87 88 89 90 91 92 93 94 95 96
    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):
97
        self.attrs = {"low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
98 99 100 101 102 103 104
        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]))
105 106
        self.assertTrue(np.allclose(hist, prob, rtol=0, atol=0.001),
                        "hist: " + str(hist))
S
silingtong123 已提交
107

F
From00 已提交
108 109 110 111
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
112 113

class TestRandint_attr_tensor(OpTest):
114

S
silingtong123 已提交
115 116 117 118 119 120 121
    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):
122
        self.attrs = {"low": -10, "high": 10, "seed": 10}
S
silingtong123 已提交
123 124 125 126 127 128 129
        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]))
130 131
        self.assertTrue(np.allclose(hist, prob, rtol=0, atol=0.001),
                        "hist: " + str(hist))
S
silingtong123 已提交
132

F
From00 已提交
133 134 135 136
    def test_check_output_eager(self):
        with _test_eager_guard():
            self.test_check_output()

S
silingtong123 已提交
137 138 139

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

S
silingtong123 已提交
141
    def test_api(self):
142
        with program_guard(Program(), Program()):
S
silingtong123 已提交
143
            # results are from [0, 5).
144
            out1 = paddle.randint(5)
S
silingtong123 已提交
145
            # shape is a list and dtype is 'int32'
146 147 148 149
            out2 = paddle.randint(low=-100,
                                  high=100,
                                  shape=[64, 64],
                                  dtype='int32')
S
silingtong123 已提交
150
            # shape is a tuple and dtype is 'int64'
151 152 153 154
            out3 = paddle.randint(low=-100,
                                  high=100,
                                  shape=(32, 32, 3),
                                  dtype='int64')
S
silingtong123 已提交
155
            # shape is a tensorlist and dtype is 'float32'
156 157
            dim_1 = paddle.fluid.layers.fill_constant([1], "int64", 32)
            dim_2 = paddle.fluid.layers.fill_constant([1], "int32", 50)
158 159 160 161
            out4 = paddle.randint(low=-100,
                                  high=100,
                                  shape=[dim_1, 5, dim_2],
                                  dtype='int32')
S
silingtong123 已提交
162
            # shape is a tensor and dtype is 'float64'
163 164 165 166 167 168 169 170 171 172
            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()
173
            exe = paddle.static.Executor(place)
S
silingtong123 已提交
174 175
            outs = exe.run(
                feed={'var_shape': np.array([100, 100]).astype('int64')},
176
                fetch_list=[out1, out2, out3, out4, out5])
S
silingtong123 已提交
177

F
From00 已提交
178 179 180 181
    def test_api_eager(self):
        with _test_eager_guard():
            self.test_api()

S
silingtong123 已提交
182

183
class TestRandintImperative(unittest.TestCase):
184

185
    def test_api(self):
186
        paddle.disable_static()
F
From00 已提交
187 188 189 190 191 192 193 194 195 196

        self.run_test_case()

        with _test_eager_guard():
            self.run_test_case()

        paddle.enable_static()

    def run_test_case(self):
        n = 10
197 198 199 200 201 202
        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 已提交
203 204


205
class TestRandomValue(unittest.TestCase):
206

207 208 209 210 211 212 213 214 215 216 217
    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.
        if not "V100" in paddle.device.cuda.get_device_name():
            return

        print("Test Fixed Random number on GPU------>")
        paddle.disable_static()
F
From00 已提交
218 219 220 221 222 223 224 225 226

        self.run_test_case()

        with _test_eager_guard():
            self.run_test_case()

        paddle.enable_static()

    def run_test_case(self):
227 228 229
        paddle.set_device('gpu')
        paddle.seed(100)

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

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


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