Skip to content

Commit 03c1c3d

Browse files
committed
Merge pull request #318 from danpetry/anaconda-sync
2 parents dfadf15 + 53ab2c8 commit 03c1c3d

25 files changed

+451
-133
lines changed

README.md

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

recipe/bld.bat

+14-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ if EXIST pyproject.toml (
77
if %ERRORLEVEL% neq 0 exit 1
88
)
99

10+
@REM The PyTorch test suite includes some symlinks, which aren't resolved on Windows, leading to packaging errors.
11+
@REM ATTN! These change and have to be updated manually, often with each release.
12+
@REM (no current symlinks being packaged. Leaving this information here as it took some months to find the issue. Look out
13+
@REM for a failure with error message: "conda_package_handling.exceptions.ArchiveCreationError: <somefile> Cannot stat
14+
@REM while writing file")
15+
1016
set PYTORCH_BUILD_VERSION=%PKG_VERSION%
1117
@REM Always pass 0 to avoid appending ".post" to version string.
1218
@REM https://github.com/conda-forge/pytorch-cpu-feedstock/issues/315
@@ -97,6 +103,10 @@ if not "%cuda_compiler_version%" == "None" (
97103

98104
set DISTUTILS_USE_SDK=1
99105

106+
@REM Use our Pybind11, Eigen
107+
set USE_SYSTEM_PYBIND11=1
108+
set USE_SYSTEM_EIGEN_INSTALL=1
109+
100110
set CMAKE_INCLUDE_PATH=%LIBRARY_PREFIX%\include
101111
set LIB=%LIBRARY_PREFIX%\lib;%LIB%
102112

@@ -128,7 +138,7 @@ set "USE_LITE_PROTO=ON"
128138
set "USE_OPENMP=OFF"
129139

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

186-
@REM Do not package `fmt.lib` (and its metadata); delete it before the move into
187-
@REM %LIBRARY_BIN% because it may exist in host before installation already
188-
del torch\lib\fmt.lib torch\lib\pkgconfig\fmt.pc
189-
if %ERRORLEVEL% neq 0 exit 1
190-
@REM also delete rest of fmt metadata
191-
rmdir /s /q torch\lib\cmake\fmt
192-
193196
@REM Move the binaries into the packages site-package directory
194197
@REM the only content of torch\bin, {asmjit,fbgemm}.dll, also exists in torch\lib
195-
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_BIN%\ torch*.dll c10.dll shm.dll asmjit.dll fbgemm.dll
198+
robocopy /NP /NFL /NDL /NJH /E torch\bin\ %LIBRARY_BIN%\ torch*.dll c10.dll shm.dll asmjit.dll fbgemm.dll
196199
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_LIB%\ torch*.lib c10.lib shm.lib asmjit.lib fbgemm.lib
197200
if not "%cuda_compiler_version%" == "None" (
198-
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_BIN%\ c10_cuda.dll caffe2_nvrtc.dll
201+
robocopy /NP /NFL /NDL /NJH /E torch\bin\ %LIBRARY_BIN%\ c10_cuda.dll caffe2_nvrtc.dll
199202
robocopy /NP /NFL /NDL /NJH /E torch\lib\ %LIBRARY_LIB%\ c10_cuda.lib caffe2_nvrtc.lib
200203
)
201204
robocopy /NP /NFL /NDL /NJH /E torch\share\ %LIBRARY_PREFIX%\share
@@ -216,7 +219,7 @@ if "%PKG_NAME%" == "libtorch" (
216219
if %ERRORLEVEL% neq 0 exit 1
217220
) else if "%PKG_NAME%" == "pytorch" (
218221
@REM Move libtorch_python and remove the other directories afterwards.
219-
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_BIN%\ torch_python.dll
222+
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\bin\ %LIBRARY_BIN%\ torch_python.dll
220223
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_LIB%\ torch_python.lib
221224
robocopy /NP /NFL /NDL /NJH /E %SP_DIR%\torch\lib\ %LIBRARY_LIB%\ _C.lib
222225
rmdir /s /q %SP_DIR%\torch\lib

recipe/build.sh

+56-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/bin/bash
22

3-
echo "=== Building ${PKG_NAME} (py: ${PY_VER}) ==="
4-
53
set -ex
64

5+
echo "#########################################################################"
6+
echo "Building ${PKG_NAME} (py: ${PY_VER}) using BLAS implementation $blas_impl"
7+
echo "#########################################################################"
8+
79
# This is used to detect if it's in the process of building pytorch
810
export IN_PYTORCH_BUILD=1
911

@@ -20,9 +22,22 @@ rm -rf pyproject.toml
2022
export USE_CUFILE=0
2123
export USE_NUMA=0
2224
export USE_ITT=0
25+
26+
#################### ADJUST COMPILER AND LINKER FLAGS #####################
27+
# Pytorch's build system doesn't like us setting the c++ standard through CMAKE_CXX_FLAGS
28+
# and will issue a warning. We need to use at least C++17 to match the abseil ABI, see
29+
# https://github.com/conda-forge/abseil-cpp-feedstock/issues/45, which pytorch 2.5 uses already:
30+
# https://github.com/pytorch/pytorch/blob/v2.5.1/CMakeLists.txt#L36-L48
31+
export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-std=c++[0-9][0-9]//g')"
32+
# The below three lines expose symbols that would otherwise be hidden or
33+
# optimised away. They were here before, so removing them would potentially
34+
# break users' programs
2335
export CFLAGS="$(echo $CFLAGS | sed 's/-fvisibility-inlines-hidden//g')"
2436
export CXXFLAGS="$(echo $CXXFLAGS | sed 's/-fvisibility-inlines-hidden//g')"
2537
export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,--as-needed//g')"
38+
# The default conda LDFLAGs include -Wl,-dead_strip_dylibs, which removes all the
39+
# MKL sequential, core, etc. libraries, resulting in a "Symbol not found: _mkl_blas_caxpy"
40+
# error on osx-64.
2641
export LDFLAGS="$(echo $LDFLAGS | sed 's/-Wl,-dead_strip_dylibs//g')"
2742
export LDFLAGS_LD="$(echo $LDFLAGS_LD | sed 's/-dead_strip_dylibs//g')"
2843
if [[ "$c_compiler" == "clang" ]]; then
@@ -45,6 +60,7 @@ fi
4560
# can be imported on system without a GPU
4661
LDFLAGS="${LDFLAGS//-Wl,-z,now/-Wl,-z,lazy}"
4762

63+
################ CONFIGURE CMAKE FOR CONDA ENVIRONMENT ###################
4864
export CMAKE_GENERATOR=Ninja
4965
export CMAKE_LIBRARY_PATH=$PREFIX/lib:$PREFIX/include:$CMAKE_LIBRARY_PATH
5066
export CMAKE_PREFIX_PATH=$PREFIX
@@ -73,6 +89,8 @@ export USE_SYSTEM_SLEEF=1
7389
# use our protobuf
7490
export BUILD_CUSTOM_PROTOBUF=OFF
7591
rm -rf $PREFIX/bin/protoc
92+
export USE_SYSTEM_PYBIND11=1
93+
export USE_SYSTEM_EIGEN_INSTALL=1
7694

7795
# prevent six from being downloaded
7896
> third_party/NNPACK/cmake/DownloadSix.cmake
@@ -98,18 +116,29 @@ if [[ "${CI}" == "github_actions" ]]; then
98116
# reduce parallelism to avoid getting OOM-killed on
99117
# cirun-openstack-gpu-2xlarge, which has 32GB RAM, 8 CPUs
100118
export MAX_JOBS=4
101-
else
119+
elif [[ "${CI}" == "azure" ]]; then
102120
export MAX_JOBS=${CPU_COUNT}
103-
fi
104-
105-
if [[ "$blas_impl" == "generic" ]]; then
106-
# Fake openblas
107-
export BLAS=OpenBLAS
108-
export OpenBLAS_HOME=${PREFIX}
109121
else
110-
export BLAS=MKL
122+
# Leave a spare core for other tasks, per common practice.
123+
# Reducing further can help with out-of-memory errors.
124+
export MAX_JOBS=$((CPU_COUNT > 1 ? CPU_COUNT - 1 : 1))
111125
fi
112126

127+
case "$blas_impl" in
128+
"generic")
129+
# Fake openblas
130+
export BLAS=OpenBLAS
131+
export OpenBLAS_HOME=${PREFIX}
132+
;;
133+
"mkl")
134+
export BLAS=MKL
135+
;;
136+
*)
137+
echo "[ERROR] Unsupported BLAS implementation '${blas_impl}'" >&2
138+
exit 1
139+
;;
140+
esac
141+
113142
if [[ "$PKG_NAME" == "pytorch" ]]; then
114143
# Trick Cmake into thinking python hasn't changed
115144
sed "s/3\.12/$PY_VER/g" build/CMakeCache.txt.orig > build/CMakeCache.txt
@@ -163,12 +192,24 @@ elif [[ ${cuda_compiler_version} != "None" ]]; then
163192
echo "unknown CUDA arch, edit build.sh"
164193
exit 1
165194
esac
195+
196+
# Compatibility matrix for update: https://en.wikipedia.org/wiki/CUDA#GPUs_supported
197+
# Warning from pytorch v1.12.1: In the future we will require one to
198+
# explicitly pass TORCH_CUDA_ARCH_LIST to cmake instead of implicitly
199+
# setting it as an env variable.
200+
# Doing this is nontrivial given that we're using setup.py as an entry point, but should
201+
# be addressed to pre-empt upstream changing it, as it probably won't result in a failed
202+
# configuration.
203+
#
204+
# See:
205+
# https://pytorch.org/docs/stable/cpp_extension.html (Compute capabilities)
206+
# https://github.com/pytorch/pytorch/blob/main/.ci/manywheel/build_cuda.sh
166207
case ${cuda_compiler_version} in
167-
12.6)
208+
12.[0-6])
168209
export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX"
169210
;;
170211
*)
171-
echo "unsupported cuda version. edit build.sh"
212+
echo "No CUDA architecture list exists for CUDA v${cuda_compiler_version}. See build.sh for information on adding one."
172213
exit 1
173214
esac
174215
export TORCH_NVCC_FLAGS="-Xfatbin -compress-all"
@@ -203,15 +244,16 @@ case ${PKG_NAME} in
203244

204245
mv build/lib.*/torch/bin/* ${PREFIX}/bin/
205246
mv build/lib.*/torch/lib/* ${PREFIX}/lib/
206-
mv build/lib.*/torch/share/* ${PREFIX}/share/
247+
# need to merge these now because we're using system pybind11, meaning the destination directory is not empty
248+
rsync -a build/lib.*/torch/share/* ${PREFIX}/share/
207249
mv build/lib.*/torch/include/{ATen,caffe2,tensorpipe,torch,c10} ${PREFIX}/include/
208250
rm ${PREFIX}/lib/libtorch_python.*
209251

210252
# Keep the original backed up to sed later
211253
cp build/CMakeCache.txt build/CMakeCache.txt.orig
212254
;;
213255
pytorch)
214-
$PREFIX/bin/python -m pip install . --no-deps -vvv --no-clean \
256+
$PREFIX/bin/python -m pip install . --no-deps --no-build-isolation -vvv --no-clean \
215257
| sed "s,${CXX},\$\{CXX\},g" \
216258
| sed "s,${PREFIX},\$\{PREFIX\},g"
217259
# Keep this in ${PREFIX}/lib so that the library can be found by

recipe/cmake_test/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project(cf_dummy LANGUAGES C CXX)
2+
cmake_minimum_required(VERSION 3.12)
3+
find_package(Torch CONFIG REQUIRED)
4+
find_package(ATen CONFIG REQUIRED)

0 commit comments

Comments
 (0)