Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync some features with anaconda's recipe #318

Merged
merged 54 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6facae4
Add some comments to avoid duplicated error-finding
danpetry Jan 7, 2025
63aa7dd
Don't override upstream C++ standard
danpetry Jan 7, 2025
81363c0
Use N-1 cores
danpetry Jan 7, 2025
e8d6740
Add fall-through for blas if-else construct
danpetry Jan 7, 2025
e1f50ac
Don't isolate build
danpetry Jan 7, 2025
9fcb3a7
Unvendor pybind11 and eigen
danpetry Jan 7, 2025
ca90fa0
Add and run upstream smoke test
danpetry Jan 7, 2025
ff040de
Use unittest-xml-reporting instead of the historical xmlrunner
danpetry Jan 14, 2025
a2694a9
Add some testing for openmp bug, python api and torch.compile
danpetry Jan 14, 2025
6074128
reinstate `MAX_JOBS=${CPU_COUNT}` on azure
h-vetinari Jan 15, 2025
407a78d
Add description and doc_url
danpetry Jan 14, 2025
393d0d5
correct selector, move comment
danpetry Jan 14, 2025
0f9b587
Revert "Use N-1 cores"
danpetry Jan 17, 2025
4feaee5
Use N-1 cores by default
danpetry Jan 21, 2025
0fe0ba4
Remove CMAKE_ARGS conversion to individual env variables; legacy, rem…
danpetry Jan 21, 2025
14ca3d2
Hand TORCH_CUDA_ARCH_LIST to cmake rather than setting as an env vari…
danpetry Jan 21, 2025
eaaae74
Improve smoke test env variable comment
danpetry Jan 21, 2025
e19af70
Format patch with git format-patch, rename for clarity
danpetry Jan 21, 2025
939dae1
Bump version
danpetry Jan 21, 2025
af73dbe
Correct build number comment
danpetry Jan 21, 2025
65bcd3b
add build dependency on rsync rather than using platform
danpetry Jan 21, 2025
a26ede2
Don't use selectors in jinja expressions [ci skip] ***NO_CI***
danpetry Jan 23, 2025
03b2fc7
Add back disabling hypothesis tests
danpetry Jan 23, 2025
f3bfe5f
Hand contents of CMAKE_ARGS directly to CMake, to remove TORCH_CUDA_A…
danpetry Jan 23, 2025
a811bb2
Temporarily remove smoke test; it's not in the pytorch repo for v2.5.1
danpetry Jan 23, 2025
bd450c9
Revert "Hand contents of CMAKE_ARGS directly to CMake, to remove TORC…
danpetry Jan 24, 2025
6f49c62
Revert "Remove CMAKE_ARGS conversion to individual env variables; leg…
danpetry Jan 24, 2025
10bfd83
Revert "Use unittest-xml-reporting instead of the historical xmlrunner"
danpetry Jan 24, 2025
46f4e8e
appease linter
danpetry Jan 24, 2025
c1c1e6c
Merge branch 'main' into anaconda-sync
h-vetinari Jan 25, 2025
3a34b59
remove obsolete fmt handling in bld.bat
h-vetinari Jan 26, 2025
e19e11c
add pybind11 as a run-dependency of pytorch
h-vetinari Jan 26, 2025
5f19e7f
build non-CUDA builds on CPU agents
h-vetinari Jan 26, 2025
f6bbd00
MNT: Re-rendered with conda-build 25.1.1, conda-smithy 3.45.4, and co…
h-vetinari Jan 26, 2025
9864e70
Combine header messages into one
danpetry Jan 27, 2025
a002741
Change blas_impl if-else block into a case block instead
danpetry Jan 27, 2025
226f526
Correct cpu/gpu build config
danpetry Jan 27, 2025
ad37dec
MNT: Re-rendered with conda-build 25.1.1, conda-smithy 3.45.4, and co…
danpetry Jan 27, 2025
59af084
Revert "Correct cpu/gpu build config"
danpetry Jan 27, 2025
78b5aca
Run all linux builds on a gpu machine
danpetry Jan 27, 2025
db810d0
MNT: Re-rendered with conda-build 25.1.1, conda-smithy 3.45.4, and co…
danpetry Jan 27, 2025
a623264
update comment about `-std=c++<ver>` flag
h-vetinari Jan 27, 2025
e44b0d5
limit torch.backends CUDA availability tests to linux64
h-vetinari Jan 27, 2025
415a628
Revert change to hand TORCH_CUDA_ARCH_LIST as an environment variable…
danpetry Jan 28, 2025
0a2094f
set CMAKE_INSTALL_PREFIX; used in some installation commands
h-vetinari Jan 28, 2025
3889dee
fix install location of DLLs in CMake metadata; add test
h-vetinari Jan 28, 2025
982cadf
unset CMAKE_INSTALL_PREFIX again; instead, patch ATEN_INCLUDE_DIR
h-vetinari Jan 28, 2025
4683abe
distinguish destinations between lib & bin on windows
h-vetinari Jan 28, 2025
e91c713
do not modify destination for `install(FILES`
h-vetinari Jan 28, 2025
46d06c1
back to deleting DESTINATION, but only for TARGETS
h-vetinari Jan 28, 2025
cdabb36
remove Caffee2 from cmake_test; imported through torch anyway
h-vetinari Jan 28, 2025
89d7354
Skip tests failing due to unsupported GPUs [ci skip] ***NO_CI***
danpetry Jan 28, 2025
8a92b36
backport patch for skipping some inducer test failures
h-vetinari Jan 28, 2025
8338fd7
add cuda compiler for CMake tests of CUDA-enabled libtorch
h-vetinari Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions recipe/bld.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ if EXIST pyproject.toml (
if %ERRORLEVEL% neq 0 exit 1
)

@REM The PyTorch test suite includes some symlinks, which aren't resolved on Windows, leading to packaging errors.
@REM ATTN! These change and have to be updated manually, often with each release.
@REM (no current symlinks being packaged. Leaving this information here as it took some months to find the issue. Look out
@REM for a failure with error message: "conda_package_handling.exceptions.ArchiveCreationError: <somefile> Cannot stat
@REM while writing file")

set PYTORCH_BUILD_VERSION=%PKG_VERSION%
@REM Always pass 0 to avoid appending ".post" to version string.
@REM https://github.com/conda-forge/pytorch-cpu-feedstock/issues/315
Expand Down Expand Up @@ -97,6 +103,10 @@ if not "%cuda_compiler_version%" == "None" (

set DISTUTILS_USE_SDK=1

@REM Use our Pybind11, Eigen
set USE_SYSTEM_PYBIND11=1
set USE_SYSTEM_EIGEN_INSTALL=1

set CMAKE_INCLUDE_PATH=%LIBRARY_PREFIX%\include
set LIB=%LIBRARY_PREFIX%\lib;%LIB%

Expand Down Expand Up @@ -128,7 +138,7 @@ set "USE_LITE_PROTO=ON"
set "USE_OPENMP=OFF"

@REM The activation script for cuda-nvcc doesnt add the CUDA_CFLAGS on windows.
@REM Therefor we do this manually here. See:
@REM Therefore we do this manually here. See:
@REM https://github.com/conda-forge/cuda-nvcc-feedstock/issues/47
echo "CUDA_CFLAGS=%CUDA_CFLAGS%"
set "CUDA_CFLAGS=-I%PREFIX%/Library/include -I%BUILD_PREFIX%/Library/include"
Expand Down Expand Up @@ -183,19 +193,12 @@ if "%PKG_NAME%" == "libtorch" (
pushd torch-%PKG_VERSION%
if %ERRORLEVEL% neq 0 exit 1

@REM Do not package `fmt.lib` (and its metadata); delete it before the move into
@REM %LIBRARY_BIN% because it may exist in host before installation already
del torch\lib\fmt.lib torch\lib\pkgconfig\fmt.pc
if %ERRORLEVEL% neq 0 exit 1
@REM also delete rest of fmt metadata
rmdir /s /q torch\lib\cmake\fmt

@REM Move the binaries into the packages site-package directory
@REM the only content of torch\bin, {asmjit,fbgemm}.dll, also exists in torch\lib
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_BIN%\ torch*.dll c10.dll shm.dll asmjit.dll fbgemm.dll
robocopy /NP /NFL /NDL /NJH /E torch\bin\ %LIBRARY_BIN%\ torch*.dll c10.dll shm.dll asmjit.dll fbgemm.dll
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_LIB%\ torch*.lib c10.lib shm.lib asmjit.lib fbgemm.lib
if not "%cuda_compiler_version%" == "None" (
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_BIN%\ c10_cuda.dll caffe2_nvrtc.dll
robocopy /NP /NFL /NDL /NJH /E torch\bin\ %LIBRARY_BIN%\ c10_cuda.dll caffe2_nvrtc.dll
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_LIB%\ c10_cuda.lib caffe2_nvrtc.lib
)
robocopy /NP /NFL /NDL /NJH /E torch\share\ %LIBRARY_PREFIX%\share
Expand All @@ -216,7 +219,7 @@ if "%PKG_NAME%" == "libtorch" (
if %ERRORLEVEL% neq 0 exit 1
) else if "%PKG_NAME%" == "pytorch" (
@REM Move libtorch_python and remove the other directories afterwards.
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_BIN%\ torch_python.dll
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\bin\ %LIBRARY_BIN%\ torch_python.dll
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_LIB%\ torch_python.lib
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_LIB%\ _C.lib
rmdir /s /q %SP_DIR%\torch\lib
Expand Down
70 changes: 56 additions & 14 deletions recipe/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash

echo "=== Building ${PKG_NAME} (py: ${PY_VER}) ==="

set -ex

echo "#########################################################################"
echo "Building ${PKG_NAME} (py: ${PY_VER}) using BLAS implementation $blas_impl"
echo "#########################################################################"

# This is used to detect if it's in the process of building pytorch
export IN_PYTORCH_BUILD=1

Expand All @@ -20,9 +22,22 @@ rm -rf pyproject.toml
export USE_CUFILE=0
export USE_NUMA=0
export USE_ITT=0

#################### ADJUST COMPILER AND LINKER FLAGS #####################
# Pytorch's build system doesn't like us setting the c++ standard through CMAKE_CXX_FLAGS
# and will issue a warning. We need to use at least C++17 to match the abseil ABI, see
# https://github.com/conda-forge/abseil-cpp-feedstock/issues/45, which pytorch 2.5 uses already:
# https://github.com/pytorch/pytorch/blob/v2.5.1/CMakeLists.txt#L36-L48
export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-std=c++[0-9][0-9]//g')"
danpetry marked this conversation as resolved.
Show resolved Hide resolved
# The below three lines expose symbols that would otherwise be hidden or
# optimised away. They were here before, so removing them would potentially
# break users' programs
export CFLAGS="$(echo $CFLAGS | sed 's/-fvisibility-inlines-hidden//g')"
export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-fvisibility-inlines-hidden//g')"
export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,--as-needed//g')"
# The default conda LDFLAGs include -Wl,-dead_strip_dylibs, which removes all the
# MKL sequential, core, etc. libraries, resulting in a "Symbol not found: _mkl_blas_caxpy"
# error on osx-64.
export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,-dead_strip_dylibs//g')"
export LDFLAGS_LD="$(echo $LDFLAGS_LD | sed 's/-dead_strip_dylibs//g')"
if [[ "$c_compiler" == "clang" ]]; then
Expand All @@ -45,6 +60,7 @@ fi
# can be imported on system without a GPU
LDFLAGS="${LDFLAGS//-Wl,-z,now/-Wl,-z,lazy}"

################ CONFIGURE CMAKE FOR CONDA ENVIRONMENT ###################
export CMAKE_GENERATOR=Ninja
export CMAKE_LIBRARY_PATH=$PREFIX/lib:$PREFIX/include:$CMAKE_LIBRARY_PATH
export CMAKE_PREFIX_PATH=$PREFIX
Expand Down Expand Up @@ -73,6 +89,8 @@ export USE_SYSTEM_SLEEF=1
# use our protobuf
export BUILD_CUSTOM_PROTOBUF=OFF
rm -rf $PREFIX/bin/protoc
export USE_SYSTEM_PYBIND11=1
export USE_SYSTEM_EIGEN_INSTALL=1
Comment on lines +92 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens to the PyTorch vendored copies of these when using the system copies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It ignores them: pybind11 eigen

Although in the case of eigen it will use the vendored one if it doesn't find the system one, rather than erroring, which can mean accidental vendoring if you don't look at the logs carefully, which isn't great IMHO

Copy link
Contributor Author

@danpetry danpetry Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a patch to prevent this for mkl but I thought it best to look at the blas/openmp stuff closer and maybe address in a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you ok with this?


# prevent six from being downloaded
> third_party/NNPACK/cmake/DownloadSix.cmake
Expand All @@ -98,18 +116,29 @@ if [[ "${CI}" == "github_actions" ]]; then
# reduce parallelism to avoid getting OOM-killed on
# cirun-openstack-gpu-2xlarge, which has 32GB RAM, 8 CPUs
export MAX_JOBS=4
else
elif [[ "${CI}" == "azure" ]]; then
export MAX_JOBS=${CPU_COUNT}
fi

if [[ "$blas_impl" == "generic" ]]; then
# Fake openblas
export BLAS=OpenBLAS
export OpenBLAS_HOME=${PREFIX}
else
export BLAS=MKL
# Leave a spare core for other tasks, per common practice.
# Reducing further can help with out-of-memory errors.
export MAX_JOBS=$((CPU_COUNT > 1 ? CPU_COUNT - 1 : 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if CPU_COUNT is undefined or empty string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What CI provider do things run on for anaconda? I think it might need to become specific to that, and not in the overall else: clause here. Our OSX builds on azure work are successful withing the 6h limit, but seem to fail with this PR. This is a likely culprit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's AWS. Happy to just leave it as-is. I think originally we had it maxed at four cores and then increased it to CPU_COUNT-1 because of common practice and a desire to speed things up rather than concrete technical reasons. We can leave this as-is and if we have issues our end solve them later

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed our case in 6074128, so I have no objection to doing what helps you in the else: branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have made this a default in the else branch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that CPU_COUNT is a passthrough environment variable

So one can set this in their CI scripts to the intended value and conda-build will use it. This is what conda-forge does

Instead of adding this to the build recipe, would recommend doing this in Anaconda's CI scripts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU_COUNT isn't being set here. If it's set (somewhere else, like in CI as you suggest) then it'll be used to set MAX_JOBS. If it's not set, this expression will evaluate to 1

fi

case "$blas_impl" in
"generic")
# Fake openblas
export BLAS=OpenBLAS
export OpenBLAS_HOME=${PREFIX}
;;
"mkl")
export BLAS=MKL
;;
*)
echo "[ERROR] Unsupported BLAS implementation '${blas_impl}'" >&2
exit 1
;;
esac

if [[ "$PKG_NAME" == "pytorch" ]]; then
# Trick Cmake into thinking python hasn't changed
sed "s/3\.12/$PY_VER/g" build/CMakeCache.txt.orig > build/CMakeCache.txt
Expand Down Expand Up @@ -163,12 +192,24 @@ elif [[ ${cuda_compiler_version} != "None" ]]; then
echo "unknown CUDA arch, edit build.sh"
exit 1
esac

# Compatibility matrix for update: https://en.wikipedia.org/wiki/CUDA#GPUs_supported
# Warning from pytorch v1.12.1: In the future we will require one to
# explicitly pass TORCH_CUDA_ARCH_LIST to cmake instead of implicitly
# setting it as an env variable.
danpetry marked this conversation as resolved.
Show resolved Hide resolved
# Doing this is nontrivial given that we're using setup.py as an entry point, but should
# be addressed to pre-empt upstream changing it, as it probably won't result in a failed
# configuration.
#
# See:
# https://pytorch.org/docs/stable/cpp_extension.html (Compute capabilities)
# https://github.com/pytorch/pytorch/blob/main/.ci/manywheel/build_cuda.sh
case ${cuda_compiler_version} in
12.6)
12.[0-6])
export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX"
;;
*)
echo "unsupported cuda version. edit build.sh"
echo "No CUDA architecture list exists for CUDA v${cuda_compiler_version}. See build.sh for information on adding one."
exit 1
esac
export TORCH_NVCC_FLAGS="-Xfatbin -compress-all"
Expand Down Expand Up @@ -203,15 +244,16 @@ case ${PKG_NAME} in

mv build/lib.*/torch/bin/* ${PREFIX}/bin/
mv build/lib.*/torch/lib/* ${PREFIX}/lib/
mv build/lib.*/torch/share/* ${PREFIX}/share/
# need to merge these now because we're using system pybind11, meaning the destination directory is not empty
rsync -a build/lib.*/torch/share/* ${PREFIX}/share/
danpetry marked this conversation as resolved.
Show resolved Hide resolved
mv build/lib.*/torch/include/{ATen,caffe2,tensorpipe,torch,c10} ${PREFIX}/include/
rm ${PREFIX}/lib/libtorch_python.*

# Keep the original backed up to sed later
cp build/CMakeCache.txt build/CMakeCache.txt.orig
;;
pytorch)
$PREFIX/bin/python -m pip install . --no-deps -vvv --no-clean \
$PREFIX/bin/python -m pip install . --no-deps --no-build-isolation -vvv --no-clean \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we sync these flags across calls and Windows? Seems they vary a bit

Idk whether it is worth it, but we could also consider using script_env to set most of these flags once and reuse them throughout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for windows we've got

%PYTHON% -m pip %PIP_ACTION% . --no-build-isolation --no-deps -vvv --no-clean

and unix

$PREFIX/bin/python -m pip install . --no-deps --no-build-isolation -vvv --no-clean

So the same AFAICS?
Keeping them here seems more in line with what people will expect given other feedstocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any further comments?

| sed "s,${CXX},\$\{CXX\},g" \
| sed "s,${PREFIX},\$\{PREFIX\},g"
# Keep this in ${PREFIX}/lib so that the library can be found by
Expand Down
4 changes: 4 additions & 0 deletions recipe/cmake_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project(cf_dummy LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.12)
find_package(Torch CONFIG REQUIRED)
find_package(ATen CONFIG REQUIRED)
Loading