diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index 4fcbae1a023a50..6802e180cee08d 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -18,7 +18,7 @@ from contextlib import contextmanager import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from scipy.special import erf, expit from utils import static_guard @@ -328,15 +328,7 @@ def setUp(self): self.x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) self.out_ref = np.expm1(self.x) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_static_api(self): def run(place): diff --git a/test/legacy_test/test_activation_sparse_op.py b/test/legacy_test/test_activation_sparse_op.py index 09a015fd712630..ee6f52bcf67b65 100644 --- a/test/legacy_test/test_activation_sparse_op.py +++ b/test/legacy_test/test_activation_sparse_op.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator +from op_test import get_places import paddle from paddle.base import core @@ -52,16 +52,7 @@ def check_with_place(self, place): np.testing.assert_array_equal(result_array, np.square(np_array)) def test_sparse_acti(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place) @@ -94,16 +85,7 @@ def check_with_place(self, place): np.testing.assert_allclose(result_array, np.sqrt(np_array), rtol=1e-05) def test_sparse_acti(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place) diff --git a/test/legacy_test/test_adagrad_op.py b/test/legacy_test/test_adagrad_op.py index 5eff818a08d9f8..d82ce1b4cc3847 100644 --- a/test/legacy_test/test_adagrad_op.py +++ b/test/legacy_test/test_adagrad_op.py @@ -18,7 +18,7 @@ import numpy as np from op import Operator -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle.base import core @@ -205,16 +205,7 @@ def get_out(param, lr, grad, m, epsilon): ) def test_sparse_adagrad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place) diff --git a/test/legacy_test/test_adam_op.py b/test/legacy_test/test_adam_op.py index f55319798aa769..f736159bf16c32 100644 --- a/test/legacy_test/test_adam_op.py +++ b/test/legacy_test/test_adam_op.py @@ -17,7 +17,7 @@ import numpy as np from op import Operator -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base @@ -577,16 +577,7 @@ def check_with_place(self, place, lazy_mode): self.assertLess((actual[i] - np_array[i]), 0.00001) def test_sparse_adam(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): for lazy_mode in (True, False): self.check_with_place(place, lazy_mode) diff --git a/test/legacy_test/test_adam_optimizer_fp32_fp64.py b/test/legacy_test/test_adam_optimizer_fp32_fp64.py index b21f98c7d85a49..a685dfe88452f3 100644 --- a/test/legacy_test/test_adam_optimizer_fp32_fp64.py +++ b/test/legacy_test/test_adam_optimizer_fp32_fp64.py @@ -12,28 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest +from op_test import get_places from utils import static_guard import paddle from paddle import base -def get_places(): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - return places - - def main_test_func(place, dtype): with static_guard(): main = base.Program() diff --git a/test/legacy_test/test_adaptive_avg_pool1d.py b/test/legacy_test/test_adaptive_avg_pool1d.py index 4407f6cf512c84..b4394f6c76069e 100644 --- a/test/legacy_test/test_adaptive_avg_pool1d.py +++ b/test/legacy_test/test_adaptive_avg_pool1d.py @@ -16,6 +16,7 @@ import unittest import numpy as np +from op_test import get_places import paddle import paddle.nn.functional as F @@ -83,15 +84,7 @@ def avg_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_adaptive_avg_dygraph_results(self, place): with base.dygraph.guard(place): diff --git a/test/legacy_test/test_adaptive_max_pool1d.py b/test/legacy_test/test_adaptive_max_pool1d.py index ed8c1d7a453a19..445a896a0146e1 100644 --- a/test/legacy_test/test_adaptive_max_pool1d.py +++ b/test/legacy_test/test_adaptive_max_pool1d.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import check_out_dtype, paddle_static_guard +from op_test import check_out_dtype, get_places, paddle_static_guard import paddle import paddle.nn.functional as F from paddle import base -from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -74,15 +72,7 @@ def max_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_adaptive_max_dygraph_results(self, place): with base.dygraph.guard(place): @@ -149,15 +139,7 @@ def test_max_pool(self): class TestPool1D_API_ZeroSize(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_adaptive_max_dygraph_results(self, place): with base.dygraph.guard(place): diff --git a/test/legacy_test/test_arg_min_max_v2_op.py b/test/legacy_test/test_arg_min_max_v2_op.py index 1913684ff3aec9..2dc0ea922f0709 100644 --- a/test/legacy_test/test_arg_min_max_v2_op.py +++ b/test/legacy_test/test_arg_min_max_v2_op.py @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle -from paddle import base from paddle.base import Program, core, program_guard @@ -166,15 +164,7 @@ class ArgMaxMinTestCase(unittest.TestCase): def setUp(self): np.random.seed(123) self.input_data = np.random.rand(10, 10).astype("float32") - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() self.op = eval(f"paddle.{op_type}") self.numpy_op = eval(f"np.{op_type}") diff --git a/test/legacy_test/test_assign_op.py b/test/legacy_test/test_assign_op.py index 7abdddf538249b..7dca1d87317317 100644 --- a/test/legacy_test/test_assign_op.py +++ b/test/legacy_test/test_assign_op.py @@ -12,18 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np import op_test from decorator_helper import prog_scope -from op_test import convert_float_to_uint16, convert_uint16_to_float +from op_test import convert_float_to_uint16, convert_uint16_to_float, get_places import paddle from paddle import base -from paddle.base import Program, core, program_guard +from paddle.base import Program, program_guard from paddle.base.backward import append_backward @@ -327,16 +326,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) paddle.disable_static() @@ -365,16 +355,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) paddle.disable_static() diff --git a/test/legacy_test/test_bce_with_logits_loss.py b/test/legacy_test/test_bce_with_logits_loss.py index 1a6d9c1c3819e5..a897cd34bd2084 100644 --- a/test/legacy_test/test_bce_with_logits_loss.py +++ b/test/legacy_test/test_bce_with_logits_loss.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle import base @@ -143,15 +143,7 @@ class TestBCEWithLogitsLoss(unittest.TestCase): def test_BCEWithLogitsLoss(self): logit_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float64) label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float64) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: diff --git a/test/legacy_test/test_box_coder_op.py b/test/legacy_test/test_box_coder_op.py index 7f2dfd99492216..c3d0f49bf3d0ca 100644 --- a/test/legacy_test/test_box_coder_op.py +++ b/test/legacy_test/test_box_coder_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle @@ -340,15 +339,7 @@ def setUp(self): self.box_normalized, self.axis, ) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_dygraph_api(self): def run(place): diff --git a/test/legacy_test/test_bucketize_api.py b/test/legacy_test/test_bucketize_api.py index 4723a5a8b573c5..987ae121ba24bf 100644 --- a/test/legacy_test/test_bucketize_api.py +++ b/test/legacy_test/test_bucketize_api.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle.base import core np.random.seed(10) @@ -29,15 +28,7 @@ class TestBucketizeAPI(unittest.TestCase): def setUp(self): self.sorted_sequence = np.array([2, 4, 8, 16]).astype("float64") self.x = np.array([[0, 8, 4, 16], [-1, 2, 8, 4]]).astype("float64") - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): paddle.enable_static() diff --git a/test/legacy_test/test_cast_op.py b/test/legacy_test/test_cast_op.py index 3bf8b2038cdc8e..b8e6be0557588e 100644 --- a/test/legacy_test/test_cast_op.py +++ b/test/legacy_test/test_cast_op.py @@ -12,17 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16, convert_uint16_to_float +from op_test import ( + OpTest, + convert_float_to_uint16, + convert_uint16_to_float, + get_places, +) import paddle from paddle import base -from paddle.base import Program, core, program_guard +from paddle.base import Program, program_guard def cast_wrapper(x, out_dtype=None): @@ -240,16 +244,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) paddle.disable_static() @@ -278,16 +273,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) paddle.disable_static() diff --git a/test/legacy_test/test_clip_by_norm_op.py b/test/legacy_test/test_clip_by_norm_op.py index fba68687b5ef04..78b3e0068ab4ef 100644 --- a/test/legacy_test/test_clip_by_norm_op.py +++ b/test/legacy_test/test_clip_by_norm_op.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle.base import core @@ -188,17 +187,7 @@ def check_with_place(self, place): ) def test_clip_by_norm_with_selected_ros(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for place in places: + for place in get_places(): self.check_with_place(place) def config_test_case(self): diff --git a/test/legacy_test/test_complex_abs.py b/test/legacy_test/test_complex_abs.py index ecebeeeba94646..35297be1bb2816 100644 --- a/test/legacy_test/test_complex_abs.py +++ b/test/legacy_test/test_complex_abs.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle import paddle.base.dygraph as dg @@ -81,15 +80,7 @@ def test_check_grad(self): class TestAbs(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_all_positive(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_complex_elementwise_layers.py b/test/legacy_test/test_complex_elementwise_layers.py index 75670c05e9f94d..ebcd72a4b05874 100644 --- a/test/legacy_test/test_complex_elementwise_layers.py +++ b/test/legacy_test/test_complex_elementwise_layers.py @@ -12,15 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from numpy.random import random as rand +from op_test import get_places import paddle import paddle.base.dygraph as dg -from paddle import base paddle_apis = { "add": paddle.add, @@ -33,15 +32,7 @@ class TestComplexElementwiseLayers(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def paddle_calc(self, x, y, op, place): with dg.guard(place): diff --git a/test/legacy_test/test_complex_getitem.py b/test/legacy_test/test_complex_getitem.py index 975c52a9832c92..e2073a5eec9efc 100644 --- a/test/legacy_test/test_complex_getitem.py +++ b/test/legacy_test/test_complex_getitem.py @@ -12,27 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg -from paddle import base class TestComplexGetitemLayer(unittest.TestCase): def setUp(self): - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self._places.append(base.CUDAPlace(0)) + self._places = get_places() def test_case1(self): x_np = np.random.randn(2, 3, 4) + 1j * np.random.randn(2, 3, 4) diff --git a/test/legacy_test/test_complex_kron.py b/test/legacy_test/test_complex_kron.py index 0f6a897285152e..528e1a22201199 100644 --- a/test/legacy_test/test_complex_kron.py +++ b/test/legacy_test/test_complex_kron.py @@ -12,14 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg -from paddle import base class ComplexKronTestCase(unittest.TestCase): @@ -30,15 +29,7 @@ def __init__(self, methodName='runTest', x=None, y=None): def setUp(self): self.ref_result = np.kron(self.x, self.y) - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if base.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def runTest(self): for place in self._places: diff --git a/test/legacy_test/test_complex_sum_layer.py b/test/legacy_test/test_complex_sum_layer.py index 1afafe311b6ae2..4c422af5d80c37 100644 --- a/test/legacy_test/test_complex_sum_layer.py +++ b/test/legacy_test/test_complex_sum_layer.py @@ -12,29 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from numpy.random import random as rand +from op_test import get_places import paddle import paddle.base.dygraph as dg -from paddle import base, tensor +from paddle import tensor class TestComplexSumLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_complex_basic_api(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_complex_transpose.py b/test/legacy_test/test_complex_transpose.py index ecc2c8ad3fe8d4..aa8fa5f2b6c30e 100644 --- a/test/legacy_test/test_complex_transpose.py +++ b/test/legacy_test/test_complex_transpose.py @@ -12,28 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg -from paddle import base class TestComplexTransposeLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_transpose_by_complex_api(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_concat_op.py b/test/legacy_test/test_concat_op.py index 3318837814eb98..72965297cdd366 100644 --- a/test/legacy_test/test_concat_op.py +++ b/test/legacy_test/test_concat_op.py @@ -12,13 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci +from op_test import ( + OpTest, + convert_float_to_uint16, + get_places, + skip_check_grad_ci, +) import paddle import paddle.distributed as dist @@ -899,16 +903,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -948,16 +943,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_conj_op.py b/test/legacy_test/test_conj_op.py index 447f27c1609744..8f8083a68534ed 100644 --- a/test/legacy_test/test_conj_op.py +++ b/test/legacy_test/test_conj_op.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import sys import unittest @@ -22,7 +21,7 @@ sys.path.append("..") from numpy.random import random as rand -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle.base.dygraph as dg from paddle import static @@ -95,15 +94,7 @@ def init_input_output(self): class TestComplexConjOp(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_conj_api(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_conv2d_transpose_op.py b/test/legacy_test/test_conv2d_transpose_op.py index ff5d98240fa2b6..3e2ac30510e4f4 100644 --- a/test/legacy_test/test_conv2d_transpose_op.py +++ b/test/legacy_test/test_conv2d_transpose_op.py @@ -26,7 +26,12 @@ paddle.enable_static() import sys -from op_test import OpTest, convert_float_to_uint16, get_numeric_gradient +from op_test import ( + OpTest, + convert_float_to_uint16, + get_numeric_gradient, + get_places, +) sys.path.append("../deprecated/legacy_test") from test_attribute_var import UnittestBase @@ -1535,15 +1540,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCHW" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_conv_nn_grad.py b/test/legacy_test/test_conv_nn_grad.py index 6439dbe58e80db..17296ba5488998 100644 --- a/test/legacy_test/test_conv_nn_grad.py +++ b/test/legacy_test/test_conv_nn_grad.py @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope +from op_test import get_places import paddle import paddle.nn.functional as F @@ -45,16 +45,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -78,16 +69,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -111,16 +93,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -144,16 +117,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -177,16 +141,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -210,16 +165,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -243,16 +189,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -276,16 +213,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -310,16 +238,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -344,16 +263,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -377,16 +287,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -410,16 +311,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -443,16 +335,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -477,16 +360,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) @@ -511,16 +385,7 @@ def func_pir(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func_pir(p) diff --git a/test/legacy_test/test_cosine_similarity_api.py b/test/legacy_test/test_cosine_similarity_api.py index 98307f45357e1b..c99ded86749d29 100644 --- a/test/legacy_test/test_cosine_similarity_api.py +++ b/test/legacy_test/test_cosine_similarity_api.py @@ -12,28 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.nn.functional as F from paddle import nn, static -from paddle.base import Executor, core +from paddle.base import Executor class TestCosineSimilarityAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def _get_numpy_out(self, x1, x2, axis=1, eps=1e-8): bs = np.broadcast_shapes([x1.shape[axis]], [x2.shape[axis]]) diff --git a/test/legacy_test/test_custom_grad_input.py b/test/legacy_test/test_custom_grad_input.py index 50eb3659dd35e2..902d10cb12fcfb 100644 --- a/test/legacy_test/test_custom_grad_input.py +++ b/test/legacy_test/test_custom_grad_input.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg @@ -24,15 +24,7 @@ class TestTensorBackward(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_tensor_backward(self): for dtype in self._dtypes: @@ -59,15 +51,7 @@ def test_tensor_backward(self): class TestBackwardAPI(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) + self._places = get_places() def test_backward_api(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_diff_op.py b/test/legacy_test/test_diff_op.py index 686b07c9c68c0e..cff2a731bfa4dd 100644 --- a/test/legacy_test/test_diff_op.py +++ b/test/legacy_test/test_diff_op.py @@ -12,14 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base, static -from paddle.base import core +from paddle import static class TestDiffOp(unittest.TestCase): @@ -53,15 +52,7 @@ def get_output(self): def setUp(self): self.set_args() self.get_output() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def func_dygraph(self): for place in self.places: @@ -86,16 +77,7 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_digamma_op.py b/test/legacy_test/test_digamma_op.py index 81ede789be3f30..f5203df20d5bb9 100644 --- a/test/legacy_test/test_digamma_op.py +++ b/test/legacy_test/test_digamma_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from scipy.special import psi import paddle @@ -112,15 +111,7 @@ def setUp(self): paddle.enable_static() # prepare test attrs self.dtypes = ["float32", "float64"] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() self._shape = [8, 3, 32, 32] def test_in_static_mode(self): diff --git a/test/legacy_test/test_dynamic_rnn_stop_gradient.py b/test/legacy_test/test_dynamic_rnn_stop_gradient.py index 0a350928c05323..e843bcd6db18b2 100644 --- a/test/legacy_test/test_dynamic_rnn_stop_gradient.py +++ b/test/legacy_test/test_dynamic_rnn_stop_gradient.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle import base @@ -96,17 +96,7 @@ def run_main(self, place): np.testing.assert_array_equal(value1, value2) def test_check_main(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for p in places: + for p in get_places(): self.run_main(p) diff --git a/test/legacy_test/test_eager_deletion_while_op.py b/test/legacy_test/test_eager_deletion_while_op.py index 509b9c31408c98..994b3b33a3da85 100644 --- a/test/legacy_test/test_eager_deletion_while_op.py +++ b/test/legacy_test/test_eager_deletion_while_op.py @@ -19,6 +19,7 @@ import unittest import numpy +from op_test import get_places import paddle from paddle import base @@ -32,17 +33,7 @@ class TestEagerDeletionWhileOpBase(unittest.TestCase): def test_main(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for p in places: + for p in get_places(): with ( base.program_guard(base.Program(), base.Program()), base.scope_guard(base.Scope()), diff --git a/test/legacy_test/test_elementwise_floordiv_op.py b/test/legacy_test/test_elementwise_floordiv_op.py index a04487bc0bbd63..5052002b29fc44 100644 --- a/test/legacy_test/test_elementwise_floordiv_op.py +++ b/test/legacy_test/test_elementwise_floordiv_op.py @@ -12,16 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import random import unittest from contextlib import contextmanager import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle -from paddle import base, static +from paddle import static class TestElementwiseModOp(OpTest): @@ -117,16 +116,7 @@ class TestFloorDivideOp(unittest.TestCase): def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): for dtype in ( 'int32', 'int64', @@ -164,16 +154,7 @@ def test_static(self): def test_dygraph(self): paddle.disable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): for dtype in ( 'uint8', 'int8', diff --git a/test/legacy_test/test_elementwise_pow_op.py b/test/legacy_test/test_elementwise_pow_op.py index 75d22bdc1c5ec8..2f46a9d1abfb78 100644 --- a/test/legacy_test/test_elementwise_pow_op.py +++ b/test/legacy_test/test_elementwise_pow_op.py @@ -12,11 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci +from op_test import ( + OpTest, + convert_float_to_uint16, + get_places, + skip_check_grad_ci, +) import paddle from paddle import base @@ -277,16 +281,7 @@ def setUp(self): ).astype("int") def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if base.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): with base.dygraph.guard(place): x = paddle.to_tensor(self.x) y = paddle.to_tensor(self.y) diff --git a/test/legacy_test/test_erfinv_op.py b/test/legacy_test/test_erfinv_op.py index bb9190b9b09d00..41e2e3f6b8ac5d 100644 --- a/test/legacy_test/test_erfinv_op.py +++ b/test/legacy_test/test_erfinv_op.py @@ -12,11 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16, convert_uint16_to_float +from op_test import ( + OpTest, + convert_float_to_uint16, + convert_uint16_to_float, + get_places, +) from scipy.special import erfinv import paddle @@ -78,15 +82,7 @@ def setUp(self): self.init_dtype() self.x = np.random.rand(5).astype(self.dtype) self.res_ref = erfinv(self.x) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_static_api(self): paddle.enable_static() diff --git a/test/legacy_test/test_executor_return_tensor_not_overwriting.py b/test/legacy_test/test_executor_return_tensor_not_overwriting.py index 8523d40d50ea5c..ea542ff693326b 100644 --- a/test/legacy_test/test_executor_return_tensor_not_overwriting.py +++ b/test/legacy_test/test_executor_return_tensor_not_overwriting.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, skip_check_grad_ci +from op_test import OpTest, get_places, skip_check_grad_ci import paddle from paddle import base @@ -58,17 +57,7 @@ def calc_mul_out(self, place=None, parallel=None): return outs def test_executor_run_twice(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for place in places: + for place in get_places(): for parallel in [True, False]: add_out = self.calc_add_out(place, parallel) add_out1 = np.array(add_out[0]) @@ -100,17 +89,7 @@ def calc_sub_out(self, place=None): return out def test_executor_run_twice(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for place in places: + for place in get_places(): add_out = self.calc_add_out(place) add_out1 = np.array(add_out[0]) sub_out = self.calc_sub_out(place) diff --git a/test/legacy_test/test_expand_v2_op.py b/test/legacy_test/test_expand_v2_op.py index 5ac7d0dfb881f9..3b4f193dd3e217 100644 --- a/test/legacy_test/test_expand_v2_op.py +++ b/test/legacy_test/test_expand_v2_op.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from utils import static_guard import paddle @@ -510,16 +509,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -547,16 +537,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_flip.py b/test/legacy_test/test_flip.py index 72c96c0c123d44..beafbb3a7998a6 100644 --- a/test/legacy_test/test_flip.py +++ b/test/legacy_test/test_flip.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle import base @@ -260,16 +259,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -297,16 +287,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_fold_op.py b/test/legacy_test/test_fold_op.py index 8602d1f226b33b..78b5f95e4e17f4 100644 --- a/test/legacy_test/test_fold_op.py +++ b/test/legacy_test/test_fold_op.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base -from paddle.base import core paddle.enable_static() @@ -185,15 +183,7 @@ def setUp(self): self.op_type = 'fold' self.python_api = paddle.nn.functional.fold self.set_data() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_api(self): for place in self.places: diff --git a/test/legacy_test/test_ftrl_op.py b/test/legacy_test/test_ftrl_op.py index fe564d5958bfd2..150700b38297ea 100644 --- a/test/legacy_test/test_ftrl_op.py +++ b/test/legacy_test/test_ftrl_op.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle.base import core @@ -241,16 +240,7 @@ def init_kernel(self): pass def test_sparse_ftrl(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place) diff --git a/test/legacy_test/test_functional_conv1d.py b/test/legacy_test/test_functional_conv1d.py index f5490e93da7f76..7431d4c9a17d97 100644 --- a/test/legacy_test/test_functional_conv1d.py +++ b/test/legacy_test/test_functional_conv1d.py @@ -12,16 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import TestCase import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import base class TestFunctionalConv1DError(TestCase): @@ -112,15 +111,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCL" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_functional_conv1d_transpose.py b/test/legacy_test/test_functional_conv1d_transpose.py index 3818c062b14ae6..037b311fe26ed6 100644 --- a/test/legacy_test/test_functional_conv1d_transpose.py +++ b/test/legacy_test/test_functional_conv1d_transpose.py @@ -12,16 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import TestCase import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import base class TestFunctionalConv1DError(TestCase): @@ -98,15 +97,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCL" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py index e9fd4f46ff57b9..a9c0e0320307e7 100644 --- a/test/legacy_test/test_functional_conv2d.py +++ b/test/legacy_test/test_functional_conv2d.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import TestCase import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg @@ -269,15 +269,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCHW" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py index 2145ea4a7d063f..ed1cfd27b1f9cf 100644 --- a/test/legacy_test/test_functional_conv3d.py +++ b/test/legacy_test/test_functional_conv3d.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import TestCase import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg @@ -250,15 +250,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCDHW" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_functional_conv3d_transpose.py b/test/legacy_test/test_functional_conv3d_transpose.py index e51b3e0602c7b6..ea67ed1edffafa 100644 --- a/test/legacy_test/test_functional_conv3d_transpose.py +++ b/test/legacy_test/test_functional_conv3d_transpose.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import TestCase import numpy as np +from op_test import get_places import paddle import paddle.base.dygraph as dg @@ -282,15 +282,7 @@ def setUp(self): self.dilation = 1 self.groups = 1 self.data_format = "NCDHW" - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_gather_op.py b/test/legacy_test/test_gather_op.py index cdb5c9e8488113..7974f6435fa82c 100644 --- a/test/legacy_test/test_gather_op.py +++ b/test/legacy_test/test_gather_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from utils import dygraph_guard import paddle @@ -915,15 +914,7 @@ def setUp(self): self.dtype = 'float32' self.index = (1, 3, 5) self.index_dtype = 'int64' - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def test_gather_backward(self): if len(self.places) != 2: diff --git a/test/legacy_test/test_get_tensor_from_selected_rows_op.py b/test/legacy_test/test_get_tensor_from_selected_rows_op.py index a708861ab79f02..eef54eb478e91d 100644 --- a/test/legacy_test/test_get_tensor_from_selected_rows_op.py +++ b/test/legacy_test/test_get_tensor_from_selected_rows_op.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator +from op_test import get_places import paddle from paddle.base import Program, core, program_guard @@ -46,16 +46,7 @@ def test_SELECTED_ROWS(): class TestGetTensorFromSelectedRows(unittest.TestCase): def get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - return places + return get_places() def check_with_place(self, place): scope = core.Scope() diff --git a/test/legacy_test/test_i0e_op.py b/test/legacy_test/test_i0e_op.py index 503b5a6733e2c5..fef8911939fc99 100644 --- a/test/legacy_test/test_i0e_op.py +++ b/test/legacy_test/test_i0e_op.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from scipy import special import paddle -from paddle.base import core np.random.seed(100) paddle.seed(100) @@ -46,15 +44,7 @@ class TestI0eAPI(unittest.TestCase): def setUp(self): self.x = np.array(self.DATA).astype(self.DTYPE) self.out_ref = output_i0e(self.x) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): def run(place): diff --git a/test/legacy_test/test_i1e_op.py b/test/legacy_test/test_i1e_op.py index cd6cdc4beafafd..af0ced5316da96 100644 --- a/test/legacy_test/test_i1e_op.py +++ b/test/legacy_test/test_i1e_op.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from scipy import special import paddle -from paddle.base import core np.random.seed(42) paddle.seed(42) @@ -45,15 +43,7 @@ class TestI1e_API(unittest.TestCase): def setUp(self): self.x = np.array(self.DATA).astype(self.DTYPE) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): diff --git a/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py b/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py index 2accdd2c06d639..cca2cfe7215a1c 100644 --- a/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py +++ b/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places from test_imperative_base import new_program_scope import paddle @@ -214,17 +214,7 @@ def run_case(self, place, is_sort_sum_gradient, is_sparse, dtype): np.testing.assert_array_equal(value, dy_param_updated[key]) def simple_net_float(self, is_sparse, dtype): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for place in places: + for place in get_places(): for is_sort_sum_gradient in [True, False]: self.run_case(place, is_sort_sum_gradient, is_sparse, dtype) diff --git a/test/legacy_test/test_inverse_op.py b/test/legacy_test/test_inverse_op.py index 081821b4baf549..48f8db48a79940 100644 --- a/test/legacy_test/test_inverse_op.py +++ b/test/legacy_test/test_inverse_op.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base -from paddle.base import core class TestInverseOp(OpTest): @@ -135,15 +133,7 @@ def test_grad(self): class TestInverseAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with paddle.static.program_guard( @@ -202,15 +192,7 @@ def test_errors(self): class TestInverseSingularAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with paddle.static.program_guard( @@ -255,15 +237,7 @@ def test_dygraph(self): class TestInverseAPI_ZeroSize(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dygraph(self): for place in self.places: diff --git a/test/legacy_test/test_isclose_op.py b/test/legacy_test/test_isclose_op.py index 178d9bb83d0706..84446406d0cd67 100644 --- a/test/legacy_test/test_isclose_op.py +++ b/test/legacy_test/test_isclose_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle.base import core @@ -120,16 +119,7 @@ def test_api_case(self): paddle.enable_static() x_data = np.random.rand(10, 10) y_data = np.random.rand(10, 10) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.base.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.base.CUDAPlace(0)) - for place in places: + for place in get_places(): main = paddle.static.Program() startup = paddle.static.Program() with paddle.static.program_guard(main, startup): @@ -152,16 +142,7 @@ def test_api_case(self): class TestIscloseDygraph(unittest.TestCase): def test_api_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static() x_data = np.random.rand(10, 10) y_data = np.random.rand(10, 10) diff --git a/test/legacy_test/test_lerp_op.py b/test/legacy_test/test_lerp_op.py index a021d6164de84c..97d78d7b743e9c 100644 --- a/test/legacy_test/test_lerp_op.py +++ b/test/legacy_test/test_lerp_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle.base import core @@ -147,15 +146,7 @@ def setUp(self): self.y = np.full(4, 10.0).astype(self.dtype) self.w = np.asarray([0.75]).astype(self.dtype) self.res_ref = self.x + self.w * (self.y - self.x) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_static_api(self): paddle.enable_static() diff --git a/test/legacy_test/test_linalg_cholesky_inverse.py b/test/legacy_test/test_linalg_cholesky_inverse.py index e3efbe34343e0f..811c4d3b5730c0 100644 --- a/test/legacy_test/test_linalg_cholesky_inverse.py +++ b/test/legacy_test/test_linalg_cholesky_inverse.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base from paddle.base import core RTOL = {'float32': 1e-7, 'float64': 1e-11} @@ -38,15 +37,7 @@ def setUp(self): self.init_dtype() self.generate_input() self.generate_output() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def generate_input(self): self._shape = (3, 3) @@ -85,15 +76,7 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for place in places: with paddle.static.program_guard( diff --git a/test/legacy_test/test_linalg_matrix_exp.py b/test/legacy_test/test_linalg_matrix_exp.py index 3eccfcebe9ba54..cd4bcd1a0b3c0f 100644 --- a/test/legacy_test/test_linalg_matrix_exp.py +++ b/test/legacy_test/test_linalg_matrix_exp.py @@ -18,10 +18,9 @@ import numpy as np import scipy +from op_test import get_places import paddle -from paddle import base -from paddle.base import core os.environ['NVIDIA_TF32_OVERRIDE'] = '0' @@ -44,15 +43,7 @@ def setUp(self): self.init_config() self.generate_input() self.generate_output() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def generate_input(self): self._input_shape = (5, 5) @@ -84,17 +75,8 @@ def test_dygraph(self): # def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - - for place in places: + + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_lr_scheduler.py b/test/legacy_test/test_lr_scheduler.py index d76a8e3b80fa50..25d56b15ec1ce3 100644 --- a/test/legacy_test/test_lr_scheduler.py +++ b/test/legacy_test/test_lr_scheduler.py @@ -13,13 +13,12 @@ # limitations under the License. import math -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle.base import core def reduce_lr_on_plateau( @@ -71,15 +70,7 @@ def test_ReduceLR(self): with self.assertRaises(TypeError): paddle.optimizer.lr.ReduceOnPlateau(learning_rate=0.5).step("test") - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() for place in places: for m, n in zip( @@ -295,15 +286,7 @@ def test_CosineRestartsLR(self): T_mult=1.0, ) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() for place in places: for T_0 in [1, 2, 3]: @@ -1305,15 +1288,7 @@ def test_scheduler(self): ] for python_func, paddle_api, kwarg in func_api_kwargs: - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() for place in places: paddle.enable_static() diff --git a/test/legacy_test/test_lu_op.py b/test/legacy_test/test_lu_op.py index 73c5f713d87ba5..7f039fad59e90e 100644 --- a/test/legacy_test/test_lu_op.py +++ b/test/legacy_test/test_lu_op.py @@ -14,17 +14,15 @@ import copy import itertools -import os import unittest import numpy as np import scipy import scipy.linalg -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base -from paddle.base import core def scipy_lu(A, pivot): @@ -203,16 +201,7 @@ def run_lu_dygraph(shape, dtype): min_mn = min(m, n) pivot = True - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static(place) batch_size = a.size // (a.shape[-1] * a.shape[-2]) x = paddle.to_tensor(a, dtype=dtype) @@ -259,16 +248,7 @@ def run_lu_static(shape, dtype): min_mn = min(m, n) pivot = True - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_lu_unpack_op.py b/test/legacy_test/test_lu_unpack_op.py index c94d2be8995879..dbd1b099462bf1 100644 --- a/test/legacy_test/test_lu_unpack_op.py +++ b/test/legacy_test/test_lu_unpack_op.py @@ -14,13 +14,12 @@ import copy import itertools -import os import unittest import numpy as np import scipy import scipy.linalg -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base @@ -230,16 +229,7 @@ def run_lu_unpack_dygraph(shape, dtype): n = a.shape[-1] min_mn = min(m, n) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static(place) x = paddle.to_tensor(a, dtype=dtype) @@ -279,16 +269,7 @@ def run_lu_static(shape, dtype): n = a.shape[-1] min_mn = min(m, n) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_masked_fill.py b/test/legacy_test/test_masked_fill.py index 8c3d919fda4daf..a943fa17492376 100644 --- a/test/legacy_test/test_masked_fill.py +++ b/test/legacy_test/test_masked_fill.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import convert_float_to_uint16 +from op_test import convert_float_to_uint16, get_places import paddle from paddle import base @@ -133,15 +132,7 @@ def init(self): class TestMaskedFillGrad(unittest.TestCase): def setUp(self): self.typelist = ['float32', 'float64', 'int32', 'int64'] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() self.dtype = "float32" def test_backward(self): diff --git a/test/legacy_test/test_masked_scatter.py b/test/legacy_test/test_masked_scatter.py index 9d603fc60d6c08..98e1b93a30e152 100644 --- a/test/legacy_test/test_masked_scatter.py +++ b/test/legacy_test/test_masked_scatter.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import convert_float_to_uint16 +from op_test import convert_float_to_uint16, get_places import paddle from paddle import base @@ -323,16 +322,7 @@ def setUp(self): self.value_np = np.random.randn(*self.value_shape).astype(self.dtype) self.out_np = np_masked_scatter(self.x_np, self.mask_np, self.value_np) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - self.places = places + self.places = get_places() def init(self): self.x_shape = (3, 0) diff --git a/test/legacy_test/test_masked_select_op.py b/test/legacy_test/test_masked_select_op.py index 656fa6d5f23c23..ca85fe12484cd0 100644 --- a/test/legacy_test/test_masked_select_op.py +++ b/test/legacy_test/test_masked_select_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle.base import core @@ -296,15 +295,7 @@ def init(self): class TestMaskedSelectOp_ZeroSize3(unittest.TestCase): def setUp(self): - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def _test_out_0size(self, place): paddle.disable_static(place) diff --git a/test/legacy_test/test_matmul_v2_op.py b/test/legacy_test/test_matmul_v2_op.py index 2f0d9425000461..5e1938dc704141 100644 --- a/test/legacy_test/test_matmul_v2_op.py +++ b/test/legacy_test/test_matmul_v2_op.py @@ -12,11 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16, get_numeric_gradient +from op_test import ( + OpTest, + convert_float_to_uint16, + get_numeric_gradient, + get_places, +) from testsuite import create_op import paddle @@ -593,15 +597,7 @@ def test_check_grad(self): class TestMatMulV2API(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): paddle.enable_static() diff --git a/test/legacy_test/test_matrix_power_op.py b/test/legacy_test/test_matrix_power_op.py index 38ecb286063959..0611d12fb6640e 100644 --- a/test/legacy_test/test_matrix_power_op.py +++ b/test/legacy_test/test_matrix_power_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from utils import dygraph_guard, static_guard import paddle @@ -436,15 +435,7 @@ def config(self): class TestMatrixPowerAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with static.program_guard(static.Program(), static.Program()): @@ -526,15 +517,7 @@ def test_old_ir_errors(self): class TestMatrixPowerSingularAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with static.program_guard(static.Program(), static.Program()): @@ -577,16 +560,7 @@ def test_dygraph(self): class TestMatrixPowerEmptyTensor(unittest.TestCase): def _get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if paddle.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - return places + return get_places() def _test_matrix_power_empty_static(self, place): with ( diff --git a/test/legacy_test/test_matrix_rank_atol_rtol_op.py b/test/legacy_test/test_matrix_rank_atol_rtol_op.py index e1e43562433329..acee7f463ace19 100644 --- a/test/legacy_test/test_matrix_rank_atol_rtol_op.py +++ b/test/legacy_test/test_matrix_rank_atol_rtol_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from utils import dygraph_guard, static_guard import paddle @@ -593,16 +592,7 @@ def test_errors(self): class TestMatrixRankAtolRtolZeroSizeTensor(unittest.TestCase): def _get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - return places + return get_places() def _test_matrix_rank_static(self, place, atol, rtol): with ( diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index eadad1c7c0a435..2dcb292fba4b70 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from utils import dygraph_guard, static_guard import paddle @@ -343,15 +342,7 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for place in places: with static.program_guard(static.Program(), static.Program()): @@ -408,16 +399,7 @@ def test_static(self): class TestMatrixRankZeroSizeTensor(unittest.TestCase): def _get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - return places + return get_places() def _test_matrix_rank_static(self, place): with ( diff --git a/test/legacy_test/test_mean_op.py b/test/legacy_test/test_mean_op.py index 449398b21cbbf3..5285100da0a6e4 100644 --- a/test/legacy_test/test_mean_op.py +++ b/test/legacy_test/test_mean_op.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, OpTestTool, convert_float_to_uint16 +from op_test import OpTest, OpTestTool, convert_float_to_uint16, get_places from test_sum_op import TestReduceOPTensorAxisBase import paddle @@ -910,16 +909,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -947,16 +937,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_merge_selectedrows_op.py b/test/legacy_test/test_merge_selectedrows_op.py index c618b2c1d39d16..7f939ca68d0b27 100644 --- a/test/legacy_test/test_merge_selectedrows_op.py +++ b/test/legacy_test/test_merge_selectedrows_op.py @@ -12,27 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator +from op_test import get_places from paddle.base import core class TestMergeSelectedRows(unittest.TestCase): def get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - return places + return get_places() def check_with_place(self, place): scope = core.Scope() diff --git a/test/legacy_test/test_min_op.py b/test/legacy_test/test_min_op.py index 26a0bb81efbc09..ef0cc06b117ab7 100644 --- a/test/legacy_test/test_min_op.py +++ b/test/legacy_test/test_min_op.py @@ -16,10 +16,9 @@ import unittest sys.path.append("../../legacy_test") -import os import numpy as np -from op_test import OpTest, check_out_dtype +from op_test import OpTest, check_out_dtype, get_places from test_sum_op import TestReduceOPTensorAxisBase from utils import dygraph_guard, static_guard @@ -256,16 +255,7 @@ def test_empty_tensor(self): class TestMinWithNan(unittest.TestCase): def _get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if paddle.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - return places + return get_places() def _test_with_nan_static( self, func, shape, dtype=np.float32, place=paddle.CPUPlace() diff --git a/test/legacy_test/test_mul_nn_grad.py b/test/legacy_test/test_mul_nn_grad.py index 9327f86f93cd1d..ff8183c59af266 100644 --- a/test/legacy_test/test_mul_nn_grad.py +++ b/test/legacy_test/test_mul_nn_grad.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope +from op_test import get_places import paddle -from paddle import base -from paddle.base import core paddle.enable_static() @@ -54,16 +52,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_multimarginloss.py b/test/legacy_test/test_multimarginloss.py index 1395e8e7944ae8..36dc857c0699e6 100644 --- a/test/legacy_test/test_multimarginloss.py +++ b/test/legacy_test/test_multimarginloss.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle @@ -249,15 +249,7 @@ def test_MultiMarginLoss(self): np.int64 ) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: diff --git a/test/legacy_test/test_nn_grad.py b/test/legacy_test/test_nn_grad.py index 7cb790aa6f8b6f..0b3bbde5c31709 100644 --- a/test/legacy_test/test_nn_grad.py +++ b/test/legacy_test/test_nn_grad.py @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope +from op_test import get_places import paddle from paddle import base @@ -49,16 +49,7 @@ def config(self): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: + for place in get_places(): self.func(place) @@ -91,16 +82,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -122,16 +104,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -154,16 +127,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -191,16 +155,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -228,16 +183,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -266,16 +212,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -304,16 +241,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -337,16 +265,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -366,16 +285,7 @@ def func(self, place): gradient_checker.double_grad_check([x], out, x_init=x_arr, place=place) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -395,16 +305,7 @@ def func(self, place): gradient_checker.double_grad_check([x], out, x_init=x_arr, place=place) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -434,16 +335,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -494,16 +386,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -538,16 +421,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -614,16 +488,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -656,16 +521,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -697,16 +553,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -734,16 +581,7 @@ def func(self, place): ) def test_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_nn_margin_rank_loss.py b/test/legacy_test/test_nn_margin_rank_loss.py index d199252a90d0fa..e976dd5626becf 100644 --- a/test/legacy_test/test_nn_margin_rank_loss.py +++ b/test/legacy_test/test_nn_margin_rank_loss.py @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base -from paddle.base import core from paddle.base.framework import in_pir_mode @@ -42,15 +40,7 @@ def setUp(self): self.label_data = np.random.choice([-1, 1], size=[10, 10]).astype( "float64" ) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def run_static_functional_api(self, place): paddle.enable_static() @@ -212,15 +202,7 @@ def setUp(self): self.label_data = np.random.choice( [-1, 1], size=self.label_shape ).astype("float64") - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def run_dynamic_functional_api(self, place): paddle.disable_static(place) diff --git a/test/legacy_test/test_norm_nn_grad.py b/test/legacy_test/test_norm_nn_grad.py index 5292a7869a4434..1b4bc5e3e44bf4 100644 --- a/test/legacy_test/test_norm_nn_grad.py +++ b/test/legacy_test/test_norm_nn_grad.py @@ -12,16 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope +from op_test import get_places import paddle from paddle import base -from paddle.base import core class TestInstanceNormDoubleGradCheck(unittest.TestCase): @@ -59,16 +58,7 @@ def func_pir(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): with paddle.pir_utils.OldIrGuard(): self.func(p) self.func_pir(p) @@ -144,16 +134,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -266,16 +247,7 @@ def func_pir(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): with paddle.pir_utils.OldIrGuard(): self.func(p) self.func_pir(p) diff --git a/test/legacy_test/test_normalization_wrapper.py b/test/legacy_test/test_normalization_wrapper.py index cdda3096613800..6c87026d779b47 100644 --- a/test/legacy_test/test_normalization_wrapper.py +++ b/test/legacy_test/test_normalization_wrapper.py @@ -12,14 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle import base -from paddle.base import core class TestNormalization(unittest.TestCase): @@ -49,17 +48,7 @@ def set_program(self, axis, epsilon): def run_program(self): """Run the test program.""" - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for place in places: + for place in get_places(): self.set_inputs(place) exe = base.Executor(place) diff --git a/test/legacy_test/test_optimizer_grad.py b/test/legacy_test/test_optimizer_grad.py index fcf73673e9cf26..179e1d88b8beac 100644 --- a/test/legacy_test/test_optimizer_grad.py +++ b/test/legacy_test/test_optimizer_grad.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from collections import defaultdict import numpy as np +from op_test import get_places import paddle from paddle import base @@ -304,15 +304,7 @@ def _check_grads(self, use_bf16=False): """ main logic code to check the validity of apply_optimize. """ - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() # test on CPU and GPU for place in places: for param_lr in self.param_lr: diff --git a/test/legacy_test/test_pairwise_distance.py b/test/legacy_test/test_pairwise_distance.py index d9a5af3133fb93..0263f835235bfa 100644 --- a/test/legacy_test/test_pairwise_distance.py +++ b/test/legacy_test/test_pairwise_distance.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle import base @@ -100,15 +100,7 @@ def test_pairwise_distance(self): all_shape = [[5], [100, 100]] dtypes = ['float32', 'float64'] p_list = [-1, 0, 1, 2, np.inf, -np.inf] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() keeps = [False, True] for place in places: for shape in all_shape: @@ -343,15 +335,7 @@ def test_pairwise_distance(self): all_shape = [[0], [100, 0]] dtype = 'float32' p = 0 - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() keeps = [False, True] for place in places: for shape in all_shape: diff --git a/test/legacy_test/test_polar.py b/test/legacy_test/test_polar.py index dd7dda740adadc..5c8afcdd67fe3c 100644 --- a/test/legacy_test/test_polar.py +++ b/test/legacy_test/test_polar.py @@ -12,14 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest # import torch import numpy as np +from op_test import get_places import paddle -from paddle.base import core np.random.seed(10) @@ -34,15 +33,7 @@ class TestPolarAPI(unittest.TestCase): def setUp(self): self.abs = np.array([1, 2]).astype("float64") self.angle = np.array([np.pi / 2, 5 * np.pi / 4]).astype("float64") - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): paddle.enable_static() @@ -113,15 +104,7 @@ def init_input(self): def setUp(self): self.init_input() - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_dygraph(self): def run(place): diff --git a/test/legacy_test/test_polygamma_op.py b/test/legacy_test/test_polygamma_op.py index 97ac5ebfb5e2e8..8274dd5d2c1a86 100644 --- a/test/legacy_test/test_polygamma_op.py +++ b/test/legacy_test/test_polygamma_op.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places from scipy import special import paddle -from paddle.base import core np.random.seed(100) paddle.seed(100) @@ -61,15 +59,7 @@ class TestPolygammaAPI(unittest.TestCase): def setUp(self): self.x = np.array(self.DATA).astype(self.DTYPE) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): def run(place): diff --git a/test/legacy_test/test_pool1d_api.py b/test/legacy_test/test_pool1d_api.py index 8a157df4535d80..1817a65bc346e3 100644 --- a/test/legacy_test/test_pool1d_api.py +++ b/test/legacy_test/test_pool1d_api.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle import paddle.nn.functional as F @@ -174,15 +174,7 @@ def lp_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_avg_static_results(self, place): with paddle.static.program_guard(paddle.static.Program()): @@ -829,15 +821,7 @@ def run_zero_tuple_stride(): class TestPool1D_API_ZeroSize(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_avg_dygraph_results(self, place): with base.dygraph.guard(place): diff --git a/test/legacy_test/test_put_along_axis_op.py b/test/legacy_test/test_put_along_axis_op.py index b1b8ff49a54ac0..134796e6b2488c 100644 --- a/test/legacy_test/test_put_along_axis_op.py +++ b/test/legacy_test/test_put_along_axis_op.py @@ -13,11 +13,10 @@ # limitations under the License. import copy -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from utils import dygraph_guard import paddle @@ -680,19 +679,11 @@ def setUp(self): self.index_shape = [1, 1] self.index_np = np.array([[0]]).astype('int64') self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] + self.place = get_places() self.axis = 0 self.value_np = 99.0 self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def test_api_static(self): paddle.enable_static() @@ -824,19 +815,11 @@ def setUp(self): self.index_shape = [2, 2] self.index_np = np.array([[0, 0], [1, 0]]).astype('int64') self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] + self.place = get_places() self.axis = 0 self.value_np = 99.0 self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) class TestPutAlongAxisAPICase3(TestPutAlongAxisAPI): @@ -848,19 +831,11 @@ def setUp(self): 'int64' ) self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] + self.place = get_places() self.axis = 0 self.value_np = 99.0 self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def test_inplace_dygraph(self): pass @@ -879,15 +854,7 @@ def setUp(self): self.value = ( np.arange(1, 11).reshape(self.value_shape).astype(np.float32) ) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_dygraph(self): def run(place): diff --git a/test/legacy_test/test_python_operator_overriding.py b/test/legacy_test/test_python_operator_overriding.py index 450c8b153993e9..483ef9d150791f 100644 --- a/test/legacy_test/test_python_operator_overriding.py +++ b/test/legacy_test/test_python_operator_overriding.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base paddle.enable_static() @@ -58,15 +57,7 @@ def test_override(self): ] # places to check - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() # dtypes to check dtypes = ['int32', 'float32'] diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index 90b9dc53e090d6..7105b0ee2df425 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base, static @@ -186,15 +185,7 @@ def setUp(self): # prepare test attrs self.api = "real" self.dtypes = ["complex64", "complex128"] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() self._shape = [2, 20, 2, 3] def test_in_static_mode(self): @@ -270,15 +261,7 @@ def setUp(self): # prepare test attrs self.api = "imag" self.dtypes = ["complex64", "complex128"] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() self.init_shape() def init_shape(self): diff --git a/test/legacy_test/test_regularizer.py b/test/legacy_test/test_regularizer.py index fe5bbeb53c091c..10d671f6f949f0 100644 --- a/test/legacy_test/test_regularizer.py +++ b/test/legacy_test/test_regularizer.py @@ -13,15 +13,14 @@ # limitations under the License. import contextlib -import os import random import unittest import numpy as np +from op_test import get_places import paddle from paddle import base, regularizer -from paddle.base import core class TestL1Decay(unittest.TestCase): @@ -68,16 +67,7 @@ def setUp(self): ] def get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - return places + return get_places() @contextlib.contextmanager def scope_prog_guard(self, main_prog, startup_prog): diff --git a/test/legacy_test/test_regularizer_api.py b/test/legacy_test/test_regularizer_api.py index 594f9764fd2cc1..9622a7f77bfc14 100644 --- a/test/legacy_test/test_regularizer_api.py +++ b/test/legacy_test/test_regularizer_api.py @@ -13,15 +13,14 @@ # limitations under the License. import contextlib -import os import random import unittest import numpy as np +from op_test import get_places import paddle from paddle import base -from paddle.base import core class TestRegularizer(unittest.TestCase): @@ -32,16 +31,7 @@ def setUp(self): ] def get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - return places + return get_places() @contextlib.contextmanager def scope_prog_guard(self, main_prog, startup_prog): diff --git a/test/legacy_test/test_rmsprop_op.py b/test/legacy_test/test_rmsprop_op.py index d59eceaae81c9d..1552ad4aa00ee6 100644 --- a/test/legacy_test/test_rmsprop_op.py +++ b/test/legacy_test/test_rmsprop_op.py @@ -17,6 +17,7 @@ import numpy as np from op import Operator +from op_test import get_places import paddle from paddle import base @@ -225,15 +226,7 @@ def run_and_check(self): ) def test_rmsprop(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) + places = get_places() size = (128, 320) for place in places: diff --git a/test/legacy_test/test_scale_op.py b/test/legacy_test/test_scale_op.py index 9f612af217bb0b..0af4af8d8f4d31 100644 --- a/test/legacy_test/test_scale_op.py +++ b/test/legacy_test/test_scale_op.py @@ -12,17 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope from op import Operator -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle -from paddle import base from paddle.base import core @@ -126,29 +124,11 @@ def check_with_place(self, place, in_name, out_name): assert in_rows == out_rows def test_scale_selected_rows(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place, 'in', 'out') def test_scale_selected_rows_inplace(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place, 'in', 'in') @@ -290,16 +270,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -327,16 +298,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_scatter_op.py b/test/legacy_test/test_scatter_op.py index 13671e13cf499c..6082142bf9709c 100644 --- a/test/legacy_test/test_scatter_op.py +++ b/test/legacy_test/test_scatter_op.py @@ -16,7 +16,7 @@ import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from utils import dygraph_guard, static_guard import paddle @@ -713,15 +713,7 @@ def test_check_grad(self): class TestScatterAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() self.executed_api() def executed_api(self): diff --git a/test/legacy_test/test_searchsorted_op.py b/test/legacy_test/test_searchsorted_op.py index 84f6e5e1bb9b12..d152bb85381ba0 100644 --- a/test/legacy_test/test_searchsorted_op.py +++ b/test/legacy_test/test_searchsorted_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle.base import core @@ -198,15 +197,7 @@ def init_test_case(self): def setUp(self): self.init_test_case() - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_static_api(self): paddle.enable_static() diff --git a/test/legacy_test/test_select_scatter_op.py b/test/legacy_test/test_select_scatter_op.py index 9b19677fb40673..1daa47cc1e8344 100644 --- a/test/legacy_test/test_select_scatter_op.py +++ b/test/legacy_test/test_select_scatter_op.py @@ -13,13 +13,12 @@ # limitations under the License. import copy -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle.framework import core paddle.enable_static() @@ -30,20 +29,12 @@ def setUp(self): self.shape = [2, 3, 4] self.type = np.float32 self.x_np = np.random.random(self.shape).astype(self.type) - self.place = [] + self.place = get_places() self.axis = 1 self.index = 1 self.value_shape = [2, 4] self.value_np = np.random.random(self.value_shape).astype(self.type) self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def get_out_ref(self, out_ref, index, value_np): for i in range(2): @@ -101,20 +92,12 @@ def setUp(self): self.shape = [2, 3, 4, 5] self.type = np.float64 self.x_np = np.random.random(self.shape).astype(self.type) - self.place = [] + self.place = get_places() self.axis = 2 self.index = 1 self.value_shape = [2, 3, 5] self.value_np = np.random.random(self.value_shape).astype(self.type) self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def get_out_ref(self, out_ref, index, value_np): for i in range(2): @@ -129,20 +112,12 @@ def setUp(self): self.shape = [2, 3, 4, 5, 6] self.type = np.int32 self.x_np = np.random.random(self.shape).astype(self.type) - self.place = [] + self.place = get_places() self.axis = 2 self.index = 1 self.value_shape = [2, 3, 5, 6] self.value_np = np.random.random(self.value_shape).astype(self.type) self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def get_out_ref(self, out_ref, index, value_np): for i in range(2): @@ -157,20 +132,12 @@ def setUp(self): np.random.seed(0) self.shape = [2, 3, 4] self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] + self.place = get_places() self.axis = 1 self.index = 1 self.value_shape = [2, 4] self.value_np = np.random.random(self.value_shape).astype(np.float32) self.x_feed = copy.deepcopy(self.x_np) - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def test_len_of_shape_not_equal_error(self): with self.assertRaises(RuntimeError): diff --git a/test/legacy_test/test_sgd_op.py b/test/legacy_test/test_sgd_op.py index 0a3050baa25fce..9043978cb234cc 100644 --- a/test/legacy_test/test_sgd_op.py +++ b/test/legacy_test/test_sgd_op.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator -from op_test import OpTest +from op_test import OpTest, get_places from utils import dygraph_guard import paddle @@ -119,16 +118,7 @@ def check_with_place(self, place): self.assertAlmostEqual(-3.0, result_array[rows[2], 8]) def test_sparse_sgd(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - for place in places: + for place in get_places(): self.check_with_place(place) def conf(self): diff --git a/test/legacy_test/test_shape_op.py b/test/legacy_test/test_shape_op.py index 68518dffbe92e7..4cb71ab408b560 100644 --- a/test/legacy_test/test_shape_op.py +++ b/test/legacy_test/test_shape_op.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle.base import core @@ -72,16 +71,7 @@ def config(self): class TestShapeWithSelectedRows(unittest.TestCase): def get_places(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - return places + return get_places() def check_with_place(self, place): scope = core.Scope() diff --git a/test/legacy_test/test_signbit.py b/test/legacy_test/test_signbit.py index 997777e0b2003c..a7aa05cebeae81 100644 --- a/test/legacy_test/test_signbit.py +++ b/test/legacy_test/test_signbit.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle.base import core @@ -45,15 +45,7 @@ def setUp(self) -> None: 'int32', 'int64', ] - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_dtype(self): def run(place): diff --git a/test/legacy_test/test_sinc.py b/test/legacy_test/test_sinc.py index 85012889615853..ccee6f76f39110 100644 --- a/test/legacy_test/test_sinc.py +++ b/test/legacy_test/test_sinc.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import convert_float_to_uint16, convert_uint16_to_float +from op_test import convert_float_to_uint16, convert_uint16_to_float, get_places import paddle from paddle import base @@ -42,15 +41,7 @@ def setUp(self): 'float32', 'float64', ] - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() self.shapes = [[6], [16, 64]] def test_dtype(self): @@ -173,15 +164,7 @@ def setUp(self): 'float32', 'float64', ] - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() self.shapes = [[6], [16, 64]] def test_inplace(self): @@ -370,15 +353,7 @@ def setUp(self): 'float32', 'float64', ] - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() self.shapes = [[0], [16, 0]] def test_dygraph(self): diff --git a/test/legacy_test/test_slice_op.py b/test/legacy_test/test_slice_op.py index 57f00ed0bba705..990b0db9850d7e 100644 --- a/test/legacy_test/test_slice_op.py +++ b/test/legacy_test/test_slice_op.py @@ -12,13 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16, paddle_static_guard +from op_test import ( + OpTest, + convert_float_to_uint16, + get_places, + paddle_static_guard, +) import paddle from paddle import base @@ -1215,16 +1219,7 @@ def func(self, place): def test_grad(self): with paddle_static_guard(): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -1256,16 +1251,7 @@ def func(self, place): def test_grad(self): with paddle_static_guard(): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_smooth_l1_loss.py b/test/legacy_test/test_smooth_l1_loss.py index 68917a9845d1a1..30338b3e0b2bba 100644 --- a/test/legacy_test/test_smooth_l1_loss.py +++ b/test/legacy_test/test_smooth_l1_loss.py @@ -12,13 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base, core +from paddle import base def smooth_l1_loss_forward(val, delta): @@ -283,16 +283,7 @@ def _test_smooth_l1_loss_mean(self, place): paddle.enable_static() def test_smooth_l1_loss_mean(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self._test_smooth_l1_loss_mean(p) diff --git a/test/legacy_test/test_solve_op.py b/test/legacy_test/test_solve_op.py index e7bf5609233fa7..cc4dfca8731dee 100644 --- a/test/legacy_test/test_solve_op.py +++ b/test/legacy_test/test_solve_op.py @@ -12,17 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License.w -import os import sys import unittest import numpy as np import paddle -from paddle.base import core sys.path.append("..") -from op_test import OpTest +from op_test import OpTest, get_places from paddle import base @@ -318,16 +316,8 @@ def test_errors(self): class TestSolveOpAPI_1(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): with base.program_guard(base.Program(), base.Program()): @@ -384,16 +374,8 @@ def run(place): class TestSolveOpAPI_2(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() @@ -450,16 +432,8 @@ def run(place): class TestSolveOpAPI_3(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float32" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() @@ -517,16 +491,8 @@ def run(place): class TestSolveOpAPI_4(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): with base.program_guard(base.Program(), base.Program()): @@ -598,16 +564,8 @@ def np_solve_right(x, y): class TestSolveOpAPIRight_1(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): with base.program_guard(base.Program(), base.Program()): @@ -666,16 +624,8 @@ def run(place): class TestSolveOpAPIRight_2(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() @@ -734,16 +684,8 @@ def run(place): class TestSolveOpAPIRight_3(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float32" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() @@ -803,16 +745,8 @@ def run(place): class TestSolveOpAPIRight_4(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): with base.program_guard(base.Program(), base.Program()): @@ -870,16 +804,8 @@ def run(place): class TestSolveOpSingularAPI(unittest.TestCase): # Singular matrix is ​​not invertible def setUp(self): - self.places = [] + self.places = get_places() self.dtype = "float64" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): with base.program_guard(base.Program(), base.Program()): @@ -926,16 +852,8 @@ def test_dygraph(self): class TestSolveOpAPIZeroDimCase(unittest.TestCase): def setUp(self): np.random.seed(2021) - self.place = [] + self.place = get_places() self.dtype = "float32" - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place, x_shape, y_shape, np_y_shape): paddle.enable_static() diff --git a/test/legacy_test/test_sparse_mask_as_op.py b/test/legacy_test/test_sparse_mask_as_op.py index 8b4ba926370754..dc1dccc849be89 100644 --- a/test/legacy_test/test_sparse_mask_as_op.py +++ b/test/legacy_test/test_sparse_mask_as_op.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle @@ -35,15 +35,7 @@ def generate_data(shape, dtype): class TestMaskAs(unittest.TestCase): def setUp(self): self.init_format() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def init_format(self): self.format = None diff --git a/test/legacy_test/test_square_error_cost.py b/test/legacy_test/test_square_error_cost.py index 42668ed85ce980..6e0e5d8780c234 100644 --- a/test/legacy_test/test_square_error_cost.py +++ b/test/legacy_test/test_square_error_cost.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle from paddle import base @@ -88,15 +88,7 @@ def init_shape(self): self.shape = [0, 3] def test_square_error_cost(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() self.init_shape() shape = self.shape input_val = np.random.uniform(0.1, 0.5, shape).astype("float32") diff --git a/test/legacy_test/test_take_along_axis_op.py b/test/legacy_test/test_take_along_axis_op.py index fba92803d382ed..14369eae033a58 100644 --- a/test/legacy_test/test_take_along_axis_op.py +++ b/test/legacy_test/test_take_along_axis_op.py @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import sys import unittest import numpy as np -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places from utils import dygraph_guard import paddle @@ -228,15 +227,7 @@ def setUp(self): -dim_size, dim_size, size=([1, 3]) ).astype('int64') self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): paddle.enable_static() @@ -291,15 +282,7 @@ def setUp(self): -dim_size, dim_size, size=(4, 2) ).astype('int64') self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() class TestTakeAlongAxisAPICase2(unittest.TestCase): @@ -313,15 +296,7 @@ def setUp(self): -dim_size, dim_size, size=(1, 3) ).astype('int64') self.x_np = np.random.random(self.shape).astype(np.float32) - self.place = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.place.append(paddle.CPUPlace()) - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) + self.place = get_places() def test_api_static(self): paddle.enable_static() diff --git a/test/legacy_test/test_tensor_fill_diagonal_.py b/test/legacy_test/test_tensor_fill_diagonal_.py index 4d629c83cf5b6c..17298bd39306ac 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_.py +++ b/test/legacy_test/test_tensor_fill_diagonal_.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base class TensorFillDiagonal_Test(unittest.TestCase): @@ -31,15 +30,7 @@ def test_dim2_normal(self): ) typelist = ['float32', 'float64', 'int32', 'int64'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -72,15 +63,7 @@ def test_offset(self): ) typelist = ['float32', 'float64', 'int32', 'int64'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -110,15 +93,7 @@ def test_bool(self): ) typelist = ['bool'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -157,15 +132,7 @@ def test_dim2_unnormal_wrap(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -214,15 +181,7 @@ def test_dim2_unnormal_unwrap(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -263,15 +222,7 @@ def test_dim_larger2_normal(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: @@ -301,15 +252,7 @@ def _test_normal(self, shape): expected_np = np.random.random(shape) expected_grad = np.random.random(shape) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) + places = get_places() for idx, p in enumerate(places): if idx == 0: diff --git a/test/legacy_test/test_tensor_fill_diagonal_tensor.py b/test/legacy_test/test_tensor_fill_diagonal_tensor.py index 131f68558cc582..f5c4e7ea0117da 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_tensor.py +++ b/test/legacy_test/test_tensor_fill_diagonal_tensor.py @@ -12,27 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base class TensorFillDiagTensor_Test(unittest.TestCase): def setUp(self): self.typelist = ['float32', 'float64', 'int32', 'int64'] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dim2(self): expected_np = np.array( diff --git a/test/legacy_test/test_tensor_fill_diagonal_tensor_.py b/test/legacy_test/test_tensor_fill_diagonal_tensor_.py index c1b4ce7a19c69f..84e91dba73f78b 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_tensor_.py +++ b/test/legacy_test/test_tensor_fill_diagonal_tensor_.py @@ -12,27 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle import base class TensorFillDiagTensor_Test(unittest.TestCase): def setUp(self): self.typelist = ['float32', 'float64', 'int32', 'int64'] - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def test_dim2(self): expected_np = np.array( diff --git a/test/legacy_test/test_tensordot.py b/test/legacy_test/test_tensordot.py index 1df90d2a226267..f340e4fb29bace 100644 --- a/test/legacy_test/test_tensordot.py +++ b/test/legacy_test/test_tensordot.py @@ -12,13 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle -from paddle.base import core np.random.seed(2021) @@ -77,15 +76,7 @@ def setUp(self): self.set_test_axes() def set_place(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(core.CUDAPlace(0)) + self.places = get_places() def set_dtype(self): self.dtype = np.float32 diff --git a/test/legacy_test/test_transpose_op.py b/test/legacy_test/test_transpose_op.py index c20d0dbf471ccf..418c131314471e 100644 --- a/test/legacy_test/test_transpose_op.py +++ b/test/legacy_test/test_transpose_op.py @@ -12,17 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import gradient_checker import numpy as np from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16 +from op_test import OpTest, convert_float_to_uint16, get_places import paddle from paddle import base -from paddle.base import core paddle.enable_static() @@ -757,16 +755,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) @@ -794,16 +783,7 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: + for p in get_places(): self.func(p) diff --git a/test/legacy_test/test_trapezoid.py b/test/legacy_test/test_trapezoid.py index c3189fd66f82a2..129ebd5ca1cb67 100644 --- a/test/legacy_test/test_trapezoid.py +++ b/test/legacy_test/test_trapezoid.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle @@ -53,15 +53,7 @@ def setUp(self): self.set_api() self.set_args() self.get_output() - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - self.places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - self.places.append(paddle.CUDAPlace(0)) + self.places = get_places() def func_dygraph(self): for place in self.places: @@ -80,16 +72,7 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_triplet_margin_loss.py b/test/legacy_test/test_triplet_margin_loss.py index e3205fb5f06b93..bd2c416bdf1fe9 100644 --- a/test/legacy_test/test_triplet_margin_loss.py +++ b/test/legacy_test/test_triplet_margin_loss.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle @@ -201,15 +201,7 @@ def test_TripletMarginLoss(self): positive = np.random.uniform(0, 2, size=shape).astype(np.float64) negative = np.random.uniform(0, 2, size=shape).astype(np.float64) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: diff --git a/test/legacy_test/test_triplet_margin_with_distance_loss.py b/test/legacy_test/test_triplet_margin_with_distance_loss.py index 67c02db24b8bb6..2b610a2ab84902 100644 --- a/test/legacy_test/test_triplet_margin_with_distance_loss.py +++ b/test/legacy_test/test_triplet_margin_with_distance_loss.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np +from op_test import get_places import paddle @@ -201,15 +201,7 @@ def test_TripletMarginDistanceLoss(self): positive = np.random.uniform(0, 2, size=shape).astype(np.float64) negative = np.random.uniform(0, 2, size=shape).astype(np.float64) - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) + places = get_places() reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: diff --git a/test/legacy_test/test_unique_consecutive_op.py b/test/legacy_test/test_unique_consecutive_op.py index 31cefebf21630d..cd574647839865 100644 --- a/test/legacy_test/test_unique_consecutive_op.py +++ b/test/legacy_test/test_unique_consecutive_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle from paddle import base @@ -200,15 +199,7 @@ def setUp(self): class TestUniqueConsecutiveAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with paddle.static.program_guard( @@ -244,15 +235,7 @@ def test_dygraph(self): class TestUniqueConsecutiveCase2API(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with paddle.static.program_guard( @@ -292,15 +275,7 @@ def test_dygraph(self): class TestUniqueConsecutiveCase3API(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - self.places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - self.places.append(base.CUDAPlace(0)) + self.places = get_places() def check_static_result(self, place): with paddle.static.program_guard( diff --git a/test/legacy_test/test_unpool3d_op.py b/test/legacy_test/test_unpool3d_op.py index 2c26f22eabe29b..6f8267e5640ee5 100644 --- a/test/legacy_test/test_unpool3d_op.py +++ b/test/legacy_test/test_unpool3d_op.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle import paddle.nn.functional as F @@ -295,16 +294,7 @@ def data_outputsize_error2(): class TestUnpool3DOpAPI_dygraph(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static() input_data = np.random.rand(1, 3, 4, 4, 6) input_x = paddle.to_tensor(input_data) @@ -331,16 +321,7 @@ def test_case(self): class TestUnpool3DOpAPI_dygraph2(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static() input_data = np.random.rand(1, 3, 4, 4, 6) input_x = paddle.to_tensor(input_data) @@ -367,16 +348,7 @@ def test_case(self): class TestUnpool3DOpAPI_dygraph3(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static() input_data = np.random.rand(1, 3, 4, 4, 6) input_x = paddle.to_tensor(input_data) @@ -404,16 +376,7 @@ def test_case(self): class TestUnpool3DOpAPI_dygraph4(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static() input_data = ( np.arange(3 * 4 * 4 * 6) @@ -450,16 +413,7 @@ class TestUnpool3DOpAPI_static(unittest.TestCase): def test_case(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_unpool_indices.py b/test/legacy_test/test_unpool_indices.py index 1eaf4e65171c03..d8cc5ed8a7f584 100644 --- a/test/legacy_test/test_unpool_indices.py +++ b/test/legacy_test/test_unpool_indices.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, get_places import paddle import paddle.nn.functional as F @@ -353,16 +352,7 @@ def init_test_case(self): class TestUnpool1DAPI_dy(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static(place) input_data = np.arange(3 * 16).reshape([1, 3, 16]).astype("float32") input_x = paddle.to_tensor(input_data) @@ -390,16 +380,7 @@ class TestUnpool1DAPI_st(unittest.TestCase): def test_case(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -438,16 +419,7 @@ def test_case(self): class TestUnpool2DAPI_dy(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static(place) input_data = np.array( [ @@ -486,16 +458,7 @@ class TestUnpool2DAPI_st(unittest.TestCase): def test_case(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -545,16 +508,7 @@ def test_case(self): class TestUnpool3DAPI_dy(unittest.TestCase): def test_case(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): paddle.disable_static(place) input_data = ( np.arange(3 * 4 * 4 * 6) @@ -591,16 +545,7 @@ class TestUnpool3DAPI_st2(unittest.TestCase): def test_case(self): paddle.enable_static() - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.base.core.is_compiled_with_cuda() - ): - places.append(paddle.CPUPlace()) - if paddle.base.core.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - for place in places: + for place in get_places(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): diff --git a/test/legacy_test/test_variable.py b/test/legacy_test/test_variable.py index a3d7071ce5dd4a..e93e1ebdc823d4 100644 --- a/test/legacy_test/test_variable.py +++ b/test/legacy_test/test_variable.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from functools import reduce import numpy as np +from op_test import get_places import paddle from paddle import base @@ -272,15 +272,7 @@ def _test_slice_index_scalar_bool(self, place): self.assertTrue((result[0] == expected[0]).all()) def test_slice(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) + places = get_places() for place in places: self._test_slice_index_tensor(place) @@ -488,17 +480,7 @@ def _test_item_none_and_decrease(self, place): self.assertTrue((result[i] == expected[i]).all()) def test_slice(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for place in places: + for place in get_places(): self._test_item_none(place) self._test_item_none_and_decrease(place)