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 组) #495

Merged
merged 39 commits into from
Oct 22, 2024

Conversation

decade-afk
Copy link
Contributor

@decade-afk decade-afk commented Oct 14, 2024

PR Docs

PaddlePaddle/docs#6887

PR APIs

torch.distributed.rpc.remote
torch.distributed.optim.DistributedOptimizer
torch.distributions.chi2.Chi2
torch.is_inference
torch.distributions.poisson.Poisson
torch.distributions.transforms.PositiveDefiniteTransform
torch.distributions.studentT.StudentT
torch.distributions.lkj_cholesky.LKJCholesky
torch.distributions.constraints.Constraint
torch.distributions.gamma.Gamma
torch.Tensor.random_
torch.Tensor.is_inference
torch.Tensor.geometric_
torch.Tensor.cauchy_

Copy link

paddle-bot bot commented Oct 14, 2024

Thanks for your contribution!

@decade-afk
Copy link
Contributor Author

image
image
image
请问这是我哪里写错了吗,我是参考的上面的那个matcher写的,然后我在本地测试是通过的

@decade-afk
Copy link
Contributor Author

而且GPU那个测试还是通过的

@paddle-bot paddle-bot bot added the contributor External developers label Oct 14, 2024
@decade-afk
Copy link
Contributor Author

image
这个好像不能显示指定from关键字

@decade-afk
Copy link
Contributor Author

decade-afk commented Oct 15, 2024

你好,我发现写了辅助函数后,ci里面并不能检测到,但是我在本地是可以跑通的,请问这是怎么回事? @luotao1

@zhwesky2010
Copy link
Collaborator

写了辅助函数的单测需要加一个 is_aux_api=True

infoflow 2024-10-16 11-42-44

@@ -751,6 +751,147 @@ def get_paddle_nodes(self, args, kwargs):
for i in range(1, len(new_args)):
code = "{}({}, {})".format(self.get_paddle_api(), code, new_args[i])
return ast.parse(code).body


class StudentTMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是什么差异,需要重写Matcher。我也没有在映射文档中看到任何描述这两个API的差异,不能直接对上用GenericMatcher吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
就是paddle输入的参数需要都是tensor或者float

Copy link
Collaborator

Choose a reason for hiding this comment

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

image 就是paddle输入的参数需要都是tensor或者float

paddle是什么样的不重要,你要先看torch是什么样,再看paddle的处理方案。

这里我还是看不出两者的差异,你的映射文档写得不清楚,用户一眼看不出应如何转写

shape_list = list(range(x.ndim))
shape_list[-1], shape_list[-2] = shape_list[-2], shape_list[-1]
y = x.transpose(perm=shape_list)
return 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.

return x @ x.T 就行吧

x = x.tril(-1) + x.diagonal(axis1=-2, axis2=-1).exp().diag_embed()
shape_list = list(range(x.ndim))
shape_list[-1], shape_list[-2] = shape_list[-2], shape_list[-1]
y = x.transpose(perm=shape_list)
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就行

return API_TEMPLATE


class LKJCholeskyMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是什么差异,需要重写Matcher。我也没有在映射文档中看到任何描述这两个API的差异,不能直接对上用GenericMatcher吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有,输出的维度不一样,需要补上一个



class Is_InferenceMatcher(BaseMatcher):
def generate_aux_code(self):
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) -> x.stop_gradient
x.is_inference() -> x.stop_gradient

也不需要not吧,两者都为True时对应不需要梯度计算

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
Collaborator

Choose a reason for hiding this comment

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

没错没错

那你文档又为啥还是not的?

return code


class DistributionsConstrainMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是什么差异,需要重写Matcher。我也没有在映射文档中看到任何描述这两个API的差异,不能直接对上用GenericMatcher吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

差异是一个是__call__,一个用的是call,是为了封装

Copy link
Collaborator

Choose a reason for hiding this comment

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

差异是一个是__call__,一个用的是call,是为了封装

你在文档里需要写清楚差异

"""
import torch
input = torch.tensor([-0.6341, -1.4208, -1.0900, 0.5826])
result = input.cauchy_(median=0, sigma=1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

测一个case:全部不指定关键字?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test_case2就是全都不指定关键字的

"""
import torch
input = torch.tensor([-0.6341, -1.4208, -1.0900, 0.5826])
result = input.random_(0, to=5, generator=None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

测一个case:全部指定关键字?关键字乱序?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
from关键字不能显式指定,我在本地也不行

Copy link
Collaborator

Choose a reason for hiding this comment

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

image from关键字不能显式指定,我在本地也不行

这个from关键字确定吗?这个应该会触发错误,这个是python的关键字

@decade-afk
Copy link
Contributor Author

2255639de10866192e40038435d6321
**torch.distributed.optim.DistributedOptimizer(optimizer_class, params_rref, *args, kwargs)
这个好像转写不了,优化器需要的优化参数在pytorch里面是一个远程引用(RRef)列表,里面的rref类型代表优化参数,但是paddle里面是要直接获取优化参数

@zhwesky2010
Copy link
Collaborator

2255639de10866192e40038435d6321 **torch.distributed.optim.DistributedOptimizer(optimizer_class, params_rref, *args, kwargs) 这个好像转写不了,优化器需要的优化参数在pytorch里面是一个远程引用(RRef)列表,里面的rref类型代表优化参数,但是paddle里面是要直接获取优化参数

那这个映射是不是有问题?

@zhwesky2010
Copy link
Collaborator

@decade-afk 目前问题挺多的,你的映射文档和Matcher是很多diff对不齐的。先写清楚映射文档,再写Matcher,并保证两者是完全一致无diff的。

@decade-afk
Copy link
Contributor Author

2255639de10866192e40038435d6321 **torch.distributed.optim.DistributedOptimizer(optimizer_class, params_rref, *args, kwargs) 这个好像转写不了,优化器需要的优化参数在pytorch里面是一个远程引用(RRef)列表,里面的rref类型代表优化参数,但是paddle里面是要直接获取优化参数

那这个映射是不是有问题?

是指那里,他们功能是一样的,就是参数类型不对,那个docs文档还没有改

@decade-afk
Copy link
Contributor Author

已经改了,请review
@zhwesky2010

@@ -5067,6 +5194,36 @@ def generate_code(self, kwargs):
return code


class RpcRemoteMatcher(BaseMatcher):
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-10-22 15-33-27

我没有看出来为什么需要重写Matcher。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

因为rpc_async使用to_wait获取值,这个我改改文档

return code


class DistributionsConstrainMatcher(BaseMatcher):
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Oct 22, 2024

Choose a reason for hiding this comment

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

infoflow 2024-10-22 15-34-33

我没有看出来为什么需要重写Matcher?

即使重写,为何需要先定义一个func,在func里再包一个class,这不是多此一举吗?你包这两层的意义呢,直接定义一个class不更简洁吗

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

@decade-afk decade-afk Oct 22, 2024

Choose a reason for hiding this comment

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

不包装一下,直接调用check会报错

def generate_aux_code(self):
API_TEMPLATE = textwrap.dedent(
"""
import paddle
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Oct 22, 2024

Choose a reason for hiding this comment

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

你用func包class,包这两层的意义呢,直接定义一个class不更简洁吗

API_TEMPLATE = textwrap.dedent(
"""
import paddle
def StudentT_Aux_Func(df, loc, scale):
Copy link
Collaborator

Choose a reason for hiding this comment

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

你用func包class,包这两层的意义呢,直接定一个class不更简洁吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已经改好了,docs那里也同步修改了。

API_TEMPLATE = textwrap.dedent(
"""
remote_obj = paddle.distributed.rpc.rpc_async({})
paddle_aux.rpc_remote(remote_obj)
Copy link
Collaborator

Choose a reason for hiding this comment

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

都已经用上辅助函数了,建议直接写成一行。

remote_obj = paddle.distributed.rpc.rpc_async({}) 你直接放上面就行

@decade-afk
Copy link
Contributor Author

好的好的,已经修改了,ci也跑通了的,请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 616434d into PaddlePaddle:master Oct 22, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants