Skip to content
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

Merged
merged 18 commits into from
Oct 9, 2024

Conversation

inaomIIsfarell
Copy link
Contributor

@CLAassistant
Copy link

CLAassistant commented Sep 21, 2024

CLA assistant check
All committers have signed the CLA.

@@ -0,0 +1,11 @@
## [功能缺失]torch.Tensor.positive
Copy link
Collaborator

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)
Copy link
Collaborator

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 能实现该功能。
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要写一个方法实现逻辑功能,在api_matcher中通过generate_aux_code方法实现转写,文档的转写示例里直接把方法写出来吗?另外tensor和paddle的类型提升支持范围有差异。
paddle_about_cast
torch_about_cast

Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要写一个方法实现逻辑功能,在api_matcher中通过generate_aux_code方法实现转写,文档的转写示例里直接把方法写出来吗?另外tensor和paddle的类型提升支持范围有差异。 paddle_about_cast torch_about_cast

可以这样做,简单组合直接走辅助函数,然后辅助函数的逻辑也需要在映射文档中呈现

Copy link
Contributor Author

@inaomIIsfarell inaomIIsfarell Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 从源码看,pytorch的无符号整数的类型支持uint8,uint16,uint32,uint64,但paddle关于无符号整数的类型只支持uint8;
  2. 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)
Copy link
Collaborator

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,需要组合实现。
Copy link
Collaborator

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)
Copy link
Collaborator

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` 。
Copy link
Collaborator

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个情况比较多,需要核对下,是不是与Torch结果一致的

Copy link
Contributor Author

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)
Copy link
Collaborator

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`。 |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处 paddle 的 api 文档应该有误
params_in_docs
params_in_code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处 paddle 的 api 文档应该有误 params_in_docs params_in_code

以代码为准就行

Copy link
Contributor Author

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)
Copy link
Collaborator

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要改参数名,上下对应,这样读者容易比对差异

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Sep 29, 2024

@inaomIIsfarell CI还没通过:

2024-09-27 18:50:31 Exception: Unexpected End State at 9 in parsing file: /FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md, current meta: {'source_file': '/FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md', 'src_api': 'torch.isin', 'mapping_type': '?????????????????????', 'src_api_url': 'https://pytorch.org/docs/stable/generated/torch.isin.html#torch.isin', 'src_signature': {'api_name': 'torch.isin', 'args': [{'arg_name': 'elements', 'arg_default': None}, {'arg_name': 'test_elements', 'arg_default': None}, {'arg_name': '*', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}]}, 'dst_api': 'paddle.isin', 'dst_api_url': 'https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/isin_cn.html', 'dst_signature': {'api_name': 'paddle.isin', 'args': [{'arg_name': 'x', 'arg_default': None}, {'arg_name': 'test_x', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}, {'arg_name': 'name', 'arg_default': 'None'}]}}
2024-09-27 18:50:31 Error: API mapping generate script failed, please check changes in /FluidDoc/docs/guides/model_convert/convert_from_pytorch

@inaomIIsfarell
Copy link
Contributor Author

@inaomIIsfarell CI还没通过:

2024-09-27 18:50:31 Exception: Unexpected End State at 9 in parsing file: /FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md, current meta: {'source_file': '/FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md', 'src_api': 'torch.isin', 'mapping_type': '?????????????????????', 'src_api_url': 'https://pytorch.org/docs/stable/generated/torch.isin.html#torch.isin', 'src_signature': {'api_name': 'torch.isin', 'args': [{'arg_name': 'elements', 'arg_default': None}, {'arg_name': 'test_elements', 'arg_default': None}, {'arg_name': '*', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}]}, 'dst_api': 'paddle.isin', 'dst_api_url': 'https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/isin_cn.html', 'dst_signature': {'api_name': 'paddle.isin', 'args': [{'arg_name': 'x', 'arg_default': None}, {'arg_name': 'test_x', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}, {'arg_name': 'name', 'arg_default': 'None'}]}}
2024-09-27 18:50:31 Error: API mapping generate script failed, please check changes in /FluidDoc/docs/guides/model_convert/convert_from_pytorch

这个不懂怎么解决诶。。

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Oct 8, 2024

@inaomIIsfarell CI还没通过:

2024-09-27 18:50:31 Exception: Unexpected End State at 9 in parsing file: /FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md, current meta: {'source_file': '/FluidDoc/docs/guides/model_convert/convert_from_pytorch/tools/../api_difference/torch/torch.isin.md', 'src_api': 'torch.isin', 'mapping_type': '?????????????????????', 'src_api_url': 'https://pytorch.org/docs/stable/generated/torch.isin.html#torch.isin', 'src_signature': {'api_name': 'torch.isin', 'args': [{'arg_name': 'elements', 'arg_default': None}, {'arg_name': 'test_elements', 'arg_default': None}, {'arg_name': '*', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}]}, 'dst_api': 'paddle.isin', 'dst_api_url': 'https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/isin_cn.html', 'dst_signature': {'api_name': 'paddle.isin', 'args': [{'arg_name': 'x', 'arg_default': None}, {'arg_name': 'test_x', 'arg_default': None}, {'arg_name': 'assume_unique', 'arg_default': 'False'}, {'arg_name': 'invert', 'arg_default': 'False'}, {'arg_name': 'name', 'arg_default': 'None'}]}}
2024-09-27 18:50:31 Error: API mapping generate script failed, please check changes in /FluidDoc/docs/guides/model_convert/convert_from_pytorch

这个不懂怎么解决诶。。

你自己看看报错内容分析下,就是 torch.isin.md 这篇文档有问题,格式不对,mapping_type不在指定的选项里

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Oct 8, 2024

@inaomIIsfarell

开发注意事项:

  • 务必按照 映射文档模板 编写,一比一对照编写(可以增加内容但不能减少内容),提交review前先自查问题
  • 本次放出来的API,内部已初步分析过,基本上都有相关功能,功能缺失的可能性较小。需尽可能去寻找组合替代实现,判定为功能缺失要慎重,除非paddle完全无相类似功能
  • 先写好映射文档,再根据合入的文档来实现Matcher,注意不要出现文档与Matcher的diff。如果后面实现Matcher时,发现文档有误,需及时返工更正文档

Copy link
Collaborator

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhwesky2010 zhwesky2010 merged commit 9d29094 into PaddlePaddle:develop Oct 9, 2024
2 checks passed
@luotao1 luotao1 changed the title 【Hackathon 7th No.38】为Paddle代码转换工具新增API转换规则(第5组) 【Hackathon 7th No.38】为Paddle代码转换工具新增API转换规则(第5组)-part Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor PaddlePaddle Hackathon 飞桨黑客松活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants