Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PIR]Open uts for 251-260 #60820

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/legacy_test/test_gaussian_nll_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import paddle
import paddle.nn.functional as F
from paddle.base import core
from paddle.pir_utils import test_with_pir_api

np.random.seed(10)

Expand Down Expand Up @@ -127,6 +128,7 @@ def test_dynamic_case(self, type=None, full=False, reduction='none'):
np.allclose(out_ref, r.numpy(), rtol=1e-5, atol=1e-5)
paddle.enable_static()

@test_with_pir_api
def test_static_case(self, type=None, full=False, reduction='none'):
self.setUp(type)
paddle.enable_static()
Expand Down
4 changes: 0 additions & 4 deletions test/legacy_test/test_hinge_embedding_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ def run_static_check(self, place=paddle.CPUPlace):
)
np.testing.assert_allclose(result_numpy, expected, rtol=1e-05)

@test_with_pir_api
def test_cpu(self):
self.run_dynamic_check(place=paddle.CPUPlace())
self.run_static_check(place=paddle.CPUPlace())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_static_check已经装饰过了,所以将这里的装饰器删掉


@test_with_pir_api
def test_gpu(self):
if not paddle.is_compiled_with_cuda():
return
Expand Down Expand Up @@ -184,12 +182,10 @@ def run_static_check(self, place=paddle.CPUPlace):
)
np.testing.assert_allclose(result_numpy, expected, rtol=1e-05)

@test_with_pir_api
def test_cpu(self):
self.run_dynamic_check(place=paddle.CPUPlace())
self.run_static_check(place=paddle.CPUPlace())

@test_with_pir_api
def test_gpu(self):
if not paddle.is_compiled_with_cuda():
return
Expand Down
77 changes: 77 additions & 0 deletions test/legacy_test/test_lr_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,82 @@ def _test_static(self, python_func, paddle_api, kwarg, place):
scheduler.step()
num += 1

def _test_pir(self, python_func, paddle_api, kwarg, place):
def get_lr_var(program):
for param in program.global_block().all_parameters():
if param.name.startswith('learning_rate_'):
return param

with paddle.pir_utils.IrGuard():
scheduler = paddle_api(**kwarg)
adam = paddle.optimizer.Adam(learning_rate=scheduler)

main_prog = paddle.static.Program()
start_prog = paddle.static.Program()
with paddle.static.program_guard(main_prog, start_prog):
x = paddle.static.data(name='x', shape=[3, 4, 5])
loss = paddle.mean(x)
adam.minimize(loss)

test_prog, _ = paddle.base.libpaddle.pir.clone_program(main_prog)

num = 0
exe = paddle.static.Executor(place)
exe.run(start_prog)

for epoch in range(5):
for batch_id in range(2):
out = exe.run(
main_prog,
feed={'x': np.random.randn(3, 4, 5).astype('float32')},
fetch_list=get_lr_var(main_prog),
)
self.assertEqual(out, np.array(python_func(num, **kwarg)))
scheduler.step()
num += 1

for epoch in range(5):
for batch_id in range(2):
out = exe.run(
test_prog,
feed={'x': np.random.randn(3, 4, 5).astype('float32')},
fetch_list=get_lr_var(test_prog),
)
self.assertEqual(out, np.array(python_func(num, **kwarg)))
scheduler.step()
num += 1

if isinstance(place, paddle.CPUPlace):
compiled_train_prog = main_prog
for epoch in range(5):
python_result = python_func(num, **kwarg)
for batch_id in range(2):
out = exe.run(
compiled_train_prog,
feed={
'x': np.random.randn(3, 4, 5).astype('float32')
},
fetch_list=get_lr_var(compiled_train_prog),
)
self.assertEqual(out, np.array(python_result))
scheduler.step()
num += 1

compiled_test_prog = test_prog
for epoch in range(5):
python_result = python_func(num, **kwarg)
for batch_id in range(2):
out = exe.run(
compiled_test_prog,
feed={
'x': np.random.randn(3, 4, 5).astype('float32')
},
fetch_list=get_lr_var(compiled_test_prog),
)
self.assertEqual(out, np.array(python_result))
scheduler.step()
num += 1

def _test_dygraph(self, python_func, paddle_api, kwarg, place):
paddle.disable_static(place)
x = np.random.uniform(-1, 1, [10, 10]).astype("float32")
Expand Down Expand Up @@ -1212,6 +1288,7 @@ def test_scheduler(self):
for place in places:
paddle.enable_static()
self._test_static(python_func, paddle_api, kwarg, place)
self._test_pir(python_func, paddle_api, kwarg, place)
paddle.disable_static(place)
self._test_dygraph(python_func, paddle_api, kwarg, place)
paddle.enable_static()
Expand Down