-
Notifications
You must be signed in to change notification settings - Fork 765
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 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API #6418
Conversation
感谢你贡献飞桨文档,文档预览构建中,Docs-New 跑完后即可预览,预览链接:http://preview-pr-6418.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html |
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.
LGTM
- @zhwesky2010 辛苦review一下映射文档
- 映射文档的表格格式问题,在 [Fix] fix pytorch-paddle mapping index generate #6463 已修复,建议等这个 PR 合入之后,再合该PR
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| input | x | 表示输入的 Tensor ,仅参数名不一致。 | | ||
| kernel_size | kernel_size | 当 Paddle 不使用此参数时,为 disjoint 模式;当 Paddle 使用此参数时,与 PyTorch 功能一致。 | |
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.
output_size没写
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.
一样的参数也要写是吧?~ 我添加一下 ~
| kernel_size | kernel_size | 当 Paddle 不使用此参数时,为 disjoint 模式;当 Paddle 使用此参数时,与 PyTorch 功能一致。 | | ||
| output_ratio | - | Paddle 根据 output_size 推算输出比例,不需要此参数。 | | ||
| return_indices | return_mask | 是否返回最大值索引,仅参数名不一致。 | | ||
| _random_samples | random_u | 随机数,PyTorch 为随机数列表,Paddle 为单个随机数。功能一致。 | |
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.
这个是不是有些差异,需要写清楚,以及在下面也需要加转写示例
| _random_samples | random_u | 随机数,PyTorch 为随机数列表,Paddle 为单个随机数。功能一致。 | | ||
|
||
|
||
### 转写示例 |
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.
需指明是在转写哪个参数:
output_ratio、_random_samples 这两个都需要转写示例
@@ -0,0 +1,36 @@ | |||
## [ torch 参数更多 ]torch.nn.functional.fractional_max_pool3d |
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.
问题同上
Update 20240112
请评审 ~ |
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.
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| input | x | 表示输入的 Tensor ,仅参数名不一致。 | | ||
| kernel_size | kernel_size | 当 Paddle 不使用此参数时,为 disjoint 模式;当 Paddle 使用此参数时,与 PyTorch 功能一致。 | |
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.
这个要从torch角度往paddle看,是否有差异。不要写paddle该怎么用,paddle怎么用取决于torch先怎么用,torch的每个用法应该怎么转
| input | x | 表示输入的 Tensor ,仅参数名不一致。 | | ||
| kernel_size | kernel_size | 当 Paddle 不使用此参数时,为 disjoint 模式;当 Paddle 使用此参数时,与 PyTorch 功能一致。 | | ||
| output_size | output_size | 目标输出尺寸。功能一致。 | | ||
| output_ratio | - | Paddle 根据 output_size 推算输出比例,不需要此参数。 | |
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.
这个要从torch角度往paddle看,是否有差异。不要写paddle该怎么用,paddle怎么用取决于torch先怎么用,torch的每个用法应该怎么转
这个不能写不需要,而要写torch如果设置了,paddle对应怎么实现
| output_size | output_size | 目标输出尺寸。功能一致。 | | ||
| output_ratio | - | Paddle 根据 output_size 推算输出比例,不需要此参数。 | | ||
| return_indices | return_mask | 是否返回最大值索引,仅参数名不一致。 | | ||
| _random_samples | random_u | 随机数,PyTorch 为随机数列表,Paddle 为单个随机数。参数形式不同,功能一致。 | |
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.
不要写 功能一致,要写清楚具体的代码写法的转变,你类比下 torch.rand与paddle.rand的size
| _random_samples | random_u | 随机数,PyTorch 为随机数列表,Paddle 为单个随机数。参数形式不同,功能一致。 | | ||
|
||
|
||
### 转写示例 |
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.
仅针对上文中 需要转写 的参数进行转写
Update 20240115
@zhwesky2010 请评审 ~ |
| PyTorch | PaddlePaddle | 备注 | | ||
| ------------- | ------------ | ------------------------------------------------------ | | ||
| input | x | 表示输入的 Tensor 。仅参数名不一致。 | | ||
| kernel_size | kernel_size | 表示核大小。与 PyTorch 默认值不同,(Paddle 可以不设置此参数)。 | |
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.
pytorch没有默认值,从torch往paddle看,这个应该是 参数完全相同 吧,将原本的kernel_size原封不动复制过来
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.
嗯,也可以这么理解吧 ~ 那我改为 参数完全相同 ~
| output_size | output_size | 表示目标输出尺寸。参数完全一致。 | | ||
| output_ratio | - | 表示目标输出比例。Paddle 无此参数,需要转写。 | | ||
| return_indices | return_mask | 表示是否返回最大值索引。仅参数名不一致。 | | ||
| _random_samples | random_u | 表示随机数。PyTorch 以列表形式的 Tensor 方式传入,Paddle 以 float 的方式传入,需要转写。 | |
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.
如果torch的列表有多个,是不是就无法转写了,这个也要写清楚
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.
嗯 ~ 这个参数比较特殊,是个 私有 参数,torch 本来是不打算暴露出来的,可以翻翻 torch 的 repo 里面有讨论 ~
这里可以有多个不同的数值,但是 多个随机数 与 一个随机数 ,从数学上来说没啥区别,所以 paddle 这里只做了单个 float 的输入(同 tensorflow),torch 多个随机数的方式,paddle 不能直接 mapping ~
torch 这里应该是移植原代码的遗留问题,说来话长 ~ 那我写一下无法转写的情况吧 ~
|
||
```python | ||
# Pytorch 写法 | ||
torch.nn.functional.fractional_max_pool2d(input, 2, output_size=[3, 3], return_indices=True, _random_samples=torch.tensor([[[0.3, 0.3]]])) |
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.
会有多个不同的_random_samples吗
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.
理论上会有,但是,这个随机数是 私有 参数,应该不提倡使用 ~
#### output_ratio:目标输出比例 | ||
|
||
```python | ||
# 假设 intput 的 with=7, height=7, |
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.
是不是写个能整除的例子,更好理解点
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.
不好吧 ~ 因为这里本来的意图就是这种小数的方式 ~
Update 20240116
关于 @zhwesky2010 请评审 ~ |
| ------------- | ------------ | ------------------------------------------------------ | | ||
| input | x | 表示输入的 Tensor 。仅参数名不一致。 | | ||
| kernel_size | kernel_size | 表示核大小。参数完全一致。 | | ||
| output_size | output_size | 表示目标输出尺寸。参数完全一致。 | |
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.
为什么torch的output_size是可选,我们是必选,如果torch不输入output_size,我们是怎么转写呢
所有的差异,都从torch往paddle转的角度来看
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.
为啥torch的output_size是可选,我们是必选
从 torch 的角度来看,output_size 与 output_ratio 必须输入一个,而 paddle 这里只使用 output_size 一个参数的原因,至少有以下几点:
- output_size 与 output_ratio 的用处是一样的,最终都是获取到 output_size
- paddle 目前所有的 pooling 都是用 output_size,而不是 output_ratio
- torch 同时使用这两个参数,至少有以下几点弊端:
- 都是可选参数,容易误导用户不需要这两个参数
- 同时设置这两个参数会产生冲突,而 torch 却没有处理这种冲突
参考以下 torch 的代码:
In [16]: import numpy as np
In [17]: import torch
In [18]: input3 = np.random.rand(1, 1, 7, 7, 7)
In [19]: out_torch = torch.nn.functional.fractional_max_pool3d(torch.tensor(input3), 2, output_size=[4, 4, 4], output_ratio=[0.75, 0.75, 0.75]) # 这里 output_ratio 实际对应的 output_size 为 5
In [20]: out_torch.shape
Out[20]: torch.Size([1, 1, 4, 4, 4])
In [21]: out_torch = torch.nn.functional.fractional_max_pool3d(torch.tensor(input3), 2, output_ratio=[0.75, 0.75, 0.75])
In [22]: out_torch.shape
Out[22]: torch.Size([1, 1, 5, 5, 5])
In [23]: out_torch = torch.nn.functional.fractional_max_pool3d(torch.tensor(input3), 2) # 误导用户不输入 output_size 和 output_ratio
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[23], line 1
----> 1 out_torch = torch.nn.functional.fractional_max_pool3d(torch.tensor(input3), 2)
File ~/venv38dev/lib/python3.8/site-packages/torch/_jit_internal.py:499, in boolean_dispatch.<locals>.fn(*args, **kwargs)
497 return if_true(*args, **kwargs)
498 else:
--> 499 return if_false(*args, **kwargs)
File ~/venv38dev/lib/python3.8/site-packages/torch/nn/functional.py:622, in _fractional_max_pool3d(input, kernel_size, output_size, output_ratio, return_indices, _random_samples)
611 if has_torch_function_variadic(input, _random_samples):
612 return handle_torch_function(
613 fractional_max_pool3d,
614 (input, _random_samples),
(...)
620 _random_samples=_random_samples,
621 )
--> 622 return fractional_max_pool3d_with_indices(
623 input, kernel_size, output_size, output_ratio, return_indices, _random_samples
624 )[0]
File ~/venv38dev/lib/python3.8/site-packages/torch/nn/functional.py:588, in fractional_max_pool3d_with_indices(input, kernel_size, output_size, output_ratio, return_indices, _random_samples)
577 return handle_torch_function(
578 fractional_max_pool3d_with_indices,
579 (input, _random_samples),
(...)
585 _random_samples=_random_samples,
586 )
587 if output_size is None and output_ratio is None:
--> 588 raise ValueError("fractional_max_pool3d requires specifying either an output_size or an output_ratio")
589 if output_size is None:
590 assert output_ratio is not None
ValueError: fractional_max_pool3d requires specifying either an output_size or an output_ratio
可以看到,当同时使用 output_size 与 output_ratio 时,torch 是以 output_size 为准的,但是,即便这两个参数实际上有冲突,torch 也没有提示用户存在问题 ~
由以上考虑,paddle 这里在设计时,只使用 output_size 一个参数 ~
如果torch不输入output_size,我们是怎么转写呢
如上面所说,如果没有 output_size,则必须有 output_ratio,不存在 output_size 与 output_ratio 同时没有的情况(torch 误导用户的地方),而 output_ratio 可以转写为 output_size ~
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.
@megemini 把这个点在output_size这一栏里写下吧,output_ratio属于暂无转写方式,而output_size如果不输入时,其实就对应这种暂无转写方式的情况,不然可能用户就有疑问了。
这个文档的价值就在于让不清楚两者设计背景的读者,一眼就能看懂从torch怎么转到paddle来
Update 20240117
@zhwesky2010 请评审 ~ |
| ------------- | ------------ | ------------------------------------------------------ | | ||
| input | x | 表示输入的 Tensor 。仅参数名不一致。 | | ||
| kernel_size | kernel_size | 表示核大小。参数完全一致。 | | ||
| output_size | output_size | 表示目标输出尺寸。参数完全一致。如果不使用此 output_size 参数,则需要通过 output_ratio 参数转换为 output_size,需要转写。 | |
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.
表示目标输出尺寸,Pytorch为可选参数,Paddle为必选参数,仅参数默认值不一致。Pytorch的output_size与output_ratio输入二选一,如不输入output_size,则必须输入output_ratio,此时需要转写。转写方式与下文output_ratio一致。
| input | x | 表示输入的 Tensor 。仅参数名不一致。 | | ||
| kernel_size | kernel_size | 表示核大小。参数完全一致。 | | ||
| output_size | output_size | 表示目标输出尺寸。参数完全一致。如果不使用此 output_size 参数,则需要通过 output_ratio 参数转换为 output_size,需要转写。 | | ||
| output_ratio | - | 表示目标输出比例。Paddle 无此参数,暂无转写方式。 | |
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 无此参数,需要转写。 |
|
||
### 转写示例 | ||
|
||
#### output_size:由 output_ratio 目标输出比例转写 |
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.
output_ratio:目标输出比例
@zhwesky2010 已修改 ~ |
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.
LGTM
PR types
Others
PR changes
Docs
Description
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API
关联 PR : PaddlePaddle/Paddle#59847
请评审 ~