From f507592a8415f32dde3508d8e433375925dd80a2 Mon Sep 17 00:00:00 2001 From: spade Date: Mon, 11 Oct 2021 15:14:17 +0800 Subject: [PATCH 01/13] add alpha_dropout test --- framework/api/nn/test_alpha_dropout.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 framework/api/nn/test_alpha_dropout.py diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py new file mode 100644 index 0000000000..e69de29bb2 From 44b0ef64d526281d959b4f3ee29b8c47a7421e03 Mon Sep 17 00:00:00 2001 From: spade Date: Mon, 11 Oct 2021 16:06:51 +0800 Subject: [PATCH 02/13] overwrite base_run --- framework/api/nn/test_alpha_dropout.py | 270 +++++++++++++++++++++++++ 1 file changed, 270 insertions(+) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index e69de29bb2..ac49c1fbe0 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -0,0 +1,270 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python +""" + test AlphaDropout +""" +import logging + +from paddle.fluid import layers + +from apibase import APIBase, compare, compare_grad +from apibase import randtool +import paddle +import pytest +import numpy as np + + +class TestAlphaDropout(APIBase): + """ + test + """ + + def hook(self): + self.types = [np.float32, np.float64] + self.seed = 100 + self.enable_backward = False + + # 重写base run,确保动态图和静态图的随机数产出相同 + def _baserun(self, res, data=None, **kwargs): + """ + baserun + Args: + res: expect result + **kwargs: kwargs + + Returns: + Assertion + """ + if self.debug: + # start run paddle dygraph + if self.dygraph: + paddle.disable_static(self.place) + if str(self.place) == "CPUPlace": + paddle.set_device("cpu") + else: + paddle.set_device("gpu:0") + paddle.seed(self.seed) + logging.info("[start] run " + self.__class__.__name__ + " dygraph") + self._check_params(res, data, **kwargs) + dygraph_forward_res = self._dygraph_forward() + logging.info("dygraph forward result is :") + if isinstance(dygraph_forward_res, (list)): + compare(dygraph_forward_res, res, self.delta, self.rtol) + logging.info(dygraph_forward_res) + else: + compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) + logging.info(dygraph_forward_res.numpy()) + if self.enable_backward: + dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) + logging.info("[dygraph grad]") + logging.info(dygraph_backward_res) + paddle.enable_static() + if self.static: + # start run paddle static + logging.info("[start] run " + self.__class__.__name__ + " static") + if self.enable_backward: + static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) + logging.info("static forward result is :") + logging.info(static_forward_res) + logging.info("[static grad]") + logging.info(static_backward_res) + else: + static_forward_res = self._static_forward(res, data, **kwargs) + logging.info("static forward result is :") + logging.info(static_forward_res) + compare(static_forward_res, res, self.delta, self.rtol) + # start run torch + if self.enable_backward: + grad = self.compute_grad(res, data, **kwargs) + logging.info("[numeric grad]") + logging.info(grad) + if self.static and self.dygraph: + compare_grad(static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var) + if self.dygraph: + compare_grad( + dygraph_backward_res, + grad, + mode="dygraph", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + if self.static: + compare_grad( + static_backward_res, + grad, + mode="static", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + else: + # start run paddle dygraph + logging.info("[start] run " + self.__class__.__name__ + " dygraph") + paddle.disable_static(self.place) + if str(self.place) == "CPUPlace": + paddle.set_device("cpu") + else: + paddle.set_device("gpu:0") + paddle.seed(self.seed) + self._check_params(res, data, **kwargs) + dygraph_forward_res = self._dygraph_forward() + if isinstance(dygraph_forward_res, (list)): + compare(dygraph_forward_res, res, self.delta, self.rtol) + else: + compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) + if self.enable_backward: + dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) + paddle.enable_static() + # start run paddle static + paddle.seed(self.seed) + logging.info("[start] run " + self.__class__.__name__ + " static") + if self.enable_backward: + static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) + else: + static_forward_res = self._static_forward(res, data, **kwargs) + compare(static_forward_res, res, self.delta, self.rtol) + # start run torch + if self.enable_backward: + grad = self.compute_grad(res, data, **kwargs) + compare_grad(static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var) + compare_grad( + dygraph_backward_res, + grad, + mode="dygraph", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + compare_grad( + static_backward_res, + grad, + mode="static", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + + +obj = TestAlphaDropout(paddle.nn.AlphaDropout) + + +def numpy_alpha_dropout(x, p, training=True): + """ + numpy version alpha dropout + """ + + def f_scale(x, scale=1.0, bias=0.0): + out = scale * x + bias + return out + + if training: + if p == 1: + return f_scale(x, scale=0.0) + # get transformation params + alpha = 1.6732632423543772848170429916717 + scale = 1.0507009873554804934193349852946 + alpha_p = -alpha * scale + a = ((1 - p) * (1 + p * alpha_p ** 2)) ** -0.5 + b = -a * alpha_p * p + + dtype = x.dtype + input_shape = x.shape + random_tensor = layers.uniform_random(input_shape, dtype="float32", min=0.0, max=1.0) + random_tensor = random_tensor.numpy() + p = np.ones(input_shape, dtype="float32") * p + keep_mask = np.greater_equal(random_tensor, p) + keep_mask = keep_mask.astype(dtype) + drop_mask = np.subtract(np.ones(shape=input_shape), keep_mask) + + b = np.ones(input_shape, dtype=dtype) * b + y = x * keep_mask + f_scale(drop_mask, scale=alpha_p) + res = f_scale(y, scale=a) + b + return res + else: + return x + + +@pytest.mark.api_nn_AlphaDropout_vartype +def test_alpha_dropout_base(): + """ + base + """ + x = randtool("float", 0, 2, [2, 3]) + p = 0.5 + paddle.seed(100) + res = numpy_alpha_dropout(x, p) + obj.base(res, data=x) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout1(): + """ + default + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 0.5 # defult is 0.5 + res = numpy_alpha_dropout(x, p) + func = paddle.nn.AlphaDropout() + paddle.seed(100) + paddle_res = func(paddle.to_tensor(x)) + compare(paddle_res, res) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p=1 + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 1.0 # defult is 0.5 + res = numpy_alpha_dropout(x, p) + func = paddle.nn.AlphaDropout(p=p) + paddle.seed(100) + paddle_res = func(paddle.to_tensor(res)) + compare(res, paddle_res) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p=0 + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 0.0 # defult is 0.5 + res = numpy_alpha_dropout(x, p) + func = paddle.nn.AlphaDropout(p=p) + paddle.seed(100) + paddle_res = func(paddle.to_tensor(res)) + compare(res, paddle_res) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = -1 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-1) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = 2, 使用exception接口 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-2) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = '1' + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=TypeError, mode="python", data=x, p="1") From 36c7b6a45a5376bb40c4ac629b6f4140d69784fa Mon Sep 17 00:00:00 2001 From: spade Date: Mon, 11 Oct 2021 19:06:48 +0800 Subject: [PATCH 03/13] test linux-ci --- framework/api/nn/test_alpha_dropout.py | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index ac49c1fbe0..1322defb17 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -252,19 +252,19 @@ def test_alpha_dropout2(): obj.exception(etype=ValueError, mode="python", data=x, p=-1) -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = 2, 使用exception接口 - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=ValueError, mode="python", data=x, p=-2) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = '1' - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=TypeError, mode="python", data=x, p="1") +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = 2, 使用exception接口 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=ValueError, mode="python", data=x, p=-2) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = '1' +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=TypeError, mode="python", data=x, p="1") From c3c8ca33924717b537df780e27f87e24bf7241a7 Mon Sep 17 00:00:00 2001 From: spade Date: Mon, 11 Oct 2021 20:29:18 +0800 Subject: [PATCH 04/13] remove Error Test --- framework/api/nn/test_alpha_dropout.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 1322defb17..a843a84321 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -243,13 +243,13 @@ def test_alpha_dropout2(): compare(res, paddle_res) -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = -1 - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=ValueError, mode="python", data=x, p=-1) +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = -1 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=ValueError, mode="python", data=x, p=-1) # @pytest.mark.api_nn_AlphaDropout_parameters From 13a5a59f31f89f51e74235afbdc42aa8a513a244 Mon Sep 17 00:00:00 2001 From: spade Date: Tue, 12 Oct 2021 08:52:27 +0800 Subject: [PATCH 05/13] retest linux-ci --- framework/api/nn/test_alpha_dropout.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index a843a84321..5faf4eb01e 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -207,10 +207,11 @@ def test_alpha_dropout1(): paddle.seed(100) p = 0.5 # defult is 0.5 res = numpy_alpha_dropout(x, p) - func = paddle.nn.AlphaDropout() + paddle.seed(100) + func = paddle.nn.AlphaDropout() paddle_res = func(paddle.to_tensor(x)) - compare(paddle_res, res) + compare(res, paddle_res) @pytest.mark.api_nn_AlphaDropout_parameters From 974e64441bd9b8c16a358ae0027351e61495d45e Mon Sep 17 00:00:00 2001 From: spade Date: Tue, 12 Oct 2021 13:15:45 +0800 Subject: [PATCH 06/13] fix test bug --- framework/api/nn/test_alpha_dropout.py | 229 +++++++++++++++++++------ 1 file changed, 179 insertions(+), 50 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 5faf4eb01e..3cef8e529a 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -5,15 +5,15 @@ test AlphaDropout """ import logging - from paddle.fluid import layers - from apibase import APIBase, compare, compare_grad from apibase import randtool import paddle import pytest import numpy as np +RANDOM_SEED = 100 + class TestAlphaDropout(APIBase): """ @@ -22,7 +22,7 @@ class TestAlphaDropout(APIBase): def hook(self): self.types = [np.float32, np.float64] - self.seed = 100 + self.seed = RANDOM_SEED self.enable_backward = False # 重写base run,确保动态图和静态图的随机数产出相同 @@ -146,11 +146,151 @@ def _baserun(self, res, data=None, **kwargs): no_grad_var=self.no_grad_var, ) + def run(self, res, data=None, **kwargs): + """ + run + Args: + res: expect result + **kwargs: kwargs + + Returns: + Assertion + """ + # 取默认type + if self.dtype is None: + if np.float64 in self.types: + self.dtype = np.float64 + else: + self.dtype = self.types[0] + if self.debug: + for place in self.places: + self.place = place + logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) + # start run paddle dygraph + if self.dygraph: + paddle.disable_static(self.place) + if str(self.place) == "CPUPlace": + paddle.set_device("cpu") + else: + paddle.set_device("gpu:0") + logging.info("[start] run " + self.__class__.__name__ + " dygraph") + paddle.seed(self.seed) + self._check_params(res, data, **kwargs) + dygraph_forward_res = self._dygraph_forward() + logging.info("dygraph forward result is :") + if isinstance(dygraph_forward_res, (list)): + compare(dygraph_forward_res, res, self.delta, self.rtol) + logging.info(dygraph_forward_res) + else: + compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) + logging.info(dygraph_forward_res.numpy()) + if self.enable_backward: + dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) + logging.info("[dygraph grad]") + logging.info(dygraph_backward_res) + paddle.enable_static() + if self.static: + + # start run paddle static + logging.info("[start] run " + self.__class__.__name__ + " static") + if self.enable_backward: + static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) + logging.info("static forward result is :") + logging.info(static_forward_res) + logging.info("[static grad]") + logging.info(static_backward_res) + else: + static_forward_res = self._static_forward(res, data, **kwargs) + logging.info("static forward result is :") + logging.info(static_forward_res) + compare(static_forward_res, res, self.delta, self.rtol) + # start run torch + if self.enable_backward: + paddle.seed(100) + grad = self.compute_grad(res, data, **kwargs) + logging.info("[numeric grad]") + logging.info(grad) + if self.static and self.dygraph: + compare_grad( + static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var + ) + if self.dygraph: + compare_grad( + dygraph_backward_res, + grad, + mode="dygraph", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + if self.static: + compare_grad( + static_backward_res, + grad, + mode="static", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + else: + for place in self.places: + self.place = place + paddle.disable_static(self.place) + if str(self.place) == "CPUPlace": + paddle.set_device("cpu") + else: + paddle.set_device("gpu:0") + logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) + # start run paddle dygraph + logging.info("[start] run " + self.__class__.__name__ + " dygraph") + paddle.disable_static(self.place) + paddle.seed(self.seed) + self._check_params(res, data, **kwargs) + dygraph_forward_res = self._dygraph_forward() + if isinstance(dygraph_forward_res, (list)): + compare(dygraph_forward_res, res, self.delta, self.rtol) + else: + compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) + if self.enable_backward: + dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) + paddle.enable_static() + # start run paddle static + paddle.seed(100) + logging.info("[start] run " + self.__class__.__name__ + " static") + if self.enable_backward: + static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) + else: + static_forward_res = self._static_forward(res, data, **kwargs) + compare(static_forward_res, res, self.delta, self.rtol) + # start run torch + if self.enable_backward: + grad = self.compute_grad(res, data, **kwargs) + compare_grad(static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var) + compare_grad( + dygraph_backward_res, + grad, + mode="dygraph", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + compare_grad( + static_backward_res, + grad, + mode="static", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + obj = TestAlphaDropout(paddle.nn.AlphaDropout) +paddle.seed(RANDOM_SEED) +np_random_tensor = layers.uniform_random([2, 3], dtype="float32", min=0.0, max=1.0) +np_random_tensor = np_random_tensor.numpy() -def numpy_alpha_dropout(x, p, training=True): +def numpy_alpha_dropout(x, p, random_tensor, training=True): """ numpy version alpha dropout """ @@ -171,8 +311,7 @@ def f_scale(x, scale=1.0, bias=0.0): dtype = x.dtype input_shape = x.shape - random_tensor = layers.uniform_random(input_shape, dtype="float32", min=0.0, max=1.0) - random_tensor = random_tensor.numpy() + p = np.ones(input_shape, dtype="float32") * p keep_mask = np.greater_equal(random_tensor, p) keep_mask = keep_mask.astype(dtype) @@ -194,7 +333,7 @@ def test_alpha_dropout_base(): x = randtool("float", 0, 2, [2, 3]) p = 0.5 paddle.seed(100) - res = numpy_alpha_dropout(x, p) + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) obj.base(res, data=x) @@ -206,12 +345,8 @@ def test_alpha_dropout1(): x = randtool("float", 0, 2, [2, 3]) paddle.seed(100) p = 0.5 # defult is 0.5 - res = numpy_alpha_dropout(x, p) - - paddle.seed(100) - func = paddle.nn.AlphaDropout() - paddle_res = func(paddle.to_tensor(x)) - compare(res, paddle_res) + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + obj.run(res, x) @pytest.mark.api_nn_AlphaDropout_parameters @@ -222,11 +357,8 @@ def test_alpha_dropout2(): x = randtool("float", 0, 2, [2, 3]) paddle.seed(100) p = 1.0 # defult is 0.5 - res = numpy_alpha_dropout(x, p) - func = paddle.nn.AlphaDropout(p=p) - paddle.seed(100) - paddle_res = func(paddle.to_tensor(res)) - compare(res, paddle_res) + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + obj.run(res, x) @pytest.mark.api_nn_AlphaDropout_parameters @@ -237,35 +369,32 @@ def test_alpha_dropout2(): x = randtool("float", 0, 2, [2, 3]) paddle.seed(100) p = 0.0 # defult is 0.5 - res = numpy_alpha_dropout(x, p) - func = paddle.nn.AlphaDropout(p=p) - paddle.seed(100) - paddle_res = func(paddle.to_tensor(res)) - compare(res, paddle_res) - - -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = -1 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=ValueError, mode="python", data=x, p=-1) - - -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = 2, 使用exception接口 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=ValueError, mode="python", data=x, p=-2) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = '1' -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=TypeError, mode="python", data=x, p="1") + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + obj.run(res, x) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = -1 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-1) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = 2, 使用exception接口 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-2) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p = '1' + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=TypeError, mode="python", data=x, p="1") From a00536cedf50e9c875180012b709fcebfae6ade2 Mon Sep 17 00:00:00 2001 From: spade Date: Tue, 12 Oct 2021 19:51:58 +0800 Subject: [PATCH 07/13] fix bug --- framework/api/nn/test_alpha_dropout.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 3cef8e529a..150fde8250 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -5,7 +5,6 @@ test AlphaDropout """ import logging -from paddle.fluid import layers from apibase import APIBase, compare, compare_grad from apibase import randtool import paddle @@ -285,9 +284,7 @@ def run(self, res, data=None, **kwargs): obj = TestAlphaDropout(paddle.nn.AlphaDropout) -paddle.seed(RANDOM_SEED) -np_random_tensor = layers.uniform_random([2, 3], dtype="float32", min=0.0, max=1.0) -np_random_tensor = np_random_tensor.numpy() +np_random_tensor = np.array([[0.55355287, 0.20714243, 0.01162981], [0.51577556, 0.36369765, 0.26091650]]) def numpy_alpha_dropout(x, p, random_tensor, training=True): From 21d66d78a69d22f83817fd05c6f964fd6b5c8c5a Mon Sep 17 00:00:00 2001 From: spadeaiverxin Date: Wed, 13 Oct 2021 10:51:35 +0800 Subject: [PATCH 08/13] fix gpu random tensor different from cpu bug. --- framework/api/nn/test_alpha_dropout.py | 176 ++++++++++++++++--------- 1 file changed, 111 insertions(+), 65 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 150fde8250..13e89e8400 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -20,10 +20,52 @@ class TestAlphaDropout(APIBase): """ def hook(self): - self.types = [np.float32, np.float64] + self.types = [np.float32] self.seed = RANDOM_SEED self.enable_backward = False + def _check_dtype(self, res, data, **kwargs): + """ + check dtype + Args: + res: res[0] result of cpu, res[1] result of gpu + **kwargs: kwargs + + Returns: + Assertion + """ + # check whether dtype is wrong, but it won't stop test cases behind, it will report at last + # remember user enable_backward + backward_tag = self.enable_backward + assert len(res) == 2 + + for place in self.places: + self.place = place + logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) + tag = True + for dtype in self.types: + # 判断是否应该做反向计算,只有float类型的需要反向,同时如果api明确没有反向,需要根据配置进行反向截断。 + if dtype in self.backward_dtype and backward_tag: + self.enable_backward = True + else: + self.enable_backward = False + logging.info("[test dtype] " + self.__class__.__name__ + str(dtype)) + try: + self.dtype = dtype + if str(place) == "CPUPlace": + self._baserun(res[0], data, **kwargs) + else: + self._baserun(res[1], data, **kwargs) + except Exception as e: + logging.error("[test dtype] " + self.__class__.__name__ + str(dtype) + " failed!!!") + tag = False + # assume(tag, "[Place {}] type check Error {}".format(str(self.place), str(dtype))) + assert tag, "[Place {}] type check Error {}".format(str(self.place), str(dtype)) + if self.debug: + logging.error(e) + self.dtype = None + self.enable_backward = backward_tag + # 重写base run,确保动态图和静态图的随机数产出相同 def _baserun(self, res, data=None, **kwargs): """ @@ -285,6 +327,7 @@ def run(self, res, 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]]) def numpy_alpha_dropout(x, p, random_tensor, training=True): @@ -331,67 +374,70 @@ def test_alpha_dropout_base(): p = 0.5 paddle.seed(100) res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) - obj.base(res, data=x) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout1(): - """ - default - """ - x = randtool("float", 0, 2, [2, 3]) - paddle.seed(100) - p = 0.5 # defult is 0.5 - res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) - obj.run(res, x) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p=1 - """ - x = randtool("float", 0, 2, [2, 3]) - paddle.seed(100) - p = 1.0 # defult is 0.5 - res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) - obj.run(res, x) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p=0 - """ - x = randtool("float", 0, 2, [2, 3]) - paddle.seed(100) - p = 0.0 # defult is 0.5 - res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) - obj.run(res, x) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = -1 - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=ValueError, mode="python", data=x, p=-1) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = 2, 使用exception接口 - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=ValueError, mode="python", data=x, p=-2) - - -@pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): - """ - p = '1' - """ - x = randtool("float", 0, 2, [2, 3]) - obj.exception(etype=TypeError, mode="python", data=x, p="1") + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + obj.base([res, gpu_res], data=x) + + +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout1(): +# """ +# default +# """ +# x = randtool("float", 0, 2, [2, 3]) +# paddle.seed(100) +# p = 0.5 # defult is 0.5 +# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) +# obj.run(res, x) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p=1 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# paddle.seed(100) +# p = 1.0 # defult is 0.5 +# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) +# obj.run(res, x) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p=0 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# paddle.seed(100) +# p = 0.0 # defult is 0.5 +# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) +# obj.run(res, x) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = -1 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=ValueError, mode="python", data=x, p=-1) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = 2, 使用exception接口 +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=ValueError, mode="python", data=x, p=-2) +# +# +# @pytest.mark.api_nn_AlphaDropout_parameters +# def test_alpha_dropout2(): +# """ +# p = '1' +# """ +# x = randtool("float", 0, 2, [2, 3]) +# obj.exception(etype=TypeError, mode="python", data=x, p="1") From 56eeb0fb37432991d8c2d44c0545e9a27003f498 Mon Sep 17 00:00:00 2001 From: spadeaiverxin Date: Wed, 13 Oct 2021 12:01:08 +0800 Subject: [PATCH 09/13] finished alpha_dropout_test --- framework/api/nn/test_alpha_dropout.py | 205 +++++++++---------------- 1 file changed, 69 insertions(+), 136 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 13e89e8400..82766f8df3 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -187,7 +187,7 @@ def _baserun(self, res, data=None, **kwargs): no_grad_var=self.no_grad_var, ) - def run(self, res, data=None, **kwargs): + def run(self, res_list, data=None, **kwargs): """ run Args: @@ -203,126 +203,58 @@ def run(self, res, data=None, **kwargs): self.dtype = np.float64 else: self.dtype = self.types[0] - if self.debug: - for place in self.places: - self.place = place - logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) - # start run paddle dygraph - if self.dygraph: - paddle.disable_static(self.place) - if str(self.place) == "CPUPlace": - paddle.set_device("cpu") - else: - paddle.set_device("gpu:0") - logging.info("[start] run " + self.__class__.__name__ + " dygraph") - paddle.seed(self.seed) - self._check_params(res, data, **kwargs) - dygraph_forward_res = self._dygraph_forward() - logging.info("dygraph forward result is :") - if isinstance(dygraph_forward_res, (list)): - compare(dygraph_forward_res, res, self.delta, self.rtol) - logging.info(dygraph_forward_res) - else: - compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) - logging.info(dygraph_forward_res.numpy()) - if self.enable_backward: - dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) - logging.info("[dygraph grad]") - logging.info(dygraph_backward_res) - paddle.enable_static() - if self.static: - # start run paddle static - logging.info("[start] run " + self.__class__.__name__ + " static") - if self.enable_backward: - static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) - logging.info("static forward result is :") - logging.info(static_forward_res) - logging.info("[static grad]") - logging.info(static_backward_res) - else: - static_forward_res = self._static_forward(res, data, **kwargs) - logging.info("static forward result is :") - logging.info(static_forward_res) - compare(static_forward_res, res, self.delta, self.rtol) - # start run torch - if self.enable_backward: - paddle.seed(100) - grad = self.compute_grad(res, data, **kwargs) - logging.info("[numeric grad]") - logging.info(grad) - if self.static and self.dygraph: - compare_grad( - static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var - ) - if self.dygraph: - compare_grad( - dygraph_backward_res, - grad, - mode="dygraph", - delta=self.delta, - rtol=self.rtol, - no_grad_var=self.no_grad_var, - ) - if self.static: - compare_grad( - static_backward_res, - grad, - mode="static", - delta=self.delta, - rtol=self.rtol, - no_grad_var=self.no_grad_var, - ) - else: - for place in self.places: - self.place = place - paddle.disable_static(self.place) - if str(self.place) == "CPUPlace": - paddle.set_device("cpu") - else: - paddle.set_device("gpu:0") - logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) - # start run paddle dygraph - logging.info("[start] run " + self.__class__.__name__ + " dygraph") - paddle.disable_static(self.place) - paddle.seed(self.seed) - self._check_params(res, data, **kwargs) - dygraph_forward_res = self._dygraph_forward() - if isinstance(dygraph_forward_res, (list)): - compare(dygraph_forward_res, res, self.delta, self.rtol) - else: - compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) - if self.enable_backward: - dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) - paddle.enable_static() - # start run paddle static - paddle.seed(100) - logging.info("[start] run " + self.__class__.__name__ + " static") - if self.enable_backward: - static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) - else: - static_forward_res = self._static_forward(res, data, **kwargs) - compare(static_forward_res, res, self.delta, self.rtol) - # start run torch - if self.enable_backward: - grad = self.compute_grad(res, data, **kwargs) - compare_grad(static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var) - compare_grad( - dygraph_backward_res, - grad, - mode="dygraph", - delta=self.delta, - rtol=self.rtol, - no_grad_var=self.no_grad_var, - ) - compare_grad( - static_backward_res, - grad, - mode="static", - delta=self.delta, - rtol=self.rtol, - no_grad_var=self.no_grad_var, - ) + for place in self.places: + self.place = place + paddle.disable_static(self.place) + if str(self.place) == "CPUPlace": + paddle.set_device("cpu") + res = res_list[0] + else: + paddle.set_device("gpu:0") + res = res_list[1] + logging.info("[Place] is ===============================>>>>>>>>" + str(self.place)) + # start run paddle dygraph + logging.info("[start] run " + self.__class__.__name__ + " dygraph") + paddle.disable_static(self.place) + paddle.seed(self.seed) + self._check_params(res, data, **kwargs) + dygraph_forward_res = self._dygraph_forward() + if isinstance(dygraph_forward_res, (list)): + compare(dygraph_forward_res, res, self.delta, self.rtol) + else: + compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) + if self.enable_backward: + dygraph_backward_res = self._dygraph_backward(dygraph_forward_res) + paddle.enable_static() + # start run paddle static + paddle.seed(100) + logging.info("[start] run " + self.__class__.__name__ + " static") + if self.enable_backward: + static_forward_res, static_backward_res = self._static_forward(res, data, **kwargs) + else: + static_forward_res = self._static_forward(res, data, **kwargs) + compare(static_forward_res, res, self.delta, self.rtol) + # start run torch + if self.enable_backward: + grad = self.compute_grad(res, data, **kwargs) + compare_grad(static_backward_res, dygraph_backward_res, mode="both", no_grad_var=self.no_grad_var) + compare_grad( + dygraph_backward_res, + grad, + mode="dygraph", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) + compare_grad( + static_backward_res, + grad, + mode="static", + delta=self.delta, + rtol=self.rtol, + no_grad_var=self.no_grad_var, + ) obj = TestAlphaDropout(paddle.nn.AlphaDropout) @@ -378,19 +310,19 @@ def test_alpha_dropout_base(): obj.base([res, gpu_res], data=x) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout1(): -# """ -# default -# """ -# x = randtool("float", 0, 2, [2, 3]) -# paddle.seed(100) -# p = 0.5 # defult is 0.5 -# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) -# obj.run(res, x) -# +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout1(): + """ + default + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 0.5 # defult is 0.5 + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + obj.run([res, gpu_res], x) + + # # @pytest.mark.api_nn_AlphaDropout_parameters # def test_alpha_dropout2(): @@ -401,7 +333,8 @@ def test_alpha_dropout_base(): # paddle.seed(100) # p = 1.0 # defult is 0.5 # res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) -# obj.run(res, x) +# gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) +# obj.run([res, gpu_res], x) # # # @pytest.mark.api_nn_AlphaDropout_parameters @@ -411,9 +344,9 @@ def test_alpha_dropout_base(): # """ # x = randtool("float", 0, 2, [2, 3]) # paddle.seed(100) -# p = 0.0 # defult is 0.5 # res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) -# obj.run(res, x) +# gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) +# obj.run([res, gpu_res], x) # # # @pytest.mark.api_nn_AlphaDropout_parameters From 7c664efe145357c6c8b8103c4b1ed874a3ea151c Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 14 Oct 2021 22:03:03 +0800 Subject: [PATCH 10/13] add doc --- framework/api/nn/test_alpha_dropout.py | 109 +++++++++++++------------ 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 82766f8df3..15dca1ddc3 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -300,7 +300,10 @@ def f_scale(x, scale=1.0, bias=0.0): @pytest.mark.api_nn_AlphaDropout_vartype def test_alpha_dropout_base(): """ - base + 基础测试, 包括: + 1. 数据类型测试 + 2. cpu/gpu测试 + 3. 动态图静态图结果正确性验证 """ x = randtool("float", 0, 2, [2, 3]) p = 0.5 @@ -313,7 +316,7 @@ def test_alpha_dropout_base(): @pytest.mark.api_nn_AlphaDropout_parameters def test_alpha_dropout1(): """ - default + 默认参数结果测试 """ x = randtool("float", 0, 2, [2, 3]) paddle.seed(100) @@ -323,54 +326,54 @@ def test_alpha_dropout1(): obj.run([res, gpu_res], x) -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p=1 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# paddle.seed(100) -# p = 1.0 # defult is 0.5 -# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) -# gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) -# obj.run([res, gpu_res], x) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p=0 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# paddle.seed(100) -# res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) -# gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) -# obj.run([res, gpu_res], x) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = -1 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=ValueError, mode="python", data=x, p=-1) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = 2, 使用exception接口 -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=ValueError, mode="python", data=x, p=-2) -# -# -# @pytest.mark.api_nn_AlphaDropout_parameters -# def test_alpha_dropout2(): -# """ -# p = '1' -# """ -# x = randtool("float", 0, 2, [2, 3]) -# obj.exception(etype=TypeError, mode="python", data=x, p="1") +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + 右边界测试,p=1 + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 1.0 # defult is 0.5 + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + obj.run([res, gpu_res], x) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + 左边界测试,p=0 + """ + x = randtool("float", 0, 2, [2, 3]) + paddle.seed(100) + p = 0 + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + obj.run([res, gpu_res], x) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p为负值异常测试, p=-1 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-1) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p大于1异常测试, p=2 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=ValueError, mode="python", data=x, p=-2) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout2(): + """ + p值类型错误测试,p = '1' string类型 + """ + x = randtool("float", 0, 2, [2, 3]) + obj.exception(etype=TypeError, mode="python", data=x, p="1") From d2066d9062e1e11f875569bbfd35377bf1a9a838 Mon Sep 17 00:00:00 2001 From: spade Date: Sun, 17 Oct 2021 14:09:05 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=EF=BC=8C=E5=A4=9A=E7=BB=B4=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_alpha_dropout.py | 34 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 15dca1ddc3..48a36a7d10 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -340,7 +340,7 @@ def test_alpha_dropout2(): @pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): +def test_alpha_dropout3(): """ 左边界测试,p=0 """ @@ -353,7 +353,7 @@ def test_alpha_dropout2(): @pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): +def test_alpha_dropout4(): """ p为负值异常测试, p=-1 """ @@ -362,7 +362,7 @@ def test_alpha_dropout2(): @pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): +def test_alpha_dropout5(): """ p大于1异常测试, p=2 """ @@ -371,9 +371,35 @@ def test_alpha_dropout2(): @pytest.mark.api_nn_AlphaDropout_parameters -def test_alpha_dropout2(): +def test_alpha_dropout6(): """ p值类型错误测试,p = '1' string类型 """ x = randtool("float", 0, 2, [2, 3]) obj.exception(etype=TypeError, mode="python", data=x, p="1") + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout7(): + """ + 多维度测试 + """ + _shape = (5, 10, 15, 20) + paddle.disable_static() + x = randtool("float", 0, 2, _shape) + paddle.seed(100) + paddle.set_device("cpu") + np_random_tensor = paddle.uniform(_shape, dtype="float32", min=0.0, max=1.0) + np_random_tensor = np.array(np_random_tensor) + + p = 0.5 + gpu_res = None + if paddle.device.is_compiled_with_cuda() is True: + paddle.set_device("gpu") + paddle.seed(100) + np_random_tensor_gpu = paddle.uniform(_shape, dtype="float32", min=0.0, max=1.0) + np_random_tensor_gpu = np.array(np_random_tensor_gpu) + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + obj.run([res, gpu_res], x) From 54644ab024ad29aca56ace1cc3632fb9de9f2149 Mon Sep 17 00:00:00 2001 From: spade Date: Mon, 18 Oct 2021 10:02:33 +0800 Subject: [PATCH 12/13] fixed bug --- framework/api/nn/test_alpha_dropout.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 48a36a7d10..388f8dd0b4 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -331,12 +331,13 @@ def test_alpha_dropout2(): """ 右边界测试,p=1 """ + paddle.disable_static() x = randtool("float", 0, 2, [2, 3]) paddle.seed(100) p = 1.0 # defult is 0.5 res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) - obj.run([res, gpu_res], x) + obj.run([res, gpu_res], x, p=p) @pytest.mark.api_nn_AlphaDropout_parameters @@ -349,7 +350,7 @@ def test_alpha_dropout3(): p = 0 res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) - obj.run([res, gpu_res], x) + obj.run([res, gpu_res], x, p=p) @pytest.mark.api_nn_AlphaDropout_parameters From e9831afa30075649a950365cbaf026c6db31f912 Mon Sep 17 00:00:00 2001 From: spade Date: Tue, 19 Oct 2021 14:47:41 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0x=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E6=AD=A3=E8=B4=9F=E3=80=81=E6=89=A9=E5=A4=A7=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_alpha_dropout.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/framework/api/nn/test_alpha_dropout.py b/framework/api/nn/test_alpha_dropout.py index 388f8dd0b4..33f901a4fe 100644 --- a/framework/api/nn/test_alpha_dropout.py +++ b/framework/api/nn/test_alpha_dropout.py @@ -387,7 +387,7 @@ def test_alpha_dropout7(): """ _shape = (5, 10, 15, 20) paddle.disable_static() - x = randtool("float", 0, 2, _shape) + x = randtool("float", -5, 10, _shape) paddle.seed(100) paddle.set_device("cpu") np_random_tensor = paddle.uniform(_shape, dtype="float32", min=0.0, max=1.0) @@ -404,3 +404,16 @@ def test_alpha_dropout7(): res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) obj.run([res, gpu_res], x) + + +@pytest.mark.api_nn_AlphaDropout_parameters +def test_alpha_dropout8(): + """ + x正负测试范围测试 + """ + x = randtool("float", -100, 100, [2, 3]) + paddle.seed(100) + p = 0.5 + res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor) + gpu_res = numpy_alpha_dropout(x, p, random_tensor=np_random_tensor_gpu) + obj.run([res, gpu_res], x, p=p)