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

【Hackathon 7th No.20】为 Paddle 新增 Tensor.set_ / Tensor.resize_ API #968

Merged
merged 4 commits into from
Oct 11, 2024

Conversation

NKNaN
Copy link
Contributor

@NKNaN NKNaN commented Sep 26, 2024

No description provided.

测试case:

paddle.Tensor.set_
- 正确性验证:可以与 as_strided 的结果对齐;
Copy link
Contributor

Choose a reason for hiding this comment

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

此处要关注as_strided仅动态图支持,set_是否能同时支持动静态图

Copy link
Contributor Author

Choose a reason for hiding this comment

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

set_按这样设计只支持动态图,目前是否有支持静态图的inplace API?

Copy link
Contributor

Choose a reason for hiding this comment

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

inplace API 只支持动态图没有问题,但需要加上@inplace_apis_in_dygraph_only 的 tag。表明该API仅支持动态图

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

- old_size > new_size ;
- 验证不同dtype类型:`bfloat16`,`float16`,`float32`,`float64`,`bool`,`int8`,`int16`,`int32`,`int64`,`uint8`,`complex64`,`complex128`;
- 验证不同计算设备:覆盖 CPU 和 GPU 等实现;

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.

好的

const std::vector<int64_t>& stride,
int64_t offset,
DenseTensor* out) {
AsStridedKernel<Context>(dev_ctx, source, dims, stride, offset, out);
Copy link
Contributor

Choose a reason for hiding this comment

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

如与as_strided 类似,是否还需要新增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.

因为 set_ 的参数比 as_strided 多了一个 source tensor,如果把 AsStridedKernel 改为 inplace,然后传入 source 的shape/stride/offset,

return _C_ops.as_strided_(x, shape, stride, offset)

这样没法做到让 x 和 source 共享 holder_,也就是 x._is_shared_buffer_with(source) 会返回 false
image

Copy link
Contributor

Choose a reason for hiding this comment

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

好的


## 2、功能目标
- 实现 paddle.Tensor.set_ 做为 Tensor 的方法使用。设置 Tensor 与 source 共享相同的存储空间,且可设置为与 source 相同或不同的 shape,stride,offset。
- 实现 paddle.Tensor.resize_ 做为 Tensor 的方法使用。重新调整 Tensor 的 size,若新 numel 小于等于原 numel,不改变存储空间大小;若新 numel 大于原 numel,需调整存储空间大小。
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么小于原 numel时,不改变存储空间大小?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
这个存储空间是指 storage 接口返回的数组,paddle 对应的应该是 holder_ 这个 Tensor 的内部属性吧

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

@zxcd zxcd 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 88ed593 into PaddlePaddle:master Oct 11, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants