-
Notifications
You must be signed in to change notification settings - Fork 55
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.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20) #318
Conversation
Thanks for your contribution! |
针对 示例代码: import paddle
import numpy as np
import torch
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
result1 = paddle.signal.stft(n_fft=n_fft, x=x)
result1 = paddle.as_real(result1)
x1 = torch.tensor(data=input, dtype=torch.float)
n_fft = 400
result = x1.stft(n_fft=n_fft)
print(np.array_equal(result1.numpy(), result.numpy()) ) |
""" | ||
import torch | ||
x = torch.tensor([0., 1., 2., 3.],dtype=torch.float64) | ||
result = x.quantile(0.6) |
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.
第一个参数,指定下关键字吧,这四种情况的用例必须实现:
全部指定关键字、全部不指定关键字、改变关键字顺序、默认参数均不指定
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.
这些comment不修改吗
obj = APIBase("torch.Tensor.is_pinned") | ||
|
||
|
||
def test_case_1(): |
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.
加个GPU的单测,使用if torch.cuda.is_available():
判断下
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.
这个comment不修改吗
可以在 |
这个stfu的精度误差有点大,无法降低精度阈值要求来通过。 import paddle
import numpy as np
import torch
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
result1 = paddle.signal.stft(n_fft=n_fft, x=x)
result1 = paddle.as_real(result1)
x1 = torch.tensor(data=input, dtype=torch.float)
n_fft = 400
result = x1.stft(n_fft=n_fft)
print(np.allclose(result1.numpy(), result.numpy(),rtol=1e-01, atol=1e-01) )
# False
max_diff = np.max(result1.numpy() - result.numpy())
print(max_diff)
# 85.4296 |
那这个是API映射本身就有问题吧,或者说是不是有什么默认参数没有对齐?这个映射关系是否成立呢?抽出来单独实现这个 |
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.
stft的映射关系,是不是根本就是错的。另外其他的comment都没有修复
"torch.Tensor.quantile": {}, | ||
"torch.Tensor.quantile": { | ||
"Matcher": "GenericMatcher", | ||
"paddle_api": "paddle.Tensor.quantile", |
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.
"min_input_args"
配置了吗
obj = APIBase("torch.Tensor.is_pinned") | ||
|
||
|
||
def test_case_1(): |
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.
这个comment不修改吗
""" | ||
import torch | ||
x = torch.tensor([0., 1., 2., 3.],dtype=torch.float64) | ||
result = x.quantile(0.6) |
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.
这些comment不修改吗
在上一次 commit 中, 有修改相应的comment的。 |
对于 stft 查阅了一下资料后,发现该API目前仅支持numpy端的精度对齐,还无法对齐 pytorch 的精度。 相关资料: |
@@ -1473,7 +1473,9 @@ | |||
}, | |||
"torch.Tensor.is_inference": {}, | |||
"torch.Tensor.is_meta": {}, | |||
"torch.Tensor.is_pinned": {}, | |||
"torch.Tensor.is_pinned": { |
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.
torch.Tensor的API,都需要配置min_input_args
tests/test_Tensor_is_pinned.py
Outdated
""" | ||
import torch | ||
if torch.cuda.is_available(): | ||
x = torch.randn(4,4).cuda() |
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.
使用.pin_memory()
,测试更好点
已修改 |
@Li-fAngyU 42题全部完成了么 |
还有个 torch.stft 不知道该如何处理 |
https://github.com/PaddlePaddle/PaConvert/pull/301/files#diff-6bb398e612dfb5646bd931c9370571fbd52a8c9869da6c75b0d62da21213e6da |
好的感谢,我有参考过该PR,stft的问题应该是计算结果不一致,不知道可否像 torch.nn.CTCLoss 一样设置为功能缺失。 |
@Li-fAngyU 这个就不处理了,可能是Paddle自身的API问题,完成比率超过70%即视作题目结束。 |
好的! |
@Li-fAngyU 补充下:完成率超过70%达到黑客松发放奖金的条件,具体奖金数额与完成率相关。 |
ok |
import paddle
import numpy as np
import torch
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
result1 = paddle.signal.stft(n_fft=n_fft, x=x)
# result1 = paddle.as_real(result1)
x1 = torch.tensor(data=input, dtype=torch.float)
n_fft = 400
# result = torch.stft(input=x1, n_fft=n_fft, return_complex=True)
result = x1.stft(n_fft=n_fft, return_complex=True)
print(np.testing.assert_allclose(result1.numpy(), result.numpy(), rtol=1e-5, atol=1e-5)) stft requires the return_complex parameter be given for real inputs, and will further require that return_complex=True in a future PyTorch release. |
感谢提醒,经过再次验证当 示例代码: import paddle
import numpy as np
import torch
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
paddle_complex_result = paddle.signal.stft(n_fft=n_fft, x=x)
paddle_real_result = paddle.as_real(paddle_complex_result)
x1 = torch.tensor(data=input, dtype=torch.float)
n_fft = 400
torch_complex_result = x1.stft(n_fft=n_fft, return_complex=True)
torch_real_result = x1.stft(n_fft=n_fft, return_complex=False)
torch_transform_real_result = torch.view_as_real(torch_complex_result)
compare_complex = np.allclose(paddle_complex_result.numpy(), torch_complex_result.numpy(),rtol=1e-01, atol=1e-01)
compare_real = np.allclose(paddle_real_result.numpy(), torch_real_result.numpy(),rtol=1e-01, atol=1e-01)
compare_torch_real = np.allclose(torch_transform_real_result.numpy(), torch_real_result.numpy(),rtol=1e-01, atol=1e-01)
print(f'paddle vs. torch complex result:{compare_complex}')
print(f'paddle vs. torch real result:{compare_real}')
print(f'torch vs. torch real result:{compare_torch_real}')
# paddle vs. torch complex result:True
# paddle vs. torch real result:False
# torch vs. torch real result:True |
单独使用 import torch
import numpy as np
import paddle
np.random.seed(42)
x = np.random.rand(4000, ) + np.random.rand(4000, ) * 1j
x = x.astype(np.complex64)
x_t = torch.tensor(data=x)
y_t = torch.view_as_real(x_t)
x_p = paddle.to_tensor(data=x)
y_p = paddle.as_real(x_p)
print(np.testing.assert_allclose(y_t.numpy(), y_p.numpy(), rtol=1e-5, atol=1e-5))
在 import paddle
import numpy as np
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
res0 = paddle.signal.stft(n_fft=n_fft, x=x)
res_a = paddle.as_real(res0)
res_np = res0.numpy()
res_p = paddle.to_tensor(data=res_np)
res_b = paddle.as_real(res_p)
print(np.testing.assert_allclose(res_a.numpy(), res_b.numpy(), rtol=1e-5, atol=1e-5))
|
是的,这有点奇怪。 import paddle
import numpy as np
np.random.seed(42)
input = np.random.randn(4410)
x = paddle.to_tensor(data=input, dtype='float32')
n_fft = 400
res0 = paddle.signal.stft(n_fft=n_fft, x=x)
res_a = paddle.as_real(res0)
res_np = res0.numpy()
res_p = paddle.to_tensor(data=res_np)
res_b = paddle.as_real(res_p)
print(np.testing.assert_allclose(res0.numpy(), res_p.numpy(), rtol=1e-5, atol=1e-5))
print(np.testing.assert_allclose(res_a.numpy(), res_b.numpy(), rtol=1e-5, atol=1e-5))
|
PR Docs
PR APIs
其中,
torch.Tensor.to_sparse
直接使用了 PR 中的案例。