-
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 No.8】add api hypot & hypot_ #57295
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dcd3779
add hypot
llyyxx0413 0dcf923
add init
llyyxx0413 6cdd5d1
fix test
llyyxx0413 75c1b90
Update test_inplace.py
llyyxx0413 d50c4e3
Update test_inplace.py
llyyxx0413 cee8e02
fix format error
llyyxx0413 b88d589
Update test_inplace.py
llyyxx0413 1d40d25
add type_error test
llyyxx0413 bf8852a
add type error test
llyyxx0413 ff8085e
fix value error to type error
llyyxx0413 83ecda7
Update math.py
llyyxx0413 7434f2e
merge develop
llyyxx0413 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,98 @@ | ||
# 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 | ||
from paddle import base | ||
from paddle.base import core | ||
|
||
paddle.enable_static() | ||
|
||
|
||
class TestHypotAPI(unittest.TestCase): | ||
def setUp(self): | ||
self.x_shape = [10, 10] | ||
self.y_shape = [10, 1] | ||
self.x_np = np.random.uniform(-10, 10, self.x_shape).astype(np.float32) | ||
self.y_np = np.random.uniform(-10, 10, self.y_shape).astype(np.float32) | ||
|
||
def test_static_graph(self): | ||
paddle.enable_static() | ||
startup_program = base.Program() | ||
train_program = base.Program() | ||
with base.program_guard(startup_program, train_program): | ||
x = paddle.static.data( | ||
name='input1', dtype='float32', shape=self.x_shape | ||
) | ||
y = paddle.static.data( | ||
name='input2', dtype='float32', shape=self.y_shape | ||
) | ||
out = paddle.hypot(x, y) | ||
|
||
place = ( | ||
base.CUDAPlace(0) | ||
if core.is_compiled_with_cuda() | ||
else base.CPUPlace() | ||
) | ||
exe = base.Executor(place) | ||
res = exe.run( | ||
base.default_main_program(), | ||
feed={'input1': self.x_np, 'input2': self.y_np}, | ||
fetch_list=[out], | ||
) | ||
np_out = np.hypot(self.x_np, self.y_np) | ||
np.testing.assert_allclose(res[0], np_out, atol=1e-5, rtol=1e-5) | ||
paddle.disable_static() | ||
|
||
def test_dygraph(self): | ||
paddle.disable_static() | ||
x = paddle.to_tensor(self.x_np) | ||
y = paddle.to_tensor(self.y_np) | ||
result = paddle.hypot(x, y) | ||
np.testing.assert_allclose( | ||
np.hypot(self.x_np, self.y_np), result.numpy(), rtol=1e-05 | ||
) | ||
|
||
paddle.enable_static() | ||
|
||
|
||
class TestHypotAPIBroadCast(TestHypotAPI): | ||
def setUp(self): | ||
self.x_np = np.arange(6).astype(np.float32) | ||
self.y_np = np.array([20]).astype(np.float32) | ||
self.x_shape = [6] | ||
self.y_shape = [1] | ||
|
||
|
||
class TestHypotAPI3(TestHypotAPI): | ||
def setUp(self): | ||
self.x_shape = [] | ||
self.y_shape = [] | ||
self.x_np = np.random.uniform(-10, 10, self.x_shape).astype(np.float32) | ||
self.y_np = np.random.uniform(-10, 10, self.y_shape).astype(np.float32) | ||
|
||
|
||
class TestHypotAPI4(TestHypotAPI): | ||
def setUp(self): | ||
self.x_shape = [1] | ||
self.y_shape = [1] | ||
self.x_np = np.random.uniform(-10, 10, self.x_shape).astype(np.float32) | ||
self.y_np = np.random.uniform(-10, 10, self.y_shape).astype(np.float32) | ||
|
||
|
||
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.
inplace API should be with decorator of
@inplace_apis_in_dygraph_only