-
Notifications
You must be signed in to change notification settings - Fork 823
【Hackathon 7th No.38】为Paddle代码转换工具新增API转换规则(第5组)-part #6885
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
18 commits
Select commit
Hold shift + click to select a range
a9373ff
【Hackathon 7th No.38】为 Paddle 代码转换工具新增 API 转换规则(第5组)
inaomIIsfarell 84fb9f7
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into hkt1
inaomIIsfarell 17e10a0
fix docs
inaomIIsfarell 7697816
fix
inaomIIsfarell 8c0edfb
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into hkt1
inaomIIsfarell 940ec07
fix
inaomIIsfarell 1a76e87
update_param_exp
inaomIIsfarell 571d1e8
fix
inaomIIsfarell 23bb1bd
fix
inaomIIsfarell de8ac23
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into hkt1
inaomIIsfarell eacf377
fix
inaomIIsfarell b66bfd3
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into hkt1
inaomIIsfarell abd6f06
fix_
inaomIIsfarell a9d83e4
Merge branch 'develop' of https://github.com/PaddlePaddle/docs into hkt1
inaomIIsfarell b81e406
fix
inaomIIsfarell 2c23071
fix
inaomIIsfarell 36695d1
fix
inaomIIsfarell fffb285
fix
inaomIIsfarell 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
15 changes: 15 additions & 0 deletions
15
...del_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.isneginf.md
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## [无参数]torch.Tensor.isneginf | ||
|
||
### [torch.Tensor.isneginf ](https://pytorch.org/docs/stable/generated/torch.Tensor.isneginf.html#torch.Tensor.isneginf) | ||
|
||
```python | ||
torch.Tensor.isneginf() | ||
``` | ||
|
||
### [paddle.Tensor.isneginf](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#isneginf-name-none) | ||
|
||
```python | ||
paddle.Tensor.isneginf(name=None) | ||
``` | ||
|
||
两者功能一致,无参数。 |
15 changes: 15 additions & 0 deletions
15
...del_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.isposinf.md
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## [无参数]torch.Tensor.isposinf | ||
|
||
### [torch.Tensor.isposinf](https://pytorch.org/docs/stable/generated/torch.Tensor.isposinf.html#torch.Tensor.isposinf) | ||
|
||
```python | ||
torch.Tensor.isposinf() | ||
``` | ||
|
||
### [paddle.Tensor.isposinf](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#isposinf-name-none) | ||
|
||
```python | ||
paddle.Tensor.isposinf(name=None) | ||
``` | ||
|
||
两者功能一致,无参数。 |
15 changes: 15 additions & 0 deletions
15
...model_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.isreal.md
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## [无参数]torch.Tensor.isreal | ||
|
||
### [torch.Tensor.isreal](https://pytorch.org/docs/stable/generated/torch.Tensor.isreal.html#torch.Tensor.isreal) | ||
|
||
```python | ||
torch.Tensor.isreal() | ||
``` | ||
|
||
### [paddle.Tensor.isreal](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#isreal-name-none) | ||
|
||
```python | ||
paddle.Tensor.isreal(name=None) | ||
``` | ||
|
||
两者功能一致,无参数。 |
27 changes: 27 additions & 0 deletions
27
...del_convert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.positive.md
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## [组合替代实现]torch.Tensor.positive | ||
|
||
### [torch.Tensor.positive](https://pytorch.org/docs/stable/generated/torch.Tensor.positive.html#torch.Tensor.positive) | ||
|
||
```python | ||
torch.Tensor.positive() | ||
``` | ||
|
||
判断 `input` 是否是 bool 类型的 Tensor,如果是则抛出 RuntimeError 异常,否则返回 `input` 。 | ||
|
||
Paddle 无此 API,需要组合实现。 | ||
|
||
### 转写示例 | ||
|
||
```python | ||
# PyTorch 写法 | ||
x.positive() | ||
|
||
# Paddle 写法 | ||
def positive(x): | ||
if x.dtype != paddle.bool: | ||
return x | ||
else: | ||
raise RuntimeError("boolean tensors is not supported.") | ||
|
||
positive(x) | ||
``` |
26 changes: 26 additions & 0 deletions
26
...nvert/convert_from_pytorch/api_difference/Tensor/torch.Tensor.scatter_reduce.md
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## [paddle 参数更多]torch.Tensor.scatter_reduce | ||
|
||
### [torch.Tensor.scatter_reduce](https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_reduce.html#torch-tensor-scatter-reduce) | ||
|
||
```python | ||
torch.Tensor.scatter_reduce(dim, index, src, reduce, *, include_self=True) | ||
``` | ||
|
||
### [paddle.Tensor.put_along_axis](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#put-along-axis-indices-value-axis-reduce-assign-include-self-true-broadcast-true) | ||
|
||
```python | ||
paddle.Tensor.put_along_axis(indices, values, axis, reduce="assign", include_self=True, broadcast=True) | ||
``` | ||
|
||
其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下: | ||
|
||
### 参数映射 | ||
|
||
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------ | ------------ | ------------------------------------------------------------ | | ||
| dim | axis | 表示 scatter 的维度,仅参数名不一致。 | | ||
| index | indices | 表示输入的索引张量,仅参数名不一致。 | | ||
| src | values | 表示需要插入的值,仅参数名不一致。 | | ||
| reduce | reduce | 表示插入 values 时的计算方式,参数默认值不一致。PyTorch 中该参数无默认值,需要输入,Paddle 中默认值为 `assign`,应设置为与 PyTorch 一致。其中 PyTorch 的 `sum` 对应 Paddle 中的 `add`,PyTorch 的 `prod` 对应 Paddle 中 `multiply`。 | | ||
| include_self | include_self | 表示插入 values 时是否包含输入元素中的值。 | | ||
| - | broadcast | 表示是否需要广播索引张量矩阵,PyTorch 无此参数,Paddle 应设置为 `False` 才与 PyTorch 一致 | |
33 changes: 33 additions & 0 deletions
33
...des/model_convert/convert_from_pytorch/api_difference/torch/torch.block_diag.md
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## [输入参数类型不一致]torch.block_diag | ||
|
||
### [torch.block_diag](https://pytorch.org/docs/stable/generated/torch.block_diag.html#torch-block-diag) | ||
|
||
```python | ||
torch.block_diag(*tensors) | ||
``` | ||
|
||
### [paddle.block_diag](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/block_diag_cn.html) | ||
|
||
```python | ||
paddle.block_diag(inputs, name=None) | ||
``` | ||
|
||
二者功能一致但参数类型不一致,具体如下: | ||
|
||
### 参数映射 | ||
|
||
| PyTorch | PaddlePaddle | 备注 | | ||
| -------- | ------------ | ------------------------------------------------------------ | | ||
| *tensors | inputs | 一组输入 Tensor,PyTorch 参数 tensors 为可变参数,Paddle 参数 inputs 为 list(Tensor) 或 tuple(Tensor) 的形式。 | | ||
|
||
### 转写示例 | ||
|
||
#### *tensors:一组输入 Tensor | ||
|
||
```python | ||
# PyTorch 写法 | ||
torch.block_diag(x, y, z) | ||
|
||
# Paddle 写法 | ||
paddle.block_diag([x, y, z]) | ||
``` |
194 changes: 194 additions & 0 deletions
194
...uides/model_convert/convert_from_pytorch/api_difference/torch/torch.can_cast.md
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 |
---|---|---|
@@ -0,0 +1,194 @@ | ||
## [组合替代实现]torch.can_cast | ||
|
||
### [torch.can_cast](https://pytorch.org/docs/stable/generated/torch.can_cast.html#torch-can-cast) | ||
|
||
```python | ||
torch.can_cast(from_, to) | ||
``` | ||
|
||
判断类型的转换在 PyTorch 的[casting 规则](https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc)中是否被允许。 | ||
|
||
Paddle 无此 API,需要组合实现。 | ||
|
||
### 转写示例 | ||
|
||
```python | ||
# PyTorch 写法 | ||
torch.can_cast(x, y) | ||
|
||
# Paddle 写法 | ||
def can_cast(from_, to): | ||
can_cast_dict = { | ||
paddle.bfloat16: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False | ||
}, | ||
paddle.float16: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False, | ||
}, | ||
paddle.float32: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False, | ||
}, | ||
paddle.float64: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False, | ||
}, | ||
paddle.complex64: { | ||
paddle.bfloat16: False, | ||
paddle.float16: False, | ||
paddle.float32: False, | ||
paddle.float64: False, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False, | ||
}, | ||
paddle.complex128: { | ||
paddle.bfloat16: False, | ||
paddle.float16: False, | ||
paddle.float32: False, | ||
paddle.float64: False, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: False, | ||
paddle.int8: False, | ||
paddle.int16: False, | ||
paddle.int32: False, | ||
paddle.int64: False, | ||
paddle.bool: False, | ||
}, | ||
paddle.uint8: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: False, | ||
}, | ||
paddle.int8: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: False, | ||
}, | ||
paddle.int16: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: False, | ||
}, | ||
paddle.int32: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: False, | ||
}, | ||
paddle.int64: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: False, | ||
}, | ||
paddle.bool: { | ||
paddle.bfloat16: True, | ||
paddle.float16: True, | ||
paddle.float32: True, | ||
paddle.float64: True, | ||
paddle.complex64: True, | ||
paddle.complex128: True, | ||
paddle.uint8: True, | ||
paddle.int8: True, | ||
paddle.int16: True, | ||
paddle.int32: True, | ||
paddle.int64: True, | ||
paddle.bool: True, | ||
} | ||
} | ||
return can_cast_dict[from_][to] | ||
|
||
can_cast(x, y) | ||
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. 这个情况比较多,需要核对下,是不是与Torch结果一致的 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. 我写了一个测试代码,把示例中 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") |
||
``` |
33 changes: 33 additions & 0 deletions
33
...model_convert/convert_from_pytorch/api_difference/torch/torch.cartesian_prod.md
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## [输入参数类型不一致]torch.cartesian_prod | ||
|
||
### [torch.cartesian_prod](https://pytorch.org/docs/stable/generated/torch.cartesian_prod.html#torch-cartesian-prod) | ||
|
||
```python | ||
torch.cartesian_prod(*tensors) | ||
``` | ||
|
||
### [paddle.cartesian_prod](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/cartesian_prod_cn.html) | ||
|
||
```python | ||
paddle.cartesian_prod(x, name=None) | ||
``` | ||
|
||
两者功能一致但参数类型不一致,具体如下: | ||
|
||
### 参数映射 | ||
|
||
| PyTorch | PaddlePaddle | 备注 | | ||
| -------- | ------------ | ------------------------------------------------------------ | | ||
| *tensors | x | 一组输入 Tensor , PyTorch 参数 tensors 为可变参, Paddle 参数 x 为 list(Tensor) 或 tuple(Tensor) 的形式。 | | ||
|
||
### 转写示例 | ||
|
||
#### *tensors:一组输入 Tensor | ||
|
||
```python | ||
# PyTorch 写法 | ||
torch.cartesian_prod(a, b) | ||
|
||
# Paddle 写法 | ||
paddle.cartesian_prod([a, b]) | ||
``` |
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.
此处 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.
以代码为准就行