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

Support 0-size tensor in parameter creating, forward and backward #70504

Merged

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Dec 27, 2024

PR Category

Operator Mechanism

PR Types

New features

Description

Pcard-75624

相关issue:deepmodeling/deepmd-kit#4514 (comment)
0-size Tensor的支持需要满足前反向机制改造 + 前反向 Kernel 改造,本PR在框架的前反向相关机制上进行了适配:“放宽”了部分代码执行分支的判断条件,将0-size视为正常Tensor而不是非法Tensor抛出异常,从而让0-size Tensor能够进入某些分支进行一些预期的操作。前向、反向kernel适配见相关PR集合:paddle Tensor 规范化第二期

从端到端的角度来看,本PR的改动体现在以下三个方面:

  1. 支持创建0-size的parameter
  2. 运行机制上支持0-size的前向计算(视具体计算场景,可能需要额外依赖涉及的所有前向kernel均支持0-size输入)
  3. 运行机制上支持0-size的反向传播,包括有/无梯度累加的case(视具体计算场景,可能需要额外依赖涉及的所有反向kernel支持0-size输入,如使涉及tanh_grad,那么需要tanh_grad kernel支持0-size输入)
import paddle


def test_zero_param():
    class Model(paddle.nn.Layer):
        def __init__(self) -> None:
            super().__init__()
            self.w = self.create_parameter(shape=[0, 4], dtype="float32")

    model = Model()
    print(f"model.w.shape = {model.w.shape}")
    print(f"model.w.data_ptr() = {model.w.data_ptr()}")
    print(f"model.w.place = {model.w.place}")
    print(f"model.w.strides = {model.w.strides}")
    print(f"model.w.is_contiguous() = {model.w.is_contiguous()}")


def test_zero_forward():
    x = paddle.randn([0, 2])
    y = x + 1
    print(y.shape)


def test_zero_backward():
    x = paddle.randn([0, 2])
    x.stop_gradient = False
    y = x.tanh()
    g = paddle.grad(y, x)[0]
    print(g.shape)


if __name__ == "__main__":
    test_zero_param()
    test_zero_forward()
    test_zero_backward()

Copy link

paddle-bot bot commented Dec 27, 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.

@HydrogenSulfate HydrogenSulfate changed the title [WIP] Support 0-size tensor forward and backward Support 0-size tensor forward and backward Jan 3, 2025
@HydrogenSulfate HydrogenSulfate changed the title Support 0-size tensor forward and backward Support 0-size tensor in creating, forward and backward Jan 3, 2025
@@ -65,7 +65,7 @@ static void CopyOrAddTensor(paddle::Tensor* tensor,
*reinterpret_cast<phi::DenseTensor*>(tensor->impl().get()),
*reinterpret_cast<phi::DenseTensor*>(t.impl().get()),
reinterpret_cast<phi::DenseTensor*>(tensor->impl().get()));
} else {
} else if (t.initialized() && tensor->initialized()) {
paddle::imperative::TensorAdd<paddle::Tensor>(t, tensor);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里建议保留一个抛出异常的else分支

@@ -354,7 +355,8 @@ std::vector<paddle::Tensor> RunBackward(
paddle::Tensor& grad_output_tensor = grad_output_tensors[i][j];

if ((!grad_output_tensor.defined() ||
!grad_output_tensor.initialized())) {
!(grad_output_tensor.defined() &&
grad_output_tensor.has_allocation()))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

image

has_allocation内部有defined的判断,应该可以合并

Copy link
Contributor Author

@HydrogenSulfate HydrogenSulfate Jan 7, 2025

Choose a reason for hiding this comment

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

  1. 已将所有paddle::Tensor相关的defined() && has_allocation()改为has_allocation()
  2. 移除了几处分布式相关的修改(本PR只修改单机,暂不修改分布式相关逻辑)

@HydrogenSulfate HydrogenSulfate changed the title Support 0-size tensor in creating, forward and backward Support 0-size tensor in parameter creating, forward and backward Jan 7, 2025
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@HydrogenSulfate HydrogenSulfate merged commit 0452626 into PaddlePaddle:develop Jan 7, 2025
33 of 34 checks passed
@HydrogenSulfate HydrogenSulfate deleted the support_0-size_fwd_bwd branch January 7, 2025 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants