Skip to content

Commit

Permalink
update new random number when FLAGS_use_currand=ON
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 committed Apr 1, 2022
1 parent c77733c commit a226540
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 112 deletions.
4 changes: 2 additions & 2 deletions framework/api/nn/test_alpha_dropout.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ def run(self, res_list, data=None, **kwargs):


obj = TestAlphaDropout(paddle.nn.AlphaDropout)
np_random_tensor = np.array([[0.55355287, 0.20714243, 0.01162981], [0.51577556, 0.36369765, 0.26091650]])
np_random_tensor_gpu = np.array([[0.00224779, 0.50324494, 0.13526054], [0.16112770, 0.79557019, 0.96897715]])
np_random_tensor = np.array([[0.55355281, 0.20714243, 0.01162981], [0.51577556, 0.36369765, 0.26091650]])
np_random_tensor_gpu = np.array([[0.35628906, 0.03034166, 0.70878232], [0.20091671, 0.02237142, 0.98958546]])


def numpy_alpha_dropout(x, p, random_tensor, training=True):
Expand Down
219 changes: 139 additions & 80 deletions framework/api/nn/test_initializer_kaiming_normal.py

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions framework/api/nn/test_initializer_kaiming_uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_initializer_kaiming_uniform_base():
"""
base
"""
obj.static = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 2, 2]).astype("float32")
in_channels = 3
Expand All @@ -44,7 +45,10 @@ def test_initializer_kaiming_uniform_base():
padding = 1
dilation = 1
res = np.array(
[[[[1.1869886, 2.3453705], [1.2281079, 1.0826794]]], [[[0.24804217, 1.8928909], [1.5467637, 1.5817381]]]]
[
[[[-0.39864674, 0.44492644], [-2.1563835, -0.09215282]]],
[[[0.05160024, 0.5901147], [-1.3453339, -1.1876528]]],
]
)
if paddle.device.is_compiled_with_cuda() is True:
obj.base(
Expand All @@ -66,6 +70,7 @@ def test_initializer_kaiming_uniform1():
"""
default
"""
obj.dygraph = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 2, 2]).astype("float32")
in_channels = 3
Expand Down Expand Up @@ -114,6 +119,7 @@ def test_initializer_kaiming_uniform2():
"""
dilation = 2
"""
obj.static = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 2, 2]).astype("float32")
in_channels = 3
Expand All @@ -126,18 +132,18 @@ def test_initializer_kaiming_uniform2():
[
[
[
[0.39888406, 0.14239825, 0.84748095, 1.0942918, 0.7107052, 0.3309278],
[-0.9275552, -0.49287915, 0.6899886, 0.5361054, 0.00426798, -0.12755814],
[-0.75464916, -0.761385, 0.59295094, 0.64171284, -0.0829928, -0.01879808],
[1.0284215, 0.67908627, 0.7174556, 0.04409777, 0.47986156, 0.18009461],
[0.35504752, -0.37692446, 0.05799875, -0.16684613, 0.8788808, 0.23547846],
[-0.7535841, -0.3518779, 0.27215683, -0.1930912, -0.07401706, 0.26988265],
[-0.45363197, -0.6825423, -0.2646422, 0.31390566, 0.49114504, -0.20408536],
[-0.35144585, -0.3957303, -0.8134686, -0.36817482, -0.5290287, -0.54556954],
]
],
[
[
[0.3629937, -0.66693234, 0.7846964, 0.919392, 0.3969715, 0.6188619],
[-0.94021267, -0.453328, 0.5836061, 0.5021896, 0.07433788, -0.09116814],
[-0.65587324, -0.4368534, 0.44602627, 0.9121258, 0.00741166, -0.34975865],
[0.915076, 0.69684297, 0.84074783, 0.22948253, 0.43271816, 0.34738517],
[-0.04252443, 0.1658059, -0.09269778, 0.24450657, 0.45426783, 0.46441948],
[-0.7518698, -0.4010634, 0.37955555, 0.02937469, -0.13803437, 0.00215799],
[-0.4779903, -0.4235527, 0.08779142, -0.37562048, 0.09073773, 0.2590201],
[-0.31621772, -0.19313698, -0.8164357, -0.42716488, -0.47560513, -0.3033054],
]
],
]
Expand All @@ -162,6 +168,7 @@ def test_initializer_kaiming_uniform3():
"""
out_channels = 3 groups=3 data_format="NHWC" output_padding=1
"""
obj.dygraph = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 6, 2, 2]).astype("float32").transpose(0, 2, 3, 1)
in_channels = 6
Expand Down
21 changes: 15 additions & 6 deletions framework/api/nn/test_initializer_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ def test_initializer_normal_base():
base
weight_attr=paddle.nn.initializer.Normal(mean=0.0, std=2.0)
"""
obj.static = False
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
out_channels = 1
kernel_size = [3, 3]
stride = 1
padding = 0
res = np.array(
[[[[9.696032, 6.2314734], [4.5925426, 19.060331]]], [[[14.223241, 13.581673], [6.131522, 9.923124]]]]
[[[[2.0433278, -0.2727771], [0.41961208, -5.5582423]]], [[[-1.3874869, -5.1710925], [3.2667, -2.1549852]]]]
)
if paddle.device.is_compiled_with_cuda() is True:
obj.base(
Expand All @@ -65,6 +66,7 @@ def test_initializer_normal1():
kernel_size = [2, 2] stride = 2 padding=0 groups=3
weight_attr=paddle.nn.initializer.Normal(mean=0.0, std=2.0)
"""
obj.dygraph = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
Expand Down Expand Up @@ -100,6 +102,7 @@ def test_initializer_normal2():
kernel_size = [3, 3] stride = 1 padding=1 w=0.7 b=-0.3 data_format="NHWC"
weight_attr=paddle.nn.initializer.Normal(mean=0.0, std=2.0)
"""
obj.static = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4]).transpose(0, 2, 3, 1)
in_channels = 3
Expand All @@ -110,8 +113,8 @@ def test_initializer_normal2():
data_format = "NHWC"
res = np.array(
[
[[[9.696032, 0.757347], [6.2314734, 1.9067024]], [[4.5925426, 0.7299702], [19.060331, 4.5685883]]],
[[[14.223241, 6.7473884], [13.581673, 4.739414]], [[6.131522, 1.6402493], [9.923124, 0.7511649]]],
[[[2.0433278, -8.995044], [-0.2727771, -1.7580405]], [[0.41961208, -8.936554], [-5.5582423, -10.978077]]],
[[[-1.3874869, -18.076727], [-5.1710925, -6.579859]], [[3.2667, -10.983443], [-2.1549852, -11.922855]]],
]
)
if paddle.device.is_compiled_with_cuda() is True:
Expand All @@ -135,6 +138,7 @@ def test_initializer_normal3():
padding_mode = "reflect"
weight_attr=paddle.nn.initializer.Normal(mean=0.0, std=2.0)
"""
obj.dygraph = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
Expand Down Expand Up @@ -170,6 +174,7 @@ def test_initializer_normal4():
padding_mode = "replicate"
weight_attr=paddle.nn.initializer.Normal(mean=-1.0, std=2.0)
"""
obj.static = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
Expand All @@ -180,8 +185,8 @@ def test_initializer_normal4():
padding_mode = "replicate"
res = np.array(
[
[[[-0.25635287, 0.6774132, -3.2480803, -7.2378592], [-3.132905, -8.737241, 5.810273, -1.7762027]]],
[[[0.8862386, -0.61035603, 4.994542, -5.26088], [-6.272961, -9.037898, -3.7375047, -1.8376815]]],
[[[-19.855627, -16.224363, -5.913007, -7.2382164], [-9.950214, -12.91017, -18.8083, -12.357948]]],
[[[-9.542725, -13.213186, -14.944015, -10.585907], [-19.823824, -11.902721, -15.815616, -10.774846]]],
]
)
if paddle.device.is_compiled_with_cuda() is True:
Expand All @@ -205,6 +210,7 @@ def test_initializer_normal5():
padding_mode = "circular"
weight_attr=paddle.nn.initializer.Normal(mean=2.0, std=5.0)
"""
obj.dygraph = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
Expand Down Expand Up @@ -240,6 +246,7 @@ def test_initializer_normal6():
padding_mode = "zeros" dilation = (2, 2) padding = [1, 2]
weight_attr=paddle.nn.initializer.Normal(mean=-0.2, std=1.99)
"""
obj.static = False
np.random.seed(obj.seed)
x = randtool("float", 0, 1, [2, 3, 4, 4])
in_channels = 3
Expand All @@ -249,7 +256,9 @@ def test_initializer_normal6():
padding = [1, 2]
dilation = (2, 2)
padding_mode = "zeros"
res = np.array([[[[6.9537334, 3.4081156, 2.5187492, 6.7148337]]], [[[3.6040933, 3.8371656, 2.5087252, 6.355131]]]])
res = np.array(
[[[[-5.971743, 0.11934806, -5.1818447, -9.398735]]], [[[-5.281911, -2.1525862, -6.082208, -7.7175527]]]]
)
if paddle.device.is_compiled_with_cuda() is True:
obj.run(
res=res,
Expand Down
4 changes: 2 additions & 2 deletions framework/api/paddlebase/test_rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_rand1():
"""
obj.seed = 1
# res = np.array([[0, 7], [6, 0]])
obj.exception(etype="NotFoundError", shape=[2, 2], dtype=np.int32)
obj.exception(etype="NotFound", shape=[2, 2], dtype=np.int32)


@skip_platform_not_linux
Expand Down Expand Up @@ -108,5 +108,5 @@ def test_rand5():
"""
obj.places = [fluid.CUDAPlace(0)]
obj.seed = 33
res = np.array([[7.4177142e-04, 8.0607080e-01], [8.4463596e-01, 4.2317215e-01]])
res = np.array([[0.7515413, 0.72745854], [0.7739062, 0.41597638]])
obj.run(res=res, shape=np.array([2, 2]))
16 changes: 11 additions & 5 deletions framework/api/paddlebase/test_randint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np

sys.path.append("../..")
from utils.interceptor import skip_platform_not_linux
from utils.interceptor import skip_platform_not_linux, skip_not_compile_gpu


class TestRandint(APIBase):
Expand All @@ -34,13 +34,15 @@ def hook(self):
obj = TestRandint(paddle.randint)


@skip_not_compile_gpu
@skip_platform_not_linux
@pytest.mark.api_base_randint_vartype
def test_randint_base():
"""
base
"""
res = np.array([[9, 9], [5, 8]])
obj.places = [paddle.CUDAPlace(0)]
res = np.array([[5, 4], [9, 2]])
obj.base(res=res, low=0, high=10, shape=[2, 2])


Expand All @@ -50,18 +52,21 @@ def test_randint():
"""
default
"""
obj.places = [paddle.CPUPlace()]
res = np.array([[[9, 9], [5, 8], [3, 4]], [[9, 4], [4, 7], [2, 9]], [[8, 4], [6, 2], [9, 3]]])
obj.run(res=res, low=0, high=10, shape=[3, 3, 2])


@skip_not_compile_gpu
@skip_platform_not_linux
@pytest.mark.api_base_randint_parameters
def test_randint1():
"""
seed = 1
"""
obj.seed = 1
res = np.array([[0, 7], [6, 0]])
obj.places = [paddle.CUDAPlace(0)]
res = np.array([[2, 0], [9, 7]])
obj.run(res=res, low=0, high=10, shape=[2, 2])


Expand All @@ -82,6 +87,7 @@ def test_randint3():
dtype is int64
"""
obj.seed = 1
obj.places = [paddle.CPUPlace()]
res = np.array([[0, 7], [6, 0]])
obj.run(res=res, low=0, high=10, shape=[2, 2], dtype=np.int64)

Expand All @@ -93,7 +99,7 @@ def test_randint4():
exception dtype is float BUG
"""
# res = np.array([[9, 9], [5, 8]])
obj.exception(etype="NotFoundError", low=0, high=10, shape=[2, 2], dtype=np.float32)
obj.exception(etype="NotFound", low=0, high=10, shape=[2, 2], dtype=np.float32)


@skip_platform_not_linux
Expand All @@ -103,7 +109,7 @@ def test_randint5():
exception low > high
"""
# res = np.array([[9, 9], [5, 8]])
obj.exception(etype="InvalidArgumentError", low=100, high=10, shape=[2, 2])
obj.exception(etype="InvalidArgument", low=100, high=10, shape=[2, 2])


@skip_platform_not_linux
Expand Down
4 changes: 2 additions & 2 deletions framework/api/paddlebase/test_randn.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_randn1():
"""
obj.seed = 1
# res = np.array([[0, 7], [6, 0]])
obj.exception(etype="NotFoundError", shape=[2, 2], dtype=np.int32)
obj.exception(etype="NotFound", shape=[2, 2], dtype=np.int32)


@skip_platform_not_linux
Expand Down Expand Up @@ -108,5 +108,5 @@ def test_randn5():
"""
obj.places = [fluid.CUDAPlace(0)]
obj.seed = 1
res = np.array([[-4.080414, -1.3719953], [0.2568497, 1.2351426]])
res = np.array([[-0.296278, 2.676448], [-0.14084621, -0.84409523]])
obj.run(res=res, shape=np.array([2, 2]))
16 changes: 11 additions & 5 deletions framework/api/paddlebase/test_randperm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

sys.path.append("../..")
from utils.interceptor import skip_platform_not_linux
from utils.interceptor import skip_platform_not_linux, skip_not_compile_gpu


class TestRandperm(APIBase):
Expand Down Expand Up @@ -40,18 +40,21 @@ def test_randperm_base():
"""
base
"""
obj.places = [paddle.CPUPlace()]
res = np.array([0, 1, 4, 3, 2, 5])
n = 6
obj.base(res=res, n=n)


@skip_not_compile_gpu
@skip_platform_not_linux
@pytest.mark.api_base_randperm_parameters
def test_randperm():
"""
default
"""
res = np.array([9, 1, 7, 8, 2, 5, 6, 4, 3, 0])
obj.places = [paddle.CUDAPlace(0)]
res = np.array([9, 4, 0, 7, 1, 5, 2, 3, 6, 8])
n = 10
obj.run(res=res, n=n)

Expand All @@ -62,20 +65,23 @@ def test_randperm1():
"""
seed = 1
"""
obj.places = [paddle.CPUPlace()]
obj.seed = 1
res = np.array([6, 3, 7, 8, 9, 2, 1, 5, 4, 0])
n = 10
obj.run(res=res, n=n)


@skip_not_compile_gpu
@skip_platform_not_linux
@pytest.mark.api_base_randperm_parameters
def test_randperm2():
"""
dtype = np.float32
"""
obj.places = [paddle.CUDAPlace(0)]
obj.seed = 33
res = np.array([9.0, 1.0, 7.0, 8.0, 2.0, 5.0, 6.0, 4.0, 3.0, 0.0])
res = np.array([9.0, 4.0, 0.0, 7.0, 1.0, 5.0, 2.0, 3.0, 6.0, 8.0])
n = 10
obj.run(res=res, n=n, dtype=np.float32)

Expand All @@ -89,7 +95,7 @@ def test_randperm3():
obj.seed = 33
# res = np.array([0.0, 1.0, 6.0, 2.0, 9.0, 3.0, 5.0, 7.0, 4.0, 8.0])
n = -1
obj.exception(etype="InvalidArgumentError", n=n, dtype=np.float32)
obj.exception(etype="InvalidArgument", n=n, dtype=np.float32)


@skip_platform_not_linux
Expand All @@ -101,4 +107,4 @@ def test_randperm4():
obj.seed = 33
# res = np.array([0.0, 1.0, 6.0, 2.0, 9.0, 3.0, 5.0, 7.0, 4.0, 8.0])
n = -1
obj.exception(etype="NotFoundError", n=n, dtype=np.int8)
obj.exception(etype="NotFound", n=n, dtype=np.int8)
1 change: 1 addition & 0 deletions framework/api/run_paddle_ci.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
home=$PWD
python3.7 -m pip install pytest
python3.7 -m pip install scipy
export FLAGS_use_curand=1

case_dir_list=('fft' 'device' 'incubate' 'linalg' 'paddlebase' 'loss' 'nn' 'optimizer')
result_array=()
Expand Down
2 changes: 1 addition & 1 deletion framework/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ home=$PWD
echo "branch is ${AGILE_COMPILE_BRANCH}"
python3.7 -m pip install pytest
python3.7 -m pip install scipy

export FLAGS_use_curand=1

# api
cd api
Expand Down

0 comments on commit a226540

Please sign in to comment.