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

【complex op No.33】abs_coo/abs_csr(sparse) #62237

Merged
merged 26 commits into from
May 21, 2024

Conversation

bapijun
Copy link
Contributor

@bapijun bapijun commented Feb 29, 2024

PR Category

Operator Mechanism

PR Types

New features

Description

add complex support for abs_coo/abs_csr in sparse

#61975

Copy link

paddle-bot bot commented Feb 29, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Feb 29, 2024
@CLAassistant
Copy link

CLAassistant commented Feb 29, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

paddle-bot bot commented Feb 29, 2024

❌ The PR is not created using PR's template. You can refer to this Demo.
Please use PR's template, it helps save our maintainers' time so that more developers get helped.

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Feb 29, 2024
@luotao1
Copy link
Contributor

luotao1 commented Mar 1, 2024

@zbt78 可以进行 review 了

@zbt78
Copy link
Contributor

zbt78 commented Mar 1, 2024

测试的时候建议先不添加新的测试文件,在原测试文件test_sparse_unary_op.py里进行修改。

@luotao1
Copy link
Contributor

luotao1 commented Mar 13, 2024

测试的时候建议先不添加新的测试文件,在原测试文件test_sparse_unary_op.py里进行修改。

@bapijun 请根据review意见进行修改

@bapijun
Copy link
Contributor Author

bapijun commented Mar 13, 2024

测试的时候建议先不添加新的测试文件,在原测试文件test_sparse_unary_op.py里进行修改。

@bapijun 请根据review意见进行修改
已经提交了更改

@luotao1
Copy link
Contributor

luotao1 commented Mar 14, 2024

@zbt78 可以再review一下,目前Coverage覆盖率没过

self.check_result(dense_func, sparse_func, 'coo')
self.check_result(dense_func, sparse_func, 'csr')
def compare_with_dense(self, dense_func, sparse_func, dtype='float32'):
for device in devices:
Copy link
Contributor

Choose a reason for hiding this comment

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

这里device的作用是什么呢,看起来没起作用

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

Choose a reason for hiding this comment

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

现在覆盖率就过了👍

@zbt78
Copy link
Contributor

zbt78 commented Mar 15, 2024

LGTM

PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(pow, Pow)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(expm1, Expm1)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(relu6, Relu6)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(leaky_relu, LeakyRelu)

PD_REGISTER_KERNEL(abs_coo_grad,
Copy link
Contributor

Choose a reason for hiding this comment

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

注册的形式是一样的,这里是不是也写一个带复数注册的宏会更好一点,也更方便后面稀疏方法的注册,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

注册的形式是一样的,这里是不是也写一个带复数注册的宏会更好一点,也更方便后面稀疏方法的注册,

事实上我在后面的复数修改就重新写了新的宏,我晚上有时间就去把那边pr的代码移动过来

DEFINE_SPARSE_UNARY_KERNEL(Expm1)
DEFINE_SPARSE_UNARY_KERNEL(Relu6)
DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(Pow, factor)
DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha)

template <typename T, typename Context>
void AbsCooKernel(const Context& dev_ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

这种的是不是也可以写一个宏,毕竟实现方式也是一样的

if (out->dtype() == DataType::COMPLEX64 ||
out->dtype() == DataType::COMPLEX128) {
DenseTensor* out_values = out->mutable_non_zero_elements();
out->set_type(out_values->dtype());
Copy link
Contributor

Choose a reason for hiding this comment

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

这里为什么会需要重新设置一下out的dtype

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里为什么会需要重新设置一下out的dtype
这是因为abs的结果是会是浮点数,比如对于value是complex64的输入,他的abs结果会是一个float32所以这里对于values需要重设他的类型

Copy link
Contributor

Choose a reason for hiding this comment

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

那这里应该是修改一下sparse_ops.yaml 文件里面infermeta的函数,可以先参考一下ops.yaml里面abs的infermeta函数是否可用,如果不行就需要新增一个,kernel 内部非必要不会去增加设置meta信息的逻辑,不利于整体op的维护。

Copy link
Contributor Author

@bapijun bapijun Mar 18, 2024

Choose a reason for hiding this comment

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

那这里应该是修改一下sparse_ops.yaml 文件里面infermeta的函数,可以先参考一下ops.yaml里面abs的infermeta函数是否可用,如果不行就需要新增一个,kernel 内部非必要不会去增加设置meta信息的逻辑,不利于整体op的维护。

我回去试了一下,只改infermeta函数是不行的,因为这个Kernel里面有一个EmptyLikeCXXKernel 函数,函数里面会试着把x的属性复制到out里面去,导致导致在infermeta重设过得dtype变回来,我的想法是要么在这里处理,要么写一个新的EmptyLikeCXXKernel 处理这个逻辑问题,我看了一下在op.yaml里面除了abs之外,还有另外三个算子也是会出现需要复数的输入,转化为浮点数的结果

Copy link
Contributor

Choose a reason for hiding this comment

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

当初sparse 设计的时候没有考虑像复数这种存在C-R 或是R-C导致输入输出meta 信息不一致的场景。所以为了规范sparse_kernel设计,这里需要修改相应逻辑,EmptyLikeCXXKernel实际上承担了两部分的工作,infermeta(坐标、dim、dtype等等)、内存分配。所以现在需要把这个两个功能分开,将infermeta的功能放到infermeta函数中,将分配内存的功能放到kernel内部,这里先修改abs_kernel,主要有两个修改点:

  1. 新增一个sparse的 unchangedinfermeta函数去设置坐标、dim、dtype这种
  2. abs kernel 内部的EmptyLikeCXXKernel 替换成分配内存的逻辑

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. unchangedinfermeta

好的我回去试试

@luotao1
Copy link
Contributor

luotao1 commented Mar 15, 2024

@bapijun 可以考虑参加下 【HACKATHON 预备营】飞桨启航计划集训营(第二期) 的复数团,简历截止3月18日

@@ -49,6 +48,35 @@ void EmptyLikeCsrKernel(const Context& dev_ctx,
dev_ctx.template Alloc<T>(out_values);
}

template <typename T, typename Context>
void EmptyLikeCooRealComplexKernel(const Context& dev_ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

不是这么改,不应该额外增加这个kernel了,这么做其实和之前的做法没有本质的区别,还是将输入输出的meta信息计算和数据计算耦合在一起了,这样是不符合现有的框架设计的,而且目前非必要也不推荐去新增kernel,后续维护容易出现坑。
正确的方式是新增SparseUnchangedInferMeta,作为通用的sparse的infermeta函数,做设置坐标*(out->mutable_indices()) = x.indices(); ,设置dim out_values->Resize(x_values.dims());,还有设置dtype 的工作,只不过针对C-R 这种场景,需要新增一个SparseC2RInferMeta,可以在这个SparseC2RInferMeta里面调用SparseUnchangedInferMeta, 然后设置输出的dtype 是实数。
dev_ctx.template Alloc<T>(out_values); 分配内存的事情就放到kernel内部,所以sparse_abs的kernel 应该是这样的。

#define DEFINE_SPARSE_UNARY_KERNEL(prefix)                                 \
  template <typename T, typename Context>                                  \
  void prefix##CooKernel(const Context& dev_ctx,                           \
                         const SparseCooTensor& x,                         \
                         SparseCooTensor* out) {                           \
    dev_ctx.template Alloc<T>(out);                       \
    phi::prefix##Kernel<T, Context>(                                       \
        dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); \
    out->SetIndicesDict(x.GetIndicesDict());                               \
  }                                                                        \

这样才更通用,扩展性更强。

const SparseCooTensor& x_or_out,
const SparseCooTensor& dout,
SparseCooTensor* dx) {
EmptyLikeCooKernel<T, Context>(dev_ctx, x_or_out, dx);
Copy link
Contributor

Choose a reason for hiding this comment

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

反向的也可以拆分吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

关于这个我回去看了一下,非稀疏的abs,看起来结果下,abs的梯度也就是复数形式的,应该是不需要c to r的写法

if device == 'cpu' or (
device == 'gpu' and paddle.is_compiled_with_cuda()
):
paddle.set_device(device)
Copy link
Contributor

Choose a reason for hiding this comment

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

这种全局设置要慎用,很容易在并行测试的时候影响其他的单测,可以https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/Tensor_cn.html#tensor 主动用tensor.to 指定输入tensor的设备

@bapijun
Copy link
Contributor Author

bapijun commented Apr 2, 2024

@GGBond8488
麻烦老师再看一下

Copy link
Contributor

@GGBond8488 GGBond8488 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

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

请对应修改中文文档中的数据类型

bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(coo_to_csr,
Copy link
Contributor

Choose a reason for hiding this comment

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

这些kernel 对应的grad kernel 也注册上复数吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这些kernel 对应的grad kernel 也注册上复数吧

这一次提交里面,在对应的sparse_backward.yaml下面能找到的后向我改掉了,但是对应的to_dense和value的梯度,涉及到
mask_kernel下面的两个kernel
另外还有之前提到的,稀疏格式在backward的时候涉及到的meta丢失的问题,在修改to_dense的梯度的时候也出现了,这一次也改掉了

Copy link

paddle-ci-bot bot commented Apr 6, 2024

Sorry to inform you that e589c98's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@bapijun
Copy link
Contributor Author

bapijun commented Apr 7, 2024

请对应修改中文文档中的数据类型

指的是这个么?
https://github.com/PaddlePaddle/docs

@luotao1
Copy link
Contributor

luotao1 commented Apr 7, 2024

指的是这个么?
https://github.com/PaddlePaddle/docs

对的

Copy link

paddle-ci-bot bot commented Apr 15, 2024

Sorry to inform you that de638a6's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@zhiminzhang0830
Copy link

LGTM

Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

@luotao1 luotao1 merged commit 8218dce into PaddlePaddle:develop May 21, 2024
31 of 32 checks passed
@luotao1
Copy link
Contributor

luotao1 commented May 21, 2024

hi, @bapijun

  • 非常感谢你对飞桨的贡献,我们正在运营一个PFCC组织,会通过定期分享技术知识与发布开发者主导任务的形式持续为飞桨做贡献,详情可见 https://github.com/luotao1 主页说明。
  • 如果你对PFCC有兴趣,请发送邮件至 ext_paddle_oss@baidu.com,我们会邀请你加入~

@bapijun
Copy link
Contributor Author

bapijun commented May 21, 2024

PaddlePaddle/docs#6645
@luotao1
对应的文档 or也麻烦老师看看

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants