用途:为 nvtripy 添加新算子、模块或功能的测试,理解测试目录结构与约定,测试错误情况和 dtype 校验,并为多种配置编写参数化测试。
测试目录结构:tests/ 镜像源码树,包含 frontend/ops、frontend/module、trace/ops、backend、integration 等目录,以及 helper.py(测试工具)和 conftest.py(共享 fixture)。测试文件应放在 tests/<mirror_of_source_path>/test_<name>.py,并按类组织(如 TestMyOp)。
主要工具(tests/helper.py):helper.raises 用于错误测试,支持 tp.TripyException、match 正则匹配和 has_stack_info_for 栈信息校验;helper.config 用于临时修改配置(如 enable_input_validation),退出后自动恢复;NUMPY_TO_TRIPY 提供 numpy 到 tripy 的 dtype 映射(bool→tp.bool,np.int8→tp.int8,np.int32→tp.int32,np.int64→tp.int64,np.float16→tp.float16,np.float32→tp.float32)。
常见测试模式:基础算子测试断言输出;用 @pytest.mark.parametrize 覆盖支持的 dtype,并对不支持的 dtype 用 helper.raises 匹配 "Expected.*one of";从 NumPy 数据测试 dtype 映射;测试 dtype 不匹配错误;模块测试覆盖 weight/bias 与 state_dict;trace 测试检查 output.trace_tensor.producer 类型与 rank;用 tp.allclose 或 torch/numpy 参考实现比对结果。
输出验证方法:cp.from_dlpack(output).get() 转换为 numpy 数组,np.array_equal 用于整数/布尔精确比较,np.allclose 用于浮点近似比较,tp.allclose 直接比较两个 tripy 张量,torch.from_dlpack(tensor) 转为 torch 张量,output.shape 和 output.dtype 检查形状与类型。
运行方式:pytest tests/ 运行全部;pytest tests/frontend/ops/test_softmax.py 运行指定文件;pytest tests/frontend/ops/test_softmax.py::TestSoftmax::test_basic 运行指定测试;pytest -v 详细输出;pytest -k "softmax" 按模式匹配。
检查清单:测试文件位于镜像路径下;测试按类组织;包含基础功能断言、支持 dtype 的参数化测试、helper.raises 错误用例、形状校验、参考实现比对,并为参数化测试提供可读的 ids。
限制:资料未提供具体的安装命令、依赖版本、环境配置及异常处理细节,暂未提供。