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

[WIP] Support 0-size tensor forward and backward #70504

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Dec 27, 2024

PR Category

Operator Mechanism

PR Types

New features

Description

Pcard-75624

0-size Tensor的支持需要满足前反向机制改造 + 前反向 Kernel 改造,本PR仅修改了框架的前反向相关机制,“放宽”了部分代码执行分支的判断条件,将0-size视为正常Tensor不报错,而不是非法Tensor抛出异常,以及0-size Tensor能够进入某些预期的分支进行一些操作,接着再根据所需跑通的模型,为涉及到的Kernel适配0-size Tensor的逻辑,最终支持0-size Tensor的计算。

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()

TODO:

  • 支持0-size静态图前反向

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.

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.

1 participant