Skip to content

[API Compatibility] Fix Tensor.type - #79641

Closed
Manfredss wants to merge 35 commits into
PaddlePaddle:developfrom
Manfredss:fix_Tensor_type
Closed

[API Compatibility] Fix Tensor.type#79641
Manfredss wants to merge 35 commits into
PaddlePaddle:developfrom
Manfredss:fix_Tensor_type

Conversation

@Manfredss

Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

New features

Description

修正 compat 模式下 Tensor.type() 的两处行为:

  1. 不传参时返回的是 paddle 的 dtype 字符串(如 paddle.float32),而 torch 返回的是 tensor type 名。现在按 torch.Tensor.type 的规则拼出 torch.FloatTensor / torch.cuda.FloatTensor / torch.cuda.sparse.FloatTensor,把 dtypedevicesparse COO 布局都编码进名字里;没有对应 tensor type 名的 dtype 仍回退到 dtype 字符串
  2. 内部把 dtype 当字符串传递,再靠去掉 paddle. 前缀的字符串比较来判断是否需要转换。现在统一转成真正的 paddle dtype 对象,直接比较 input.dtype,避免字符串绕路

这样 paconvert 对应用例不用再加 check_value=False 就能通过

是否引起精度变化

Manfredss added 29 commits July 27, 2026 07:51
@Manfredss
Manfredss marked this pull request as draft August 10, 2026 13:51

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

序号 位置 优先级 状态
1 decorator_utils.py:279 P2 🟡
2 compat/proxy.py:551 P2 🟡
3 compat/init.py:112 P1
4 compat/init.py:156 P1
5 compat/init.py:116 P1 🚧
Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

and args[4] in data_formats
)
is_paddle_place = isinstance(args[3], paddle.base.libpaddle.Place)
is_device = args[3] is None or (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 这里会把合法的 Paddle weight_attr 字符串误判为 PyTorch device。例如 PReLU(2, 0.5, "cpu") 的第三个业务参数本应由 ParamAttr._to_attr 转成名为 cpu 的参数属性,当前却被移到 device 并将 weight_attr 留空,破坏原生 Paddle 调用兼容性。请避免按字符串值猜测签名,并补充 "cpu" 等碰撞值的回归测试。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已核对 #79550 当前 head:相关 prelu_decorator 实现与本 PR 一致,且该意见已在 #79550 调整为 P2 并接受。撤回本条阻塞意见,无需在本 PR 修改。

_register_compat_override()
_swap_torch_modules_to_cache()
_modify_scope_of_torch_proxy(scope, silent=silent)
sys.meta_path.insert(0, TORCH_PROXY_FINDER)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 重复调用 enable_compat() 会插入同一个 finder 多次,而 disable_compat() 只移除一次;本 PR 新增的测试也明确保留了两个 finder。于是两次 enable 后进入 use_compat_guard(enable=False),内部仍残留一个 finder,torch 依旧会被代理,违反 guard 的禁用契约。请让注册幂等,或在 guard 中完整移除并恢复原 finder 数量,并覆盖该组合。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已核对 #79550 当前 head:相关 finder 生命周期实现与本 PR 一致,且该意见已在 #79550 调整为 P2 并接受。撤回本条阻塞意见,无需在本 PR 修改。

Comment thread python/paddle/compat/__init__.py Outdated
if tensor_type is None:
return str(input.dtype)
segments = ["torch"]
if input.place.is_gpu_place():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 这里只识别 GPU,导致 XPU 和 custom device 上的 Tensor 也返回 torch.FloatTensor 等无设备前缀名称,把设备元数据误报成 CPU。既然该实现承诺像 torch.Tensor.type() 一样编码 place,请按实际 place 处理 XPU/私有后端(或对无法可靠映射的设备显式回退),并补充相应设备类型测试。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

当前 head 已补齐 GPU、XPU 和 custom place 的类型名分支,并加入对应覆盖测试,原问题已修复。

return _tensor_type_name(input)

device = None
if getattr(dtype, "__name__", None) in _TENSOR_TYPE_DTYPES:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 通过类名识别会丢失工厂类的限定设备:paddle.cuda.DoubleTensorpaddle.DoubleTensor 实际导入的是同一类对象,经 torch proxy 调用 t.type(torch.cuda.DoubleTensor) 也会命中这里并被强制设为 device="cpu",把本应转到 CUDA 的 Tensor 移到 CPU。请保留限定模块的设备信息;若类对象无法区分来源,应拒绝该形式而不是静默迁移到 CPU,并补 CUDA 工厂类测试。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

当前 head 已改为带设备标记的独立 CUDA factory,并加入 CUDA 构造覆盖测试,原问题已修复。

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@codecov-commenter

codecov-commenter commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.75676% with 6 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@9401c5b). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/compat/proxy.py 89.47% 4 Missing ⚠️
python/paddle/compat/__init__.py 98.48% 1 Missing ⚠️
python/paddle/utils/decorator_utils.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #79641   +/-   ##
==========================================
  Coverage           ?   96.75%           
==========================================
  Files              ?        8           
  Lines              ?      185           
  Branches           ?        0           
==========================================
  Hits               ?      179           
  Misses             ?        6           
  Partials           ?        0           

☔ View full report in Codecov by Harness.
📢 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.

@Manfredss
Manfredss requested a review from risemeup1111 August 11, 2026 07:11
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss

Copy link
Copy Markdown
Contributor Author

@risemeup1111 关于 enable_compat 计数问题和 PReLU 函数的问题你已经在 #79550 里通过了,这个 PR 里也没有涉及这两处的修改,所以通过一下

@risemeup1111

Copy link
Copy Markdown
Contributor

已核对 #79550 当前 head:prelu_decorator 的相关实现与本 PR 一致,compat/proxy.py 也完全一致;这两项在 #79550 已调整为 P2 并接受,因此我撤回本 PR 对应的两项阻塞意见。其余改动按当前 head 继续复核。

segments = ["torch"]
place = input.place
if place.is_gpu_place():
segments.append("cuda")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 这里开始生成 torch.xpu.*/custom-device 类型名,但 _tensor_type() 解析字符串时只把 torch.cuda./paddle.cuda. 映射到 GPU,其余合法 torch.* 名称都落到 device="cpu"。因此 t.type("torch.xpu.FloatTensor")(以及 custom-device 对应名称)会把 Tensor 从 XPU/自定义设备错误迁移到 CPU;当前测试只覆盖名称生成,没有覆盖反向转换。请补齐 XPU/custom 的设备解析与 round-trip 测试。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@risemeup1111 现在的改动可以吗

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

XPU/custom 的解析已补齐,但当前修改仍不完整:torch.FloatTensor/paddle.FloatTensor 这类无设备段的 tensor type 现在返回 device=None,GPU Tensor 调用后会保留在 GPU;PyTorch 中这些名称表示 CPU tensor type,应转到 CPU。请让已识别的 tensor-type 字符串在无设备段时默认 cpu,同时保留 torch.float32 这类 dtype-only 字符串的 device=None,并补 GPU→torch.FloatTensor 回归测试。

@Manfredss Manfredss Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@risemeup1111 我觉得像 torch.FloatTensor/paddle.FloatTensor 这样的调用,本身就不应该涉及设备的转换,所以 device=None 保持设备不变是对的;即参数中有设备段或者显式表明了类(比如 paddle.cuda.DoubleTensor)才要管设备,否则保持不变

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这里的 device=None 只适用于 dtype-only 参数(例如 torch.float32),不适用于 tensor type factory。PyTorch 中 torch.FloatTensor 是 CPU tensor class,torch.cuda.FloatTensor 才是 CUDA class;本 PR 的 _tensor_type_name() 也明确把 CPU Tensor 编码成 torch.FloatTensor、GPU Tensor 编码成 torch.cuda.FloatTensor。因此 t.type(t.type()) 必须保持 round-trip,GPU Tensor 的 t.type("torch.FloatTensor") 应转换到 CPU;否则返回的类型名不能表达并恢复 Tensor 的 place。请将无设备段的 tensor-type 字符串区分为 device="cpu",同时继续让 torch.float32 保持 device=None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@risemeup1111 现在可以没 审查一下

@paddle-bot paddle-bot Bot added the contributor External developers label Aug 11, 2026
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss
Manfredss requested a review from risemeup1111 August 12, 2026 07:14
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss Manfredss closed this Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants