|
18 | 18 | from contextlib import contextmanager
|
19 | 19 |
|
20 | 20 | import numpy as np
|
21 |
| -from op_test import OpTest, convert_float_to_uint16, get_places |
| 21 | +from op_test import ( |
| 22 | + OpTest, |
| 23 | + convert_float_to_uint16, |
| 24 | + get_device_place, |
| 25 | + get_places, |
| 26 | +) |
22 | 27 | from scipy.special import erf, expit
|
23 | 28 | from utils import static_guard
|
24 | 29 |
|
@@ -637,11 +642,7 @@ class TestSiluAPI(unittest.TestCase):
|
637 | 642 | # test paddle.nn.Silu, paddle.nn.functional.silu
|
638 | 643 | def setUp(self):
|
639 | 644 | self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
|
640 |
| - self.place = ( |
641 |
| - paddle.CUDAPlace(0) |
642 |
| - if core.is_compiled_with_cuda() |
643 |
| - else paddle.CPUPlace() |
644 |
| - ) |
| 645 | + self.place = get_device_place() |
645 | 646 |
|
646 | 647 | def test_static_api(self):
|
647 | 648 | with static_guard():
|
@@ -739,11 +740,7 @@ class TestLogSigmoidAPI(unittest.TestCase):
|
739 | 740 | def setUp(self):
|
740 | 741 | np.random.seed(1024)
|
741 | 742 | self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
|
742 |
| - self.place = ( |
743 |
| - paddle.CUDAPlace(0) |
744 |
| - if paddle.is_compiled_with_cuda() |
745 |
| - else paddle.CPUPlace() |
746 |
| - ) |
| 743 | + self.place = get_device_place() |
747 | 744 |
|
748 | 745 | def test_static_api(self):
|
749 | 746 | with static_guard():
|
@@ -883,11 +880,7 @@ def setUp(self):
|
883 | 880 | self.dtype = 'float32'
|
884 | 881 | np.random.seed(1024)
|
885 | 882 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype)
|
886 |
| - self.place = ( |
887 |
| - paddle.CUDAPlace(0) |
888 |
| - if paddle.is_compiled_with_cuda() |
889 |
| - else paddle.CPUPlace() |
890 |
| - ) |
| 883 | + self.place = get_device_place() |
891 | 884 | self.executed_api()
|
892 | 885 |
|
893 | 886 | def executed_api(self):
|
@@ -1328,11 +1321,7 @@ class TestTanhshrinkAPI(unittest.TestCase):
|
1328 | 1321 | def setUp(self):
|
1329 | 1322 | np.random.seed(1024)
|
1330 | 1323 | self.x_np = np.random.uniform(10, 20, [10, 17]).astype(np.float64)
|
1331 |
| - self.place = ( |
1332 |
| - paddle.CUDAPlace(0) |
1333 |
| - if paddle.is_compiled_with_cuda() |
1334 |
| - else paddle.CPUPlace() |
1335 |
| - ) |
| 1324 | + self.place = get_device_place() |
1336 | 1325 |
|
1337 | 1326 | def test_static_api(self):
|
1338 | 1327 | with static_guard():
|
@@ -1441,11 +1430,7 @@ class TestHardShrinkAPI(unittest.TestCase):
|
1441 | 1430 | def setUp(self):
|
1442 | 1431 | np.random.seed(1024)
|
1443 | 1432 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
|
1444 |
| - self.place = ( |
1445 |
| - paddle.CUDAPlace(0) |
1446 |
| - if paddle.is_compiled_with_cuda() |
1447 |
| - else paddle.CPUPlace() |
1448 |
| - ) |
| 1433 | + self.place = get_device_place() |
1449 | 1434 |
|
1450 | 1435 | def test_static_api(self):
|
1451 | 1436 | with static_guard():
|
@@ -1510,11 +1495,7 @@ def setUp(self):
|
1510 | 1495 | np.random.seed(1024)
|
1511 | 1496 | self.init_shape()
|
1512 | 1497 | self.x_np = np.random.uniform(-3, 3, self.x_shape).astype('float32')
|
1513 |
| - self.place = ( |
1514 |
| - paddle.CUDAPlace(0) |
1515 |
| - if paddle.is_compiled_with_cuda() |
1516 |
| - else paddle.CPUPlace() |
1517 |
| - ) |
| 1498 | + self.place = get_device_place() |
1518 | 1499 |
|
1519 | 1500 | def init_shape(self):
|
1520 | 1501 | self.x_shape = [10, 12]
|
@@ -1620,11 +1601,7 @@ def setUp(self):
|
1620 | 1601 | self.threshold = 0.8
|
1621 | 1602 | np.random.seed(1024)
|
1622 | 1603 | self.x_np = np.random.uniform(0.25, 10, [10, 12]).astype(np.float64)
|
1623 |
| - self.place = ( |
1624 |
| - paddle.CUDAPlace(0) |
1625 |
| - if paddle.is_compiled_with_cuda() |
1626 |
| - else paddle.CPUPlace() |
1627 |
| - ) |
| 1604 | + self.place = get_device_place() |
1628 | 1605 |
|
1629 | 1606 | def test_static_api(self):
|
1630 | 1607 | with static_guard():
|
@@ -2247,11 +2224,7 @@ def setUp(self):
|
2247 | 2224 | np.random.uniform(-1, 1, self.shape)
|
2248 | 2225 | + 1j * np.random.uniform(-1, 1, self.shape)
|
2249 | 2226 | ).astype(self.dtype)
|
2250 |
| - self.place = ( |
2251 |
| - paddle.CUDAPlace(0) |
2252 |
| - if paddle.is_compiled_with_cuda() |
2253 |
| - else paddle.CPUPlace() |
2254 |
| - ) |
| 2227 | + self.place = get_device_place() |
2255 | 2228 |
|
2256 | 2229 | out = np.tan(self.x_np)
|
2257 | 2230 |
|
@@ -2302,11 +2275,7 @@ def setUp(self):
|
2302 | 2275 | np.random.seed(1024)
|
2303 | 2276 | self.dtype = 'float32'
|
2304 | 2277 | self.x_np = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
|
2305 |
| - self.place = ( |
2306 |
| - paddle.CUDAPlace(0) |
2307 |
| - if paddle.is_compiled_with_cuda() |
2308 |
| - else paddle.CPUPlace() |
2309 |
| - ) |
| 2278 | + self.place = get_device_place() |
2310 | 2279 |
|
2311 | 2280 | def test_dygraph_api(self):
|
2312 | 2281 | with dynamic_guard():
|
@@ -2852,11 +2821,7 @@ class TestReluAPI(unittest.TestCase):
|
2852 | 2821 | def setUp(self):
|
2853 | 2822 | np.random.seed(1024)
|
2854 | 2823 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
|
2855 |
| - self.place = ( |
2856 |
| - paddle.CUDAPlace(0) |
2857 |
| - if paddle.is_compiled_with_cuda() |
2858 |
| - else paddle.CPUPlace() |
2859 |
| - ) |
| 2824 | + self.place = get_device_place() |
2860 | 2825 | self.executed_api()
|
2861 | 2826 |
|
2862 | 2827 | def executed_api(self):
|
@@ -2996,11 +2961,7 @@ class TestLeakyReluAPI(unittest.TestCase):
|
2996 | 2961 | def setUp(self):
|
2997 | 2962 | np.random.seed(1024)
|
2998 | 2963 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
|
2999 |
| - self.place = ( |
3000 |
| - paddle.CUDAPlace(0) |
3001 |
| - if paddle.is_compiled_with_cuda() |
3002 |
| - else paddle.CPUPlace() |
3003 |
| - ) |
| 2964 | + self.place = get_device_place() |
3004 | 2965 |
|
3005 | 2966 | def test_static_api(self):
|
3006 | 2967 | with static_guard():
|
@@ -3176,11 +3137,7 @@ class TestGELUAPI(unittest.TestCase):
|
3176 | 3137 | def setUp(self):
|
3177 | 3138 | np.random.seed(1024)
|
3178 | 3139 | self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
|
3179 |
| - self.place = ( |
3180 |
| - paddle.CUDAPlace(0) |
3181 |
| - if paddle.is_compiled_with_cuda() |
3182 |
| - else paddle.CPUPlace() |
3183 |
| - ) |
| 3140 | + self.place = get_device_place() |
3184 | 3141 | self.enable_cinn = False
|
3185 | 3142 |
|
3186 | 3143 | # The backward decomposite of gelu is inconsistent with raw kernel on
|
@@ -3335,11 +3292,7 @@ def setUp(self):
|
3335 | 3292 | np.random.seed(1024)
|
3336 | 3293 | self.x_np = np.random.uniform(-1, 10, [10, 12]).astype(np.float64)
|
3337 | 3294 | self.x_np[np.abs(self.x_np) < 0.005] = 0.02
|
3338 |
| - self.place = ( |
3339 |
| - paddle.CUDAPlace(0) |
3340 |
| - if paddle.is_compiled_with_cuda() |
3341 |
| - else paddle.CPUPlace() |
3342 |
| - ) |
| 3295 | + self.place = get_device_place() |
3343 | 3296 |
|
3344 | 3297 | def test_static_api(self):
|
3345 | 3298 | with static_guard():
|
@@ -3524,11 +3477,7 @@ class TestHardswishAPI(unittest.TestCase):
|
3524 | 3477 | # test paddle.nn.Hardswish, paddle.nn.functional.hardswish
|
3525 | 3478 | def setUp(self):
|
3526 | 3479 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
3527 |
| - self.place = ( |
3528 |
| - paddle.CUDAPlace(0) |
3529 |
| - if paddle.is_compiled_with_cuda() |
3530 |
| - else paddle.CPUPlace() |
3531 |
| - ) |
| 3480 | + self.place = get_device_place() |
3532 | 3481 |
|
3533 | 3482 | def test_static_api(self):
|
3534 | 3483 | with static_guard():
|
@@ -3691,11 +3640,7 @@ class TestELUAPI(unittest.TestCase):
|
3691 | 3640 | def setUp(self):
|
3692 | 3641 | np.random.seed(1024)
|
3693 | 3642 | self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32')
|
3694 |
| - self.place = ( |
3695 |
| - paddle.CUDAPlace(0) |
3696 |
| - if paddle.is_compiled_with_cuda() |
3697 |
| - else paddle.CPUPlace() |
3698 |
| - ) |
| 3643 | + self.place = get_device_place() |
3699 | 3644 | self.executed_api()
|
3700 | 3645 |
|
3701 | 3646 | def executed_api(self):
|
@@ -3806,11 +3751,7 @@ class TestCELUAPI(unittest.TestCase):
|
3806 | 3751 | def setUp(self):
|
3807 | 3752 | np.random.seed(1024)
|
3808 | 3753 | self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32')
|
3809 |
| - self.place = ( |
3810 |
| - paddle.CUDAPlace(0) |
3811 |
| - if paddle.is_compiled_with_cuda() |
3812 |
| - else paddle.CPUPlace() |
3813 |
| - ) |
| 3754 | + self.place = get_device_place() |
3814 | 3755 | self.executed_api()
|
3815 | 3756 |
|
3816 | 3757 | def executed_api(self):
|
@@ -4841,11 +4782,7 @@ def setUp(self):
|
4841 | 4782 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
|
4842 | 4783 | self.scale_a = self.get_scale_a()
|
4843 | 4784 | self.scale_b = self.get_scale_b()
|
4844 |
| - self.place = ( |
4845 |
| - paddle.CUDAPlace(0) |
4846 |
| - if core.is_compiled_with_cuda() |
4847 |
| - else paddle.CPUPlace() |
4848 |
| - ) |
| 4785 | + self.place = get_device_place() |
4849 | 4786 |
|
4850 | 4787 | def test_static_api(self):
|
4851 | 4788 | with static_guard():
|
@@ -5025,11 +4962,7 @@ def setUp(self):
|
5025 | 4962 | self.threshold = 15
|
5026 | 4963 | np.random.seed(1024)
|
5027 | 4964 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
5028 |
| - self.place = ( |
5029 |
| - paddle.CUDAPlace(0) |
5030 |
| - if paddle.is_compiled_with_cuda() |
5031 |
| - else paddle.CPUPlace() |
5032 |
| - ) |
| 4965 | + self.place = get_device_place() |
5033 | 4966 |
|
5034 | 4967 | def test_static_api(self):
|
5035 | 4968 | with static_guard():
|
@@ -5160,11 +5093,7 @@ class TestSoftsignAPI(unittest.TestCase):
|
5160 | 5093 | def setUp(self):
|
5161 | 5094 | np.random.seed(1024)
|
5162 | 5095 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
5163 |
| - self.place = ( |
5164 |
| - paddle.CUDAPlace(0) |
5165 |
| - if paddle.is_compiled_with_cuda() |
5166 |
| - else paddle.CPUPlace() |
5167 |
| - ) |
| 5096 | + self.place = get_device_place() |
5168 | 5097 |
|
5169 | 5098 | def test_static_api(self):
|
5170 | 5099 | with static_guard():
|
@@ -5265,11 +5194,7 @@ def setUp(self):
|
5265 | 5194 | np.random.seed(1024)
|
5266 | 5195 | self.x_np = np.random.uniform(-20, 20, [10, 12]).astype(np.float64)
|
5267 | 5196 | self.x_np[np.abs(self.x_np) < 0.005] = 0.02
|
5268 |
| - self.place = ( |
5269 |
| - paddle.CUDAPlace(0) |
5270 |
| - if paddle.is_compiled_with_cuda() |
5271 |
| - else paddle.CPUPlace() |
5272 |
| - ) |
| 5197 | + self.place = get_device_place() |
5273 | 5198 |
|
5274 | 5199 | def test_static_api(self):
|
5275 | 5200 | with static_guard():
|
@@ -5397,11 +5322,7 @@ class TestHardsigmoidAPI(unittest.TestCase):
|
5397 | 5322 | # test paddle.nn.Hardsigmoid, paddle.nn.functional.hardsigmoid
|
5398 | 5323 | def setUp(self):
|
5399 | 5324 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
5400 |
| - self.place = ( |
5401 |
| - paddle.CUDAPlace(0) |
5402 |
| - if paddle.is_compiled_with_cuda() |
5403 |
| - else paddle.CPUPlace() |
5404 |
| - ) |
| 5325 | + self.place = get_device_place() |
5405 | 5326 |
|
5406 | 5327 | def test_static_api(self):
|
5407 | 5328 | with static_guard():
|
@@ -5508,11 +5429,7 @@ class TestSwishAPI(unittest.TestCase):
|
5508 | 5429 | def setUp(self):
|
5509 | 5430 | np.random.seed(1024)
|
5510 | 5431 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
5511 |
| - self.place = ( |
5512 |
| - paddle.CUDAPlace(0) |
5513 |
| - if paddle.is_compiled_with_cuda() |
5514 |
| - else paddle.CPUPlace() |
5515 |
| - ) |
| 5432 | + self.place = get_device_place() |
5516 | 5433 |
|
5517 | 5434 | def test_static_api(self):
|
5518 | 5435 | with static_guard():
|
@@ -5619,11 +5536,7 @@ class TestMishAPI(unittest.TestCase):
|
5619 | 5536 | def setUp(self):
|
5620 | 5537 | np.random.seed(1024)
|
5621 | 5538 | self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
|
5622 |
| - self.place = ( |
5623 |
| - paddle.CUDAPlace(0) |
5624 |
| - if paddle.is_compiled_with_cuda() |
5625 |
| - else paddle.CPUPlace() |
5626 |
| - ) |
| 5539 | + self.place = get_device_place() |
5627 | 5540 |
|
5628 | 5541 | def test_static_api(self):
|
5629 | 5542 | with static_guard():
|
|
0 commit comments