已合并
refactor(examples): Rewrite the basic example #4922
lidongsheng创建于 7月16日
refactor(examples): Rewrite the basic example #4922
已合并
lidongsheng创建于 7月16日
lidongsheng成员
7月16日

Rewrite the hello_world/basic example

Related Issues: #2672

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 lidongsheng 的贡献)
Llidongsheng成员
7月16日 创建了 pull request,commit 74fa5d19
atomgit-bot
atomgit-bot
7月16日 评论:

变更摘要

本次 PR 对 PyPTO 的入门示例进行了大规模重写,旨在精简和现代化示例代码。核心变化包括:大幅简化 hello_worldbasic_ops 示例,从旧版 API(如 pypto.frontend.jitout.move()、显式 shape/dtype 声明)迁移到新版 API(pypto.jitpypto.Tensor[...] 自动推断、out[:] 赋值语法);删除了 symbolic_scalar.pytensor_creation.pyvalidate_examples.pyvalidate_examples.sh 等冗余文件;同时在 pypto/__init__.py 中暴露了顶层的 jit 便捷接口。共计删除约 3037 行,新增约 158 行。

主要改动

  • hello_world.py 大幅精简:移除了 165 行复杂代码(设备 ID 校验、示例注册/列表系统、get_device_id 辅助函数等),将单一核函数 add_kernel 改为使用 @pypto.jit 装饰器、pypto.Tensor[...] 自动推断 shape/dtype 以及 out[:] 赋值语法,并通过 torch.testing.assert_close 统一结果校验。

  • basic_ops.py 重写为现代化示例集:将原始 7 个示例合并为 5 个核心测试函数(test_addtest_erfctest_matmultest_sumtest_dynamic_add),全面采用 pypto.Tensor[[...], dtype] 语法、out[:] 赋值替代 out.move(),以及 torch.testing.assert_close 替代手动 numpy 比较;新增 dynamic_add_kernel 演示 pypto.DYNAMIC 动态维度与 pypto.loop/pypto.view/pypto.assemble 的分块循环模式。

  • 删除冗余示例文件:移除了 symbolic_scalar.py(307 行)、tensor_creation.py(399 行)、validate_examples.py(1662 行)和 validate_examples.sh(44 行),这些文件的功能已不再需要或已整合到简化后的示例中。

  • pypto/__init__.py 暴露顶层 jit 接口:新增 jit = frontend.jit 一行,使用户可直接通过 pypto.jit 访问 JIT 装饰器,简化了内核函数的声明方式。

  • 命令行接口统一简化hello_world.pybasic_ops.py 的命令行参数均改为 -m/--run_mode 选择 npu/sim 模式,basic_ops.py 额外支持 -t/--tests 按名称选择测试用例,移除了旧的 --list 列表式示例注册机制。

likedislike
atomgit-bot
atomgit-bot
7月16日 评论:

代码审查

已完成对所有 7 个变更文件的逐文件审查。以下是总结:


审查总结

按优先级统计

  • P0: 0
  • P1: 0
  • P2: 3
  • P3: 0

各文件审查结果

文件 状态
examples/00_hello_world/README.md ✅ 无问题
examples/00_hello_world/README_en.md ✅ 无问题
examples/00_hello_world/hello_world.py ✅ 无问题
examples/01_beginner/basic/README.md ⚠️ 1 个 P2(命令示例与描述不匹配)
examples/01_beginner/basic/README_en.md ⚠️ 1 个 P2(同上)
examples/01_beginner/basic/basic_ops.py ⚠️ 1 个 P2(dynamic_add 在 SIM 模式下静默跳过验证)
python/pypto/__init__.py ✅ 无问题

整体风险判断

低风险。 本次重构主要是对 Hello World 和 Basic 示例的精简重写,代码逻辑正确。发现的 3 个 P2 问题均属于文档/示例层面的缺陷(README 命令示例与描述不符、测试在 SIM 模式下静默假通过),不影响核心运行时正确性,但可能误导初学者用户。建议在合入前修复这些 README 和示例脚本的问题。

类型 数量
🔴 阻塞 0
🟡 建议 4

💬 仅评论

likedislike
CANN-robotCANN-robot成员
7月16日 添加了label:stat/needs-squash
lidongsheng成员
7月16日 评论:

compile

likedislike
此处折叠了137条消息 查看更多
CANN-robotCANN-robot成员
7月20日 添加了label:ci-pipeline-passed
杨旭
杨旭成员
7月20日 评论:

/lgtm

likedislike
Jie Yin成员
7月20日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月20日 添加了label:lgtmapproved
CANN-robotCANN-robot成员
7月20日 合入了pull request