-
Notifications
You must be signed in to change notification settings - Fork 276
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
【PaddlePaddle Hackathon 4 No.17】为 Paddle 新增 cummax / cummin API #401
Conversation
|
||
### 实现方法 | ||
|
||
在实现方法上, PyTorch 通用实现采用的遍历,[CPU](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/ReduceOps.cpp#L638),CUDA 采用的[GPU](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/cuda/ScanKernels.cpp#L17)。 |
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.
这一句写的 有点混乱,链接不太准确,cummax同
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.
已添加
|
||
## 底层OP设计 | ||
|
||
参考 paddle.cumsum 实现。 |
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.
补充一下详细的设计吧,例:cummax/cummin是否由通用的函数实现?
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.
补充一下具体的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.
已添加
@@ -50,11 +50,11 @@ Keyword Arguments | |||
``` | |||
即输入参数为 Tensor 和指定的维,两个值和索引的切片。 |
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.
已修改
|
||
## PyTorch | ||
|
||
PyTorch 中有 API(https://pytorch.org/docs/stable/generated/torch.cummin.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.
这里链接多了)
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.
已修改
Keyword Arguments | ||
- out (tuple, optional) – the result tuple of two output tensors (values, indices) | ||
``` | ||
即输入参数为 Tensor 和指定的维,两个值和索引的切片。 |
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.
已修改
API设计为`paddle.cummin(x, axis , dtype, name)`以及`paddle.Tensor.cummin(axis, dtype, name)`。参数设计参考`paddle.cumsum`。 | ||
- x (Tensor) - 需要进行累积最大值统计的 Tensor。 | ||
- axis (int, 可选) - 指明需要统计的维度。-1代表最后一维。默认:None,将输入展开为一维变量再进行累加计算。 | ||
- dtype (str,可选) - 输出Tensor的数据类型,支持int32、int64、float32、float64. 如果指定了,那么在执行操作之前,输入张量将被转换为dtype. 这对于防止数据类型溢出非常有用。默认为:None。 |
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.
这里dtype参数是否有必要?max/min操作并不会出现溢出现象
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.
已删除
|
||
cpu: | ||
前向计算,可调用cumsum算子所实现的ScanKernel作为核心,增加一些细节处理即可 | ||
后向计算,定义CummaxGradKernel函数实现cummax函数subgradient计算,或者参考其它不可导函数的subgradient计算方法 |
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.
这里对应的名字写对吧,然后反向计算应该不能参考cummax的梯度计算方式,但是实现也不复杂,这里直接给出反向的计算方式吧
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的实现
const ArgumentMappingContext& ctx) { | ||
return KernelSignature("cummin", | ||
{"X"}, | ||
{"axis", "flatten", "exclusive", "reverse"}, |
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.
这里 exclusive以及reverse参数的考虑是什么样的呢
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.
已删除,exclusive和reverse参数我猜测是cumsum算子开发时添加的超出其功能的设计,在cummax/cummin中应该不需要
## 底层OP设计 | ||
|
||
cpu: | ||
前向计算,可调用cumsum算子所实现的ScanKernel作为核心,增加一些细节处理即可 |
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.
注意,使用前向的cumsum的Scankernel需要查看Eigen是否有对应的MaxReduce/MinReduce,如若没有则需要自己实现。GPU同样需要看对应cuda下是否提供对应的函数
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.
重新构思了一下,应该不能以ScanKernel为核心来实现,主要还是参考pytorch的实现,计算出index方便反向梯度计算
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.
cummax同cummin
前向函数签名 | ||
|
||
~~~cpp | ||
KernelSignature CumminOpArgumentMapping( |
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.
op 都写成kernel定义的形式,类似下面这种:
template <typename T, typename Context>
void CumsumKernel(const Context& dev_ctx,
const DenseTensor& x,
const Scalar& axis,
bool flatten,
bool exclusive,
bool reverse,
DenseTensor* out)
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.
Done
- x (Tensor) - 需要进行累积最大值统计的 Tensor。 | ||
- axis (int, 可选) - 指明需要统计的维度。-1代表最后一维。默认:None,将输入展开为一维变量再进行累加计算。 | ||
- name (str,可选) - 操作的名称(可选,默认值为None)。 | ||
|
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.
且返回值中有标的api,需要提供dtype参数,限制返回的下标数据类型(int32, int64),默认为int64 具体参考 https://www.paddlepaddle.org.cn/documentation/docs/zh/dev_guides/api_contributing_guides/api_design_guidelines_standard_cn.html 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.
Done
|
||
# 六、测试和验收的考量 | ||
|
||
测试考虑的case如下: |
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.
Done
|
||
## 1、相关背景 | ||
|
||
cummin 是指求累积最小值(cumulative min)的功能。即求 |
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.
Done
- x (Tensor) - 累积最小值的输入,需要进行累积最小值操作的 Tensor。 | ||
- axis (int, 可选) - 指明需要统计的维度。-1代表最后一维。默认:None,将输入展开为一维变量再进行累加计算。 | ||
- name (str,可选) - 具体用法请参见 [Name](https://www.paddlepaddle.org.cn/documentation/docs/zh/api_guides/low_level/program.html#api-guide-name),一般无需设置,默认值为 None。 | ||
返回 |
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.
返回索引的api,需要对索引的dtype进行限制,增加一个索引输出的dtype参数吧
具体可参照这里的数据类型规范 https://www.paddlepaddle.org.cn/documentation/docs/zh/dev_guides/api_contributing_guides/api_design_guidelines_standard_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.
Done
参数 | ||
::::::::: | ||
- x (Tensor) - 累积最小值的输入,需要进行累积最小值操作的 Tensor。 | ||
- axis (int, 可选) - 指明需要统计的维度。-1代表最后一维。默认:None,将输入展开为一维变量再进行累加计算。 |
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.
Done
|
||
~~~cpp | ||
template <typename T, typename Context> | ||
void CumminKernel(const Context& dev_ctx, |
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也需要添加dtype参数,注意这里的dtype类型应该为int
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.
添加了dtype,类型约束可以由python端完成
attr_dtype = convert_np_dtype_to_dtype_(dtype)
check_dtype(dtype, 'dtype', ['int32', 'int64'], 'cummax')
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.
cummax/cummin和对应的rfc对应好
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.
修改一下吧,其他的没问题了
@@ -21,7 +21,7 @@ PyTorch、NumPy 和 Pandas 提供了相似算子。 | |||
|
|||
## 2、功能目标 | |||
|
|||
cummin API 是一个按轴寻找累计最大值和最大值所在位置的 API。此任务的目标是在 Paddle 框架中,新增 cummin API,调用路径为:`paddle.cummin`和 `paddle.tensor.cummin`。 | |||
cummin API 是一个按轴寻找累计最大值和最大值所在位置的 API。此任务的目标是在 Paddle 框架中,新增 cummin API,调用路径为:`paddle.cummin`和 `paddle.Tensor.cummin`。 |
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.
不应该是寻找最大值
Keyword Arguments | ||
- out (tuple, optional) – the result tuple of two output tensors (values, indices) | ||
``` | ||
输入数据Tensor和cummax操作的维度dim,输出一个tuple包含计算结果values和索引indices |
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.
cummax?
|
||
## 命名与参数设计 | ||
|
||
API设计为`paddle.cummin(x, axis, name)`以及`paddle.Tensor.cummin(axis, name)`。 |
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.
这里也要补上dtype
|
||
~~~cpp | ||
template <typename T, typename Context> | ||
void CumminKernel(const Context& dev_ctx, |
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.
cummax/cummin和对应的rfc对应好
OK,应该都对应了 |
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
为 Paddle 新增 cummax / cummin API
cummax和cummin类似,因此大体内容和上期提案类似