2023-05-24 13:37:59 +00:00
|
|
|
|
{ stdenv, lib, fetchFromGitHub, buildPythonPackage, python,
|
2023-08-04 22:07:22 +00:00
|
|
|
|
config, cudaSupport ? config.cudaSupport, cudaPackages, magma,
|
2023-03-15 16:39:30 +00:00
|
|
|
|
useSystemNccl ? true,
|
2021-02-05 17:12:51 +00:00
|
|
|
|
MPISupport ? false, mpi,
|
2020-05-15 21:57:56 +00:00
|
|
|
|
buildDocs ? false,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Native build inputs
|
2023-05-24 13:37:59 +00:00
|
|
|
|
cmake, linkFarm, symlinkJoin, which, pybind11, removeReferencesTo,
|
2023-04-12 12:48:02 +00:00
|
|
|
|
pythonRelaxDepsHook,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Build inputs
|
|
|
|
|
numactl,
|
2023-03-15 16:39:30 +00:00
|
|
|
|
Accelerate, CoreServices, libobjc,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Propagated build inputs
|
2023-04-12 12:48:02 +00:00
|
|
|
|
filelock,
|
|
|
|
|
jinja2,
|
|
|
|
|
networkx,
|
|
|
|
|
openai-triton,
|
|
|
|
|
sympy,
|
2022-04-15 01:41:22 +00:00
|
|
|
|
numpy, pyyaml, cffi, click, typing-extensions,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Unit tests
|
|
|
|
|
hypothesis, psutil,
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
|
# Disable MKLDNN on aarch64-darwin, it negatively impacts performance,
|
|
|
|
|
# this is also what official pytorch build does
|
|
|
|
|
mklDnnSupport ? !(stdenv.isDarwin && stdenv.isAarch64),
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# virtual pkg that consistently instantiates blas across nixpkgs
|
|
|
|
|
# See https://github.com/NixOS/nixpkgs/pull/83888
|
|
|
|
|
blas,
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# ninja (https://ninja-build.org) must be available to run C++ extensions tests,
|
|
|
|
|
ninja,
|
|
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
|
linuxHeaders_5_19,
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# dependencies for torch.utils.tensorboard
|
2022-04-15 01:41:22 +00:00
|
|
|
|
pillow, six, future, tensorboard, protobuf,
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
|
pythonOlder,
|
2023-01-20 10:41:00 +00:00
|
|
|
|
|
|
|
|
|
# ROCm dependencies
|
|
|
|
|
rocmSupport ? false,
|
|
|
|
|
gpuTargets ? [ ],
|
|
|
|
|
openmp, rocm-core, hip, rccl, miopen, miopengemm, rocrand, rocblas,
|
|
|
|
|
rocfft, rocsparse, hipsparse, rocthrust, rocprim, hipcub, roctracer,
|
|
|
|
|
rocsolver, hipfft, hipsolver, hipblas, rocminfo, rocm-thunk, rocm-comgr,
|
|
|
|
|
rocm-device-libs, rocm-runtime, rocm-opencl-runtime, hipify
|
|
|
|
|
}:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
|
let
|
2023-03-04 12:14:45 +00:00
|
|
|
|
inherit (lib) lists strings trivial;
|
2022-12-17 10:02:37 +00:00
|
|
|
|
inherit (cudaPackages) cudatoolkit cudaFlags cudnn nccl;
|
2022-04-15 01:41:22 +00:00
|
|
|
|
in
|
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
|
assert cudaSupport -> stdenv.isLinux;
|
2023-04-12 12:48:02 +00:00
|
|
|
|
assert cudaSupport -> (cudaPackages.cudaMajorVersion == "11");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
# confirm that cudatoolkits are sync'd across dependencies
|
2021-02-05 17:12:51 +00:00
|
|
|
|
assert !(MPISupport && cudaSupport) || mpi.cudatoolkit == cudatoolkit;
|
2023-03-04 12:14:45 +00:00
|
|
|
|
assert !cudaSupport || magma.cudaPackages.cudatoolkit == cudatoolkit;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
let
|
2021-04-12 18:23:04 +00:00
|
|
|
|
setBool = v: if v then "1" else "0";
|
2023-03-04 12:14:45 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
# https://github.com/pytorch/pytorch/blob/v2.0.1/torch/utils/cpp_extension.py#L1744
|
2023-03-04 12:14:45 +00:00
|
|
|
|
supportedTorchCudaCapabilities =
|
|
|
|
|
let
|
2023-08-22 20:05:09 +00:00
|
|
|
|
real = ["3.5" "3.7" "5.0" "5.2" "5.3" "6.0" "6.1" "6.2" "7.0" "7.2" "7.5" "8.0" "8.6" "8.7" "8.9" "9.0"];
|
2023-03-04 12:14:45 +00:00
|
|
|
|
ptx = lists.map (x: "${x}+PTX") real;
|
|
|
|
|
in
|
|
|
|
|
real ++ ptx;
|
|
|
|
|
|
|
|
|
|
# NOTE: The lists.subtractLists function is perhaps a bit unintuitive. It subtracts the elements
|
|
|
|
|
# of the first list *from* the second list. That means:
|
|
|
|
|
# lists.subtractLists a b = b - a
|
|
|
|
|
|
|
|
|
|
# For CUDA
|
|
|
|
|
supportedCudaCapabilities = lists.intersectLists cudaFlags.cudaCapabilities supportedTorchCudaCapabilities;
|
|
|
|
|
unsupportedCudaCapabilities = lists.subtractLists supportedCudaCapabilities cudaFlags.cudaCapabilities;
|
|
|
|
|
|
|
|
|
|
# Use trivial.warnIf to print a warning if any unsupported GPU targets are specified.
|
|
|
|
|
gpuArchWarner = supported: unsupported:
|
|
|
|
|
trivial.throwIf (supported == [ ])
|
|
|
|
|
(
|
|
|
|
|
"No supported GPU targets specified. Requested GPU targets: "
|
|
|
|
|
+ strings.concatStringsSep ", " unsupported
|
|
|
|
|
)
|
|
|
|
|
supported;
|
|
|
|
|
|
|
|
|
|
# Create the gpuTargetString.
|
|
|
|
|
gpuTargetString = strings.concatStringsSep ";" (
|
|
|
|
|
if gpuTargets != [ ] then
|
|
|
|
|
# If gpuTargets is specified, it always takes priority.
|
|
|
|
|
gpuTargets
|
|
|
|
|
else if cudaSupport then
|
|
|
|
|
gpuArchWarner supportedCudaCapabilities unsupportedCudaCapabilities
|
|
|
|
|
else if rocmSupport then
|
|
|
|
|
hip.gpuTargets
|
|
|
|
|
else
|
|
|
|
|
throw "No GPU targets specified"
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
cudatoolkit_joined = symlinkJoin {
|
|
|
|
|
name = "${cudatoolkit.name}-unsplit";
|
|
|
|
|
# nccl is here purely for semantic grouping it could be moved to nativeBuildInputs
|
|
|
|
|
paths = [ cudatoolkit.out cudatoolkit.lib nccl.dev nccl.out ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# Normally libcuda.so.1 is provided at runtime by nvidia-x11 via
|
|
|
|
|
# LD_LIBRARY_PATH=/run/opengl-driver/lib. We only use the stub
|
|
|
|
|
# libcuda.so from cudatoolkit for running tests, so that we don’t have
|
|
|
|
|
# to recompile pytorch on every update to nvidia-x11 or the kernel.
|
|
|
|
|
cudaStub = linkFarm "cuda-stub" [{
|
|
|
|
|
name = "libcuda.so.1";
|
|
|
|
|
path = "${cudatoolkit}/lib/stubs/libcuda.so";
|
|
|
|
|
}];
|
|
|
|
|
cudaStubEnv = lib.optionalString cudaSupport
|
|
|
|
|
"LD_LIBRARY_PATH=${cudaStub}\${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH ";
|
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
rocmtoolkit_joined = symlinkJoin {
|
|
|
|
|
name = "rocm-merged";
|
|
|
|
|
|
|
|
|
|
paths = [
|
|
|
|
|
rocm-core hip rccl miopen miopengemm rocrand rocblas
|
|
|
|
|
rocfft rocsparse hipsparse rocthrust rocprim hipcub
|
|
|
|
|
roctracer rocfft rocsolver hipfft hipsolver hipblas
|
|
|
|
|
rocminfo rocm-thunk rocm-comgr rocm-device-libs
|
|
|
|
|
rocm-runtime rocm-opencl-runtime hipify
|
|
|
|
|
];
|
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
in buildPythonPackage rec {
|
2022-09-09 14:08:57 +00:00
|
|
|
|
pname = "torch";
|
|
|
|
|
# Don't forget to update torch-bin to the same version.
|
2023-05-24 13:37:59 +00:00
|
|
|
|
version = "2.0.1";
|
2022-04-15 01:41:22 +00:00
|
|
|
|
format = "setuptools";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
|
disabled = pythonOlder "3.8.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
outputs = [
|
2022-09-09 14:08:57 +00:00
|
|
|
|
"out" # output standard python package
|
|
|
|
|
"dev" # output libtorch headers
|
|
|
|
|
"lib" # output libtorch libraries
|
2020-04-24 23:36:52 +00:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
2022-09-09 14:08:57 +00:00
|
|
|
|
owner = "pytorch";
|
|
|
|
|
repo = "pytorch";
|
|
|
|
|
rev = "refs/tags/v${version}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
fetchSubmodules = true;
|
2023-05-24 13:37:59 +00:00
|
|
|
|
hash = "sha256-xUj77yKz3IQ3gd/G32pI4OhL3LoN1zS7eFg0/0nZp5I=";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
patches = lib.optionals (stdenv.isDarwin && stdenv.isx86_64) [
|
2020-08-20 17:08:02 +00:00
|
|
|
|
# pthreadpool added support for Grand Central Dispatch in April
|
|
|
|
|
# 2020. However, this relies on functionality (DISPATCH_APPLY_AUTO)
|
|
|
|
|
# that is available starting with macOS 10.13. However, our current
|
|
|
|
|
# base is 10.12. Until we upgrade, we can fall back on the older
|
|
|
|
|
# pthread support.
|
|
|
|
|
./pthreadpool-disable-gcd.diff
|
2020-07-18 16:06:22 +00:00
|
|
|
|
];
|
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
postPatch = lib.optionalString rocmSupport ''
|
|
|
|
|
# https://github.com/facebookincubator/gloo/pull/297
|
|
|
|
|
substituteInPlace third_party/gloo/cmake/Hipify.cmake \
|
|
|
|
|
--replace "\''${HIPIFY_COMMAND}" "python \''${HIPIFY_COMMAND}"
|
|
|
|
|
|
|
|
|
|
# Replace hard-coded rocm paths
|
|
|
|
|
substituteInPlace caffe2/CMakeLists.txt \
|
|
|
|
|
--replace "/opt/rocm" "${rocmtoolkit_joined}" \
|
|
|
|
|
--replace "hcc/include" "hip/include" \
|
|
|
|
|
--replace "rocblas/include" "include/rocblas" \
|
|
|
|
|
--replace "hipsparse/include" "include/hipsparse"
|
|
|
|
|
|
|
|
|
|
# Doesn't pick up the environment variable?
|
|
|
|
|
substituteInPlace third_party/kineto/libkineto/CMakeLists.txt \
|
|
|
|
|
--replace "\''$ENV{ROCM_SOURCE_DIR}" "${rocmtoolkit_joined}" \
|
|
|
|
|
--replace "/opt/rocm" "${rocmtoolkit_joined}"
|
|
|
|
|
|
|
|
|
|
# Strangely, this is never set in cmake
|
|
|
|
|
substituteInPlace cmake/public/LoadHIP.cmake \
|
|
|
|
|
--replace "set(ROCM_PATH \$ENV{ROCM_PATH})" \
|
|
|
|
|
"set(ROCM_PATH \$ENV{ROCM_PATH})''\nset(ROCM_VERSION ${lib.concatStrings (lib.intersperse "0" (lib.splitString "." hip.version))})"
|
2023-04-29 16:46:19 +00:00
|
|
|
|
''
|
|
|
|
|
# error: no member named 'aligned_alloc' in the global namespace; did you mean simply 'aligned_alloc'
|
|
|
|
|
# This lib overrided aligned_alloc hence the error message. Tltr: his function is linkable but not in header.
|
|
|
|
|
+ lib.optionalString (stdenv.isDarwin && lib.versionOlder stdenv.targetPlatform.darwinSdkVersion "11.0") ''
|
|
|
|
|
substituteInPlace third_party/pocketfft/pocketfft_hdronly.h --replace '#if __cplusplus >= 201703L
|
|
|
|
|
inline void *aligned_alloc(size_t align, size_t size)' '#if __cplusplus >= 201703L && 0
|
|
|
|
|
inline void *aligned_alloc(size_t align, size_t size)'
|
2023-01-20 10:41:00 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
preConfigure = lib.optionalString cudaSupport ''
|
2023-03-04 12:14:45 +00:00
|
|
|
|
export TORCH_CUDA_ARCH_LIST="${gpuTargetString}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
export CC=${cudatoolkit.cc}/bin/gcc CXX=${cudatoolkit.cc}/bin/g++
|
|
|
|
|
'' + lib.optionalString (cudaSupport && cudnn != null) ''
|
|
|
|
|
export CUDNN_INCLUDE_DIR=${cudnn}/include
|
2023-01-20 10:41:00 +00:00
|
|
|
|
'' + lib.optionalString rocmSupport ''
|
|
|
|
|
export ROCM_PATH=${rocmtoolkit_joined}
|
|
|
|
|
export ROCM_SOURCE_DIR=${rocmtoolkit_joined}
|
2023-03-04 12:14:45 +00:00
|
|
|
|
export PYTORCH_ROCM_ARCH="${gpuTargetString}"
|
2023-01-20 10:41:00 +00:00
|
|
|
|
export CMAKE_CXX_FLAGS="-I${rocmtoolkit_joined}/include -I${rocmtoolkit_joined}/include/rocblas"
|
|
|
|
|
python tools/amd_build/build_amd.py
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# Use pytorch's custom configurations
|
|
|
|
|
dontUseCmakeConfigure = true;
|
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
# causes possible redefinition of _FORTIFY_SOURCE
|
|
|
|
|
hardeningDisable = [ "fortify3" ];
|
|
|
|
|
|
2021-04-12 18:23:04 +00:00
|
|
|
|
BUILD_NAMEDTENSOR = setBool true;
|
|
|
|
|
BUILD_DOCS = setBool buildDocs;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
|
2021-04-12 18:23:04 +00:00
|
|
|
|
# We only do an imports check, so do not build tests either.
|
|
|
|
|
BUILD_TEST = setBool false;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
|
# Unlike MKL, oneDNN (née MKLDNN) is FOSS, so we enable support for
|
|
|
|
|
# it by default. PyTorch currently uses its own vendored version
|
|
|
|
|
# of oneDNN through Intel iDeep.
|
2021-04-12 18:23:04 +00:00
|
|
|
|
USE_MKLDNN = setBool mklDnnSupport;
|
|
|
|
|
USE_MKLDNN_CBLAS = setBool mklDnnSupport;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
|
2022-06-26 10:26:21 +00:00
|
|
|
|
# Avoid using pybind11 from git submodule
|
|
|
|
|
# Also avoids pytorch exporting the headers of pybind11
|
2023-08-22 20:05:09 +00:00
|
|
|
|
USE_SYSTEM_PYBIND11 = true;
|
2022-06-26 10:26:21 +00:00
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
preBuild = ''
|
|
|
|
|
export MAX_JOBS=$NIX_BUILD_CORES
|
2023-03-04 12:14:45 +00:00
|
|
|
|
${python.pythonForBuild.interpreter} setup.py build --cmake-only
|
2020-05-15 21:57:56 +00:00
|
|
|
|
${cmake}/bin/cmake build
|
|
|
|
|
'';
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
preFixup = ''
|
|
|
|
|
function join_by { local IFS="$1"; shift; echo "$*"; }
|
|
|
|
|
function strip2 {
|
|
|
|
|
IFS=':'
|
|
|
|
|
read -ra RP <<< $(patchelf --print-rpath $1)
|
|
|
|
|
IFS=' '
|
|
|
|
|
RP_NEW=$(join_by : ''${RP[@]:2})
|
|
|
|
|
patchelf --set-rpath \$ORIGIN:''${RP_NEW} "$1"
|
|
|
|
|
}
|
|
|
|
|
for f in $(find ''${out} -name 'libcaffe2*.so')
|
|
|
|
|
do
|
|
|
|
|
strip2 $f
|
|
|
|
|
done
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
# Override the (weirdly) wrong version set by default. See
|
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/52437#issuecomment-449718038
|
|
|
|
|
# https://github.com/pytorch/pytorch/blob/v1.0.0/setup.py#L267
|
|
|
|
|
PYTORCH_BUILD_VERSION = version;
|
|
|
|
|
PYTORCH_BUILD_NUMBER = 0;
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
USE_SYSTEM_NCCL = setBool useSystemNccl; # don't build pytorch's third_party NCCL
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
# Suppress a weird warning in mkl-dnn, part of ideep in pytorch
|
|
|
|
|
# (upstream seems to have fixed this in the wrong place?)
|
|
|
|
|
# https://github.com/intel/mkl-dnn/commit/8134d346cdb7fe1695a2aa55771071d455fae0bc
|
|
|
|
|
# https://github.com/pytorch/pytorch/issues/22346
|
|
|
|
|
#
|
|
|
|
|
# Also of interest: pytorch ignores CXXFLAGS uses CFLAGS for both C and C++:
|
2022-04-15 01:41:22 +00:00
|
|
|
|
# https://github.com/pytorch/pytorch/blob/v1.11.0/setup.py#L17
|
2023-03-04 12:14:45 +00:00
|
|
|
|
env.NIX_CFLAGS_COMPILE = toString ((lib.optionals (blas.implementation == "mkl") [ "-Wno-error=array-bounds" ]
|
2023-02-16 17:41:37 +00:00
|
|
|
|
# Suppress gcc regression: avx512 math function raises uninitialized variable warning
|
|
|
|
|
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105593
|
|
|
|
|
# See also: Fails to compile with GCC 12.1.0 https://github.com/pytorch/pytorch/issues/77939
|
2023-04-12 12:48:02 +00:00
|
|
|
|
++ lib.optionals (stdenv.cc.isGNU && lib.versionAtLeast stdenv.cc.version "12.0.0") [
|
|
|
|
|
"-Wno-error=maybe-uninitialized"
|
|
|
|
|
"-Wno-error=uninitialized"
|
|
|
|
|
]
|
|
|
|
|
# Since pytorch 2.0:
|
|
|
|
|
# gcc-12.2.0/include/c++/12.2.0/bits/new_allocator.h:158:33: error: ‘void operator delete(void*, std::size_t)’
|
|
|
|
|
# ... called on pointer ‘<unknown>’ with nonzero offset [1, 9223372036854775800] [-Werror=free-nonheap-object]
|
|
|
|
|
++ lib.optionals (stdenv.cc.isGNU && lib.versions.major stdenv.cc.version == "12" ) [
|
|
|
|
|
"-Wno-error=free-nonheap-object"
|
|
|
|
|
]));
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
nativeBuildInputs = [
|
|
|
|
|
cmake
|
|
|
|
|
which
|
|
|
|
|
ninja
|
2021-05-28 09:39:13 +00:00
|
|
|
|
pybind11
|
2023-04-12 12:48:02 +00:00
|
|
|
|
pythonRelaxDepsHook
|
2022-04-27 09:35:20 +00:00
|
|
|
|
removeReferencesTo
|
2023-01-20 10:41:00 +00:00
|
|
|
|
] ++ lib.optionals cudaSupport [ cudatoolkit_joined ]
|
|
|
|
|
++ lib.optionals rocmSupport [ rocmtoolkit_joined ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-06-26 10:26:21 +00:00
|
|
|
|
buildInputs = [ blas blas.provider pybind11 ]
|
2023-01-11 07:51:40 +00:00
|
|
|
|
++ lib.optionals stdenv.isLinux [ linuxHeaders_5_19 ] # TMP: avoid "flexible array member" errors for now
|
2023-01-20 10:41:00 +00:00
|
|
|
|
++ lib.optionals cudaSupport [ cudnn nccl ]
|
|
|
|
|
++ lib.optionals rocmSupport [ openmp ]
|
|
|
|
|
++ lib.optionals (cudaSupport || rocmSupport) [ magma ]
|
2022-06-26 10:26:21 +00:00
|
|
|
|
++ lib.optionals stdenv.isLinux [ numactl ]
|
2023-03-15 16:39:30 +00:00
|
|
|
|
++ lib.optionals stdenv.isDarwin [ Accelerate CoreServices libobjc ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
propagatedBuildInputs = [
|
|
|
|
|
cffi
|
|
|
|
|
click
|
|
|
|
|
numpy
|
|
|
|
|
pyyaml
|
2023-04-12 12:48:02 +00:00
|
|
|
|
|
|
|
|
|
# From install_requires:
|
|
|
|
|
filelock
|
2020-11-12 09:05:59 +00:00
|
|
|
|
typing-extensions
|
2023-04-12 12:48:02 +00:00
|
|
|
|
sympy
|
|
|
|
|
networkx
|
|
|
|
|
jinja2
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# the following are required for tensorboard support
|
2022-04-15 01:41:22 +00:00
|
|
|
|
pillow six future tensorboard protobuf
|
2023-04-12 12:48:02 +00:00
|
|
|
|
]
|
|
|
|
|
++ lib.optionals MPISupport [ mpi ]
|
|
|
|
|
++ lib.optionals rocmSupport [ rocmtoolkit_joined ]
|
|
|
|
|
# rocm build requires openai-triton;
|
|
|
|
|
# openai-triton currently requires cuda_nvcc,
|
|
|
|
|
# so not including it in the cpu-only build;
|
|
|
|
|
# torch.compile relies on openai-triton,
|
|
|
|
|
# so we include it for the cuda build as well
|
|
|
|
|
++ lib.optionals (rocmSupport || cudaSupport) [
|
|
|
|
|
openai-triton
|
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# Tests take a long time and may be flaky, so just sanity-check imports
|
|
|
|
|
doCheck = false;
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
pythonImportsCheck = [
|
|
|
|
|
"torch"
|
|
|
|
|
];
|
|
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
|
nativeCheckInputs = [ hypothesis ninja psutil ];
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
checkPhase = with lib.versions; with lib.strings; concatStringsSep " " [
|
2022-09-09 14:08:57 +00:00
|
|
|
|
"runHook preCheck"
|
2020-05-15 21:57:56 +00:00
|
|
|
|
cudaStubEnv
|
|
|
|
|
"${python.interpreter} test/run_test.py"
|
|
|
|
|
"--exclude"
|
|
|
|
|
(concatStringsSep " " [
|
|
|
|
|
"utils" # utils requires git, which is not allowed in the check phase
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# "dataloader" # psutils correctly finds and triggers multiprocessing, but is too sandboxed to run -- resulting in numerous errors
|
|
|
|
|
# ^^^^^^^^^^^^ NOTE: while test_dataloader does return errors, these are acceptable errors and do not interfere with the build
|
|
|
|
|
|
|
|
|
|
# tensorboard has acceptable failures for pytorch 1.3.x due to dependencies on tensorboard-plugins
|
|
|
|
|
(optionalString (majorMinor version == "1.3" ) "tensorboard")
|
|
|
|
|
])
|
2022-09-09 14:08:57 +00:00
|
|
|
|
"runHook postCheck"
|
2020-05-15 21:57:56 +00:00
|
|
|
|
];
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
|
pythonRemoveDeps = [
|
|
|
|
|
# In our dist-info the name is just "triton"
|
|
|
|
|
"pytorch-triton-rocm"
|
|
|
|
|
];
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
postInstall = ''
|
2022-04-27 09:35:20 +00:00
|
|
|
|
find "$out/${python.sitePackages}/torch/include" "$out/${python.sitePackages}/torch/lib" -type f -exec remove-references-to -t ${stdenv.cc} '{}' +
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
mkdir $dev
|
|
|
|
|
cp -r $out/${python.sitePackages}/torch/include $dev/include
|
2022-09-09 14:08:57 +00:00
|
|
|
|
cp -r $out/${python.sitePackages}/torch/share $dev/share
|
2020-07-18 16:06:22 +00:00
|
|
|
|
|
2020-12-07 07:45:13 +00:00
|
|
|
|
# Fix up library paths for split outputs
|
|
|
|
|
substituteInPlace \
|
|
|
|
|
$dev/share/cmake/Torch/TorchConfig.cmake \
|
|
|
|
|
--replace \''${TORCH_INSTALL_PREFIX}/lib "$lib/lib"
|
|
|
|
|
|
|
|
|
|
substituteInPlace \
|
|
|
|
|
$dev/share/cmake/Caffe2/Caffe2Targets-release.cmake \
|
|
|
|
|
--replace \''${_IMPORT_PREFIX}/lib "$lib/lib"
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
mkdir $lib
|
2022-09-09 14:08:57 +00:00
|
|
|
|
mv $out/${python.sitePackages}/torch/lib $lib/lib
|
2022-04-27 09:35:20 +00:00
|
|
|
|
ln -s $lib/lib $out/${python.sitePackages}/torch/lib
|
2023-01-20 10:41:00 +00:00
|
|
|
|
'' + lib.optionalString rocmSupport ''
|
|
|
|
|
substituteInPlace $dev/share/cmake/Tensorpipe/TensorpipeTargets-release.cmake \
|
|
|
|
|
--replace "\''${_IMPORT_PREFIX}/lib64" "$lib/lib"
|
|
|
|
|
|
|
|
|
|
substituteInPlace $dev/share/cmake/ATen/ATenConfig.cmake \
|
|
|
|
|
--replace "/build/source/torch/include" "$dev/include"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
|
postFixup = lib.optionalString stdenv.isDarwin ''
|
2020-08-20 17:08:02 +00:00
|
|
|
|
for f in $(ls $lib/lib/*.dylib); do
|
|
|
|
|
install_name_tool -id $lib/lib/$(basename $f) $f || true
|
2020-04-24 23:36:52 +00:00
|
|
|
|
done
|
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
|
install_name_tool -change @rpath/libshm.dylib $lib/lib/libshm.dylib $lib/lib/libtorch_python.dylib
|
|
|
|
|
install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libtorch_python.dylib
|
|
|
|
|
install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libtorch_python.dylib
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
|
install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libtorch.dylib
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
|
install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libshm.dylib
|
|
|
|
|
install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libshm.dylib
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2021-04-26 19:14:03 +00:00
|
|
|
|
# Builds in 2+h with 2 cores, and ~15m with a big-parallel builder.
|
|
|
|
|
requiredSystemFeatures = [ "big-parallel" ];
|
|
|
|
|
|
2021-08-08 23:34:03 +00:00
|
|
|
|
passthru = {
|
2023-03-24 00:07:29 +00:00
|
|
|
|
inherit cudaSupport cudaPackages;
|
2022-02-10 20:34:41 +00:00
|
|
|
|
# At least for 1.10.2 `torch.fft` is unavailable unless BLAS provider is MKL. This attribute allows for easy detection of its availability.
|
2021-09-18 10:52:07 +00:00
|
|
|
|
blasProvider = blas.provider;
|
2023-03-24 00:07:29 +00:00
|
|
|
|
} // lib.optionalAttrs cudaSupport {
|
|
|
|
|
# NOTE: supportedCudaCapabilities isn't computed unless cudaSupport is true, so we can't use
|
|
|
|
|
# it in the passthru set above because a downstream package might try to access it even
|
|
|
|
|
# when cudaSupport is false. Better to have it missing than null or an empty list by default.
|
|
|
|
|
cudaCapabilities = supportedCudaCapabilities;
|
2021-08-08 23:34:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-12 07:09:13 +00:00
|
|
|
|
meta = with lib; {
|
2022-09-09 14:08:57 +00:00
|
|
|
|
changelog = "https://github.com/pytorch/pytorch/releases/tag/v${version}";
|
|
|
|
|
# keep PyTorch in the description so the package can be found under that name on search.nixos.org
|
|
|
|
|
description = "PyTorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration";
|
|
|
|
|
homepage = "https://pytorch.org/";
|
|
|
|
|
license = licenses.bsd3;
|
2021-09-18 10:52:07 +00:00
|
|
|
|
maintainers = with maintainers; [ teh thoughtpolice tscholak ]; # tscholak esp. for darwin-related builds
|
2023-05-24 13:37:59 +00:00
|
|
|
|
platforms = with platforms; linux ++ lib.optionals (!cudaSupport && !rocmSupport) darwin;
|
2023-01-20 10:41:00 +00:00
|
|
|
|
broken = rocmSupport && cudaSupport; # CUDA and ROCm are mutually exclusive
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|