test_gaussian_random_op.py 13.5 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15
import os
16
import unittest
17
import numpy as np
L
Leo Chen 已提交
18
import paddle
19 20 21 22
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.op import Operator
from paddle.fluid.executor import Executor
23
from paddle.fluid.tests.unittests.op_test import OpTest, convert_uint16_to_float
24
from paddle.fluid.framework import _test_eager_guard
25
import paddle
26 27


28
class TestGaussianRandomOp(OpTest):
29

D
dzhwinter 已提交
30 31
    def setUp(self):
        self.op_type = "gaussian_random"
32
        self.python_api = paddle.normal
33
        self.set_attrs()
D
dzhwinter 已提交
34
        self.inputs = {}
M
mozga-intel 已提交
35 36
        self.use_mkldnn = False
        self.attrs = {
37
            "shape": [123, 92],
38 39
            "mean": self.mean,
            "std": self.std,
M
mozga-intel 已提交
40 41 42
            "seed": 10,
            "use_mkldnn": self.use_mkldnn
        }
C
cnn 已提交
43
        paddle.seed(10)
D
dzhwinter 已提交
44

45
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
D
dzhwinter 已提交
46

47 48 49 50
    def set_attrs(self):
        self.mean = 1.0
        self.std = 2.

51 52
    def test_check_output(self):
        self.check_output_customized(self.verify_output)
53

54 55 56 57
    def test_eager(self):
        with _test_eager_guard():
            self.test_check_output()

58 59 60 61 62 63 64 65 66
    def verify_output(self, outs):
        self.assertEqual(outs[0].shape, (123, 92))
        hist, _ = np.histogram(outs[0], range=(-3, 5))
        hist = hist.astype("float32")
        hist /= float(outs[0].size)
        data = np.random.normal(size=(123, 92), loc=1, scale=2)
        hist2, _ = np.histogram(data, range=(-3, 5))
        hist2 = hist2.astype("float32")
        hist2 /= float(outs[0].size)
67
        np.testing.assert_allclose(hist, hist2, rtol=0, atol=0.01)
68

D
dongzhihong 已提交
69

70 71 72
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestGaussianRandomBF16Op(OpTest):
73

74 75
    def setUp(self):
        self.op_type = "gaussian_random"
76
        self.python_api = paddle.normal
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        self.set_attrs()
        self.inputs = {}
        self.use_mkldnn = False
        self.attrs = {
            "shape": [123, 92],
            "mean": self.mean,
            "std": self.std,
            "seed": 10,
            "dtype": paddle.fluid.core.VarDesc.VarType.BF16,
            "use_mkldnn": self.use_mkldnn
        }
        paddle.seed(10)

        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}

    def set_attrs(self):
        self.mean = 1.0
        self.std = 2.

    def test_check_output(self):
97 98
        self.check_output_with_place_customized(self.verify_output,
                                                place=core.CUDAPlace(0))
99

100 101 102 103
    def test_eager(self):
        with _test_eager_guard():
            self.test_check_output()

104 105 106 107 108 109 110 111 112 113
    def verify_output(self, outs):
        outs = convert_uint16_to_float(outs)
        self.assertEqual(outs[0].shape, (123, 92))
        hist, _ = np.histogram(outs[0], range=(-3, 5))
        hist = hist.astype("float32")
        hist /= float(outs[0].size)
        data = np.random.normal(size=(123, 92), loc=1, scale=2)
        hist2, _ = np.histogram(data, range=(-3, 5))
        hist2 = hist2.astype("float32")
        hist2 /= float(outs[0].size)
114
        np.testing.assert_allclose(hist, hist2, rtol=0, atol=0.05)
115 116


117
class TestMeanStdAreInt(TestGaussianRandomOp):
118

119 120 121 122 123
    def set_attrs(self):
        self.mean = 1
        self.std = 2


124 125
# Situation 2: Attr(shape) is a list(with tensor)
class TestGaussianRandomOp_ShapeTensorList(TestGaussianRandomOp):
126

127 128 129 130 131 132 133 134 135
    def setUp(self):
        '''Test gaussian_random op with specified value
        '''
        self.op_type = "gaussian_random"
        self.init_data()
        shape_tensor_list = []
        for index, ele in enumerate(self.shape):
            shape_tensor_list.append(("x" + str(index), np.ones(
                (1)).astype('int32') * ele))
D
dzhwinter 已提交
136

137 138 139 140 141 142 143
        self.attrs = {
            'shape': self.infer_shape,
            'mean': self.mean,
            'std': self.std,
            'seed': self.seed,
            'use_mkldnn': self.use_mkldnn
        }
D
dzhwinter 已提交
144

145 146
        self.inputs = {"ShapeTensorList": shape_tensor_list}
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
D
dzhwinter 已提交
147

148 149 150 151 152 153 154
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [-1, 92]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10
D
dzhwinter 已提交
155

156 157
    def test_check_output(self):
        self.check_output_customized(self.verify_output)
158

M
mozga-intel 已提交
159

160 161 162
class TestGaussianRandomOp2_ShapeTensorList(TestGaussianRandomOp_ShapeTensorList
                                            ):

163 164 165 166 167 168 169 170 171
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [-1, -1]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


172 173 174
class TestGaussianRandomOp3_ShapeTensorList(TestGaussianRandomOp_ShapeTensorList
                                            ):

175 176 177 178 179 180 181 182 183
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [123, -1]
        self.use_mkldnn = True
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


184 185 186
class TestGaussianRandomOp4_ShapeTensorList(TestGaussianRandomOp_ShapeTensorList
                                            ):

187 188 189 190 191 192 193
    def init_data(self):
        self.shape = [123, 92]
        self.infer_shape = [123, -1]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10
194

195 196 197

# Situation 3: shape is a tensor
class TestGaussianRandomOp1_ShapeTensor(TestGaussianRandomOp):
198

199
    def setUp(self):
200 201
        '''Test gaussian_random op with specified value
        '''
202
        self.op_type = "gaussian_random"
203
        self.init_data()
204
        self.use_mkldnn = False
205 206

        self.inputs = {"ShapeTensor": np.array(self.shape).astype("int32")}
207
        self.attrs = {
208 209 210 211
            'mean': self.mean,
            'std': self.std,
            'seed': self.seed,
            'use_mkldnn': self.use_mkldnn
212
        }
213
        self.outputs = {'Out': np.zeros((123, 92), dtype='float32')}
214

215 216 217 218 219 220 221 222 223 224
    def init_data(self):
        self.shape = [123, 92]
        self.use_mkldnn = False
        self.mean = 1.0
        self.std = 2.0
        self.seed = 10


# Test python API
class TestGaussianRandomAPI(unittest.TestCase):
225

226 227 228 229
    def test_api(self):
        positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2000)

        positive_2_int64 = fluid.layers.fill_constant([1], "int64", 500)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        shape_tensor_int32 = fluid.data(name="shape_tensor_int32",
                                        shape=[2],
                                        dtype="int32")

        shape_tensor_int64 = fluid.data(name="shape_tensor_int64",
                                        shape=[2],
                                        dtype="int64")

        out_1 = fluid.layers.gaussian_random(shape=[2000, 500],
                                             dtype="float32",
                                             mean=0.0,
                                             std=1.0,
                                             seed=10)

        out_2 = fluid.layers.gaussian_random(shape=[2000, positive_2_int32],
                                             dtype="float32",
                                             mean=0.,
                                             std=1.0,
                                             seed=10)

        out_3 = fluid.layers.gaussian_random(shape=[2000, positive_2_int64],
                                             dtype="float32",
                                             mean=0.,
                                             std=1.0,
                                             seed=10)

        out_4 = fluid.layers.gaussian_random(shape=shape_tensor_int32,
                                             dtype="float32",
                                             mean=0.,
                                             std=1.0,
                                             seed=10)

        out_5 = fluid.layers.gaussian_random(shape=shape_tensor_int64,
                                             dtype="float32",
                                             mean=0.,
                                             std=1.0,
                                             seed=10)

        out_6 = fluid.layers.gaussian_random(shape=shape_tensor_int64,
                                             dtype=np.float32,
                                             mean=0.,
                                             std=1.0,
                                             seed=10)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

        exe = fluid.Executor(place=fluid.CPUPlace())
        res_1, res_2, res_3, res_4, res_5, res_6 = exe.run(
            fluid.default_main_program(),
            feed={
                "shape_tensor_int32": np.array([2000, 500]).astype("int32"),
                "shape_tensor_int64": np.array([2000, 500]).astype("int64"),
            },
            fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6])

        self.assertAlmostEqual(np.mean(res_1), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_1), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_2), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_2), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_3), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_3), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_4), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_5), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_5), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_5), 1., delta=0.1)
        self.assertAlmostEqual(np.mean(res_6), 0.0, delta=0.1)
        self.assertAlmostEqual(np.std(res_6), 1., delta=0.1)
295

296 297 298
    def test_default_dtype(self):
        paddle.disable_static()

299
        def test_default_fp16():
300
            paddle.framework.set_default_dtype('float16')
301
            paddle.tensor.random.gaussian([2, 3])
302

303
        self.assertRaises(TypeError, test_default_fp16)
304

305
        def test_default_fp32():
306
            paddle.framework.set_default_dtype('float32')
307
            out = paddle.tensor.random.gaussian([2, 3])
308 309
            self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32)

310
        def test_default_fp64():
311
            paddle.framework.set_default_dtype('float64')
312
            out = paddle.tensor.random.gaussian([2, 3])
313 314
            self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64)

315 316
        test_default_fp64()
        test_default_fp32()
317 318 319 320 321

        paddle.enable_static()


class TestStandardNormalDtype(unittest.TestCase):
322

323 324 325
    def test_default_dtype(self):
        paddle.disable_static()

326
        def test_default_fp16():
327 328 329
            paddle.framework.set_default_dtype('float16')
            paddle.tensor.random.standard_normal([2, 3])

330
        self.assertRaises(TypeError, test_default_fp16)
331

332
        def test_default_fp32():
333 334 335 336
            paddle.framework.set_default_dtype('float32')
            out = paddle.tensor.random.standard_normal([2, 3])
            self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32)

337
        def test_default_fp64():
338 339 340 341
            paddle.framework.set_default_dtype('float64')
            out = paddle.tensor.random.standard_normal([2, 3])
            self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64)

342 343
        test_default_fp64()
        test_default_fp32()
344 345 346

        paddle.enable_static()

347

348
class TestRandomValue(unittest.TestCase):
349

350 351 352 353 354
    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

355
        # Different GPU generatte different random value. Only test V100 here.
356 357 358 359 360 361
        if not "V100" in paddle.device.cuda.get_device_name():
            return

        def _check_random_value(dtype, expect, expect_mean, expect_std):
            x = paddle.randn([32, 3, 1024, 1024], dtype=dtype)
            actual = x.numpy()
362 363 364
            np.testing.assert_allclose(actual[2, 1, 512, 1000:1010],
                                       expect,
                                       rtol=1e-05)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
            self.assertTrue(np.mean(actual), expect_mean)
            self.assertTrue(np.std(actual), expect_std)

        print("Test Fixed Random number on V100 GPU------>")
        paddle.disable_static()
        paddle.set_device('gpu')
        paddle.seed(2021)
        expect = [
            -0.79037829, -0.54411126, -0.32266671, 0.35791815, 1.44169267,
            -0.87785644, -1.23909874, -2.18194139, 0.49489656, 0.40703062
        ]
        expect_mean = -0.0000053026194133403266873214888799115129813799285329878330230713
        expect_std = 0.99999191058126390974081232343451119959354400634765625
        _check_random_value(core.VarDesc.VarType.FP64, expect, expect_mean,
                            expect_std)

        expect = [
            -0.7988942, 1.8644791, 0.02782744, 1.3692524, 0.6419724, 0.12436751,
            0.12058455, -1.9984808, 1.5635862, 0.18506318
        ]
        expect_mean = -0.00004762359094456769526004791259765625
        expect_std = 0.999975681304931640625
        _check_random_value(core.VarDesc.VarType.FP32, expect, expect_mean,
                            expect_std)
        paddle.enable_static()


Q
qijun 已提交
392
if __name__ == "__main__":
393
    unittest.main()