Skip to content

Use CPU-only torch for Docker builds to reduce image size#2946

Merged
adamtheturtle merged 4 commits intomainfrom
adamtheturtle/cpu-only-torch
Feb 18, 2026
Merged

Use CPU-only torch for Docker builds to reduce image size#2946
adamtheturtle merged 4 commits intomainfrom
adamtheturtle/cpu-only-torch

Conversation

@adamtheturtle
Copy link
Member

@adamtheturtle adamtheturtle commented Feb 18, 2026

Summary

Configure uv to install CPU-only torch from PyTorch's dedicated index via [tool.uv] configuration in pyproject.toml. Update the Dockerfile to use uv sync (which respects tool.uv sources) instead of uv pip install, and upgrade uv from 0.1.44 to 0.5.0 for source configuration support. This reduces the Docker image size by ~3GB by removing unnecessary CUDA dependencies.

Changes

  • pyproject.toml: Add [tool.uv] with index configuration for CPU-only torch from https://download.pytorch.org/whl/cpu
  • Dockerfile:
    • Upgrade uv from 0.1.44 to 0.5.0
    • Switch from uv pip install --editable . to uv sync --no-cache
    • Remove manual venv creation (handled by uv sync)
    • Add UV_PROJECT_ENVIRONMENT to direct uv to the container's venv location

Impact

  • Docker image is ~3GB smaller (no CUDA dependencies)
  • Build time is faster (smaller downloads)
  • Behavior unchanged: torch CPU variant works fine for scoring/evaluation in the Flask server

🤖 Generated with Claude Code


Note

Medium Risk
Changes dependency resolution and Docker build/install flow for torch/torchvision, which could cause build failures or runtime incompatibilities if the CPU wheels or uv config behave differently across environments.

Overview
Configures uv to install torch and torchvision from PyTorch’s CPU-only wheel index via new [tool.uv] entries in pyproject.toml, and adds torchvision as an explicit dependency (with a deptry ignore) so it can be routed to that index.

Updates the Flask server Docker build to set UV_PROJECT_ENVIRONMENT and use uv sync --no-cache (instead of uv pip install --editable .) when installing into the container venv.

Written by Cursor Bugbot for commit 9f484ea. This will update automatically on new commits. Configure here.

adamtheturtle and others added 2 commits February 18, 2026 09:20
Configure uv to install CPU-only torch from PyTorch's dedicated index via
[tool.uv.sources] in pyproject.toml. Update Dockerfile to use uv sync (which
respects uv.sources) instead of uv pip install, and upgrade uv from 0.1.44
to 0.5.0 for [tool.uv.sources] support. This reduces the Docker image by
~3GB (CUDA libraries are unnecessary for the CPU-only Flask server).

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Add torchvision to [tool.uv.sources] so it also comes from the CPU index
(piq depends on torchvision, which crashes on import with CPU-only torch
when installed from PyPI). Restore python3 -m venv in the Dockerfile so
pip install uv puts the uv binary into the venv's bin directory (which is
on PATH), rather than ~/.local/bin (which is not).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
tool.uv.sources only applies to direct dependencies; torchvision was only
a transitive dependency via piq, so sources.torchvision was silently ignored
and PyPI's torchvision was installed instead. PyPI's torchvision registers
torchvision::nms operators that don't exist in CPU-only torch, causing
RuntimeError on import. Making torchvision a direct dependency causes uv
to route it through the CPU index.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@adamtheturtle adamtheturtle merged commit 772c4f0 into main Feb 18, 2026
106 checks passed
@adamtheturtle adamtheturtle deleted the adamtheturtle/cpu-only-torch branch February 18, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant