可用于昇腾NPU环境下的单元测试与算子测试,包含UT测试(基于torch.inductor的单元测试套件)和OP测试(单算子正确性及性能/profiling测试),支持用例重跑与多参数配置。【此简介由AI生成】
AscendForge
昇腾 (Ascend NPU) 上的测试框架,包含两部分:
- UT 测试 (
unit_test/):基于 Pytorch社区对 torch.inductor 看护的单元测试套件(inductor_tests / dynamic_shapes / opinfo)。 - OP 测试 (
op_test/):单算子正确性 + 可选性能/profiling 测试。
说明:本仓只含测试框架本身。被测对象
torch_npu/triton_experimental(原 npu_inductor) 已合入昇腾 pytorch 主仓,需要先在你的环境里装好(见下)。
目录结构
AscendForge/
├── unit_test/ # UT 测试
│ ├── run_three_cases.sh # UT 总入口(跑全部套件)
│ ├── rerun_failed.py # 失败用例重跑(吃一个清单 txt,只重跑指定用例)
│ ├── run_inductor_comparison.py # 驱动器:pytest 收集 + 并行 + 出报告
│ ├── test_torchinductor.py # 用例集 1: inductor_tests
│ ├── test_torchinductor_dynamic_shapes.py # 用例集 2: dynamic_shapes
│ ├── test_torchinductor_opinfo.py # 用例集 3: opinfo(默认关,见下)
│ └── blacklist.txt # 不跑的用例黑名单
├── op_test/ # OP 测试
│ └── test_all.py # OP 测试入口(单文件,101 个算子 case)
├── config/
│ └── env.sh.example # 本地环境配置模板
├── docs/
│ └── output_format.md # test_results 产出格式(与可视化工具的接口契约)
└── requirements.txt # PyPI 轻量依赖(pytest/numpy/typing_extensions)
环境准备(前置条件)
跑测试需要以下环境已就绪:
- NPU 硬件:Ascend NPU 卡,
npu-smi info能看到。 - CANN:匹配 NPU 驱动版本的 CANN 工具包,需要时可
source /usr/local/Ascend/cann/set_env.sh。 - 核心依赖(无法
pip一把装,需自行准备,版本须互相匹配):torch(与 torch_npu 版本/ABI 匹配,如 2.13.0)torch_npu(对应版本 + CANN)triton_ascend(triton 昇腾分支,提供triton_experimental后端)npu_inductor(被测对象,editable 安装)
- 轻量依赖(PyPI 直装):
pip install -r requirements.txt # pytest / numpy / typing_extensions
确认环境 OK:
python -c "import torch, torch_npu; print(torch.__version__, torch_npu.__version__)"
配置本地环境
把模板拷一份,按本机情况填写:
cp config/env.sh.example config/env.sh
# 编辑 config/env.sh,至少填好 ASCEND_RT_VISIBLE_DEVICES
config/env.sh已在.gitignore里,不会被提交。
运行 UT 测试
cd AscendForge
source config/env.sh # 设好卡号等环境变量
bash unit_test/run_three_cases.sh # 跑 inductor_tests + dynamic_shapes
可选参数(环境变量):
| 变量 | 默认 | 说明 |
|---|---|---|
ASCEND_RT_VISIBLE_DEVICES |
(必填) | 用哪些卡,如 0,1,2,3 |
WORKERS |
4 | 并行 worker 数 |
TIMEOUT |
600 | 单用例超时(秒) |
BACKEND |
triton | inductor backend |
RUN_OPINFO |
0 | =1 额外跑 opinfo 套件 |
直接用一行也能跑(不依赖 config/env.sh):
ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 bash unit_test/run_three_cases.sh
RUN_OPINFO=1 bash unit_test/run_three_cases.sh # 带上 opinfo
产出:test_results/<YYYYMMDD_HHMM>/{inductor_tests,dynamic_shapes}/comparison_report.txt(+ failures/ 原始日志)。详见 docs/output_format.md。
失败用例重跑
跑完一轮 UT 后,把失败用例导成一个清单 txt(每行一个完整 pytest 节点 ID,如 test_torchinductor.py::NPUTests::test_addmv_npu),只重跑这些用例。重跑复用跑 UT 时的环境(conda / CANN / ASCEND_RT_VISIBLE_DEVICES 都已在当前 shell 配好),直接调 .py 即可:
cd AscendForge
python unit_test/rerun_failed.py rerun_list.txt
python unit_test/rerun_failed.py rerun_list.txt --backend triton --workers 4
脚本会按节点 ID 的文件名自动分流到 inductor_tests / dynamic_shapes,复用 run_inductor_comparison.py 的精确执行/超时/多 worker 能力。产出结构与正常跑一致:test_results/rerun_<时间戳>/<suite>/comparison_report.txt。
运行 OP 测试
cd AscendForge
source config/env.sh # 确保卡号、CANN 环境已设
python op_test/test_all.py --continue-on-fail # 跑全部算子
python op_test/test_all.py --list # 列出所有算子 case
python op_test/test_all.py test_add test_layernorm # 只跑指定算子
python op_test/test_all.py --perf # 正确性后追加延迟基准
python op_test/test_all.py --profile # 跑 torch_npu profiler 采 kernel
主要参数:--list / --perf / --perf-warmup / --perf-iters / --profile --profile-dir <dir> / --continue-on-fail。
产出:结果打到 stdout(CASE · xxx / PASS/FAIL)。
常见问题
Q: ImportError: npu_inductor?
A: OP 测试里 import npu_inductor 受 IMPORT_NPU_INDUCTOR=1(默认)控制。需要装好 triton_experimental(已并入昇腾主仓);或 IMPORT_NPU_INDUCTOR=0 跳过该 import(仅部分算子可跑)。
Q: UT 跑起来报 device 错?
A: 检查 ASCEND_RT_VISIBLE_DEVICES 设的卡号是否真实存在且空闲(npu-smi info),以及 CANN env 是否 source 了。
Q: 产出在哪?
A: UT 在 test_results/<date>/,OP 在 stdout。产出格式见 docs/output_format.md。