-
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.27】为 Paddle 新增 select_scatter API -part #59343
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c52c91d
support select_scatter op
YibinLiu666 19c0ab4
fix example code
YibinLiu666 0816c09
fix sc
YibinLiu666 b927bcf
update example
YibinLiu666 06cf8b3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
YibinLiu666 3d1772c
remove unused files
YibinLiu666 aadc948
add name
YibinLiu666 131d251
fix conflict
YibinLiu666 6512ec8
update
YibinLiu666 d6ac9f6
remove
YibinLiu666 4573b2c
update
YibinLiu666 195247c
add type
YibinLiu666 cd2bc76
update type
YibinLiu666 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 |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import copy | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
from paddle.framework import core | ||
from paddle.pir_utils import test_with_pir_api | ||
|
||
paddle.enable_static() | ||
|
||
|
||
class TestSelectScatterAPI(unittest.TestCase): | ||
def setUp(self): | ||
np.random.seed(0) | ||
self.shape = [2, 3, 4] | ||
self.type = np.float32 | ||
self.x_np = np.random.random(self.shape).astype(self.type) | ||
self.place = [paddle.CPUPlace()] | ||
self.axis = 1 | ||
self.index = 1 | ||
self.value_shape = [2, 4] | ||
self.value_np = np.random.random(self.value_shape).astype(self.type) | ||
self.x_feed = copy.deepcopy(self.x_np) | ||
if core.is_compiled_with_cuda(): | ||
self.place.append(paddle.CUDAPlace(0)) | ||
|
||
def get_out_ref(self, out_ref, index, value_np): | ||
for i in range(2): | ||
for j in range(4): | ||
out_ref[i, index, j] = value_np[i, j] | ||
|
||
@test_with_pir_api | ||
def test_api_static(self): | ||
paddle.enable_static() | ||
|
||
def run(place): | ||
with paddle.static.program_guard(paddle.static.Program()): | ||
x = paddle.static.data('Src', self.shape, self.type) | ||
value = paddle.static.data( | ||
'Values', self.value_shape, self.type | ||
) | ||
out = paddle.select_scatter(x, value, self.axis, self.index) | ||
exe = paddle.static.Executor(place) | ||
res = exe.run( | ||
feed={ | ||
'Src': self.x_feed, | ||
'Values': self.value_np, | ||
}, | ||
fetch_list=[out], | ||
) | ||
|
||
out_ref = copy.deepcopy(self.x_np) | ||
self.get_out_ref(out_ref, self.index, self.value_np) | ||
for out in res: | ||
np.testing.assert_allclose(out, out_ref, rtol=0.001) | ||
|
||
for place in self.place: | ||
run(place) | ||
|
||
def test_api_dygraph(self): | ||
def run(place): | ||
paddle.disable_static(place) | ||
x_tensor = paddle.to_tensor(self.x_np) | ||
value_tensor = paddle.to_tensor(self.value_np) | ||
out = paddle.select_scatter( | ||
x_tensor, value_tensor, self.axis, self.index | ||
) | ||
out_ref = copy.deepcopy(self.x_np) | ||
self.get_out_ref(out_ref, self.index, self.value_np) | ||
np.testing.assert_allclose(out.numpy(), out_ref, rtol=0.001) | ||
|
||
paddle.enable_static() | ||
|
||
for place in self.place: | ||
run(place) | ||
|
||
|
||
class TestSelectScatterAPICase2(TestSelectScatterAPI): | ||
def setUp(self): | ||
np.random.seed(0) | ||
self.shape = [2, 3, 4, 5] | ||
self.type = np.float64 | ||
self.x_np = np.random.random(self.shape).astype(self.type) | ||
self.place = [paddle.CPUPlace()] | ||
self.axis = 2 | ||
self.index = 1 | ||
self.value_shape = [2, 3, 5] | ||
self.value_np = np.random.random(self.value_shape).astype(self.type) | ||
self.x_feed = copy.deepcopy(self.x_np) | ||
if core.is_compiled_with_cuda(): | ||
self.place.append(paddle.CUDAPlace(0)) | ||
|
||
def get_out_ref(self, out_ref, index, value_np): | ||
for i in range(2): | ||
for j in range(3): | ||
for k in range(5): | ||
out_ref[i, j, index, k] = value_np[i, j, k] | ||
|
||
|
||
class TestSelectScatterAPICase3(TestSelectScatterAPI): | ||
def setUp(self): | ||
np.random.seed(0) | ||
self.shape = [2, 3, 4, 5, 6] | ||
self.type = np.int32 | ||
self.x_np = np.random.random(self.shape).astype(self.type) | ||
self.place = [paddle.CPUPlace()] | ||
self.axis = 2 | ||
self.index = 1 | ||
self.value_shape = [2, 3, 5, 6] | ||
self.value_np = np.random.random(self.value_shape).astype(self.type) | ||
self.x_feed = copy.deepcopy(self.x_np) | ||
if core.is_compiled_with_cuda(): | ||
self.place.append(paddle.CUDAPlace(0)) | ||
|
||
def get_out_ref(self, out_ref, index, value_np): | ||
for i in range(2): | ||
for j in range(3): | ||
for k in range(5): | ||
for w in range(6): | ||
out_ref[i, j, index, k, w] = value_np[i, j, k, w] | ||
|
||
|
||
class TestSelectScatterAPIError(unittest.TestCase): | ||
def setUp(self): | ||
np.random.seed(0) | ||
self.shape = [2, 3, 4] | ||
self.x_np = np.random.random(self.shape).astype(np.float32) | ||
self.place = [paddle.CPUPlace()] | ||
self.axis = 1 | ||
self.index = 1 | ||
self.value_shape = [2, 4] | ||
self.value_np = np.random.random(self.value_shape).astype(np.float32) | ||
self.x_feed = copy.deepcopy(self.x_np) | ||
if core.is_compiled_with_cuda(): | ||
self.place.append(paddle.CUDAPlace(0)) | ||
|
||
def test_len_of_shape_not_equal_error(self): | ||
with self.assertRaises(RuntimeError): | ||
x_tensor = paddle.to_tensor(self.x_np) | ||
value_tensor = paddle.to_tensor(self.value_np).reshape((2, 2, 2)) | ||
res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) | ||
|
||
def test_one_of_size_not_equal_error(self): | ||
with self.assertRaises(RuntimeError): | ||
x_tensor = paddle.to_tensor(self.x_np) | ||
value_tensor = paddle.to_tensor([[2, 2], [2, 2]]).astype(np.float32) | ||
res = paddle.select_scatter(x_tensor, value_tensor, 1, 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
paddle.enable_static() | ||
unittest.main() |
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.
这里查错辛苦直接用
self.assertRaises
吧,否则后续如果某些修改导致try中语句能过了,这个单测会检测不到。此外,这两个case能拆成两个函数吗,命名上再清晰一些说明是检查的什么case
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.
Done