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.41】NO.41 为 Paddle 代码转换工具新增 API 转换规则(第 8 组) #6887

Merged
merged 21 commits into from
Oct 22, 2024

Conversation

decade-afk
Copy link
Contributor

No description provided.

@CLAassistant
Copy link

CLAassistant commented Sep 22, 2024

CLA assistant check
All committers have signed the CLA.

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.

第8组只有这一个API吗

@decade-afk
Copy link
Contributor Author

第8组只有这一个API吗
并不是,其他的还在弄

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Sep 24, 2024

第8组只有这一个API吗
并不是,其他的还在弄

那你在PR描述里,先标注一个 -PART1

@zhwesky2010 zhwesky2010 changed the title 【Hackathon 7th No.41】NO.41 为 Paddle 代码转换工具新增 API 转换规则(第 8 组) 【Hackathon 7th No.41】NO.41 为 Paddle 代码转换工具新增 API 转换规则(第 8 组)PRAT1 Sep 24, 2024
@@ -0,0 +1,7 @@
## [功能缺失 ]torch.distributions.constraints.Constraint
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个不是功能缺失,仔细查阅paddle源码,以源代码为准

@@ -0,0 +1,7 @@
## [ 功能缺失 ]torch.distributions.transforms.PositiveDefiniteTransform
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个找办法组合实现

infoflow 2024-09-24 17-17-53

判定为功能缺失需要慎重,只有确实需要在Paddle中新增API的才可判定功能缺失,尽可能要通过组合实现、或者paconvert的辅助函数aux_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.

好的好的,目前正在修改

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已经全都弄好了,请review

@decade-afk decade-afk changed the title 【Hackathon 7th No.41】NO.41 为 Paddle 代码转换工具新增 API 转换规则(第 8 组)PRAT1 【Hackathon 7th No.41】NO.41 为 Paddle 代码转换工具新增 API 转换规则(第 8 组) Sep 26, 2024
| --------- | ------------ | ------------------------------------------------------------------------------------ |
| median | loc | 输入 Tensor,仅参数名不一致。 |
| sigma | scale | 输入 Tensor,仅参数名不一致。 |
| * | - | 其他参数 |
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 27, 2024

Choose a reason for hiding this comment

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

* 表示后面为指定关键字参数,不代表某个形参。这个去学习下相关的python语法。

| ------- | ------------ | ----------------------------- |
| p | probs | 输入 Tensor,仅参数名不一致。 |

| * | - | 其他参数 |
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 27, 2024

Choose a reason for hiding this comment

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

同上,先查阅python相关语法

### [paddle.Tensor._is_inference](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/Tensor_cn.html)

```python
paddle.Tensor._is_inference
Copy link
Collaborator

Choose a reason for hiding this comment

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

infoflow 2024-09-27 16-09-55

有这个API??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

抱歉,这个是我搞错了,他是CompiledProgram类的属性


PaddlePaddle 目前无对应 API,可使用如下代码组合实现该 API。

### 参数映射
Copy link
Collaborator

Choose a reason for hiding this comment

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

组合实现有参数映射吗??认真查阅模板

| dim | dim | 表示输入的参数。 |
| concentration | concentration | 表示输入的参数。 |
| validate_args | - | 是否添加验证环节。Paddle 无此参数,一般对训练结果影响不大,可直接删除。 |
| - | sample_method | pytorch无此参数,保持默认即可。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

paddle保持默认即可

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.

开发注意事项:

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

@@ -0,0 +1,15 @@
## [ 无参数 ]torch.Tensor.is_inference
Copy link
Collaborator

Choose a reason for hiding this comment

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

加一个 torch.is_inference.md 的文档吧,这两个API是一样的

有个问题:这个看起来和stop_gradient不是完全一致的,如果没有更好的替代实现,就在文档里注明是近似实现吧,至少让读者知道存在差异
https://www.cnblogs.com/ToryRegulus/p/18141257

@@ -0,0 +1,22 @@
## [ 仅参数名不一致 ]torch.Tensor.geometric_
Copy link
Collaborator

Choose a reason for hiding this comment

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

torch参数更多

x.random_(from=0, to=10)

# Paddle 写法
paddle.assign(paddle.cast(paddle.randint(low=0, high=2, shape=x.shape), dtype='float32'), x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

paddle.Tensor.uniform_ 能否实现?

尽量找更好的组合

### [paddle.distributed.shard_optimizer](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/distributed/shard_optimizer_cn.html)

```python
paddle.distributed.shard_optimizer(optimizer, shard_fn=None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个和 paddle.distributed.fleet.distributed_optimizer 哪个更能对应上?

### [paddle.distributed.rpc.rpc_async](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/distributed/rpc/rpc_async_cn.html#rpc-async)

```python
paddle.distributed.rpc.rpc_async(to, fn, args=None, kwargs=None, timeout=- 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

timeout=-1)

paddle.distribution.constraint.Constraint()
```

功能一致,无参数。
Copy link
Collaborator

Choose a reason for hiding this comment

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

上面的torch签名是无参数??


PaddlePaddle 目前无对应 API,可使用如下代码组合实现该 API。

### 参数映射
Copy link
Collaborator

Choose a reason for hiding this comment

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

组合替代实现 不需要参数映射

@decade-afk
Copy link
Contributor Author

PaddlePaddle/PaConvert#495
我新提了一个pr,除了分布式的两个转写还没交之外,其他都写好了的,然后我根据别人写的辅助函数进行转写,到那时ci那里一直报paddle_aux没有该函数,请问这是怎么回事,我在本地测试是都通过了的。请review
@zhwesky2010

@decade-afk
Copy link
Contributor Author

image
就是这个样子,错的全是paddle_aux没有该函数

@zhwesky2010
Copy link
Collaborator

image 就是这个样子,错的全是paddle_aux没有该函数

单测里你要加一个 is_aux=True,你看看其他有辅助函数的单测写法


| PyTorch | PaddlePaddle | 备注 |
| --------- | ------------ | ------------------------------------------------------------------------------------ |
| median | loc | 输入 Tensor,仅参数名不一致。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

参数功能简述,是不是都不对

| PyTorch | PaddlePaddle | 备注 |
| --------- | ------------ | ------------------------------------------------------------------------------------ |
| median | loc | 输入 Tensor,仅参数名不一致。 |
| sigma | scale | 输入 Tensor,仅参数名不一致。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

参数功能简述,是不是都不对


| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------- |
| p | probs | 输入 Tensor,仅参数名不一致。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

参数功能简述,是不是都不对

paddle.Tensor.stop_gradient
```

两者功能一致,无参数。 `is_inference` 会强制关闭梯度记录。并且不能在中途设置梯度,`Paddle` 为近似实现。
Copy link
Collaborator

Choose a reason for hiding this comment

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

stop_gradient也解释下。你需要证明清楚是近似实现


| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------------------------------------- |
| from | min | 最小值 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

没写清楚

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ----------------------------------------------------------- |
| from | min | 最小值 |
| to | max | 最大值 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

没写清楚

| optimizer_class | optimizer | 优化器。 |
| params_rref | - | 初始化方法,paddle 无此参数,需要转写方式。 |
| args | - | 优化器实例化参数。 |
| kwargs | - | 优化器实例化参数 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个是字典参数

torch.is_inference(input)
```

Paddle 无此 API,需要组合是实现。 `is_inference` 会强制关闭梯度记录。并且不能在中途设置梯度,`Paddle` 为近似实现。
Copy link
Collaborator

Choose a reason for hiding this comment

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

写一下paddle的stop_gradient的功能及两者差异,证明是近似实现

@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.is_inference
Copy link
Collaborator

Choose a reason for hiding this comment

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

你这个写得和torch.Tensor.is_inference的分类和内容不一样,两者应该都是 无参数

y = PositiveDefiniteTransform()(tensor1)

# Paddle 写法
T = tensor1.tril(-1) + tensor1.diagonal(-2, -1).exp().diag_embed()
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个你还是用x来表示,不然与下面的.T容易混淆


# Paddle 写法
T = tensor1.tril(-1) + tensor1.diagonal(-2, -1).exp().diag_embed()
y = T @ T.mT
Copy link
Collaborator

Choose a reason for hiding this comment

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

paddle没有.mT

| concentration | concentration | 表示输入的参数。 |
| validate_args | - | 是否添加验证环节。Paddle 无此参数,一般对训练结果影响不大,可直接删除。 |
| - | sample_method | pytorch 无此参数,paddle 保持默认即可。 |
| 输出 | 输出 | pytorch 比 Paddle 多一个维度,需转写。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个写返回值


### 转写示例

```python
Copy link
Collaborator

Choose a reason for hiding this comment

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

转写值需要写:转写的是谁

参考下其他API返回值的转写

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------------------------------------- |
| df | df | 自由度,是一个正数。 |
| loc | loc | 分布的均值位置, Paddle 中,若 df 为 Tensor 类型,则 loc 也应该为 Tensor 类型。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里的描述不规范:

Paddle是什么样不重要,需要看torch是什么样,再看paddle的处理方案

| ------------- | ------------ | ------------------------------------------------------------------------------------- |
| df | df | 自由度,是一个正数。 |
| loc | loc | 分布的均值位置, Paddle 中,若 df 为 Tensor 类型,则 loc 也应该为 Tensor 类型。 |
| scale | scale | 分布的标准差的比例, Paddle 中,若 df 为 Tensor 类型,则 scale 也应该为 Tensor 类型。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里的描述不规范:

Paddle是什么样不重要,需要看torch是什么样,再看paddle的处理方案

paddle.Tensor.stop_gradient
```

两者功能一致,无参数。 `is_inference` 会强制关闭梯度记录。并且不能在中途设置梯度,而 `stop_gradient` 仅为停止计算该算子梯度,可在中途重新设为 `True` ,`Paddle` 为近似实现。
Copy link
Collaborator

Choose a reason for hiding this comment

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

给个转写示例

torch.is_inference(x)

# Paddle 写法
not x.stop_gradient
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个和torch.Tensor.is_inference应该要对上,映射分类转写示例都需要对上


| PyTorch | PaddlePaddle | 备注 |
| --------- | ------------ | ------------------------------------------------------------------------------------ |
| from | min | 随机数生成范围的起始值,仅参数名不一致。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

from这个参数名确定吗?这个应该触发了python的关键字from,应该会报错

@decade-afk
Copy link
Contributor Author

已经改了,请review

| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------------------------------------- |
| df | df | 自由度,是一个正数。 |
| loc | loc | 分布的均值位置, Pytorch 中,可为 float 或 Tensor 类型,但在 Paddle 中, loc 应于 df 同类型。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这段话有错别字

| ------------- | ------------ | ------------------------------------------------------------------------------------- |
| df | df | 自由度,是一个正数。 |
| loc | loc | 分布的均值位置, Pytorch 中,可为 float 或 Tensor 类型,但在 Paddle 中, loc 应于 df 同类型。 |
| scale | scale | 分布的标准差的比例, Pytorch 中,可为 float 或 Tensor 类型,但在 Paddle 中, scale 应于 df 同类型。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这段话有错别字

y = torch.distributions.lkj_cholesky.LKJCholesky(dim=3, concentration=torch.tensor([1.0])).sample()

# Paddle 写法
y = paddle.unsqueeze(paddle.distribution.LKJCholesky(dim=3, concentration=paddle.to_tensor([1.0]).sample(), axis=0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

可以分两行写


# Paddle 写法
x = paddle.to_tensor(3)
y = paddle.reshape(paddle.distribution.StudentT(df=x, loc = paddle.to_tensor(0.0), scale=paddle.to_tensor(1.0)).sample(), x.shape)
Copy link
Collaborator

Choose a reason for hiding this comment

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

可以分多行写,目前写的不够直观

@decade-afk
Copy link
Contributor Author

已经改好了,请review

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 dae1b4e into PaddlePaddle:develop Oct 22, 2024
2 checks passed
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