Skip to content

Commit f9e28c3

Browse files
authored
Replace some op_test.get_places() - part7 (#73784)
1 parent 3cd2b19 commit f9e28c3

File tree

109 files changed

+365
-2244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+365
-2244
lines changed

test/legacy_test/test_activation_op.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from contextlib import contextmanager
1919

2020
import numpy as np
21-
from op_test import OpTest, convert_float_to_uint16
21+
from op_test import OpTest, convert_float_to_uint16, get_places
2222
from scipy.special import erf, expit
2323
from utils import static_guard
2424

@@ -328,15 +328,7 @@ def setUp(self):
328328
self.x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
329329
self.out_ref = np.expm1(self.x)
330330

331-
self.place = []
332-
if (
333-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
334-
in ['1', 'true', 'on']
335-
or not core.is_compiled_with_cuda()
336-
):
337-
self.place.append(paddle.CPUPlace())
338-
if core.is_compiled_with_cuda():
339-
self.place.append(paddle.CUDAPlace(0))
331+
self.place = get_places()
340332

341333
def test_static_api(self):
342334
def run(place):

test/legacy_test/test_activation_sparse_op.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
import numpy as np
1918
from op import Operator
19+
from op_test import get_places
2020

2121
import paddle
2222
from paddle.base import core
@@ -52,16 +52,7 @@ def check_with_place(self, place):
5252
np.testing.assert_array_equal(result_array, np.square(np_array))
5353

5454
def test_sparse_acti(self):
55-
places = []
56-
if (
57-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
58-
in ['1', 'true', 'on']
59-
or not core.is_compiled_with_cuda()
60-
):
61-
places.append(core.CPUPlace())
62-
if core.is_compiled_with_cuda():
63-
places.append(core.CUDAPlace(0))
64-
for place in places:
55+
for place in get_places():
6556
self.check_with_place(place)
6657

6758

@@ -94,16 +85,7 @@ def check_with_place(self, place):
9485
np.testing.assert_allclose(result_array, np.sqrt(np_array), rtol=1e-05)
9586

9687
def test_sparse_acti(self):
97-
places = []
98-
if (
99-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
100-
in ['1', 'true', 'on']
101-
or not core.is_compiled_with_cuda()
102-
):
103-
places.append(core.CPUPlace())
104-
if core.is_compiled_with_cuda():
105-
places.append(core.CUDAPlace(0))
106-
for place in places:
88+
for place in get_places():
10789
self.check_with_place(place)
10890

10991

test/legacy_test/test_adagrad_op.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import numpy as np
2020
from op import Operator
21-
from op_test import OpTest
21+
from op_test import OpTest, get_places
2222

2323
import paddle
2424
from paddle.base import core
@@ -205,16 +205,7 @@ def get_out(param, lr, grad, m, epsilon):
205205
)
206206

207207
def test_sparse_adagrad(self):
208-
places = []
209-
if (
210-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
211-
in ['1', 'true', 'on']
212-
or not core.is_compiled_with_cuda()
213-
):
214-
places.append(core.CPUPlace())
215-
if core.is_compiled_with_cuda():
216-
places.append(core.CUDAPlace(0))
217-
for place in places:
208+
for place in get_places():
218209
self.check_with_place(place)
219210

220211

test/legacy_test/test_adam_op.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import numpy as np
1919
from op import Operator
20-
from op_test import OpTest
20+
from op_test import OpTest, get_places
2121

2222
import paddle
2323
from paddle import base
@@ -577,16 +577,7 @@ def check_with_place(self, place, lazy_mode):
577577
self.assertLess((actual[i] - np_array[i]), 0.00001)
578578

579579
def test_sparse_adam(self):
580-
places = []
581-
if (
582-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
583-
in ['1', 'true', 'on']
584-
or not core.is_compiled_with_cuda()
585-
):
586-
places.append(core.CPUPlace())
587-
if core.is_compiled_with_cuda():
588-
places.append(core.CUDAPlace(0))
589-
for place in places:
580+
for place in get_places():
590581
for lazy_mode in (True, False):
591582
self.check_with_place(place, lazy_mode)
592583

test/legacy_test/test_adam_optimizer_fp32_fp64.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

17+
from op_test import get_places
1818
from utils import static_guard
1919

2020
import paddle
2121
from paddle import base
2222

2323

24-
def get_places():
25-
places = []
26-
if (
27-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
28-
in ['1', 'true', 'on']
29-
or not base.is_compiled_with_cuda()
30-
):
31-
places.append(base.CPUPlace())
32-
if base.is_compiled_with_cuda():
33-
places.append(base.CUDAPlace(0))
34-
return places
35-
36-
3724
def main_test_func(place, dtype):
3825
with static_guard():
3926
main = base.Program()

test/legacy_test/test_adaptive_avg_pool1d.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import unittest
1717

1818
import numpy as np
19+
from op_test import get_places
1920

2021
import paddle
2122
import paddle.nn.functional as F
@@ -83,15 +84,7 @@ def avg_pool1D_forward_naive(
8384
class TestPool1D_API(unittest.TestCase):
8485
def setUp(self):
8586
np.random.seed(123)
86-
self.places = []
87-
if (
88-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
89-
in ['1', 'true', 'on']
90-
or not core.is_compiled_with_cuda()
91-
):
92-
self.places.append(base.CPUPlace())
93-
if core.is_compiled_with_cuda():
94-
self.places.append(base.CUDAPlace(0))
87+
self.places = get_places()
9588

9689
def check_adaptive_avg_dygraph_results(self, place):
9790
with base.dygraph.guard(place):

test/legacy_test/test_adaptive_max_pool1d.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
import numpy as np
19-
from op_test import check_out_dtype, paddle_static_guard
18+
from op_test import check_out_dtype, get_places, paddle_static_guard
2019

2120
import paddle
2221
import paddle.nn.functional as F
2322
from paddle import base
24-
from paddle.base import core
2523

2624

2725
def adaptive_start_index(index, input_size, output_size):
@@ -74,15 +72,7 @@ def max_pool1D_forward_naive(
7472
class TestPool1D_API(unittest.TestCase):
7573
def setUp(self):
7674
np.random.seed(123)
77-
self.places = []
78-
if (
79-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
80-
in ['1', 'true', 'on']
81-
or not core.is_compiled_with_cuda()
82-
):
83-
self.places.append(base.CPUPlace())
84-
if core.is_compiled_with_cuda():
85-
self.places.append(base.CUDAPlace(0))
75+
self.places = get_places()
8676

8777
def check_adaptive_max_dygraph_results(self, place):
8878
with base.dygraph.guard(place):
@@ -149,15 +139,7 @@ def test_max_pool(self):
149139
class TestPool1D_API_ZeroSize(unittest.TestCase):
150140
def setUp(self):
151141
np.random.seed(123)
152-
self.places = []
153-
if (
154-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
155-
in ['1', 'true', 'on']
156-
or not core.is_compiled_with_cuda()
157-
):
158-
self.places.append(base.CPUPlace())
159-
if core.is_compiled_with_cuda():
160-
self.places.append(base.CUDAPlace(0))
142+
self.places = get_places()
161143

162144
def check_adaptive_max_dygraph_results(self, place):
163145
with base.dygraph.guard(place):

test/legacy_test/test_arg_min_max_v2_op.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
import numpy as np
19-
from op_test import OpTest
18+
from op_test import OpTest, get_places
2019

2120
import paddle
22-
from paddle import base
2321
from paddle.base import Program, core, program_guard
2422

2523

@@ -166,15 +164,7 @@ class ArgMaxMinTestCase(unittest.TestCase):
166164
def setUp(self):
167165
np.random.seed(123)
168166
self.input_data = np.random.rand(10, 10).astype("float32")
169-
self.places = []
170-
if (
171-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
172-
in ['1', 'true', 'on']
173-
or not paddle.is_compiled_with_cuda()
174-
):
175-
self.places.append(base.CPUPlace())
176-
if core.is_compiled_with_cuda():
177-
self.places.append(paddle.CUDAPlace(0))
167+
self.places = get_places()
178168
self.op = eval(f"paddle.{op_type}")
179169
self.numpy_op = eval(f"np.{op_type}")
180170

test/legacy_test/test_assign_op.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
import gradient_checker
1918
import numpy as np
2019
import op_test
2120
from decorator_helper import prog_scope
22-
from op_test import convert_float_to_uint16, convert_uint16_to_float
21+
from op_test import convert_float_to_uint16, convert_uint16_to_float, get_places
2322

2423
import paddle
2524
from paddle import base
26-
from paddle.base import Program, core, program_guard
25+
from paddle.base import Program, program_guard
2726
from paddle.base.backward import append_backward
2827

2928

@@ -327,16 +326,7 @@ def func(self, place):
327326

328327
def test_grad(self):
329328
paddle.enable_static()
330-
places = []
331-
if (
332-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
333-
in ['1', 'true', 'on']
334-
or not core.is_compiled_with_cuda()
335-
):
336-
places.append(base.CPUPlace())
337-
if core.is_compiled_with_cuda():
338-
places.append(base.CUDAPlace(0))
339-
for p in places:
329+
for p in get_places():
340330
self.func(p)
341331
paddle.disable_static()
342332

@@ -365,16 +355,7 @@ def func(self, place):
365355

366356
def test_grad(self):
367357
paddle.enable_static()
368-
places = []
369-
if (
370-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
371-
in ['1', 'true', 'on']
372-
or not core.is_compiled_with_cuda()
373-
):
374-
places.append(base.CPUPlace())
375-
if core.is_compiled_with_cuda():
376-
places.append(base.CUDAPlace(0))
377-
for p in places:
358+
for p in get_places():
378359
self.func(p)
379360
paddle.disable_static()
380361

test/legacy_test/test_bce_with_logits_loss.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
import numpy as np
18+
from op_test import get_places
1919

2020
import paddle
2121
from paddle import base
@@ -143,15 +143,7 @@ class TestBCEWithLogitsLoss(unittest.TestCase):
143143
def test_BCEWithLogitsLoss(self):
144144
logit_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float64)
145145
label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float64)
146-
places = []
147-
if (
148-
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
149-
in ['1', 'true', 'on']
150-
or not base.core.is_compiled_with_cuda()
151-
):
152-
places.append(base.CPUPlace())
153-
if base.core.is_compiled_with_cuda():
154-
places.append(base.CUDAPlace(0))
146+
places = get_places()
155147
reductions = ['sum', 'mean', 'none']
156148
for place in places:
157149
for reduction in reductions:

0 commit comments

Comments
 (0)