-
Notifications
You must be signed in to change notification settings - Fork 274
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 #698
Conversation
|
||
## 底层 OP 设计 | ||
|
||
由于赛题要求直接实现 python 接口,所以此处直接使用 python API 实现,无需设计底层 c++ 相关 OP。 |
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数量会跟batch_size有关系,导致launch大量kernel,考虑到性能问题,这边要求最好是用c++ 底层op去实现该算子。
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.nn.FractionalMaxPool2d` | ||
|
||
``` python | ||
paddle.nn.functional.fractional_max_pool2d( |
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.
这儿是否需要和maxpooling2D一样加一个data_format
参数?
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.
嗯 应该有!我记一下~
根据之前的讨论,此次更新,使用 c++ 实现底层算子:
另外,之前讨论的 非常感谢! |
|
||
``` yaml | ||
- op : max_pool2d_with_index | ||
args : (Tensor x, int[] kernel_size, int[] strides= {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false) |
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
是在api内部进行推导的吗,如果是的话方便加一下推导过程吗.
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.
fractional max pooling 与 adaptive max pooling 应该一样,不需要 kernel_size
参数,只需要 output_size
,kernel_size
是自动推导的,使用 ksize
代为接受 output_size
这个参数。
python 接口:
def adaptive_max_pool2d(x, output_size, return_mask=False, name=None):
...
if in_dygraph_mode():
...
else:
l_type = 'max_pool2d_with_index'
...
helper.append_op(
type=l_type,
inputs={"X": x},
outputs=outputs,
attrs={
"pooling_type": 'max',
"ksize": output_size,
"adaptive": True,
},
)
return (pool_out, mask) if return_mask else pool_out
c++ 算子:
if (adaptive || fractional) {
output_shape.insert(
output_shape.end(), kernel_size_.begin(), kernel_size_.end());
} else {
...
}
fractional max pooling 与 adaptive max pooling 最重要的是生成池化序列的方法,也就是一系列的 ksize
,如 fractional max pooling 的 1222111212...
。之前版本有用 python 写过伪序列的生成方法:
def pseudo(input_size, output_size, sample):
cum_seq = [0] * (output_size + 1)
diff = [0] * output_size
alpha = input_size / output_size
base = input_size // output_size
u_max1 = (base + 2) / alpha - 1
u_max2 = (input_size + 1 - base) / alpha - (output_size - 1)
max_u = min(u_max1, u_max2)
u = sample * max_u
cum_seq[0] = 1
cum_seq[output_size] = input_size + 1
for i in range(1, output_size):
cum_seq[i] = math.ceil(alpha * (i + u))
for i in range(output_size):
diff[i] = cum_seq[i + 1] - cum_seq[i]
return diff
这里需要用 c++ 重新写一下 ~ 是需要这个推导过程是吧?
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.
好的 我看你是复用adaptive_max_poolNd的实现,但是yaml以及函数命名记得与这个进行区分,文档中我看写的都是max_poolNd
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 20231114
由于 adaptive_max_poolNd 也是复用 max_poolNd_with_index,所以,实际上是 adaptive_max_poolNd 和 fractional_max_poolNd 共同复用了 max_poolNd_with_index,所以文档中基本上都是 max_poolNd_xxx。 @Charles-hit 请评审~ 谢谢! |
PR types
Others
PR changes
Docs
Description
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API
请评审!