How to Handle GPU Memory Limits and OOM in GitHub Actions
Cap batch size, call torch.cuda.empty_cache between tests, and assert peak memory so a job stays within the runner GPU and OOM is caught as a test failure.
Reset the peak-memory counter, run the step, then assert max_memory_allocated stays under a budget. Clear the cache between tests so one heavy test does not OOM the next.
Steps
- Reset peak memory with
torch.cuda.reset_peak_memory_stats(). - Run the step, then assert
max_memory_allocatedis under budget. - Call
torch.cuda.empty_cache()between tests.
Workflow
.github/workflows/ci.yml
steps:
- name: Peak memory gate
run: |
python - <<'PY'
import torch
from model import build, batch
torch.cuda.reset_peak_memory_stats()
m = build().cuda(); x, _ = batch()
m(x.cuda()).mean().backward()
peak = torch.cuda.max_memory_allocated() / 1e9
print(f"peak {peak:.2f} GB")
assert peak < 14.0, "exceeds GPU memory budget"
torch.cuda.empty_cache()
PYGotchas
- Fragmentation can OOM below the nominal limit; set
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:Trueto reduce it. - On a shared self-hosted GPU, another job can consume memory; serialize GPU jobs with a concurrency group.
Related guides
How to Test Mixed Precision Training in GitHub ActionsRun a mixed precision (AMP) training step in GitHub Actions with autocast and GradScaler, asserting the loss…
How to Add an Inference Latency Gate in GitHub ActionsBenchmark inference latency in GitHub Actions and fail the run when p95 exceeds a threshold, so a change that…