Skip to content
Latchkey

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 gpu marker in pytest.ini and mark GPU-only tests.
  • Run pytest -m "not gpu" on ubuntu-latest.
  • Run pytest -m gpu on 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 -q

Gotchas

  • Add skipif(not torch.cuda.is_available()) so -m gpu is 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

Run this faster and cheaper on Latchkey managed runners. Start free →