-
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 output 0D for squeeze, unbind, unstack. #52843
Merged
zhwesky2010
merged 12 commits into
PaddlePaddle:develop
from
zhengqiwen1997:0D_squ_unbind_unstack
Apr 28, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c1d0875
test=allcase
zhengqiwen1997 b8d7816
test=allcase
zhengqiwen1997 5744bf6
test=allcase
zhengqiwen1997 9e162b4
test=allcase
zhengqiwen1997 f4cef7f
test=allcase
zhengqiwen1997 a872938
fix test cases, test=allcase
zhengqiwen1997 9b35eaf
fix test cases, test=allcase
zhengqiwen1997 4be61b0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zhengqiwen1997 bd0aef1
modify the test_squeeze to not use Tensor type axis, test=allcase
zhengqiwen1997 e203149
add grad check for unbind and unstack, test=allcase
zhengqiwen1997 531e35d
check for squeeze axis tensor type, test=allcase
zhengqiwen1997 fce4037
fix bug, test=allcase
zhengqiwen1997 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2292,6 +2292,56 @@ def test_upsample(self): | |
self.assertEqual(out1.shape, [2, 3, 12, 12]) | ||
self.assertEqual(input_x.grad.shape, [2, 3, 6, 6]) | ||
|
||
def test_unstack(self): | ||
x1 = paddle.full([1], 0) | ||
x2 = paddle.full([2], 2) | ||
x1.retain_grads() | ||
x2.retain_grads() | ||
x1.stop_gradient = False | ||
x2.stop_gradient = False | ||
|
||
[out1] = paddle.unstack(x1, 0) | ||
out1.retain_grads() | ||
out1.backward() | ||
[out2_1, out2_2] = paddle.unstack(x2, 0) | ||
out2 = paddle.add_n([out2_1, out2_2]) | ||
out2.retain_grads() | ||
out2.backward() | ||
|
||
self.assertEqual(out1.shape, []) | ||
self.assertEqual(out1.numpy(), 0) | ||
|
||
self.assertEqual(out2_1.shape, []) | ||
self.assertEqual(out2_1.numpy(), 2) | ||
self.assertEqual(out2_2.shape, []) | ||
self.assertEqual(out2_2.numpy(), 2) | ||
self.assertEqual(x2.grad.shape, [2]) | ||
|
||
def test_unbind(self): | ||
x1 = paddle.full([1], 0) | ||
x2 = paddle.full([2], 2) | ||
x1.retain_grads() | ||
x2.retain_grads() | ||
x1.stop_gradient = False | ||
x2.stop_gradient = False | ||
|
||
[out1] = paddle.unbind(x1, 0) | ||
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. 这个API有反向,需要测一下反向。x1.grad/x2.grad |
||
out1.retain_grads() | ||
out1.backward() | ||
[out2_1, out2_2] = paddle.unbind(x2, 0) | ||
out2 = paddle.add_n([out2_1, out2_2]) | ||
out2.retain_grads() | ||
out2.backward() | ||
|
||
self.assertEqual(out1.shape, []) | ||
self.assertEqual(out1.numpy(), 0) | ||
|
||
self.assertEqual(out2_1.shape, []) | ||
self.assertEqual(out2_1.numpy(), 2) | ||
self.assertEqual(out2_2.shape, []) | ||
self.assertEqual(out2_2.numpy(), 2) | ||
self.assertEqual(x2.grad.shape, [2]) | ||
|
||
def test_maseked_select(self): | ||
x = paddle.rand([]) | ||
x.stop_gradient = False | ||
|
@@ -2306,6 +2356,26 @@ def test_maseked_select(self): | |
self.assertEqual(x.grad.shape, []) | ||
self.assertEqual(x.grad.numpy(), 1) | ||
|
||
def test_squeeze(self): | ||
x1 = paddle.full([], 2) | ||
x1.stop_gradient = False | ||
x1.retain_grads() | ||
out1 = paddle.squeeze(x1, axis=0) | ||
out1.retain_grads() | ||
out1.backward() | ||
self.assertEqual(out1.shape, []) | ||
self.assertEqual(x1.grad.shape, []) | ||
|
||
x2 = paddle.full([], 3) | ||
x3 = paddle.full([1], 0, dtype='int32') | ||
x2.stop_gradient = False | ||
x2.retain_grads() | ||
out2 = paddle.squeeze(x2, axis=x3) | ||
out2.retain_grads() | ||
out2.backward() | ||
self.assertEqual(out2.shape, []) | ||
self.assertEqual(x2.grad.shape, []) | ||
|
||
def test_unsqueeze(self): | ||
x1 = paddle.full([], 2) | ||
x1.stop_gradient = False | ||
|
@@ -4073,6 +4143,50 @@ def test_upsample(self): | |
self.assertEqual(res1[0].shape, (2, 3, 12, 12)) | ||
self.assertEqual(res1[1].shape, (2, 3, 6, 6)) | ||
|
||
@prog_scope() | ||
def test_unstack(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同静态图 |
||
x1 = paddle.full([1], 0, 'float32') | ||
x1.stop_gradient = False | ||
out1 = paddle.unstack(x1, 0) | ||
out1 = paddle.add_n(out1) | ||
paddle.static.append_backward(out1) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, (1,)) | ||
|
||
x2 = paddle.full([2], 2, 'float32') | ||
x2.stop_gradient = False | ||
out2 = paddle.unstack(x2, 0) | ||
out2_sum = paddle.add_n(out2) | ||
paddle.static.append_backward(out2_sum) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, (2,)) | ||
|
||
@prog_scope() | ||
def test_unbind(self): | ||
x1 = paddle.full([1], 0, 'float32') | ||
x1.stop_gradient = False | ||
out1 = paddle.unbind(x1, 0) | ||
out1 = paddle.add_n(out1) | ||
paddle.static.append_backward(out1) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, (1,)) | ||
|
||
x2 = paddle.full([2], 2, 'float32') | ||
x2.stop_gradient = False | ||
out2 = paddle.unbind(x2, 0) | ||
out2_sum = paddle.add_n(out2) | ||
paddle.static.append_backward(out2_sum) | ||
prog = paddle.static.default_main_program() | ||
res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, (2,)) | ||
|
||
@prog_scope() | ||
def test_maseked_select(self): | ||
x = paddle.rand([]) | ||
|
@@ -4089,6 +4203,34 @@ def test_maseked_select(self): | |
self.assertEqual(res[3].shape, ()) | ||
self.assertEqual(res[3], 1) | ||
|
||
@prog_scope() | ||
def test_squeeze(self): | ||
x1 = paddle.full([], 2) | ||
x1.stop_gradient = False | ||
out1 = paddle.squeeze(x1, axis=0) | ||
paddle.static.append_backward(out1.sum()) | ||
|
||
x2 = paddle.full([], 3) | ||
x3 = paddle.full([], 0, dtype='int32') | ||
x2.stop_gradient = False | ||
out2 = paddle.squeeze(x2, axis=x3) | ||
paddle.static.append_backward(out2.sum()) | ||
|
||
prog = paddle.static.default_main_program() | ||
res = self.exe.run( | ||
prog, | ||
fetch_list=[ | ||
out1, | ||
out2, | ||
x1.grad_name, | ||
x2.grad_name, | ||
], | ||
) | ||
self.assertEqual(res[0].shape, ()) | ||
self.assertEqual(res[1].shape, ()) | ||
self.assertEqual(res[2].shape, ()) | ||
self.assertEqual(res[3].shape, ()) | ||
|
||
@prog_scope() | ||
def test_unsqueeze(self): | ||
x1 = paddle.full([], 2) | ||
|
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.
这个API有反向,需要测一下反向。x1.grad/x2.grad