修复 as_strided 转换规则:字节 offset 换算为元素 offset - #722
Open
feixi139 wants to merge 1 commit into
Open
Conversation
Paddle 的 offset 以字节计,Torch 的 storage_offset 以元素计,原映射直接 透传导致 offset 非 0 时两侧起点不同,比较结果无意义。新增 AsStridedRule 完成单位换算并拒绝负数或未按 itemsize 对齐的 offset,同时补上缺失的 paddle.Tensor.as_strided 条目。附带用于覆盖重叠视图反向的语料。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
paddle.as_strided的offset以字节计,torch.as_strided的storage_offset以元素计。原 mapping 用paddle_torch_args_map把offset直接透传给storage_offset,两者单位不同,offset != 0时 Paddle 侧和 Torch 侧从不同位置开始取数,比较结果没有意义(float32 下起点相差 4 倍,float64 下 8 倍),且不会报错,属于静默错位。此外
paddle.Tensor.as_strided在 mapping 中缺失。修改内容
tester/paddle_to_torch/mapping.jsonpaddle.as_strided由通用参数映射改为"Rule": "AsStridedRule";paddle.Tensor.as_strided条目,同样指向AsStridedRule。tester/paddle_to_torch/rules.pyAsStridedRule:在 preprocess 中用x.element_size()把字节 offset 换算为元素 offset;对负数或未按 itemsize 对齐的 offset 提前抛ValueError,避免以错误单位静默产出结果;函数式调用走torch.as_strided(input=x, ...),方法式走x.as_strided(...)。tester/api_config/11_fix_op/as_strided_unfold_overlap_grad.txt(新增,31 条)paddle.as_strided18 条、paddle.Tensor.as_strided2 条、paddle.unfold11 条;覆盖重复 stride(1,1)、零 stride(0,0)/(1,0)、带 offset、float32/float64/float16/bfloat16/complex64/complex128,以及step < size的 unfold,并含非重叠对照配置。验证
以下命令在本分支上实际执行并通过:
pre-commit run --files tester/paddle_to_torch/mapping.json tester/paddle_to_torch/rules.py tester/api_config/11_fix_op/as_strided_unfold_overlap_grad.txt首轮
ruff format自动重排了rules.py一处换行,复核 diff 后重跑,全部 hook 通过。python -m py_compile tester/paddle_to_torch/rules.pygit diff --checkget_converter()完成全量 mapping 校验(该校验会检查每个条目声明的Rule名与注册表一致),两个新条目均解析到AsStridedRule;生成的 preprocess / core 均可编译。offset=4字节 →storage_offset=1;float64 +offset=16→storage_offset=2;方法式offset=0→0;offset=1(未对齐)按预期抛ValueError。未执行:新增语料尚未在本分支上跑
engineV4.py。该语料此前在另一 checkout 上用未修复的 Paddle 跑过(GPU 7 pass / 24 fail、CPU 6 pass / 25 fail),用于确认它能暴露 Paddle 侧缺陷;对应的 Paddle 修复尚未合入,因此本 PR 不声明语料全绿。