Skip to content

Commit eb6a7c2

Browse files
[NPU] [MLU] Fix test_layer_norm_op & test_index_select_op & test_memcpy_op on NPU (#1629)
1 parent 349550a commit eb6a7c2

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

backends/mlu/tests/unittests/test_index_select_op_mlu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_index_select_api(self):
142142
exe = paddle.static.Executor(paddle.CustomPlace("mlu", 0))
143143
(res,) = exe.run(
144144
feed={"x": self.data_x, "index": self.data_index},
145-
fetch_list=[z.name],
145+
fetch_list=[z],
146146
return_numpy=False,
147147
)
148148
expect_out = np.array(
@@ -158,7 +158,7 @@ def test_index_select_api(self):
158158
exe = paddle.static.Executor(paddle.CustomPlace("mlu", 0))
159159
(res,) = exe.run(
160160
feed={"x": self.data_x, "index": self.data_index},
161-
fetch_list=[z.name],
161+
fetch_list=[z],
162162
return_numpy=False,
163163
)
164164
expect_out = np.array(

backends/mlu/tests/unittests/test_layer_norm_op_mlu.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from paddle.static.amp.fp16_utils import (
2727
_keep_layer_norm_scale_bias_to_fp32,
2828
)
29+
from paddle.pir_utils import OldIrGuard
2930

3031
paddle.enable_static()
3132

@@ -170,8 +171,10 @@ def test_with_place(place, shape, begin_norm_axis, use_mkldnn=use_mkldnn):
170171
var_names += ["bias"]
171172
ground_truth = {name: var_dict[name] for name in var_names}
172173

173-
program = base.Program()
174-
with base.program_guard(program):
174+
with OldIrGuard():
175+
program = base.Program()
176+
old_program_guard = base.program_guard
177+
with old_program_guard(program):
175178
block = program.global_block()
176179
for name in ground_truth:
177180
block.create_var(
@@ -221,14 +224,15 @@ def test_with_place(place, shape, begin_norm_axis, use_mkldnn=use_mkldnn):
221224

222225
program._sync_with_cpp()
223226
exe = base.Executor(place)
224-
out = exe.run(
225-
program,
226-
feed={
227-
name: var_dict[name]
228-
for name in ["x", "scale", "bias", "y@GRAD"]
229-
},
230-
fetch_list=fetch_list,
231-
)
227+
with OldIrGuard():
228+
out = exe.run(
229+
program,
230+
feed={
231+
name: var_dict[name]
232+
for name in ["x", "scale", "bias", "y@GRAD"]
233+
},
234+
fetch_list=fetch_list,
235+
)
232236

233237
self.__assert_close(y, out[0], "y")
234238
self.__assert_close(mean, out[1], "mean")

backends/npu/tests/unittests/test_index_select_op_npu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_index_select_api(self):
141141
exe = paddle.static.Executor(paddle.CustomPlace("npu", 0))
142142
(res,) = exe.run(
143143
feed={"x": self.data_x, "index": self.data_index},
144-
fetch_list=[z.name],
144+
fetch_list=[z],
145145
return_numpy=False,
146146
)
147147
expect_out = np.array(
@@ -157,7 +157,7 @@ def test_index_select_api(self):
157157
exe = paddle.static.Executor(paddle.CustomPlace("npu", 0))
158158
(res,) = exe.run(
159159
feed={"x": self.data_x, "index": self.data_index},
160-
fetch_list=[z.name],
160+
fetch_list=[z],
161161
return_numpy=False,
162162
)
163163
expect_out = np.array(

backends/npu/tests/unittests/test_layer_norm_op_npu.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import paddle.base.core as core
2525
import paddle_custom_device
2626

27+
from paddle.pir_utils import OldIrGuard
28+
2729
paddle.enable_static()
2830

2931
SEED = 2021
@@ -180,8 +182,10 @@ def test_with_place(place, shape, begin_norm_axis, use_mkldnn=use_mkldnn):
180182
var_names += ["bias"]
181183
ground_truth = {name: var_dict[name] for name in var_names}
182184

183-
program = base.Program()
184-
with base.program_guard(program):
185+
with OldIrGuard():
186+
program = base.Program()
187+
old_program_guard = base.program_guard
188+
with old_program_guard(program):
185189
block = program.global_block()
186190
for name in ground_truth:
187191
block.create_var(
@@ -231,14 +235,15 @@ def test_with_place(place, shape, begin_norm_axis, use_mkldnn=use_mkldnn):
231235

232236
program._sync_with_cpp()
233237
exe = base.Executor(place)
234-
out = exe.run(
235-
program,
236-
feed={
237-
name: var_dict[name]
238-
for name in ["x", "scale", "bias", "y@GRAD"]
239-
},
240-
fetch_list=fetch_list,
241-
)
238+
with OldIrGuard():
239+
out = exe.run(
240+
program,
241+
feed={
242+
name: var_dict[name]
243+
for name in ["x", "scale", "bias", "y@GRAD"]
244+
},
245+
fetch_list=fetch_list,
246+
)
242247
# CANN 7.0 changes the output mode of variance
243248
if int(paddle_custom_device.npu.version()["cann"].split(".")[0]) >= 7:
244249
out[2] = (1 / out[2]) ** 2 - epsilon

backends/npu/tests/unittests/test_memcpy_op_npu.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import paddle
2121
import paddle.base as base
22-
from paddle.base import Program, program_guard
22+
23+
from paddle.pir_utils import OldIrGuard
2324

2425
paddle.enable_static()
2526
SEED = 2021
@@ -29,8 +30,11 @@ class TestMemcpy_FillConstant(unittest.TestCase):
2930
def get_prog(self):
3031
self.__class__.use_custom_device = True
3132
paddle.enable_static()
32-
main_program = Program()
33-
with program_guard(main_program):
33+
34+
with OldIrGuard():
35+
main_program = base.Program()
36+
old_program_guard = base.program_guard
37+
with old_program_guard(main_program):
3438
cpu_var_name = "tensor@Cpu"
3539
npu_var_name = "tensor@Npu"
3640
cpu_var = main_program.global_block().create_var(
@@ -79,9 +83,8 @@ def test_npu_cpoy_to_cpu(self):
7983
)
8084
place = paddle.CustomPlace("npu", 0)
8185
exe = base.Executor(place)
82-
npu_, cpu_ = exe.run(
83-
main_program, feed={}, fetch_list=[npu_var.name, cpu_var.name]
84-
)
86+
with OldIrGuard():
87+
npu_, cpu_ = exe.run(main_program, feed={}, fetch_list=[npu_var, cpu_var])
8588
np.testing.assert_allclose(npu_, cpu_)
8689
np.testing.assert_allclose(cpu_, np.ones((10, 10)))
8790

@@ -96,9 +99,8 @@ def test_cpu_cpoy_npu(self):
9699
)
97100
place = paddle.CustomPlace("npu", 0)
98101
exe = base.Executor(place)
99-
npu_, cpu_ = exe.run(
100-
main_program, feed={}, fetch_list=[npu_var.name, cpu_var.name]
101-
)
102+
with OldIrGuard():
103+
npu_, cpu_ = exe.run(main_program, feed={}, fetch_list=[npu_var, cpu_var])
102104
np.testing.assert_allclose(npu_, cpu_)
103105
np.testing.assert_allclose(npu_, np.zeros((10, 10)))
104106

0 commit comments

Comments
 (0)