Skip to content

Conversation

@zeroRains
Copy link
Contributor

@zeroRains zeroRains commented Aug 14, 2025

PR Category

User Experience

PR Types

New features

Description

增强paddle.Tensor的构造能力,具体如下:

  1. paddle.Tensor(): 空输入时,结果与torch.Tensor不对齐,使用paddle.empty([0])才能对齐
  2. paddle.Tensor(1,2,3): PR前不支持任意长度int数字进行构造,使用paddle.empty([1,2,3])对齐
  3. paddle.Tensor([1,2,3]): PR前不支持原生List和Tuple的输入,使用paddle.to_tensor([1,2,3])对齐

ps: torch.Tensor只会构建float32类型的数据,所以新增的构造方法全部都会设置成float32

在PaConvert下自测,paddle.Tensor()与torch.Tensor()对齐。

pcard-71500

image

@paddle-bot
Copy link

paddle-bot bot commented Aug 14, 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.

@zeroRains zeroRains changed the title Support more construction method for paddle.Tensor [API compatibility] Support more construction method for paddle.Tensor Aug 14, 2025
@codecov-commenter
Copy link

codecov-commenter commented Aug 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@b95cb68). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #74619   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?        19           
  Branches           ?         0           
===========================================
  Hits               ?        19           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

这个直接在 [dtype]Tensor 那里加一个别名,就可以实现

@zeroRains
Copy link
Contributor Author

zeroRains commented Aug 19, 2025

这个直接在 [dtype]Tensor 那里加一个别名,就可以实现

是import FloatTensor as Tensor这样的别名吗?因为FloatTensor的最后一个条件是通过paddle.Tensor进行构建,这个别名为Tensor之后,应该会有循环引用的问题。我先验证一下。验证好了,不会有循环引用的问题。

但是他存在之前实现版本相同的问题:起了一个别名之后新的Tensor类就不会再是paddle.framework.core.eager.Tensor这个类了,现在很多API是使用core.eager.Tensor的接口去创建Tensor的,包括C++的kernel,其实创建的都是paddle.framework.core.eager.Tensor,这就导致了许多单测使用isinstance(tensor_a, paddle.Tensor)这样的判断会返回False导致单测挂掉,因为paddle.Tensor实际上对应的是paddle.FloatTensor而不是paddle.framework.core.eager.Tensor。所以我觉得还是得按照现在的方式去实现paddle.Tensor。

>>> import paddle
>>> paddle.Tensor()
Tensor(shape=[0], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [])
>>> a = paddle.to_tensor([2,3,3])
>>> a
Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
       [2, 3, 3])
>>> isinstance(a, paddle.Tensor)
False
>>> 

@zeroRains
Copy link
Contributor Author

zeroRains commented Aug 21, 2025

{'worse': [], 'doubt': [{'paddle.nonzero': -1.227996424892444}]} 我看这个API直接调用了_C_ops的kernel,不应该和paddle.Tensor有关系啊。

第一次rerun: {'worse': [], 'doubt': [{'paddle.nonzero': -1.227996424892444}]}
第二次rerun:{'worse': [{'paddle.isnan': -1.5488282406271936}, {'paddle.nonzero': -1.338605102102648}, {'paddle.divide': -1.4344536415095108}], 'doubt': []}
第三次rerun: {'worse': [{'paddle.cumsum': -1.432650440435821}], 'doubt': []}1
第四次rerun: {'worse': [{'paddle.any': -1.429509140805425}], 'doubt': []}

rerun 之后结果不一样了。。。

SigureMo
SigureMo previously approved these changes Aug 25, 2025
@zeroRains
Copy link
Contributor Author

/re-run all-failed

SigureMo
SigureMo previously approved these changes Aug 27, 2025
zhwesky2010

This comment was marked as resolved.

zhwesky2010
zhwesky2010 previously approved these changes Aug 27, 2025
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

@zeroRains zeroRains dismissed stale reviews from zhwesky2010 and SigureMo via a50e23b August 27, 2025 07:48
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

@zhwesky2010 zhwesky2010 merged commit 81b2add into PaddlePaddle:develop Aug 28, 2025
52 checks passed
@zeroRains zeroRains deleted the Tensor branch August 28, 2025 03:07
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.

5 participants