Skip to content

Commit eb77bfe

Browse files
YuanRishengco63oc
authored andcommitted
【Fix PIR Unittest No.1-5】Fix test_sequence_mask in PIR mode (PaddlePaddle#64055)
* fix test_sequence_mask * fix py3 * fix unittest * fix py3
1 parent e909d6f commit eb77bfe

12 files changed

+72
-52
lines changed

test/deprecated/sequence/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,3 @@ string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
77
foreach(TEST_OP ${TEST_OPS})
88
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
99
endforeach()
10-
set_tests_properties(test_sequence_conv PROPERTIES TIMEOUT 120)
11-
set_tests_properties(test_sequence_pool PROPERTIES TIMEOUT 120)
12-
13-
set(PIR_COVERAGE_TESTS test_sequence_mask)
14-
15-
foreach(PIR_COVERAGE_TEST ${PIR_COVERAGE_TESTS})
16-
py_test_modules(${PIR_COVERAGE_TEST}_pir MODULES ${PIR_COVERAGE_TEST} ENVS
17-
FLAGS_enable_pir_in_executor=true)
18-
set_tests_properties(${PIR_COVERAGE_TEST}_pir PROPERTIES TIMEOUT 120)
19-
message(STATUS "PIR Copied OpTest: ${PIR_COVERAGE_TEST}_pir in sequence test")
20-
endforeach()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import numpy as np
18+
19+
import paddle
20+
21+
paddle.enable_static()
22+
23+
24+
class TestSeqConvApi(unittest.TestCase):
25+
def test_api(self):
26+
from paddle import base
27+
28+
x = paddle.static.data('x', shape=[-1, 32], lod_level=1)
29+
y = paddle.static.nn.sequence_lod.sequence_conv(
30+
input=x, num_filters=2, filter_size=3, padding_start=None
31+
)
32+
33+
place = base.CPUPlace()
34+
x_tensor = base.create_lod_tensor(
35+
np.random.rand(10, 32).astype("float32"), [[2, 3, 1, 4]], place
36+
)
37+
exe = base.Executor(place)
38+
exe.run(base.default_startup_program())
39+
ret = exe.run(feed={'x': x_tensor}, fetch_list=[y], return_numpy=False)
40+
41+
42+
if __name__ == '__main__':
43+
unittest.main()

test/legacy_test/op_test.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,20 +3283,21 @@ def check_grad_with_place(
32833283
if numeric_place is None:
32843284
numeric_place = place
32853285

3286-
numeric_grads = self.check_grad_with_place_for_static(
3287-
user_defined_grads,
3288-
inputs_to_check,
3289-
place,
3290-
output_names,
3291-
no_grad_set,
3292-
user_defined_grad_outputs,
3293-
numeric_place,
3294-
numeric_grad_delta,
3295-
in_place,
3296-
check_cinn,
3297-
max_relative_error,
3298-
atol,
3299-
)
3286+
with paddle.pir_utils.OldIrGuard():
3287+
numeric_grads = self.check_grad_with_place_for_static(
3288+
user_defined_grads,
3289+
inputs_to_check,
3290+
place,
3291+
output_names,
3292+
no_grad_set,
3293+
user_defined_grad_outputs,
3294+
numeric_place,
3295+
numeric_grad_delta,
3296+
in_place,
3297+
check_cinn,
3298+
max_relative_error,
3299+
atol,
3300+
)
33003301

33013302
if check_pir_onednn and isinstance(
33023303
place, paddle.base.libpaddle.CPUPlace

test/legacy_test/test_fusion_seqconv_eltadd_relu_op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
from op_test import OpTest
2020

21-
sys.path.append("../deprecated/sequence")
21+
sys.path.append("../sequence")
2222
from test_sequence_conv import seqconv
2323

2424

test/legacy_test/test_fusion_seqpool_concat_op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
from op_test import OpTest
2020

21-
sys.path.append("../deprecated/sequence")
21+
sys.path.append("../sequence")
2222
from test_sequence_pool import (
2323
compute_seqpool_avg,
2424
compute_seqpool_sqrt,

test/legacy_test/test_fusion_seqpool_cvm_concat_op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
from op_test import OpTest
2020

21-
sys.path.append("../deprecated/sequence")
21+
sys.path.append("../sequence")
2222
from test_cvm_op import cvm_compute
2323
from test_sequence_pool import (
2424
compute_seqpool_avg,

test/sequence/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ foreach(TEST_OP ${TEST_OPS})
88
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
99
endforeach()
1010

11+
set(PIR_COVERAGE_TESTS test_sequence_mask)
12+
1113
foreach(PIR_COVERAGE_TEST ${PIR_COVERAGE_TESTS})
1214
py_test_modules(${PIR_COVERAGE_TEST}_pir MODULES ${PIR_COVERAGE_TEST} ENVS
1315
FLAGS_enable_pir_in_executor=true)
1416
set_tests_properties(${PIR_COVERAGE_TEST}_pir PROPERTIES TIMEOUT 120)
1517
message(STATUS "PIR Copied OpTest: ${PIR_COVERAGE_TEST}_pir in sequence test")
1618
endforeach()
19+
20+
set_tests_properties(test_sequence_conv PROPERTIES TIMEOUT 120)
21+
set_tests_properties(test_sequence_pool PROPERTIES TIMEOUT 120)

test/deprecated/sequence/test_sequence_conv.py renamed to test/sequence/test_sequence_conv.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import numpy as np
1919
from op_test import OpTest
2020

21-
import paddle
22-
2321

2422
def seqconv(
2523
x,
@@ -290,23 +288,5 @@ def init_test_case(self):
290288
self.output_represention = 8 # output feature size
291289

292290

293-
class TestSeqConvApi(unittest.TestCase):
294-
def test_api(self):
295-
from paddle import base
296-
297-
x = paddle.static.data('x', shape=[-1, 32], lod_level=1)
298-
y = paddle.static.nn.sequence_lod.sequence_conv(
299-
input=x, num_filters=2, filter_size=3, padding_start=None
300-
)
301-
302-
place = base.CPUPlace()
303-
x_tensor = base.create_lod_tensor(
304-
np.random.rand(10, 32).astype("float32"), [[2, 3, 1, 4]], place
305-
)
306-
exe = base.Executor(place)
307-
exe.run(base.default_startup_program())
308-
ret = exe.run(feed={'x': x_tensor}, fetch_list=[y], return_numpy=False)
309-
310-
311291
if __name__ == '__main__':
312292
unittest.main()
File renamed without changes.

test/deprecated/sequence/test_sequence_mask.py renamed to test/sequence/test_sequence_mask.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import paddle
2828
from paddle.base.framework import (
29-
convert_np_dtype_to_dtype_,
29+
convert_np_dtype_to_proto_type,
3030
)
3131
from paddle.pir_utils import test_with_pir_api
3232

@@ -60,7 +60,7 @@ def setUp(self):
6060
self.outputs = {'Y': self.calc_ground_truth_mask()}
6161
self.attrs = {
6262
'maxlen': self.maxlen,
63-
'out_dtype': convert_np_dtype_to_dtype_(self.mask_dtype),
63+
'out_dtype': convert_np_dtype_to_proto_type(self.mask_dtype),
6464
}
6565

6666
def calc_ground_truth_mask(self):
@@ -129,7 +129,9 @@ def setUp(self):
129129

130130
self.inputs = {'X': self.x, 'MaxLenTensor': self.maxlen_tensor}
131131
self.outputs = {'Y': self.calc_ground_truth_mask()}
132-
self.attrs = {'out_dtype': convert_np_dtype_to_dtype_(self.mask_dtype)}
132+
self.attrs = {
133+
'out_dtype': convert_np_dtype_to_proto_type(self.mask_dtype)
134+
}
133135

134136
def calc_ground_truth_mask(self):
135137
maxlen = np.max(self.x) if self.maxlen < 0 else self.maxlen

0 commit comments

Comments
 (0)