-
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 6th No. 7】Add sinc / sinc_ API to Paddle -part #63521
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
Returns: | ||
out (Tensor): The Tensor of elementwise computed normalized sinc result. | ||
""" | ||
if not isinstance(x, (paddle.Tensor, Variable, paddle.pir.Value)): |
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.
propose adding dtype judgment, also adding in test_sinc.py.
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.
added
python/paddle/tensor/math.py
Outdated
if not isinstance(x, (paddle.Tensor, Variable, paddle.pir.Value)): | ||
raise TypeError(f"x must be tensor type, but got {type(x)}") | ||
|
||
if x.dtype not in [ |
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.
maybe you can use "check_variable_and_dtype", this will make your code look more concise.
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.
revised
The CI-Coverage check for python code included the last two line of signbit API accidentally. |
@@ -487,6 +487,8 @@ | |||
signbit, | |||
sin, | |||
sin_, | |||
sinc, |
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.
also add it in the list of all
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.
added
res = paddle.sinc(x) | ||
static_result = exe.run(feed={'x': x_data}, fetch_list=[res])[0] | ||
|
||
def test_inplace(self): |
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.
the type error of inplace API also should be test.
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.
added
python/paddle/__init__.py
Outdated
@@ -803,7 +805,8 @@ | |||
'standard_gamma', | |||
'sinh', | |||
'sinh_', | |||
'round', | |||
'sinc', | |||
'sinc_' 'round', |
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.
attention code style 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.
oops..
fixed
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.
LGTM
check_variable_and_dtype( | ||
x, | ||
"x", | ||
[ |
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.
should we support bfloat16
?
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.
bfloat16
added
"sinc", | ||
) | ||
|
||
tmp = paddle.where(x != 0, x, paddle.full_like(x, 1.0e-20)) |
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.
why use 1.0e-20
, if data type of x
is float16, will there be underflow?
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.
1.0e-20
is used in numpy.sinc, but there is underflow problem when data type is float16
.
Fixed the problem using paddle.isnan
before returning the final value.
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.
Can we support backward gradient calculation, especially when x=0?
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 add test case for calculating gradient
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 add test case for calculating gradient
I will add it
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.
Can we support backward gradient calculation, especially when x=0?
when x=0
,the gradient should be 0:
by LHopital's rule,
The torch example:
x = torch.rand(2,3)
x[0,0] = 0.0
x.requires_grad = True
s = torch.sinc(x).sum()
s.backward()
x.grad
# tensor([[ 0.0000, -1.2824, -1.0302],
# [-1.3687, -1.0435, -1.1842]])
The current implementation using paddle.where
can return gradient value 0 if x = 0
check_variable_and_dtype( | ||
x, | ||
"x", | ||
[ |
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.
should we support bfloat16
?
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.
bfloat16
added
"sinc_", | ||
) | ||
|
||
paddle.where_(x != 0, x, paddle.full_like(x, 1.0e-20)) |
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.
same issue as above
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.
Fixed the problem using paddle.isnan
before returning the final value.
@NKNaN 看下这里的覆盖率,如果本地单测有覆盖到,请截图贴上。 |
@NKNaN 看下这里的覆盖率,如果本地单测有覆盖到,请截图贴上。 这个是 isreal 的内容,不知道为啥这三行也会算到 coverage 检测里面 |
收到,已经豁免了 |
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.
LGTM
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.
LGTM
PR Category
User Experience
PR Types
New features
Description
Add sinc / sinc_ API to Paddle