【Hackathon 4th No.24】为 Paddle 新增 paddle.sparse.is_nan 稀疏 API by thunder95 · Pull Request #51513 · PaddlePaddle/Paddle

@thunder95

@paddle-bot

你的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.

@thunder95

zhwesky2010

}

template <typename T, typename Context>
SparseCooTensor IsnanCoo(const Context& dev_ctx, const SparseCooTensor& x) {

Choose a reason for hiding this comment

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

这个额外地C++ API可以删掉,一般用kernel表示C++ api就可以

Choose a reason for hiding this comment

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

你这个删掉没

@zhwesky2010

@zkh2016

没有支持静态图,可能需要 @zkh2016 帮忙看下静态图的应该怎么写

coo格式的静态图单测还是建议加下。test_sparse_norm_op.py和test_sparse_conv_op.py有测试例子,可以先用这种方式测下,验下正确性。

@thunder95

@thunder95

test_sparse_norm_op

@zhouwei25 @zkh2016 这个算子有点特殊, 计算结果是bool型, 在to_dense或return_numpy=True的时候,都会报错。辛苦两位老师给个建议。

InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container.
1220: [Hint: Expected dtype() == paddle::experimental::CppTypeToDataType::Type(), but received dtype():1 != paddle::experimental::CppTypeToDataType::Type():10.] (at /paddle/paddle/phi/core/dense_tensor.cc:163)

@zkh2016

test_sparse_norm_op

@zhouwei25 @zkh2016 这个算子有点特殊, 计算结果是bool型, 在to_dense或return_numpy=True的时候,都会报错。辛苦两位老师给个建议。

InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container. 1220: [Hint: Expected dtype() == paddle::experimental::CppTypeToDataType::Type(), but received dtype():1 != paddle::experimental::CppTypeToDataType::Type():10.] (at /paddle/paddle/phi/core/dense_tensor.cc:163)

当前to_dense还没注册bool类型,你可以先注册一个,测测看。

@thunder95

@zhouwei25 @zkh2016
已尝试注册bool类型,依旧是同样的报错。

有个不明白的地方,虽然我使用的是IsfiniteInferMeta, 返回的稀疏张量的dtype应该是bool才对,但是打印出来仍旧是float类型,所以定位在下面的内存或显存分配会发生报错,因为x的dtype是bool类型,但是模板T是float:

template <typename T, typename Context> DenseTensor CooToDense(const Context& dev_ctx, const SparseCooTensor& x) const T* x_data = values.data<T>(); ===> 出现类型转换错误
打印稀疏张量的时候,dtype并不是bool类型,所以注册bool类型没有产生效果

Tensor(shape=[2, 2, 2], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, indices=[[0, 0, 1, 1], [1, 1, 1, 1], [0, 1, 0, 1]], values=[False, False, False, True ])

@zkh2016

@zhouwei25 @zkh2016 已尝试注册bool类型,依旧是同样的报错。

有个不明白的地方,虽然我使用的是IsfiniteInferMeta, 返回的稀疏张量的dtype应该是bool才对,但是打印出来仍旧是float类型,所以定位在下面的内存或显存分配会发生报错,因为x的dtype是bool类型,但是模板T是float:

template <typename T, typename Context> DenseTensor CooToDense(const Context& dev_ctx, const SparseCooTensor& x) const T* x_data = values.data<T>(); ===> 出现类型转换错误 打印稀疏张量的时候,dtype并不是bool类型,所以注册bool类型没有产生效果

Tensor(shape=[2, 2, 2], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, indices=[[0, 0, 1, 1], [1, 1, 1, 1], [0, 1, 0, 1]], values=[False, False, False, True ])

可能需要辛苦你进一步定位下,我看了下当前是通过DEFINE_SPARSE_UNARY_KERNEL这个宏定义is_nan的kernel的,这里面重新创建了out,所以类型可能变了。

@thunder95

@thunder95

@thunder95

@thunder95

zhwesky2010

args : (Tensor x)
output : Tensor(out)
infer_meta :
func : IsfiniteInferMeta

Choose a reason for hiding this comment

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

这个名字不太对?

Choose a reason for hiding this comment

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

IsfiniteInferMeta吗?最新提交已修改成unchanged

Choose a reason for hiding this comment

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

@zhouwei25 如果改成unchanged,还是会报错,最新提交又修改回IsfiniteInferMeta
InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container.

Choose a reason for hiding this comment

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

@zhouwei25 如果改成unchanged,还是会报错,最新提交又修改回IsfiniteInferMeta InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container.

就是命名风格有点问题,这个不是IsNanInferMeta吗

Choose a reason for hiding this comment

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

@zhouwei25 IsfiniteInferMeta是参考dense tensor那里设计的IsfiniteInferMeta, 考虑到代码可能会冗余就这么直接复用了,老师建议这个地方是需要单独写一个IsNanInferMeta吗

Choose a reason for hiding this comment

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

PD_REGISTER_INFER_META_FN(isnan, phi::IsfiniteInferMeta);

Choose a reason for hiding this comment

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

好复用也可以

}

template <typename T, typename Context>
SparseCooTensor IsnanCoo(const Context& dev_ctx, const SparseCooTensor& x) {

Choose a reason for hiding this comment

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

你这个删掉没

}

template <typename T, typename Context>
SparseCooTensor IsnanCsr(const Context& dev_ctx, const SparseCooTensor& x) {

Choose a reason for hiding this comment

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

这个删掉,需要kernel就可以

Choose a reason for hiding this comment

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

已删掉

}

template <typename T, typename Context>
void IsnanCooKernel(const Context& dev_ctx,

Choose a reason for hiding this comment

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

这个用目前的公共组件,宏函数来注册kernel。可以复用减少代码

Choose a reason for hiding this comment

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

@zhouwei25 公共组件不能满足这个算子,因为emptylike会创建一个相同类型的输出tensor,而这个算子输出是bool型的,所以这里单独写了个kernel。

Choose a reason for hiding this comment

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

@zhouwei25 公共组件不能满足这个算子,因为emptylike会创建一个相同类型的输出tensor,而这个算子输出是bool型的,所以这里单独写了个kernel。

OK

zhwesky2010

Choose a reason for hiding this comment

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

LGTM

jeff41404

Choose a reason for hiding this comment

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

LGTM

sunzhongkai588

Return whether every element of input tensor is `NaN` or not, requiring x to be a SparseCooTensor or SparseCsrTensor.

Args:
x (Tensor): The input tensor, it's data type should be float16, float32, float64, int32, int64.

Choose a reason for hiding this comment

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

中文文档中 '可以为 Coo 或 Csr 格式' 可以表达出来

Choose a reason for hiding this comment

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

@thunder95

@thunder95

zhwesky2010

sunzhongkai588

Choose a reason for hiding this comment

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

LGTM

ZzSean

Choose a reason for hiding this comment

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

LGTM for CI-OP-Benchmark