-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Hackathon 5th No.49][pir] add some operation - Part 3 #58219
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,153 @@ | |
import unittest | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
from paddle import base | ||
|
||
paddle.enable_static() | ||
paddle.device.set_device("cpu") | ||
|
||
|
||
def new_program(): | ||
# TODO(gouzil): Optimize program code | ||
main_program = paddle.static.Program() | ||
startup_program = paddle.static.Program() | ||
place = base.CPUPlace() | ||
exe = base.Executor(place) | ||
return ( | ||
main_program, | ||
exe, | ||
paddle.static.program_guard( | ||
main_program=main_program, startup_program=startup_program | ||
), | ||
) | ||
|
||
|
||
class TestMathOpPatchesPir(unittest.TestCase): | ||
def test_pow(self): | ||
# Calculate results in dynamic graphs | ||
paddle.disable_static() | ||
x_np = np.random.random([10, 1024]).astype('float32') | ||
y_np = np.random.random([10, 1024]).astype('float32') | ||
res_np_b = x_np**y_np | ||
res_np_c = paddle.pow(paddle.to_tensor(x_np), 2) | ||
# TODO(gouzil): solve paddle.fill_constant problem | ||
# res_np_d = x_np.__pow__(2) | ||
# res_np_e = x_np.__rpow__(2) | ||
paddle.enable_static() | ||
# Calculate results under pir | ||
with paddle.pir_utils.IrGuard(): | ||
main_program, exe, program_guard = new_program() | ||
with program_guard: | ||
x = paddle.static.data( | ||
name='x', shape=[10, 1024], dtype='float32' | ||
) | ||
y = paddle.static.data( | ||
name='y', shape=[10, 1024], dtype='float32' | ||
) | ||
b = x**y | ||
c = x.pow(2) | ||
# d = x.__pow__(2) | ||
# e = x.__rpow__(2) | ||
# TODO(gouzil): Why not use `paddle.static.default_main_program()`? | ||
# Because different case do not isolate parameters (This is a known problem) | ||
(b_np, c_np) = exe.run( | ||
main_program, | ||
feed={"x": x_np, "y": y_np}, | ||
fetch_list=[b, c], | ||
) | ||
np.testing.assert_allclose(res_np_b, b_np, rtol=1e-05) | ||
np.testing.assert_allclose(res_np_c, c_np, rtol=1e-05) | ||
# np.testing.assert_allclose(res_np_d, d_np, rtol=1e-05) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
# np.testing.assert_allclose(res_np_e, e_np, rtol=1e-05) | ||
|
||
def test_mod(self): | ||
paddle.disable_static() | ||
x_np = np.random.randint(1, 100, size=[10, 1024], dtype=np.int64) | ||
y_np = np.random.randint(1, 100, size=[10, 1024], dtype=np.int64) | ||
res_np_b = x_np % y_np | ||
res_np_c = paddle.mod(paddle.to_tensor(x_np), paddle.to_tensor(y_np)) | ||
res_np_d = x_np.__mod__(y_np) | ||
paddle.enable_static() | ||
with paddle.pir_utils.IrGuard(): | ||
main_program, exe, program_guard = new_program() | ||
with program_guard: | ||
x = paddle.static.data( | ||
name='x', shape=[10, 1024], dtype='int64' | ||
) | ||
y = paddle.static.data( | ||
name='y', shape=[10, 1024], dtype='int64' | ||
) | ||
b = x % y | ||
c = x.mod(y) | ||
d = x.__mod__(y) | ||
(b_np, c_np, d_np) = exe.run( | ||
main_program, | ||
feed={"x": x_np, "y": y_np}, | ||
fetch_list=[b, c, d], | ||
) | ||
np.testing.assert_allclose(res_np_b, b_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_c, c_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_d, d_np, atol=1e-05) | ||
|
||
def test_matmul(self): | ||
paddle.disable_static() | ||
x_np = np.random.uniform(-1, 1, [2, 3]).astype('float32') | ||
y_np = np.random.uniform(-1, 1, [3, 5]).astype('float32') | ||
res_np_b = x_np @ y_np # __matmul__ | ||
res_np_c = paddle.matmul(paddle.to_tensor(x_np), paddle.to_tensor(y_np)) | ||
0x45f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res_np_d = x_np.__matmul__(y_np) | ||
paddle.enable_static() | ||
with paddle.pir_utils.IrGuard(): | ||
main_program, exe, program_guard = new_program() | ||
with program_guard: | ||
x = paddle.static.data(name='x', shape=[2, 3], dtype='float32') | ||
y = paddle.static.data(name='y', shape=[3, 5], dtype='float32') | ||
b = x @ y | ||
c = x.matmul(y) | ||
d = x.__matmul__(y) | ||
(b_np, c_np, d_np) = exe.run( | ||
main_program, | ||
feed={"x": x_np, "y": y_np}, | ||
fetch_list=[b, c, d], | ||
) | ||
np.testing.assert_allclose(res_np_b, b_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_c, c_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_d, d_np, atol=1e-05) | ||
|
||
def test_floordiv(self): | ||
paddle.disable_static() | ||
x_np = np.full([10, 1024], 10, np.int64) | ||
y_np = np.full([10, 1024], 2, np.int64) | ||
res_np_b = x_np // y_np | ||
res_np_c = paddle.floor_divide( | ||
paddle.to_tensor(x_np), paddle.to_tensor(y_np) | ||
) | ||
res_np_d = x_np.__floordiv__(y_np) | ||
paddle.enable_static() | ||
with paddle.pir_utils.IrGuard(): | ||
main_program, exe, program_guard = new_program() | ||
with program_guard: | ||
x = paddle.static.data( | ||
name='x', shape=[10, 1024], dtype='int64' | ||
) | ||
y = paddle.static.data( | ||
name='y', shape=[10, 1024], dtype='int64' | ||
) | ||
b = x // y | ||
c = x.floor_divide(y) | ||
d = x.__floordiv__(y) | ||
(b_np, c_np, d_np) = exe.run( | ||
main_program, | ||
feed={"x": x_np, "y": y_np}, | ||
fetch_list=[b, c, d], | ||
) | ||
np.testing.assert_allclose(res_np_b, b_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_c, c_np, atol=1e-05) | ||
np.testing.assert_allclose(res_np_d, d_np, atol=1e-05) | ||
|
||
def test_item(self): | ||
with paddle.pir_utils.IrGuard(): | ||
x = paddle.static.data(name='x', shape=[3, 2, 1]) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后续可以把一些没有用的注释删掉~