fix(backend): use CPU-only PyTorch to shrink Docker image - #43
Conversation
Install torch from the CPU wheel index in Docker and CI so the backend image drops from ~9 GB to ~2–3 GB, reducing ImagePullBackOff failures on K8s dev rollouts. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Summary
Approved. This is a focused, correct fix for oversized backend images. Splitting PyTorch out of the pip requirements chain and installing CPU wheels from the PyTorch index first is the right pattern — it prevents transformers (or a pinned torch line in requirements.txt) from pulling the default CUDA build from PyPI.
What looks good
- Dockerfile installs
torch/torchvisionfromdownload.pytorch.org/whl/cpubeforerequirements-core.txt, so later deps cannot override with CUDA wheels. requirements-core.txtsplit cleanly separates ML runtime deps from the torch install path;requirements.txtremains a convenience wrapper with clear comments.- CI
backend-testnow mirrors Docker: CPU torch →requirements-core→requirements-dev. This also fixes a subtle issue onmain, whererequirements.txtstill pinnedtorch==2.9.1from PyPI and could reinstall CUDA torch after the explicit CPU install. - Docs (
apps/backend/README.md) document the CPU-only image rationale and updated local setup. - Runtime impact: none expected — inference is already CPU-only (
DEVICE=cpu).
Minor nits (non-blocking)
- Root
README.mdlocal backend setup (around line 268) still sayspip install -r requirements-dev.txtwithout the CPU torch pre-install step.apps/backend/README.mdwas updated; consider aligning the root README in a follow-up. - Torch version triplication —
2.9.1/0.24.1appear in Dockerfile, CI, and README. Fine for now; a one-line header comment inrequirements-core.txt("install CPU torch first — see Dockerfile") would help future editors. - Lint CI job still installs via
requirements-dev.txtonly;transformersmay still pull a full torch wheel transitively even though lint doesn't need it. Pre-existing / out of scope, but worth knowing for CI time.
Verification
Static review in cloud env. CI on this PR: lint, frontend tests/build, and K8s render passed; backend tests were still running at review time. Author-reported image size (~2.76 GB vs ~9 GB) matches the expected win.
Safe to merge once backend tests finish green and post-merge CD smoke (per test plan) confirms the slimmer image pulls and /healthz + /readyz pass.
Sent by Cursor Automation: PD Care PR Reviewer
| @@ -0,0 +1,15 @@ | |||
| fastapi | |||
There was a problem hiding this comment.
Consider a header comment here noting that torch/torchvision must be installed separately from the CPU wheel index before this file (see Dockerfile / CI). Without that, pip install -r requirements-core.txt alone will let transformers pull the default (CUDA) PyPI torch.
| RUN python -m pip install --upgrade pip && python -m pip install -r requirements.txt | ||
| COPY requirements-core.txt ./requirements-core.txt | ||
| RUN python -m pip install --upgrade pip && \ | ||
| python -m pip install torch==2.9.1 torchvision==0.24.1 \ |
There was a problem hiding this comment.
Install order looks correct: CPU wheels first, then requirements-core.txt. That prevents transformers from dragging in the ~9 GB CUDA build during the same layer.
Document the CPU wheel pre-install in the root README and add a header comment in requirements-core.txt so editors know torch is installed separately. Co-authored-by: Cursor <cursoragent@cursor.com>


Summary
Test plan