ワンクリックで
pytorch-test
负责《Paddle API 对齐 PyTorch 项目》中 Step4 Pytorch 测试,基于 PaConvert 工具验证 Paddle API 与 PyTorch API 是否用法完全对齐一致
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
负责《Paddle API 对齐 PyTorch 项目》中 Step4 Pytorch 测试,基于 PaConvert 工具验证 Paddle API 与 PyTorch API 是否用法完全对齐一致
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | pytorch-test |
| description | 负责《Paddle API 对齐 PyTorch 项目》中 Step4 Pytorch 测试,基于 PaConvert 工具验证 Paddle API 与 PyTorch API 是否用法完全对齐一致 |
| disable-model-invocation | false |
请严格按以下 Step 依次执行,不要自行修改或跳过 Step:
${ROOT_DIR}/PaConvert/paconvert/api_mapping.json 或 ${ROOT_DIR}/PaConvert/paconvert/attribute_mapping.jsonChangePrefixMatcher,其他字段全部删除掉注意:
ChangePrefixMatcher。ChangePrefixMatcher 是 Step4 通过的标准,不可妥协:只有标记为 ChangePrefixMatcher,才能判定 API 已完成代码层面的对齐,禁止为了让测试通过而将 Matcher 改为其他类型(如 GenericMatcher、SliceScatterMatcher 等)。ChangePrefixMatcher 无法运行通过单测,则视作未对齐,需执行回退。目的: 判断是否满足如下测试规范,如不满足,则需增加测试用例使之符合规范
修改位置:
${ROOT_DIR}/PaConvert/tests/test_<API 名称>.py. 替换为 _,Tensor 类保留大写 T,最终加上 test_ 前缀和 .py 后缀
torch.argmax → test_argmax.py(顶层函数)torch.Tensor.argmax → test_Tensor_argmax.py(类方法)torch.linalg.inv → test_linalg_inv.py(子模块)测试规范:
参数覆盖要全面 测试所有可能的 Pytorch 参数用法:
func(a, b, c)func(x=a, y=b, z=c)func(a, y=b, z=c)func(z=c, x=a, y=b)func(a, dim=0,offset=1)func(a)args=(a,b); func(*args)func(input=a, other=b)输入数据要有效
测试数量要充分
测试用例模板:
import textwrap
import pytest
from apibase import APIBase
obj = APIBase("torch.target_api")
def test_case_1():
"""Basic usage"""
pytorch_code = textwrap.dedent("""
import torch
result = torch.target_api(torch.tensor([1, 2, 3]))
""")
obj.run(pytorch_code, ["result"])
def test_case_2():
"""Positional arguments test"""
pytorch_code = textwrap.dedent("""
import torch
x = torch.tensor([1.0, 2.0])
result = torch.target_api(x, 2, 1)
""")
obj.run(pytorch_code, ["result"])
def test_case_3():
"""Keyword arguments test"""
pytorch_code = textwrap.dedent("""
import torch
x = torch.tensor([1.0, 2.0])
result = torch.target_api(input=x, dim=1, dtype=torch.float32)
""")
obj.run(pytorch_code, ["result"])
def test_case_4():
"""Keyword arguments out of order test"""
pytorch_code = textwrap.dedent("""
import torch
x = torch.tensor([1.0, 2.0])
result = torch.target_api(dtype=torch.float32, dim=1, input=x)
""")
obj.run(pytorch_code, ["result"])
def test_case_5():
"""Gradient computation test"""
pytorch_code = textwrap.dedent("""
import torch
x = torch.tensor([1., 2.], requires_grad=True)
y = torch.target_api(x)
y.sum().backward()
x_grad = x.grad
""")
# Skip gradient attribute check because Paddle's stop_gradient mechanism differs from PyTorch's requires_grad mechanism at the framework level.
obj.run(pytorch_code, ["y", "x_grad"], check_stop_gradient=False)
def test_case_6():
"""Edge case test"""
pytorch_code = textwrap.dedent("""
import torch
result = torch.target_api(torch.empty(0))
""")
obj.run(pytorch_code, ["result"])
def test_case_7():
"""Expression argument test"""
pytorch_code = textwrap.dedent("""
import torch
result = torch.target_api(torch.tensor([1, 2, 3]), 1 + 1)
""")
obj.run(pytorch_code, ["result"])
单测补充完成后,按以下命令编译并验证执行:
cd ${ROOT_DIR}/Paddle/build
cmake .. && make -j$(nproc) > compile.log 2>&1
cd ${ROOT_DIR}/PaConvert/
python -m pytest tests/test_<API 名称>.py
根据报错信息修改测试用例或回退,确保所有测试用例通过。每次修改后均需要重新执行本步骤。
编译注意事项:
本步骤的通过标准是 API 可配置为 ChangePrefixMatcher,若无法配置为 ChangePrefixMatcher,则需要根据错误信息回退到对应步骤:
若判断为测试用例编写有误(如 PyTorch 代码语法错误、参数使用错误):
若判断为代码实现有误(如参数处理逻辑错误、类型转换失败等):
若判断为方案选择错误(如当前方案不适用、底层不支持等):
根本原因:PyTorch 单元测试代码自身问题
处理策略:修改 PyTorch 单元测试代码,确保是正确可执行的 Pytorch 代码
根本原因:Paddle API 修改不正确,导致代码无法执行
处理策略:回退到 Step1 或 Step2
根本原因:Paddle API 修改不正确,计算结果与 PyTorch API 存在差异
处理策略:回退到 Step1 或 Step2
场景:Paddle 暂不支持某些功能(如类型提升、标量输入、特定硬件特性等),需要跳过对应测试用例
解决方法:
使用 pytest 的 skip 标记:
@pytest.mark.skip(reason="Not supported")
def test_unsupported_case():
...
@pytest.mark.skipif(condition=not paddle.device.is_compiled_with_cuda(), reason="Test requires CUDA")
def test_case_with_cuda():
...
注意:仅允许跳过 类型提升、标量输入、特定硬件特性 导致的失败用例,其他情况不允许跳过测试用例。
场景:默认的比较逻辑不适用,需要自定义比较方式(通常不需要)
解决方法:
class CustomAPIBase(APIBase):
def compare(self, name, pytorch_result, paddle_result, ...):
# 自定义比较逻辑
pytorch_str = str(pytorch_result).removeprefix("torch.")
assert pytorch_str == paddle_result
错误现象:标记为 ChangePrefixMatcher 后,torch 的位置参数调用(如 func(a, b, c))因参数位置与 paddle 不一致而报错或结果不对齐。
根本原因:Step2 中只添加了参数名别名(通用装饰器),但没有处理参数顺序差异,导致位置传参时 torch 的第 N 个参数映射到了 paddle 错误的参数。
处理策略:返回 Step2,将通用别名装饰器升级为专用装饰器,在装饰器内通过位置参数类型/特征检测区分 torch 顺序和 paddle 顺序,将 torch 位置参数统一转换为 paddle 关键字参数后再调用。
典型案例:torch.nansum(input, dim, keepdim, *, dtype) vs paddle.nansum(x, axis, dtype, keepdim)
keepdim(bool),在 paddle 是 dtype(str/dtype)bool → keepdim,str/type → dtype)来区分两套签名顺序负责《Paddle API 对齐 PyTorch 项目》中 Step2 代码修改,实施『C++下沉』方案。通过将 Python API 下沉至 C++层,可以减少 Python 装饰器带来的性能开销,提升 API 调度效率。
负责《Paddle API 对齐 PyTorch 项目》中 Step5 更新文档,在 API 代码修改完成后,同步更新中文 API 文档,确保文档准确反映 API 的最新行为
负责《Paddle API 对齐 PyTorch 项目》中 Step2 代码修改,实施『新增 API』方案。通过新增 Paddle API(新增 API 别名、新增 Python 层 API、新增 C++算子),覆盖 Pytorch API 调用路径,实现与 PyTorch API 行为对齐。
负责《Paddle API 对齐 PyTorch 项目》中 Step2 代码修改,实施『新增 compat 类型 API』方案。在 `paddle.compat` 命名空间下新增 API,实现与 PyTorch API 行为对齐。
开展《Paddle API 对齐 PyTorch 项目》,负责项目整体统筹规划,调用多个 skill,完成输入的 API 对齐
负责《Paddle API 对齐 PyTorch 项目》中 Step3 兼容测试,为已修改的 Paddle API 添加兼容性单测并执行验证,确保 API 的 Paddle 用法与 PyTorch 用法均能正常工作。