-
Notifications
You must be signed in to change notification settings - Fork 5.9k
【Hackathon 6th No.10】Add isposinf / isneginf / isreal API to Paddle - part #63523
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0b92112
add hack10 part
NKNaN 2d1b227
update fp16
NKNaN 9df7bf7
update
NKNaN 38a2580
fix fp16 test
NKNaN bdfa3f3
update en docs
NKNaN 3581c46
add bf16
NKNaN 39aaa33
update docs
NKNaN 049b0f6
fix test
NKNaN 7eb9a53
rerun ci
NKNaN ed2d7e7
update fp16
NKNaN f5cef59
fix fp16 test
NKNaN 08e1f2f
add bf16
NKNaN 54a227e
update docs
NKNaN d7f689e
fix test
NKNaN 98824f0
rerun ci
NKNaN 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7728,3 +7728,146 @@ def signbit(x, name=None): | |
| x = paddle.sign(neg_zero_x) | ||
| out = paddle.cast(x < 0, dtype='bool') | ||
| return out | ||
|
|
||
|
|
||
| def isposinf(x, name=None): | ||
| r""" | ||
| Tests if each element of input is positive infinity or not. | ||
|
|
||
| Args: | ||
| x (Tensor): The input Tensor. Must be one of the following types: bfloat16, float16, float32, float64, int8, int16, int32, int64, uint8. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Returns: | ||
| out (Tensor): The output Tensor. Each element of output indicates whether the input element is positive infinity or not. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
| >>> paddle.set_device('cpu') | ||
| >>> x = paddle.to_tensor([-0., float('inf'), -2.1, -float('inf'), 2.5], dtype='float32') | ||
| >>> res = paddle.isposinf(x) | ||
| >>> print(res) | ||
| Tensor(shape=[5], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [False, True, False, False, False]) | ||
|
|
||
| """ | ||
| if not isinstance(x, (paddle.Tensor, Variable, paddle.pir.Value)): | ||
| raise TypeError(f"x must be tensor type, but got {type(x)}") | ||
|
|
||
| check_variable_and_dtype( | ||
| x, | ||
| "x", | ||
| [ | ||
| 'bfloat16', | ||
| 'float16', | ||
| 'float32', | ||
| 'float64', | ||
| 'int8', | ||
| 'int16', | ||
| 'int32', | ||
| 'int64', | ||
| 'uint8', | ||
| ], | ||
| "isposinf", | ||
| ) ## dtype is the intersection of dtypes supported by isinf and signbit | ||
| is_inf = paddle.isinf(x) | ||
| signbit = ~paddle.signbit(x) | ||
| return paddle.logical_and(is_inf, signbit) | ||
|
|
||
|
|
||
| def isneginf(x, name=None): | ||
| r""" | ||
| Tests if each element of input is negative infinity or not. | ||
|
|
||
| Args: | ||
| x (Tensor): The input Tensor. Must be one of the following types: bfloat16, float16, float32, float64, int8, int16, int32, int64, uint8. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Returns: | ||
| out (Tensor): The output Tensor. Each element of output indicates whether the input element is negative infinity or not. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
| >>> paddle.set_device('cpu') | ||
| >>> x = paddle.to_tensor([-0., float('inf'), -2.1, -float('inf'), 2.5], dtype='float32') | ||
| >>> res = paddle.isneginf(x) | ||
| >>> print(res) | ||
| Tensor(shape=[5], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [False, False, False, True, False]) | ||
|
|
||
| """ | ||
| if not isinstance(x, (paddle.Tensor, Variable, paddle.pir.Value)): | ||
| raise TypeError(f"x must be tensor type, but got {type(x)}") | ||
|
|
||
| check_variable_and_dtype( | ||
| x, | ||
| "x", | ||
| [ | ||
|
Contributor
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. shall we support data type of
Contributor
Author
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. yes. |
||
| 'bfloat16', | ||
| 'float16', | ||
| 'float32', | ||
| 'float64', | ||
| 'int8', | ||
| 'int16', | ||
| 'int32', | ||
| 'int64', | ||
| 'uint8', | ||
| ], | ||
| "isneginf", | ||
| ) | ||
| is_inf = paddle.isinf(x) | ||
| signbit = paddle.signbit(x) | ||
| return paddle.logical_and(is_inf, signbit) | ||
|
|
||
|
|
||
| def isreal(x, name=None): | ||
| r""" | ||
| Tests if each element of input is a real number or not. | ||
|
|
||
| Args: | ||
| x (Tensor): The input Tensor. | ||
| name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
|
||
| Returns: | ||
| out (Tensor): The output Tensor. Each element of output indicates whether the input element is a real number or not. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| >>> import paddle | ||
| >>> paddle.set_device('cpu') | ||
| >>> x = paddle.to_tensor([-0., -2.1, 2.5], dtype='float32') | ||
| >>> res = paddle.isreal(x) | ||
| >>> print(res) | ||
| Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [True, True, True]) | ||
|
|
||
| >>> x = paddle.to_tensor([(-0.+1j), (-2.1+0.2j), (2.5-3.1j)]) | ||
| >>> res = paddle.isreal(x) | ||
| >>> print(res) | ||
| Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [False, False, False]) | ||
|
|
||
| >>> x = paddle.to_tensor([(-0.+1j), (-2.1+0j), (2.5-0j)]) | ||
| >>> res = paddle.isreal(x) | ||
| >>> print(res) | ||
| Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, | ||
| [False, True, True]) | ||
| """ | ||
| if not isinstance(x, (paddle.Tensor, Variable, paddle.pir.Value)): | ||
| raise TypeError(f"x must be tensor type, but got {type(x)}") | ||
| dtype = x.dtype | ||
| is_real_dtype = not ( | ||
| dtype == core.VarDesc.VarType.COMPLEX64 | ||
| or dtype == core.VarDesc.VarType.COMPLEX128 | ||
| or dtype == core.DataType.COMPLEX64 | ||
| or dtype == core.DataType.COMPLEX128 | ||
| ) | ||
| if is_real_dtype: | ||
| return paddle.ones_like(x, dtype='bool') | ||
|
|
||
| return paddle.equal(paddle.imag(x), 0) | ||
This file contains hidden or 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
Oops, something went wrong.
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.
shall we support data type of
bf16?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.
yes.
bf16 type added.