How to Run Model Tests on CPU and GPU in GitHub Actions
Mark GPU-only tests with a pytest marker and split them into a separate GPU job so CPU tests stay fast and cheap.
Add a gpu marker to tests that need CUDA. Run pytest -m "not gpu" on a CPU runner for the bulk of the suite and pytest -m gpu on a GPU runner for the rest.
Steps
- Register a
gpumarker inpytest.iniand mark GPU-only tests. - Run
pytest -m "not gpu"onubuntu-latest. - Run
pytest -m gpuon the GPU runner.
Workflow
.github/workflows/ci.yml
jobs:
cpu-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip install -e . && pytest -m "not gpu" -q
gpu-tests:
runs-on: [self-hosted, gpu]
steps:
- uses: actions/checkout@v4
- run: pip install -e . && pytest -m gpu -qGotchas
- Add
skipif(not torch.cuda.is_available())so-m gpuis a no-op on CPU rather than an error. - Keep the CPU suite the default so most PRs never touch a GPU runner.
Related guides
How to Verify CUDA Is Available in GitHub ActionsConfirm a GitHub Actions GPU job actually sees the GPU by checking nvidia-smi and torch.cuda.is_available bef…
How to Smoke-Test Training With One Step in GitHub ActionsRun a fast training smoke test in GitHub Actions that executes a single step on tiny data, catching shape and…