-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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 No.7】为 Paddle 新增 apply API -part #59374
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b4a04ea
add tensor apply
yangguohao 594fecb
fix
yangguohao 93473ee
fix 2023-11-27
yangguohao 64fddc4
fix
yangguohao 835e174
Merge branch 'develop' into apply_tensor
yangguohao 038ff03
fix V2
yangguohao 9a58dbf
Merge branch 'PaddlePaddle:develop' into apply_tensor
yangguohao 8ea22e4
add apply in Variable
yangguohao 5659933
add apply in newir
yangguohao dceeebd
Merge branch 'develop' into apply_tensor
yangguohao bd99a1d
add test
yangguohao 4931c16
Merge branch 'develop' into apply_tensor
yangguohao a4ccbaa
fix
yangguohao 7708eed
fix2
yangguohao 1d95b72
fix example code
yangguohao fca8389
change shape
yangguohao 13e2983
fix docs
yangguohao e5b8f61
fix docs
yangguohao 0c91d95
Merge branch 'develop' into apply_tensor
yangguohao 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
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,105 @@ | ||
# Copyright (c) 2023 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 unittest | ||
|
||
import numpy as np | ||
|
||
import paddle | ||
|
||
|
||
class TestTensorApplyAPI(unittest.TestCase): | ||
def setUp(self): | ||
self.x = paddle.to_tensor([1, 2, 3, 4, 5], stop_gradient=True) | ||
self.function = lambda x: 3 * x + 2 | ||
|
||
def test_dtype(self): | ||
for dtype in ["float64", "float16", "bfloat16"]: | ||
self.x.to(dtype) | ||
self.test_dygraph() | ||
|
||
@unittest.skipIf( | ||
not paddle.is_compiled_with_cuda(), | ||
"only support cuda", | ||
) | ||
def test_on_gpu(self): | ||
self.x.to("gpu") | ||
self.test_dygraph() | ||
|
||
def test_dygraph(self): | ||
y = self.x.apply(self.function) | ||
np.testing.assert_allclose( | ||
self.function(self.x).numpy(), y.numpy(), rtol=1e-05 | ||
) | ||
|
||
def test_error(self): | ||
self.x.stop_gradient = False | ||
|
||
def fn_inplace(x): | ||
x.apply_(self.function) | ||
|
||
def fn_outplace(x, func): | ||
x.apply(func) | ||
|
||
def function(x, y, z): | ||
return x + y + z | ||
|
||
self.assertRaises(RuntimeError, fn_inplace, self.x) | ||
self.assertRaises(RuntimeError, fn_outplace, self.x, self.function) | ||
with paddle.jit.api.sot_mode_guard(False): | ||
self.assertRaises( | ||
RuntimeError, | ||
paddle.jit.to_static(fn_outplace), | ||
self.x, | ||
self.function, | ||
) | ||
self.x.stop_gradient = True | ||
self.assertRaises( | ||
ValueError, | ||
paddle.jit.to_static(fn_outplace), | ||
self.x, | ||
function, | ||
) | ||
self.x.stop_gradient = False | ||
with paddle.pir_utils.IrGuard(): | ||
paddle.disable_static() | ||
self.assertRaises( | ||
RuntimeError, | ||
paddle.jit.to_static(fn_outplace), | ||
self.x, | ||
self.function, | ||
) | ||
|
||
def test_to_static(self): | ||
def fn(x, func): | ||
y = x.apply(func) | ||
return y | ||
|
||
with paddle.jit.api.sot_mode_guard(False): | ||
jit_g = paddle.jit.to_static(fn) | ||
out_legacy_ir = jit_g(self.x, self.function) | ||
with paddle.pir_utils.IrGuard(): | ||
paddle.disable_static() | ||
jit_g = paddle.jit.to_static(fn) | ||
out_pir = jit_g(self.x, self.function) | ||
np.testing.assert_allclose( | ||
self.function(self.x).numpy(), out_legacy_ir.numpy(), rtol=1e-05 | ||
) | ||
np.testing.assert_allclose( | ||
self.function(self.x).numpy(), out_pir.numpy(), rtol=1e-05 | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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.
we need test input tensor in both CPU and GPU
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.
are we support other data types? eg float16, bfloat16 and float64. if yes, should add more case in here
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 add the test case