-
Notifications
You must be signed in to change notification settings - Fork 756
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 7th No.38】为Paddle代码转换工具新增API转换规则(第5组)-part #6885
Conversation
inaomIIsfarell
commented
Sep 21, 2024
- 【Hackathon 7th】开源贡献个人挑战赛 Paddle#68244
@@ -0,0 +1,11 @@ | |||
## [功能缺失]torch.Tensor.positive |
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.
这个想办法组合实现吧,简单的功能,不用判定为功能缺失。只有必须新增API 的才被认定为功能缺失
### [torch.Tensor.scatter_reduce](https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_reduce.html#torch-tensor-scatter-reduce) | ||
|
||
```python | ||
Tensor.scatter_reduce(dim, index, src, reduce, *, include_self=True) |
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.
API签名要写全名:torch.Tensor.scatter_reduce
torch.can_cast(from_, to) | ||
``` | ||
|
||
判断类型的转换在 PyTorch 的[casting 规则](https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc)中是否被允许,暂未发现 Paddle 中有 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.
这个有无办法组合实现?
简单的功能,不用判定为功能缺失。只有必须新增API 的才被认定为功能缺失
Paddle也是支持类型提升的 https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/advanced/auto_type_promotion_cn.html
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.
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.
- 从源码看,pytorch的无符号整数的类型支持uint8,uint16,uint32,uint64,但paddle关于无符号整数的类型只支持uint8;
- paddle.Tensor.dtype共支持12种数据类型,我个人想到实现的方式是通过字典存储各类型的转换关系,通过转换前后的类型参数直接索引(事实上pytorch也是这么实现的)。但是这样代码行数太多,写在映射文档种是否不太合适?
### [paddle.concat](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/concat_cn.html) | ||
|
||
```python | ||
paddle.concat(x, axis=0, name=None) |
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.
这个就按API alias别名来处理吧,配置在 https://github.com/PaddlePaddle/PaConvert/blob/master/paconvert/api_alias_mapping.json ,无需新增映射文档。
torch.float_power(input, exponent, *, out=None) | ||
``` | ||
|
||
Paddle 无此 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.
你这个需要写两个转写示例,先写 正常情况下如何组合实现,再写 out如何转写
|
||
```python | ||
# PyTorch 写法 | ||
torch.float_pow(x, y, out) |
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.
API签名写错?
torch.positive(input) | ||
``` | ||
|
||
判断 `input` 是否是 bool 类型的 Tensor,如果是则抛出 RuntimeError 异常,否则返回 `input` 。 |
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.
同上,简单的功能尽可能找方法去组合实现,只有必须要Paddle 新增API的才计做功能缺失
} | ||
return can_cast_dict[from_][to] | ||
|
||
can_cast(x, y) |
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结果一致的
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_cast_dict
的 paddle 全部替换成 torch,和 torch.can_cast
的结果一致
import torch
can_cast_dict = {
torch.bfloat16: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False
},
torch.float16: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False,
},
torch.float32: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False,
},
torch.float64: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False,
},
torch.complex64: {
torch.bfloat16: False,
torch.float16: False,
torch.float32: False,
torch.float64: False,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False,
},
torch.complex128: {
torch.bfloat16: False,
torch.float16: False,
torch.float32: False,
torch.float64: False,
torch.complex64: True,
torch.complex128: True,
torch.uint8: False,
torch.int8: False,
torch.int16: False,
torch.int32: False,
torch.int64: False,
torch.bool: False,
},
torch.uint8: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: False,
},
torch.int8: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: False,
},
torch.int16: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: False,
},
torch.int32: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: False,
},
torch.int64: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: False,
},
torch.bool: {
torch.bfloat16: True,
torch.float16: True,
torch.float32: True,
torch.float64: True,
torch.complex64: True,
torch.complex128: True,
torch.uint8: True,
torch.int8: True,
torch.int16: True,
torch.int32: True,
torch.int64: True,
torch.bool: True,
}
}
for _from_dtype in can_cast_dict.keys():
for _to_dtype in can_cast_dict[_from_dtype].keys():
assert torch.can_cast(_from_dtype, _to_dtype) == can_cast_dict[_from_dtype][_to_dtype], "can_cast error"
print(f"_from_dtype={_from_dtype}, _to_dtype={_to_dtype}")
print("can_cast test pass")
torch.float_power(x, y) | ||
|
||
# Paddle 写法 | ||
paddle.pow(paddle.cast(x, paddle.float64), y) |
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.
paddle.pow(x.float64(), y)
是不是看起来简洁些
| dim | axis | 表示 scatter 的维度,仅参数名不一致。 | | ||
| index | indices | 表示输入的索引张量,仅参数名不一致。 | | ||
| src | values | 表示需要插入的值,仅参数名不一致。 | | ||
| reduce | reduce | 表示插入 values 时的计算方式,参数默认值不一致。PyTorch 中该参数无默认值,需要输入,Paddle 中默认值为 `assign`,应设置为与 PyTorch 一致。其中 PyTorch 的 `sum` 对应 Paddle 中的 `add`,PyTorch 的 `prod` 对应 Paddle 中 `multiply`。 | |
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.
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.
这个api不是我负责的,我记错了把这个也改了。。。sorry
但是这么写应该也没问题
torch.isneginf(a, out=b) | ||
|
||
# Paddle 写法 | ||
paddle.assign(paddle.isneginf(x), y) |
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.isposinf(a, out=b) | ||
|
||
# Paddle 写法 | ||
paddle.assign(paddle.isposinf(x), y) |
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.
不要改参数名,上下对应,这样读者容易比对差异
@inaomIIsfarell CI还没通过:
|
这个不懂怎么解决诶。。 |
你自己看看报错内容分析下,就是 torch.isin.md 这篇文档有问题,格式不对,mapping_type不在指定的选项里 |
开发注意事项:
|
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