修复 reshape 系列转换规则:新增 ReshapeInplaceRule 并对齐 ValidateShape - #721
Open
feixi139 wants to merge 1 commit into
Open
Conversation
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.
背景
reshape系列的 Paddle→Torch 转换规则存在三个问题:paddle.reshape_与paddle.Tensor.reshape_没有任何映射;paddle._C_ops.reshape_由CopsReshapeRule以ConversionKind.DIRECT承载,其 docstring 声称通过set_实现 in-place,但代码里从未调用set_,实际不具备 in-place 语义。ReshapeRule与CopsReshapeRule各自持有一份 shape 归一化逻辑,两份都与 Paddle 的真值paddle/phi/infermeta/unary.cc中的ValidateShape不符:shape里的0下标越界(i >= x.ndim)且输入非空时,Paddle 抛(InvalidArgument) If The index of 0 in 'shape' >= the input tensor X's dimensions, It can only be Zero-Sized Tensor(unary.cc:2423),而原逻辑直接索引in_dims[i]抛出 PythonIndexError。该文本不在tester/base.pyclassify_runtime_error的白名单内,导致无效配置被误分类为torch_error。0与-1时,Paddle 按非零维乘积之比推导([0,2,4] → [0,-1]得[0,8]),原逻辑用in_size // capacity恒得0,产出静默的错误形状,表现为paddle_accuracy形状不匹配。tester/input_generation/generation_rules.py的generate_reshape_inputs在state["maxvalue"] //= shape[index]前缺少下标检查,0越界时抛IndexError。该异常在--paddle_only模式的 Input 阶段即触发,掩盖了 Paddle 侧真正的InvalidArgument。修改内容
tester/paddle_to_torch/rules.py_RESHAPE_VALIDATE_SHAPE,逐条复刻unary.cc的ValidateShape,报错文本统一带(InvalidArgument)前缀以便classify_runtime_error归类为config_input。ReshapeInplaceRule(ConversionKind.COMPOSITE),承载paddle.reshape_、paddle.Tensor.reshape_、paddle._C_ops.reshape_。实现为:目标 shape 与原 shape 相同时短路返回x(对齐python/paddle/tensor/manipulation.py的 Python 层提前返回,不触发CheckInplace);否则拦截需要梯度的叶子 Tensor(对齐paddle/fluid/eager/utils.cc的CheckInplace)后执行x.set_(torch.reshape(x, shape))。set_不包裹no_grad,与既有CopsFlattenRule一致;包裹后x会保留形状已变的旧grad_fn,反向校验失败。ReshapeRule改为复用共享常量,删除内联的重复归一化逻辑。CopsReshapeRule。tester/paddle_to_torch/mapping.json:新增paddle.reshape_、paddle.Tensor.reshape_两项指向ReshapeInplaceRule;paddle._C_ops.reshape_由CopsReshapeRule改指ReshapeInplaceRule。tester/input_generation/generation_rules.py:为generate_reshape_inputs补下标守卫,0越界时不再抛IndexError,交由 Paddle 报InvalidArgument。验证
pre-commit(全部 hook 通过,无文件被自动修改):
语法与格式检查:
转换链路核对:
paddle.reshape/paddle.Tensor.reshape为DIRECT,三个reshape_入口为COMPOSITE,五者共用同一份 60 行预处理;CopsReshapeRule已不存在。功能用例(
engineV4.py --accuracy=True --bitwise_alignment=True,即atol=rtol=0逐位对齐,日志头已确认--atol: 0.0 / --rtol: 0.0)共 67 条,结果为 43pass/ 24config_input,未出现torch_error、paddle_error、paddle_accuracy或torch_accuracy:24 条
config_input均为配置本身无效,可逐条对应到ValidateShape的具体位置:多个-1(unary.cc:2400)、除-1外的负维(unary.cc:2432)、非空输入上0下标越界(unary.cc:2423)、numel 不匹配(unary.cc:2533)。修改前,其中涉及0下标越界的用例被误分类为torch_error,zero-size 混合0与-1的用例为静默的paddle_accuracy。回归:
447 条配置,436 pass / 6 skip / 2 paddle_error / 3 torch_error,与本次修改前的基线逐字节一致;残留失败为
fused_swiglu_weighted_clamp_bwd与fused_linear_param_grad_add,与 reshape 无关。该回归是以 runner 默认参数执行的,未开启--bitwise_alignment。附带影响
paddle._C_ops.reshape_的ConversionKind由DIRECT变为COMPOSITE,tester/torch_gpu_performance.py:80会将其标记为combined。若性能报表按单 kernel 统计,此处存在口径变化。