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

转换规则 No. 114-120 #122

Merged
merged 5 commits into from
Jul 20, 2023
Merged

转换规则 No. 114-120 #122

merged 5 commits into from
Jul 20, 2023

Conversation

co63oc
Copy link
Contributor

@co63oc co63oc commented Jun 23, 2023

PR Docs

#112

已有映射文档增加转换规则
torch.autograd.backward
torch.autograd.functional.jacobian
torch.autograd.functional.hessian
torch.autograd.functional.vjp
torch.autograd.functional.jvp

115 torch.autograd.grad # 已存在

torch.cuda.device 文档 PaddlePaddle/docs#5966

PR APIs

torch.autograd.backward
torch.autograd.functional.jacobian
torch.autograd.functional.hessian
torch.autograd.functional.vjp
torch.autograd.functional.jvp
torch.cuda.device

[used AI Studio]

@paddle-bot
Copy link

paddle-bot bot commented Jun 23, 2023

Thanks for your contribution!

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Jun 23, 2023
@co63oc co63oc changed the title 转换规则 No. 114 转换规则 No. 114-120 Jun 25, 2023
paconvert/api_mapping.json Outdated Show resolved Hide resolved
tests/test_cuda_device.py Outdated Show resolved Hide resolved
paconvert/api_matcher.py Outdated Show resolved Hide resolved
tests/test_autograd_grad.py Outdated Show resolved Hide resolved
tests/apibase.py Outdated Show resolved Hide resolved
@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Jun 26, 2023
@co63oc co63oc force-pushed the doc0622 branch 3 times, most recently from 2f9eae0 to 535290c Compare June 27, 2023 06:32
@co63oc co63oc force-pushed the doc0622 branch 2 times, most recently from b7f41e8 to 01585fa Compare June 27, 2023 11:48
paconvert/api_matcher.py Outdated Show resolved Hide resolved
@luotao1 luotao1 self-assigned this Jun 29, 2023
@co63oc co63oc force-pushed the doc0622 branch 4 times, most recently from efb4795 to 3aabfd9 Compare July 4, 2023 11:49
ROckDog22
ROckDog22 previously approved these changes Jul 4, 2023
device = kwargs["device"]
if (
"replace('cuda', 'gpu')," in device
or 'replace("cuda", "gpu"),' in device
Copy link
Contributor

Choose a reason for hiding this comment

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

我觉得不用判断device是不是数字,而是直接转化为静态代码判断torch.device: int-> str().replace('cuda', '')这样是不是代码简单一些

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不判断数字那转换后的代码增加了逻辑

else:
return None
elif (
"replace('cuda', 'gpu')" in device or 'replace("cuda", "gpu")' in device
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Jul 5, 2023

Choose a reason for hiding this comment

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

  1. 这个判断比较多有些复杂,有办法统一成一个吗

  2. 下面的return None表示不支持,什么情况下会不支持呢

Copy link
Contributor Author

@co63oc co63oc Jul 5, 2023

Choose a reason for hiding this comment

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

如果不包含字符串replace('cuda',并且不是数字,那是其他不能识别的场景,返回None不支持

Copy link
Collaborator

Choose a reason for hiding this comment

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

总共就两个判断,目前感觉代码可读性不太好

if nums:
    pass
elif 'replace' in device:
    pass
else:
    return None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有以下几种情况
1 数字参数 torch.cuda.device(device=0) paddle不使用id=,是kwargs参数转为args参数
2 不是数字,包含index参数 torch.cuda.device(torch.device("cuda", 0)),转为 str('cuda').replace('cuda', 'gpu'), index=1
3 不是数字,不包含index参数 torch.cuda.device(torch.device("cuda")), 转为 str('cuda').replace('cuda', 'gpu')
4 其他输入,不是数字也不包含 torch.cuda.device(torch.device("cpu"))


args = []
new_kwargs = {}
for ele in kwargs:
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Jul 5, 2023

Choose a reason for hiding this comment

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

这里有办法不去改动GenericMatcher公共函数吗

Copy link
Contributor Author

@co63oc co63oc Jul 5, 2023

Choose a reason for hiding this comment

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

是paddle使用id,paddle没有.index调用,比如 torch.device("cuda").index 这样不能转 paddle,如果是int参数没有名称,比如id=0,paddle没有paddle.CUDAPlace(id=0),需要使用paddle.CUDAPlace(0),kwargs转args

Copy link
Collaborator

Choose a reason for hiding this comment

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

我觉得这里你写的太复杂了,这个api的参数都是固定的 就一个,应该不需要用各种循环、dict、列表来组装吧

Copy link
Contributor Author

@co63oc co63oc Jul 6, 2023

Choose a reason for hiding this comment

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

有以下几种情况
1 数字参数 torch.cuda.device(device=0) paddle不使用id=,是kwargs参数转为args参数
2 不是数字,包含index参数 torch.cuda.device(torch.device("cuda", 0)),转为 str('cuda').replace('cuda', 'gpu'), index=1
3 不是数字,不包含index参数 torch.cuda.device(torch.device("cuda")), 转为 str('cuda').replace('cuda', 'gpu')
4 其他输入,不是数字也不包含 torch.cuda.device(torch.device("cpu"))

Copy link
Collaborator

@zhwesky2010 zhwesky2010 Jul 7, 2023

Choose a reason for hiding this comment

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

这个目前先删掉吧,问题在于torch.device应该转换为paddle.CUDAPlace,目前将其转换为字符串不太对,我们会修一下torch.device的转换,将其转换为paddle的place,然后再开发吧。

4可以不用管,没有这种用法,只需处理torch可正常运行的代码。1直接判断kwargs['device'].isdigit(),2、3直接取kwargs['device']+"get_device_id()"

infoflow 2023-07-07 11-58-05

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已删除torch.cuda.device

@luotao1
Copy link
Collaborator

luotao1 commented Jul 5, 2023

需要解决下冲突

@co63oc
Copy link
Contributor Author

co63oc commented Jul 5, 2023

需要解决下冲突

已修改

@zhwesky2010
Copy link
Collaborator

@co63oc 单测失败了,需要再看一下

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Jul 11, 2023

@co63oc 之前提交的torch.Tensor.histogram存在映射关系错误,由于torch返回两个Tensor,paddle返回一个Tensor,返回数量都对不上,直接转换是错误的,映射关系应为:参数不一致。转换完之后应该是任意用法都可使用,单测要充分涵盖全面,目前在单测里目前通过了一些if-else的判断trick来绕过,所以能通过。

麻烦重新改一下,不然会造成转换后的代码存在运行失败的问题。

@co63oc
Copy link
Contributor Author

co63oc commented Jul 11, 2023

@co63oc 单测失败了,需要再看一下

图片

单测失败是 tests/test_Tensor_multinomial.py 引入torch.mps,CI环境没有torch.mps模块

@co63oc
Copy link
Contributor Author

co63oc commented Jul 11, 2023

@co63oc 之前提交的torch.Tensor.histogram存在映射关系错误,由于torch返回两个Tensor,paddle返回一个Tensor,返回数量都对不上,直接转换是错误的,映射关系应为:参数不一致。转换完之后应该是任意用法都可使用,单测要充分涵盖全面,目前在单测里目前通过了一些if-else的判断trick来绕过,所以能通过。

麻烦重新改一下,不然会造成转换后的代码存在运行失败的问题。

    result = torch.tensor([[1., 2, 1]]).histogram(bins=4, range=(0., 3.))
    if hasattr(result, "hist"):
        result = result.hist
    result = result.to(torch.float32)

paddle 没有hist属性,torch返回两个Tensor,paddle返回一个Tensor,没明白这单测是要怎么修改

@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Jul 18, 2023

@co63oc 之前提交的torch.Tensor.histogram存在映射关系错误,由于torch返回两个Tensor,paddle返回一个Tensor,返回数量都对不上,直接转换是错误的,映射关系应为:参数不一致。转换完之后应该是任意用法都可使用,单测要充分涵盖全面,目前在单测里目前通过了一些if-else的判断trick来绕过,所以能通过。
麻烦重新改一下,不然会造成转换后的代码存在运行失败的问题。

    result = torch.tensor([[1., 2, 1]]).histogram(bins=4, range=(0., 3.))
    if hasattr(result, "hist"):
        result = result.hist
    result = result.to(torch.float32)

paddle 没有hist属性,torch返回两个Tensor,paddle返回一个Tensor,没明白这单测是要怎么修改

问题不在单测,在于 怎么能保证转换后与转换前计算结果一致,比如通过组合等形式,这个工作是保证用户模型代码中的任意用法case能够运行通过,而不是保证单测能运行通过。

hist Tensor有没有办法组合替代得到呢,如果没有那就是无法实现的Matcher,在issue下直接登记 功能缺失 即可。

infoflow 2023-07-18 15-55-16

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. 目前case是测了能跑的功能,unsupport_args也写下case吧,通过在单测里设置unsupport=True来跑
  2. 目前比较结果前,做了不少设置和修改,直接比较裸的结果会更好,覆盖问题更全面

h = torch.autograd.functional.vjp(func, x, v)
result = h[:]
for item in result:
item.requires_grad = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个最后是不是不需要设置.requires_grad为False,也能比较

Copy link
Contributor Author

@co63oc co63oc Jul 19, 2023

Choose a reason for hiding this comment

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

图片

提示torch和paddle的返回requires_grad值不一致

torch requires_grad=False
paddle stop_gradient=False 两个值应相反

h = torch.autograd.functional.hessian(func, x)
result = h[:]
result.requires_grad = False
result = torch.flatten(result)
Copy link
Collaborator

Choose a reason for hiding this comment

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

后面不加这些处理,直接比较h能跑过不,或者直接比较result能跑过不

Copy link
Contributor Author

Choose a reason for hiding this comment

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

使用h返回没有numpy属性错误
图片

z2 = torch.matmul(x, y)

torch.autograd.backward([z1, z2], [grad_tensor1, grad_tensor2], True)
x.grad.requires_grad=False
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个最后是不是不设置.requires_grad,能比较不

Copy link
Contributor Author

Choose a reason for hiding this comment

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

返回 requires_grad 属性不一致错误
图片

@co63oc
Copy link
Contributor Author

co63oc commented Jul 19, 2023

hist Tensor有没有办法组合替代得到呢,如果没有那就是无法实现的Matcher,在issue下直接登记 功能缺失 即可。

已在issue添加

@zhwesky2010
Copy link
Collaborator

@co63oc torch返回的bin能否通过paddle.linspace来实现

@zhwesky2010
Copy link
Collaborator

这个stop_gradient的问题是因为paddle的所有梯度的stop_gradient也为False,而torch的梯度的requires_grad为True,不过没有太大影响,只是这个属性不同,我先合入了。

后面可以看下两点:

  1. unsupport_args的参数也可以加点case,在单测里设置unsupport=True就可以跑了
  2. torch.histogram主要是输出Tensor个数没对上导致完全无法转,应该可以用paddle.linspace arange 等API模拟生成bins,组合实现出这个API,需要再试下方案,如果可以的话, torch.histogram/ torch.Tensor.histogram这两个的文档和Matcher都要同时修改

@co63oc
Copy link
Contributor Author

co63oc commented Jul 19, 2023

@co63oc torch返回的bin能否通过paddle.linspace来实现

如果min, max都为0,边界不容易获取,需要计算数据
图片

@co63oc
Copy link
Contributor Author

co63oc commented Jul 19, 2023

这个stop_gradient的问题是因为paddle的所有梯度的stop_gradient也为False,而torch的梯度的requires_grad为True,不过没有太大影响,只是这个属性不同,我先合入了。

后面可以看下两点:

1. unsupport_args的参数也可以加点case,在单测里设置unsupport=True就可以跑了

2. torch.histogram主要是输出Tensor个数没对上导致完全无法转,应该可以用paddle.linspace arange 等API模拟生成bins,组合实现出这个API,需要再试下方案,如果可以的话, torch.histogram/ torch.Tensor.histogram这两个的文档和Matcher都要同时修改

如果min, max都为0,调用paddle.linspace 还需要使用paddle.min, paddle.max计算数据最小最大值,使用api较多

@co63oc
Copy link
Contributor Author

co63oc commented Jul 19, 2023

已增加 unsupport用例

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 73527d9 into PaddlePaddle:master Jul 20, 2023
@co63oc co63oc deleted the doc0622 branch August 4, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR status: not progressed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants