[ML] Fix PyTorch Docker builds: copy libtorch from installed package#3091
[ML] Fix PyTorch Docker builds: copy libtorch from installed package#3091edsavage wants to merge 2 commits into
Conversation
Recent PyTorch turns `python setup.py install` into a redirect to `pip install . --no-build-isolation` (pytorch/pytorch#152276). A wheel install places the torch package (headers + libs) under Python site-packages and no longer leaves torch/include populated in the source checkout, so the post-install `cp -r torch/include/*` fails with "cp: cannot stat 'torch/include/*': No such file or directory". This broke the nightly ml-cpp-pytorch-builds image, which tracks viable/strict. The pinned v2.7.1 images still build in-tree today but will break the same way when PyTorch is next bumped. Resolve the installed torch package directory via sysconfig and copy the headers and libraries from there. This works whether the build is in-tree (older tags) or a wheel install (newer), across all three Linux images. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Pinging @elastic/ml-core (Team:ML) |
There was a problem hiding this comment.
Pull request overview
This PR fixes PyTorch Docker image builds by copying LibTorch headers and shared libraries from the installed torch Python package location (site-packages), rather than from the PyTorch source tree, which is no longer populated reliably after setup.py install redirects to pip install ..
Changes:
- Resolve the installed
torchpackage directory via Pythonsysconfigand store it inTORCH_DIR. - Copy headers from
${TORCH_DIR}/includeand shared libs from${TORCH_DIR}/libinto/usr/local/gcc133for all Linux PyTorch build images. - Apply the same approach consistently across the nightly PyTorch image and both pinned
v2.7.1Linux images.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| dev-tools/docker/pytorch_linux_image/Dockerfile | Copy headers/libs from installed torch package directory instead of the source tree to prevent post-install copy failures. |
| dev-tools/docker/linux_image/Dockerfile | Same installed-package copy approach for the pinned Linux build image. |
| dev-tools/docker/linux_aarch64_native_image/Dockerfile | Same installed-package copy approach for the pinned aarch64 native build image. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Cursor <cursoragent@cursor.com>
Test plan item 1 — non-destructive validationThe nightly Ran the fixed Dockerfile's This confirms the fix's core assumption holds against a modern (wheel-installed) torch: End-to-end confirmation of the full image (source build of |
Test plan item 2 — non-destructive validation (pinned v2.7.1, both arches)Both pinned Dockerfiles (
On "unchanged in content": the pinned images build v2.7.1 from source and |
Summary
The nightly
ml-cpp-pytorch-buildsimage build fails at the post-install header copy:(build 765)
Root cause
The build itself succeeds — the failure is the artifact copy afterwards. Recent PyTorch turns
python setup.py installinto a redirect topip install . --no-build-isolation(see pytorch/pytorch#152276):A wheel install places the built
torchpackage (headers + libs) under Python site-packages and no longer leavestorch/includepopulated in the source checkout. The Dockerfiles then copy from the stale source-tree pathtorch/include/torch/liband fail.This bit the nightly first because it tracks
viable/strict. The pinnedv2.7.1images (linux_image,linux_aarch64_native_image) still build in-tree today, so they pass — but they will break the same way when PyTorch is next bumped.Fix
Resolve the installed
torchpackage directory viasysconfigand copy the headers/libraries from there. This works whether the build is in-tree (older tags) or a wheel install (newer):Applied to all three Linux images (nightly + both pinned-release builds) for consistency and future-proofing.
Test plan
torch/includecopy after thesetup.py install->pip install .redirect#comment lines from the continuedRUN(chained&&stays intact) via a localdocker buildml-cpp-pytorch-buildsproduces a working image (headers + libtorch present under/usr/local/gcc133) — validated non-destructively against a real installed torch (see comment below)v2.7.1linux / aarch64 images still build and are unchanged in content — validated non-destructively on both arches (see comment below)Made with Cursor