APITest 测试修复:🐛 修复 forward-only 用例的 autograd 冗余建图#659
Merged
Conversation
wanghuancoder
approved these changes
Jun 23, 2026
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.
Title: 🐛 修复 forward-only 用例的 autograd 冗余建图
🧭 背景
PaddleAPITest 的
accuracy_stable会同时执行 Paddle 侧实现和转换后的 Torch 参考实现,用于验证多轮前向结果稳定性。对于moe_permute而言,输入规模可能很大,例如 bf16hidden_states为[1048576, 1024],按 expert 展开后的hidden_states_unzipped可达到[8388608, 1024],单个输出 tensor 约 16GB。在这种场景下,测试只需要前向结果比对,不需要反向图;旧逻辑可能让 Torch 浮点输入默认
requires_grad=True,使MoePermuteRule中的大量索引和 slice assignment 构建巨大的CopySlicesautograd 图。这个图不会参与 backward 校验,却会增加显存、运行和对象析构成本,造成 测试成功但进程崩溃 的问题。🛠️ 实现方案
本 PR 将输入是否需要 autograd 的判断集中至
TensorConfig,通过_supports_autograd/_requires_autograd表达 Torch 与 Paddle 共用策略。判断使用tester/base_config.yaml中的forward_only_apis:命中后 Torch 输入生成requires_grad=False,Paddle 输入同步设置stop_gradient=True。🔧 主要变更
1. 对齐 Torch/Paddle forward-only autograd 行为
forward-only API 不再因为浮点 dtype 自动开启 Torch autograd;对应 Paddle 输入也同步停止梯度记录。对非 forward-only API,Torch 仍按 backward 需求设置
requires_grad=True,Paddle 仍保持stop_gradient=False。2. 修复 bf16 Torch 输入 leaf 行为
bf16 backward 场景下,Torch 输入在 dtype cast 后重新设置为 leaf requires-grad tensor,避免最终输入携带 cast 产生的
grad_fn。📁 改动文件
✅ 验证
修改后经测试,moe_permute 测试行为正常,不再发生进程崩溃。