Repository: PaddlePaddle/Paddle (language: python)

Issue:
[Eager][PHI] Fix eager Tensor deepcopy for 0 size Tensor
<!-- TemplateReference: https://github.com/PaddlePaddle/Paddle/wiki/PULL-REQUEST-TEMPLATE--REFERENCE -->
<!-- Demo: https://github.com/PaddlePaddle/Paddle/pull/24810 -->

### PR Category
<!-- One of [ User Experience | Execute Infrastructure | Operator Mechanism | CINN | Custom Device | Performance Optimization | Distributed Strategy | Parameter Server | Communication Library | Auto Parallel | Inference | Environment Adaptation ] -->

Execute Infrastructure

### PR Types
<!-- One of [ New features | Bug fixes | Improvements | Performance | BC Breaking | Deprecations | Docs | Devs | Not User Facing | Security | Deprecations | Others ] -->

Bug fixes

### Description
<!-- Describe what you’ve done -->

问题同 #68651，方案不同，增加接口 `has_allocation` 以免影响现有底层 `initialized`

closes #68651

PCard-66972


Discussion / hints:
你的PR提交成功，感谢你对开源项目的贡献!
请关注后续CI自动化测试结果，详情请参考[Paddle-CI手册](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/paddle_ci_manual_cn.html)。
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See [Paddle CI Manual](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/paddle_ci_manual_cn.html) for details.
<img width="566" alt="image" src="https://github.com/user-attachments/assets/5d76966f-b2ce-4413-9be5-08a2af408950">

这里的描述也更新下

Fix patch:
diff --git a/paddle/phi/api/lib/tensor_method.cc b/paddle/phi/api/lib/tensor_method.cc
index bdf121e757478..d79dbbda4d526 100644
--- a/paddle/phi/api/lib/tensor_method.cc
+++ b/paddle/phi/api/lib/tensor_method.cc
@@ -92,6 +92,11 @@ void Tensor::copy_(const Tensor &src,
     return;
   }
 
+  if (src.place().GetType() == AllocationType::UNDEFINED) {
+    VLOG(8) << "Src place is UNDEFINED, skip copy";
+    return;
+  }
+
   VLOG(3) << "Deep copy Tensor from " << src.name() << " to " << name();
   if (initialized()) {
     PADDLE_ENFORCE_EQ(dtype(),


Test patch:
diff --git a/test/legacy_test/test_lazy_init.py b/test/legacy_test/test_lazy_init.py
index cdfb3c1694aed..e6c1f6821fc42 100644
--- a/test/legacy_test/test_lazy_init.py
+++ b/test/legacy_test/test_lazy_init.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import copy
 import unittest
 
 import numpy as np
@@ -212,5 +213,19 @@ def set_initializer(self):
         self.b_initializer = XavierUniform()
 
 
+class LinearNet(paddle.nn.Layer):
+    def __init__(self):
+        super().__init__()
+        self.linear = paddle.nn.Linear(10, 10)
+
+
+class TestDeepCopyLazyInitializedParam(unittest.TestCase):
+    def test_deepcopy_lazy_initialized_param(self):
+        paddle.disable_static()
+        with LazyGuard():
+            net = LinearNet()
+            copy.deepcopy(net)
+
+
 if __name__ == '__main__':
     unittest.main()