Skip to content

Conversation

@zeroRains
Copy link
Contributor

@zeroRains zeroRains commented Sep 1, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

问题描述
当前paddle.Tensor的构造函数中新增了4种构造方式,他们都是依赖于paddle.empty或paddle.tensor去实现的。这两个API会自己检测当前的设备从而去确定设置到哪个place上。但新增的paddle.Tensor()构造方法其实都是希望在不指定device时,创建在CPU上的。

在实现过程中,其实只有在调original_init()的时候设置了device,而在用paddle.empty或paddle.tensor创建Tensor时没有设置device,这就导致了GPU环境下paddle.Tensor()执行时,会先创建GPU的paddle.empty([0]),然后再通过original_init()转化成cpu的Tensor存在拷贝开销。

此外在python/paddle/incubate/multiprocessing/reductions.py中,
image
这个位置会走一步先empty构造成GPU Tensor再用original_init构造成CPU Tensor的操作。

如果这个函数在multiprocess创建的子进程中调用,则会出现cuda环境初始化失败的现象,进而导致Tensor创建失败。
image

解决方案
对于新增的几个构造方法,除了给original_init添加place参数外,还给paddle.empty()和paddle.tensor()里添加device参数,确保创建Tensor的过程中,place保持一致。

pcard-71500

@paddle-bot
Copy link

paddle-bot bot commented Sep 1, 2025

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

Copy link

@Jiang-Jia-Jun Jiang-Jia-Jun 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

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 merged commit 29a3ff5 into PaddlePaddle:develop Sep 2, 2025
54 checks passed
@zeroRains zeroRains deleted the Tensor branch September 2, 2025 03:56
cqulilujia pushed a commit to cqulilujia/Paddle that referenced this pull request Oct 15, 2025
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