tinygrad 本地怎么运行 backend、null、unit 三组测试套件
【免费下载链接】tinygradYou like pytorch? You like micrograd? You love tinygrad! ❤️项目地址: https://gitcode.com/GitHub_Trending/tiny/tinygrad
你在 tinygrad 仓库里改了代码,想按 CI 的方式在本地把三组核心测试跑一遍验证改动。test/README 定义了 CI 实际运行的三组测试:backend(在各类后端上运行的测试)、null(不需要任何后端的测试)、unit(CI 中只在单个后端上运行的测试)。这篇文章基于 README.md 的 "Running tests" 一节和 CI 工作流 .github/workflows/test.yml 中的testlinux、nulltest、unittest三个 job,给出在本地依次运行这三组测试的命令、依赖安装方式和结果判断方式。
准备条件
tinygrad 推荐从源码安装(见 README.md 的 "Installation" 一节),要求 Python 3.11 及以上(pyproject.toml 中requires-python = ">=3.11"):
git clone https://github.com/tinygrad/tinygrad.git cd tinygrad python3 -m pip install -e .测试依赖按 README 的说法用 extra 安装:
python3 -m pip install -e '.[testing]' # install extra deps for testing.[testing]包含 README 中"运行测试"一节要求的全部测试依赖。如果你只想按 CI 中nulltest/unittestjob 的最小依赖来装(对应 pyproject.toml 里的testing_unit,不含 pillow、onnx 等模型类依赖),可以改装:
python3 -m pip install -e '.[testing_unit]'-n参数依赖pytest-xdist,三个 extra 都包含它;CI 命令统一使用-n=auto并行执行。
运行 null 组:不依赖任何后端
nulltestjob 在 test.yml 中的核心命令是:
SPEC=2 DEV=NULL python -m pytest -n=auto test/null/ --durations=20说明:
DEV=NULL把设备指定为 NULL 后端,test/README对 null 组的定义就是"tests that don't require any backend";SPEC=2是 CI 该 job 实际使用的环境变量,本地保持与 CI 一致;--durations=20在结束后输出最慢的 20 个测试,方便你判断耗时。
该 job 还有几条针对单个测试的定向命令,适合在整组失败时定位:
DEV=NULL python3 -m unittest test.backend.test_multitensor.TestMultiTensor.test_data_parallel_resnet_train_step DEV=NULL VIZ=1 python3 -m pytest -n=auto test/null/test_viz.py运行 unit 组:在单个后端上跑
unittestjob 的核心命令是:
DEV=CPU python -m pytest -n=auto test/unit/ --durations=20该 job 执行前还有一个前置检查,确认默认设备是 CPU:
python -c "from tinygrad import Device; assert Device.DEFAULT == 'CPU', Device.DEFAULT"如果你的机器没有 GPU 或不想跑 GPU 路径,DEV=CPU就是 CI 的选择。CI 还额外跑了一条 GC 测试python test/external/external_uop_gc.py,属于 unit job 的附加步骤,本地可以按需执行。
运行 backend 组:选定后端跑一遍
backend组的定义是"tests that run on each backend",所以本地运行时你需要通过DEV环境变量指定一个后端。CI 的testlinuxjob 对每个后端矩阵元素执行的命令是:
python -m pytest -n=auto test/backend --durations=20其中DEV在 CI 矩阵里取CPU:CLANG、CPU:LLVM、CPU:LVP、CPU:X86、CL、WEBGPU等值(部分取值还需要 CI 单独安装的 OpenCL、LLVM 20、mesa 等系统组件,见 .github/actions/setup-tinygrad/action.yml)。本地没有对应组件时,最简单的起点是 CI 的 Python 后端 job(bepython),它只装testing_unit依赖:
SKIP_SLOW_TEST=1 DEV=PYTHON python3 -m pytest -n=auto test/backend/test_dtype.py test/backend/test_dtype_alu.py test/backend/test_ops.py test/backend/test_uops.py test/backend/test_symbolic_ops.py test/backend/test_renderer_failures.py::TestRendererFailures --durations=20SKIP_SLOW_TEST=1会跳过慢测试,是 CI 该 job 自带的写法。
README 还给了一个只跑单个测试文件的最短示例:
python3 test/backend/test_ops.py # just the ops tests结果判断与限制
- 三组命令都用
pytest,成功条件是全部测试通过、没有 failed/error;--durations=20会在末尾列出耗时前 20 的测试,用于定位慢项而不是判定失败。 - conftest.py 给每个测试挂了超时保护:默认 120 秒(可用环境变量
TEST_TIMEOUT调整),超时进程直接收到SIGABRT。如果你发现测试被信号打断而不是断言失败,先检查是否触发了这个超时。 - 仓库的 AGENTS.md 建议用
-n12固定 12 个并行 worker 提速(README 也注明 "always run tests with-n12for speed")。CI 用-n=auto由机器决定并发数,本地两种写法都可用。 - 如果只想冒烟验证而不是整组测试,README 给出的全量入口是
python3 -m pytest test/,但它包含test/external之外的整个test目录,范围比三组测试大,不建议作为本场景的日常路径。
可选:安装 pre-commit 钩子
README "Running tests" 一节还提到:pre-commit install会在每次 commit 时自动运行 linter、mypy 和一部分测试。这属于开发工作流配置,不是运行三组测试的前置步骤,只在你要提交代码时安装。
三组测试跑完且无失败后,你的本地验证就与 CI 的backend/null/unit三个 job 对应上了;CI 的完整命令细节可以随时对照 test.yml 中同名的 job。
【免费下载链接】tinygradYou like pytorch? You like micrograd? You love tinygrad! ❤️项目地址: https://gitcode.com/GitHub_Trending/tiny/tinygrad
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考