-
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
[Zero-Dim] support 0D output for matrix_rank/multi_dot #52861
Merged
zhwesky2010
merged 14 commits into
PaddlePaddle:develop
from
GGBond8488:support_0D_output_for_matrix_rank_multi_dot
Apr 21, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
917c1f5
support_0D_output_for_matrix_rank_multi_dot, test=allcase
GGBond8488 93f9df2
add 0D output test for matrox_rank and mutli_dot test=allcase
GGBond8488 76d065f
fix assert error ,test=allcase
GGBond8488 29d5743
fix test error, test=allcase
GGBond8488 3754bcd
fix other test error, test=allcase
GGBond8488 b440c4a
fix other test error, test=allcase
GGBond8488 650cad3
fix test error, test=allcase
GGBond8488 3530751
fix matrix_rank and multi dot test err test=allcase
GGBond8488 fcd901b
fix test error test=allcase
GGBond8488 ffa9567
fix test zero dim test, test=allcase
GGBond8488 0bcba95
fix conflicts
GGBond8488 6b4191a
fix conflicts
GGBond8488 ea2c251
add static backward test for multi_dot, test=allcase
GGBond8488 ee3865d
add tol 2d broadcast test case, test=allcase
GGBond8488 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
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 |
---|---|---|
|
@@ -2087,6 +2087,23 @@ def test_linalg_slogdet(self): | |
self.assertTrue(out1.shape, [2, 3]) | ||
self.assertTrue(x1.grad.shape, [3, 3, 3]) | ||
|
||
def test_multi_dot(self): | ||
a = paddle.randn([4]) | ||
a.stop_gradient = False | ||
b = paddle.randn([4, 5]) | ||
b.stop_gradient = False | ||
c = paddle.randn([5]) | ||
c.stop_gradient = False | ||
|
||
out = paddle.linalg.multi_dot([a, b, c]) | ||
out.retain_grads() | ||
out.backward() | ||
|
||
self.assertEqual(out.shape, []) | ||
self.assertEqual(a.grad.shape, [4]) | ||
self.assertEqual(b.grad.shape, [4, 5]) | ||
self.assertEqual(c.grad.shape, [5]) | ||
|
||
|
||
class TestSundryAPIStatic(unittest.TestCase): | ||
def setUp(self): | ||
|
@@ -3654,6 +3671,26 @@ def test_linalg_slogdet(self): | |
self.assertEqual(res[0].shape, (2, 3)) | ||
self.assertEqual(res[1].shape, (3, 3, 3)) | ||
|
||
@prog_scope() | ||
def test_multi_dot(self): | ||
a = paddle.randn([4]) | ||
a.stop_gradient = False | ||
b = paddle.randn([4, 5]) | ||
b.stop_gradient = False | ||
c = paddle.randn([5]) | ||
c.stop_gradient = False | ||
|
||
out = paddle.linalg.multi_dot([a, b, c]) | ||
paddle.static.append_backward(out.sum()) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run( | ||
prog, fetch_list=[out, a.grad_name, b.grad_name, c.grad_name] | ||
) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, (4,)) | ||
self.assertEqual(res[2].shape, (4, 5)) | ||
self.assertEqual(res[3].shape, (5,)) | ||
|
||
|
||
# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. | ||
class TestNoBackwardAPI(unittest.TestCase): | ||
|
@@ -3845,6 +3882,38 @@ def test_unique(self): | |
self.assertEqual(inverse.shape, [1]) | ||
self.assertEqual(counts.shape, [1]) | ||
|
||
def test_matrix_rank(self): | ||
x = paddle.eye(10) | ||
x.stop_gradient = False | ||
out = paddle.linalg.matrix_rank(x) | ||
|
||
self.assertEqual(out.shape, []) | ||
np.testing.assert_equal(out, np.array(10)) | ||
|
||
c = paddle.ones(shape=[3, 4, 5]) | ||
c.stop_gradient = False | ||
out_c = paddle.linalg.matrix_rank(c) | ||
self.assertEqual(out_c.shape, [3]) | ||
np.testing.assert_equal(out_c, np.array([1, 1, 1])) | ||
|
||
# 2D, tol->float : OUTPUT 0D | ||
x_tol = paddle.eye(10) | ||
x_tol.stop_gradient = False | ||
out_tol = paddle.linalg.matrix_rank(x_tol, tol=0.1) | ||
self.assertEqual(out_tol.shape, []) | ||
|
||
# 3D, tol->float : OUTPUT 1D | ||
c_tol = paddle.ones(shape=[3, 4, 5]) | ||
c_tol.stop_gradient = False | ||
out_c_tol = paddle.linalg.matrix_rank(c_tol, tol=0.1) | ||
self.assertEqual(out_c_tol.shape, [3]) | ||
|
||
tol_2 = paddle.randn([2]) | ||
# 2D, tol->Tensor[1,2] : OUTPUT 1D | ||
d = paddle.eye(10) | ||
out_d = paddle.linalg.matrix_rank(d, tol=tol_2) | ||
self.assertEqual(out_d.shape, [2]) | ||
|
||
|
||
class TestNoBackwardAPIStatic(unittest.TestCase): | ||
def setUp(self): | ||
|
@@ -4079,6 +4148,51 @@ def test_unique(self): | |
self.assertEqual(res[2].shape, (1,)) | ||
self.assertEqual(res[3].shape, (1,)) | ||
|
||
@prog_scope() | ||
def test_static_matrix_rank(self): | ||
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. 和动态图一样可以加个case |
||
# 2D : OUTPUT 0D | ||
x = paddle.eye(10) | ||
x.stop_gradient = False | ||
out = paddle.linalg.matrix_rank(x) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, fetch_list=[out]) | ||
self.assertEqual(res[0].shape, ()) | ||
|
||
# 3D : OUTPUT 1D | ||
c = paddle.ones(shape=[3, 4, 5]) | ||
c.stop_gradient = False | ||
out_c = paddle.linalg.matrix_rank(c) | ||
prog = paddle.static.default_main_program() | ||
self.exe.run(paddle.static.default_startup_program()) | ||
res = self.exe.run(prog, fetch_list=[out_c]) | ||
self.assertEqual(res[0].shape, (3,)) | ||
|
||
# 2D, tol->float : OUTPUT 0D | ||
x_tol = paddle.eye(10) | ||
x_tol.stop_gradient = False | ||
out_tol = paddle.linalg.matrix_rank(x_tol, tol=0.1) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, fetch_list=[out_tol]) | ||
self.assertEqual(res[0].shape, ()) | ||
|
||
# 3D, tol->float : OUTPUT 1D | ||
c_tol = paddle.ones(shape=[3, 4, 5]) | ||
c_tol.stop_gradient = False | ||
out_c_tol = paddle.linalg.matrix_rank(c_tol, tol=0.1) | ||
prog = paddle.static.default_main_program() | ||
self.exe.run(paddle.static.default_startup_program()) | ||
res = self.exe.run(prog, fetch_list=[out_c_tol]) | ||
self.assertEqual(res[0].shape, (3,)) | ||
|
||
tol_2 = paddle.randn([2]) | ||
# 2D, tol->Tensor[1,2] : OUTPUT 1D | ||
d = paddle.eye(10) | ||
out_d = paddle.linalg.matrix_rank(d, tol=tol_2) | ||
prog = paddle.static.default_main_program() | ||
self.exe.run(paddle.static.default_startup_program()) | ||
res = self.exe.run(prog, fetch_list=[out_d]) | ||
self.assertEqual(res[0].shape, (2,)) | ||
|
||
|
||
unary_apis_with_complex_input = [ | ||
paddle.real, | ||
|
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.
可以测下matrix_rank的第二种用法: