A vanilla pip install torch on a CUDA machine pulls CUDA-enabled wheels (~2.5 GB). Pin CPU-only with --index-url https://download.pytorch.org/whl/cpu and you save 2 GB per layer, faster builds, smaller images. If you ever need GPU, switch the index URL at build time.
pip install torch==x.y.z —index-url https://download.pytorch.org/whl/cpu
Then pip install -r requirements.txt (avoid letting requirements.txt pull torch again).
Bake this into the builder stage; CPU-only is fine for inference unless the workload actually needs GPU.
pip install torch --index-url .../whl/cpu
THEN pip install -r requirements.txt
(do not let requirements.txt bring in another torch)
Two GB per image adds up. Pin CPU-only torch first; switch the index URL only when you need GPU.