2024-06-05 15:53:02 +00:00
|
|
|
|
{
|
|
|
|
|
stdenv,
|
|
|
|
|
lib,
|
|
|
|
|
fetchFromGitHub,
|
|
|
|
|
buildPythonPackage,
|
|
|
|
|
python,
|
|
|
|
|
config,
|
|
|
|
|
cudaSupport ? config.cudaSupport,
|
|
|
|
|
cudaPackages,
|
2024-04-21 15:54:59 +00:00
|
|
|
|
autoAddDriverRunpath,
|
2024-01-02 11:29:13 +00:00
|
|
|
|
effectiveMagma ?
|
2024-06-05 15:53:02 +00:00
|
|
|
|
if cudaSupport then
|
|
|
|
|
magma-cuda-static
|
|
|
|
|
else if rocmSupport then
|
|
|
|
|
magma-hip
|
|
|
|
|
else
|
|
|
|
|
magma,
|
2024-01-02 11:29:13 +00:00
|
|
|
|
magma,
|
|
|
|
|
magma-hip,
|
|
|
|
|
magma-cuda-static,
|
2024-01-13 08:15:51 +00:00
|
|
|
|
# Use the system NCCL as long as we're targeting CUDA on a supported platform.
|
2024-04-21 15:54:59 +00:00
|
|
|
|
useSystemNccl ? (cudaSupport && !cudaPackages.nccl.meta.unsupported || rocmSupport),
|
2024-06-05 15:53:02 +00:00
|
|
|
|
MPISupport ? false,
|
|
|
|
|
mpi,
|
2020-05-15 21:57:56 +00:00
|
|
|
|
buildDocs ? false,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
|
# tests.cudaAvailable:
|
|
|
|
|
callPackage,
|
|
|
|
|
|
2020-11-12 09:05:59 +00:00
|
|
|
|
# Native build inputs
|
2024-06-05 15:53:02 +00:00
|
|
|
|
cmake,
|
|
|
|
|
symlinkJoin,
|
|
|
|
|
which,
|
|
|
|
|
pybind11,
|
|
|
|
|
removeReferencesTo,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Build inputs
|
|
|
|
|
numactl,
|
2024-06-05 15:53:02 +00:00
|
|
|
|
Accelerate,
|
|
|
|
|
CoreServices,
|
|
|
|
|
libobjc,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Propagated build inputs
|
2024-02-29 20:09:43 +00:00
|
|
|
|
astunparse,
|
2024-01-02 11:29:13 +00:00
|
|
|
|
fsspec,
|
2023-04-12 12:48:02 +00:00
|
|
|
|
filelock,
|
|
|
|
|
jinja2,
|
|
|
|
|
networkx,
|
|
|
|
|
sympy,
|
2024-06-05 15:53:02 +00:00
|
|
|
|
numpy,
|
|
|
|
|
pyyaml,
|
|
|
|
|
cffi,
|
|
|
|
|
click,
|
|
|
|
|
typing-extensions,
|
2024-07-27 06:49:29 +00:00
|
|
|
|
# ROCm build and `torch.compile` requires `triton`
|
2024-09-26 11:04:55 +00:00
|
|
|
|
tritonSupport ? (!stdenv.hostPlatform.isDarwin),
|
2024-07-27 06:49:29 +00:00
|
|
|
|
triton,
|
2020-11-12 09:05:59 +00:00
|
|
|
|
|
|
|
|
|
# Unit tests
|
2024-06-05 15:53:02 +00:00
|
|
|
|
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
|
2024-09-26 11:04:55 +00:00
|
|
|
|
mklDnnSupport ? !(stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64),
|
2023-03-15 16:39:30 +00:00
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
|
|
# dependencies for torch.utils.tensorboard
|
2024-06-05 15:53:02 +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
|
2023-11-16 04:20:00 +00:00
|
|
|
|
rocmSupport ? config.rocmSupport,
|
2024-04-21 15:54:59 +00:00
|
|
|
|
rocmPackages_5,
|
2024-06-05 15:53:02 +00:00
|
|
|
|
gpuTargets ? [ ],
|
2023-01-20 10:41:00 +00:00
|
|
|
|
}:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
|
let
|
2024-06-05 15:53:02 +00:00
|
|
|
|
inherit (lib)
|
|
|
|
|
attrsets
|
|
|
|
|
lists
|
|
|
|
|
strings
|
|
|
|
|
trivial
|
|
|
|
|
;
|
2024-01-02 11:29:13 +00:00
|
|
|
|
inherit (cudaPackages) cudaFlags cudnn nccl;
|
2022-04-15 01:41:22 +00:00
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
|
rocmPackages = rocmPackages_5;
|
|
|
|
|
|
2021-04-12 18:23:04 +00:00
|
|
|
|
setBool = v: if v then "1" else "0";
|
2023-03-04 12:14:45 +00:00
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
|
# https://github.com/pytorch/pytorch/blob/v2.4.0/torch/utils/cpp_extension.py#L1953
|
2023-03-04 12:14:45 +00:00
|
|
|
|
supportedTorchCudaCapabilities =
|
|
|
|
|
let
|
2024-06-05 15:53:02 +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"
|
2024-09-19 14:19:46 +00:00
|
|
|
|
"9.0a"
|
2024-06-05 15:53:02 +00:00
|
|
|
|
];
|
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.
|
2024-06-05 15:53:02 +00:00
|
|
|
|
gpuArchWarner =
|
|
|
|
|
supported: unsupported:
|
|
|
|
|
trivial.throwIf (supported == [ ]) (
|
|
|
|
|
"No supported GPU targets specified. Requested GPU targets: "
|
|
|
|
|
+ strings.concatStringsSep ", " unsupported
|
|
|
|
|
) supported;
|
2023-03-04 12:14:45 +00:00
|
|
|
|
|
|
|
|
|
# Create the gpuTargetString.
|
|
|
|
|
gpuTargetString = strings.concatStringsSep ";" (
|
|
|
|
|
if gpuTargets != [ ] then
|
2024-06-05 15:53:02 +00:00
|
|
|
|
# If gpuTargets is specified, it always takes priority.
|
2023-03-04 12:14:45 +00:00
|
|
|
|
gpuTargets
|
|
|
|
|
else if cudaSupport then
|
|
|
|
|
gpuArchWarner supportedCudaCapabilities unsupportedCudaCapabilities
|
|
|
|
|
else if rocmSupport then
|
2023-10-19 13:55:26 +00:00
|
|
|
|
rocmPackages.clr.gpuTargets
|
2023-03-04 12:14:45 +00:00
|
|
|
|
else
|
|
|
|
|
throw "No GPU targets specified"
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
rocmtoolkit_joined = symlinkJoin {
|
|
|
|
|
name = "rocm-merged";
|
|
|
|
|
|
2023-10-19 13:55:26 +00:00
|
|
|
|
paths = with rocmPackages; [
|
2024-06-05 15:53:02 +00:00
|
|
|
|
rocm-core
|
|
|
|
|
clr
|
|
|
|
|
rccl
|
|
|
|
|
miopen
|
|
|
|
|
miopengemm
|
|
|
|
|
rocrand
|
|
|
|
|
rocblas
|
|
|
|
|
rocsparse
|
|
|
|
|
hipsparse
|
|
|
|
|
rocthrust
|
|
|
|
|
rocprim
|
|
|
|
|
hipcub
|
|
|
|
|
roctracer
|
|
|
|
|
rocfft
|
|
|
|
|
rocsolver
|
|
|
|
|
hipfft
|
|
|
|
|
hipsolver
|
|
|
|
|
hipblas
|
|
|
|
|
rocminfo
|
|
|
|
|
rocm-thunk
|
|
|
|
|
rocm-comgr
|
|
|
|
|
rocm-device-libs
|
|
|
|
|
rocm-runtime
|
|
|
|
|
clr.icd
|
|
|
|
|
hipify
|
2023-01-20 10:41:00 +00:00
|
|
|
|
];
|
2023-10-19 13:55:26 +00:00
|
|
|
|
|
|
|
|
|
# Fix `setuptools` not being found
|
|
|
|
|
postBuild = ''
|
|
|
|
|
rm -rf $out/nix-support
|
|
|
|
|
'';
|
2023-01-20 10:41:00 +00:00
|
|
|
|
};
|
2023-10-09 19:29:22 +00:00
|
|
|
|
|
|
|
|
|
brokenConditions = attrsets.filterAttrs (_: cond: cond) {
|
2024-01-02 11:29:13 +00:00
|
|
|
|
"CUDA and ROCm are mutually exclusive" = cudaSupport && rocmSupport;
|
2024-09-26 11:04:55 +00:00
|
|
|
|
"CUDA is not targeting Linux" = cudaSupport && !stdenv.hostPlatform.isLinux;
|
2024-06-05 15:53:02 +00:00
|
|
|
|
"Unsupported CUDA version" =
|
|
|
|
|
cudaSupport
|
|
|
|
|
&& !(builtins.elem cudaPackages.cudaMajorVersion [
|
|
|
|
|
"11"
|
|
|
|
|
"12"
|
|
|
|
|
]);
|
|
|
|
|
"MPI cudatoolkit does not match cudaPackages.cudatoolkit" =
|
|
|
|
|
MPISupport && cudaSupport && (mpi.cudatoolkit != cudaPackages.cudatoolkit);
|
2024-09-19 14:19:46 +00:00
|
|
|
|
# This used to be a deep package set comparison between cudaPackages and
|
|
|
|
|
# effectiveMagma.cudaPackages, making torch too strict in cudaPackages.
|
|
|
|
|
# In particular, this triggered warnings from cuda's `aliases.nix`
|
2024-06-05 15:53:02 +00:00
|
|
|
|
"Magma cudaPackages does not match cudaPackages" =
|
2024-09-19 14:19:46 +00:00
|
|
|
|
cudaSupport && (effectiveMagma.cudaPackages.cudaVersion != cudaPackages.cudaVersion);
|
|
|
|
|
"Rocm support is currently broken because `rocmPackages.hipblaslt` is unpackaged. (2024-06-09)" =
|
|
|
|
|
rocmSupport;
|
2023-10-09 19:29:22 +00:00
|
|
|
|
};
|
2024-06-05 15:53:02 +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.
|
2024-09-26 11:04:55 +00:00
|
|
|
|
version = "2.4.1";
|
2024-02-29 20:09:43 +00:00
|
|
|
|
pyproject = true;
|
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
|
2024-01-02 11:29:13 +00:00
|
|
|
|
"cxxdev" # propagated deps for the cmake consumers of torch
|
2020-04-24 23:36:52 +00:00
|
|
|
|
];
|
2024-01-02 11:29:13 +00:00
|
|
|
|
cudaPropagateToOutput = "cxxdev";
|
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;
|
2024-09-26 11:04:55 +00:00
|
|
|
|
hash = "sha256-x/zM/57syr46CP1TfGaefSjzvNm4jJbWFZGVGyzPMg8=";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
patches =
|
2024-09-19 14:19:46 +00:00
|
|
|
|
[
|
|
|
|
|
# Allow setting PYTHON_LIB_REL_PATH with an environment variable.
|
|
|
|
|
# https://github.com/pytorch/pytorch/pull/128419
|
|
|
|
|
./passthrough-python-lib-rel-path.patch
|
|
|
|
|
]
|
|
|
|
|
++ lib.optionals cudaSupport [ ./fix-cmake-cuda-toolkit.patch ]
|
2024-09-26 11:04:55 +00:00
|
|
|
|
++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
|
2024-06-05 15:53: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
|
|
|
|
|
]
|
2024-09-26 11:04:55 +00:00
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isLinux [
|
2024-06-05 15:53:02 +00:00
|
|
|
|
# Propagate CUPTI to Kineto by overriding the search path with environment variables.
|
|
|
|
|
# https://github.com/pytorch/pytorch/pull/108847
|
|
|
|
|
./pytorch-pr-108847.patch
|
|
|
|
|
];
|
2020-07-18 16:06:22 +00:00
|
|
|
|
|
2024-06-05 15:53:02 +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.splitVersion rocmPackages.clr.version))})"
|
|
|
|
|
''
|
|
|
|
|
# Detection of NCCL version doesn't work particularly well when using the static binary.
|
|
|
|
|
+ lib.optionalString cudaSupport ''
|
|
|
|
|
substituteInPlace cmake/Modules/FindNCCL.cmake \
|
|
|
|
|
--replace \
|
|
|
|
|
'message(FATAL_ERROR "Found NCCL header version and library version' \
|
|
|
|
|
'message(WARNING "Found NCCL header version and library version'
|
|
|
|
|
''
|
2024-09-19 14:19:46 +00:00
|
|
|
|
# Remove PyTorch's FindCUDAToolkit.cmake and use CMake's default.
|
|
|
|
|
# NOTE: Parts of pytorch rely on unmaintained FindCUDA.cmake with custom patches to support e.g.
|
|
|
|
|
# newer architectures (sm_90a). We do want to delete vendored patches, but have to keep them
|
|
|
|
|
# until https://github.com/pytorch/pytorch/issues/76082 is addressed
|
2024-06-05 15:53:02 +00:00
|
|
|
|
+ lib.optionalString cudaSupport ''
|
|
|
|
|
rm cmake/Modules/FindCUDAToolkit.cmake
|
|
|
|
|
''
|
|
|
|
|
# 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.
|
|
|
|
|
+
|
2024-09-26 11:04:55 +00:00
|
|
|
|
lib.optionalString
|
|
|
|
|
(stdenv.hostPlatform.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinSdkVersion "11.0")
|
2024-06-05 15:53:02 +00:00
|
|
|
|
''
|
|
|
|
|
substituteInPlace third_party/pocketfft/pocketfft_hdronly.h --replace-fail '#if (__cplusplus >= 201703L) && (!defined(__MINGW32__)) && (!defined(_MSC_VER))
|
|
|
|
|
inline void *aligned_alloc(size_t align, size_t size)' '#if 0
|
|
|
|
|
inline void *aligned_alloc(size_t align, size_t size)'
|
|
|
|
|
'';
|
2023-01-20 10:41:00 +00:00
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
# NOTE(@connorbaker): Though we do not disable Gloo or MPI when building with CUDA support, caution should be taken
|
|
|
|
|
# when using the different backends. Gloo's GPU support isn't great, and MPI and CUDA can't be used at the same time
|
|
|
|
|
# without extreme care to ensure they don't lock each other out of shared resources.
|
|
|
|
|
# For more, see https://github.com/open-mpi/ompi/issues/7733#issuecomment-629806195.
|
2024-06-05 15:53:02 +00:00
|
|
|
|
preConfigure =
|
|
|
|
|
lib.optionalString cudaSupport ''
|
|
|
|
|
export TORCH_CUDA_ARCH_LIST="${gpuTargetString}"
|
2024-07-27 06:49:29 +00:00
|
|
|
|
export CUPTI_INCLUDE_DIR=${lib.getDev cudaPackages.cuda_cupti}/include
|
|
|
|
|
export CUPTI_LIBRARY_DIR=${lib.getLib cudaPackages.cuda_cupti}/lib
|
2024-06-05 15:53:02 +00:00
|
|
|
|
''
|
|
|
|
|
+ lib.optionalString (cudaSupport && cudaPackages ? cudnn) ''
|
2024-07-27 06:49:29 +00:00
|
|
|
|
export CUDNN_INCLUDE_DIR=${lib.getLib cudnn}/include
|
2024-06-05 15:53:02 +00:00
|
|
|
|
export CUDNN_LIB_DIR=${cudnn.lib}/lib
|
|
|
|
|
''
|
|
|
|
|
+ lib.optionalString rocmSupport ''
|
|
|
|
|
export ROCM_PATH=${rocmtoolkit_joined}
|
|
|
|
|
export ROCM_SOURCE_DIR=${rocmtoolkit_joined}
|
|
|
|
|
export PYTORCH_ROCM_ARCH="${gpuTargetString}"
|
|
|
|
|
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
|
|
|
|
|
2024-02-29 20:09:43 +00:00
|
|
|
|
# NB technical debt: building without NNPACK as workaround for missing `six`
|
|
|
|
|
USE_NNPACK = 0;
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
preBuild = ''
|
|
|
|
|
export MAX_JOBS=$NIX_BUILD_CORES
|
2023-11-16 04:20:00 +00:00
|
|
|
|
${python.pythonOnBuildForHost.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;
|
|
|
|
|
|
2024-01-13 08:15:51 +00:00
|
|
|
|
# In-tree builds of NCCL are not supported.
|
|
|
|
|
# Use NCCL when cudaSupport is enabled and nccl is available.
|
|
|
|
|
USE_NCCL = setBool useSystemNccl;
|
|
|
|
|
USE_SYSTEM_NCCL = USE_NCCL;
|
|
|
|
|
USE_STATIC_NCCL = USE_NCCL;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
|
# Set the correct Python library path, broken since
|
|
|
|
|
# https://github.com/pytorch/pytorch/commit/3d617333e
|
|
|
|
|
PYTHON_LIB_REL_PATH = "${placeholder "out"}/${python.sitePackages}";
|
|
|
|
|
|
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
|
2024-06-05 15:53:02 +00:00
|
|
|
|
env.NIX_CFLAGS_COMPILE = toString (
|
|
|
|
|
(
|
|
|
|
|
lib.optionals (blas.implementation == "mkl") [ "-Wno-error=array-bounds" ]
|
|
|
|
|
# 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
|
|
|
|
|
++ 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"
|
|
|
|
|
]
|
|
|
|
|
# .../source/torch/csrc/autograd/generated/python_functions_0.cpp:85:3:
|
|
|
|
|
# error: cast from ... to ... converts to incompatible function type [-Werror,-Wcast-function-type-strict]
|
|
|
|
|
++ lib.optionals (stdenv.cc.isClang && lib.versionAtLeast stdenv.cc.version "16") [
|
|
|
|
|
"-Wno-error=cast-function-type-strict"
|
|
|
|
|
# Suppresses the most spammy warnings.
|
|
|
|
|
# This is mainly to fix https://github.com/NixOS/nixpkgs/issues/266895.
|
|
|
|
|
]
|
|
|
|
|
++ lib.optionals rocmSupport [
|
|
|
|
|
"-Wno-#warnings"
|
|
|
|
|
"-Wno-cpp"
|
|
|
|
|
"-Wno-unknown-warning-option"
|
|
|
|
|
"-Wno-ignored-attributes"
|
|
|
|
|
"-Wno-deprecated-declarations"
|
|
|
|
|
"-Wno-defaulted-function-deleted"
|
|
|
|
|
"-Wno-pass-failed"
|
|
|
|
|
]
|
|
|
|
|
++ [
|
|
|
|
|
"-Wno-unused-command-line-argument"
|
|
|
|
|
"-Wno-uninitialized"
|
|
|
|
|
"-Wno-array-bounds"
|
|
|
|
|
"-Wno-free-nonheap-object"
|
|
|
|
|
"-Wno-unused-result"
|
|
|
|
|
]
|
|
|
|
|
++ lib.optionals stdenv.cc.isGNU [
|
|
|
|
|
"-Wno-maybe-uninitialized"
|
|
|
|
|
"-Wno-stringop-overflow"
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
nativeBuildInputs =
|
|
|
|
|
[
|
|
|
|
|
cmake
|
|
|
|
|
which
|
|
|
|
|
ninja
|
|
|
|
|
pybind11
|
|
|
|
|
removeReferencesTo
|
|
|
|
|
]
|
|
|
|
|
++ lib.optionals cudaSupport (
|
|
|
|
|
with cudaPackages;
|
|
|
|
|
[
|
|
|
|
|
autoAddDriverRunpath
|
|
|
|
|
cuda_nvcc
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
++ lib.optionals rocmSupport [ rocmtoolkit_joined ];
|
|
|
|
|
|
|
|
|
|
buildInputs =
|
|
|
|
|
[
|
|
|
|
|
blas
|
|
|
|
|
blas.provider
|
|
|
|
|
]
|
|
|
|
|
++ lib.optionals cudaSupport (
|
|
|
|
|
with cudaPackages;
|
|
|
|
|
[
|
2024-07-27 06:49:29 +00:00
|
|
|
|
cuda_cccl # <thrust/*>
|
|
|
|
|
cuda_cudart # cuda_runtime.h and libraries
|
|
|
|
|
cuda_cupti # For kineto
|
|
|
|
|
cuda_nvcc # crt/host_config.h; even though we include this in nativeBuildinputs, it's needed here too
|
|
|
|
|
cuda_nvml_dev # <nvml.h>
|
|
|
|
|
cuda_nvrtc
|
|
|
|
|
cuda_nvtx # -llibNVToolsExt
|
|
|
|
|
libcublas
|
|
|
|
|
libcufft
|
|
|
|
|
libcurand
|
|
|
|
|
libcusolver
|
|
|
|
|
libcusparse
|
2024-06-05 15:53:02 +00:00
|
|
|
|
]
|
2024-09-19 14:19:46 +00:00
|
|
|
|
++ lists.optionals (cudaPackages ? cudnn) [ cudnn ]
|
2024-06-05 15:53:02 +00:00
|
|
|
|
++ lists.optionals useSystemNccl [
|
|
|
|
|
# Some platforms do not support NCCL (i.e., Jetson)
|
2024-07-27 06:49:29 +00:00
|
|
|
|
nccl # Provides nccl.h AND a static copy of NCCL!
|
2024-06-05 15:53:02 +00:00
|
|
|
|
]
|
|
|
|
|
++ lists.optionals (strings.versionOlder cudaVersion "11.8") [
|
2024-07-27 06:49:29 +00:00
|
|
|
|
cuda_nvprof # <cuda_profiler_api.h>
|
2024-06-05 15:53:02 +00:00
|
|
|
|
]
|
|
|
|
|
++ lists.optionals (strings.versionAtLeast cudaVersion "11.8") [
|
2024-07-27 06:49:29 +00:00
|
|
|
|
cuda_profiler_api # <cuda_profiler_api.h>
|
2024-06-05 15:53:02 +00:00
|
|
|
|
]
|
|
|
|
|
)
|
2023-10-19 13:55:26 +00:00
|
|
|
|
++ lib.optionals rocmSupport [ rocmPackages.llvm.openmp ]
|
2024-01-02 11:29:13 +00:00
|
|
|
|
++ lib.optionals (cudaSupport || rocmSupport) [ effectiveMagma ]
|
2024-09-26 11:04:55 +00:00
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isLinux [ numactl ]
|
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isDarwin [
|
2024-06-05 15:53:02 +00:00
|
|
|
|
Accelerate
|
|
|
|
|
CoreServices
|
|
|
|
|
libobjc
|
|
|
|
|
]
|
2024-07-27 06:49:29 +00:00
|
|
|
|
++ lib.optionals tritonSupport [ triton ]
|
2024-01-02 11:29:13 +00:00
|
|
|
|
++ lib.optionals MPISupport [ mpi ]
|
|
|
|
|
++ lib.optionals rocmSupport [ rocmtoolkit_joined ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
|
dependencies = [
|
2024-02-29 20:09:43 +00:00
|
|
|
|
astunparse
|
2020-04-24 23:36:52 +00:00
|
|
|
|
cffi
|
|
|
|
|
click
|
|
|
|
|
numpy
|
|
|
|
|
pyyaml
|
2023-04-12 12:48:02 +00:00
|
|
|
|
|
|
|
|
|
# From install_requires:
|
2024-01-02 11:29:13 +00:00
|
|
|
|
fsspec
|
2023-04-12 12:48:02 +00:00
|
|
|
|
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
|
2024-06-05 15:53:02 +00:00
|
|
|
|
pillow
|
|
|
|
|
six
|
|
|
|
|
future
|
|
|
|
|
tensorboard
|
|
|
|
|
protobuf
|
2023-11-16 04:20:00 +00:00
|
|
|
|
|
|
|
|
|
# torch/csrc requires `pybind11` at runtime
|
|
|
|
|
pybind11
|
2024-07-27 06:49:29 +00:00
|
|
|
|
] ++ lib.optionals tritonSupport [ triton ];
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
propagatedCxxBuildInputs =
|
|
|
|
|
[ ] ++ lib.optionals MPISupport [ mpi ] ++ lib.optionals rocmSupport [ rocmtoolkit_joined ];
|
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
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
pythonImportsCheck = [ "torch" ];
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
nativeCheckInputs = [
|
|
|
|
|
hypothesis
|
|
|
|
|
ninja
|
|
|
|
|
psutil
|
2020-05-15 21:57:56 +00:00
|
|
|
|
];
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
checkPhase =
|
|
|
|
|
with lib.versions;
|
|
|
|
|
with lib.strings;
|
|
|
|
|
concatStringsSep " " [
|
|
|
|
|
"runHook preCheck"
|
|
|
|
|
"${python.interpreter} test/run_test.py"
|
|
|
|
|
"--exclude"
|
|
|
|
|
(concatStringsSep " " [
|
|
|
|
|
"utils" # utils requires git, which is not allowed in the check phase
|
|
|
|
|
|
|
|
|
|
# "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")
|
|
|
|
|
])
|
|
|
|
|
"runHook postCheck"
|
|
|
|
|
];
|
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
|
pythonRemoveDeps = [
|
|
|
|
|
# In our dist-info the name is just "triton"
|
|
|
|
|
"pytorch-triton-rocm"
|
|
|
|
|
];
|
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
postInstall =
|
|
|
|
|
''
|
|
|
|
|
find "$out/${python.sitePackages}/torch/include" "$out/${python.sitePackages}/torch/lib" -type f -exec remove-references-to -t ${stdenv.cc} '{}' +
|
|
|
|
|
|
|
|
|
|
mkdir $dev
|
|
|
|
|
cp -r $out/${python.sitePackages}/torch/include $dev/include
|
|
|
|
|
cp -r $out/${python.sitePackages}/torch/share $dev/share
|
|
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
|
|
|
|
|
mkdir $lib
|
|
|
|
|
mv $out/${python.sitePackages}/torch/lib $lib/lib
|
|
|
|
|
ln -s $lib/lib $out/${python.sitePackages}/torch/lib
|
|
|
|
|
''
|
|
|
|
|
+ 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"
|
|
|
|
|
'';
|
2023-01-20 10:41:00 +00:00
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
|
postFixup =
|
|
|
|
|
''
|
|
|
|
|
mkdir -p "$cxxdev/nix-support"
|
|
|
|
|
printWords "''${propagatedCxxBuildInputs[@]}" >> "$cxxdev/nix-support/propagated-build-inputs"
|
|
|
|
|
''
|
2024-09-26 11:04:55 +00:00
|
|
|
|
+ lib.optionalString stdenv.hostPlatform.isDarwin ''
|
2024-06-05 15:53:02 +00:00
|
|
|
|
for f in $(ls $lib/lib/*.dylib); do
|
|
|
|
|
install_name_tool -id $lib/lib/$(basename $f) $f || true
|
|
|
|
|
done
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2024-06-05 15:53: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
|
|
|
|
|
2024-06-05 15:53: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
|
|
|
|
|
2024-06-05 15:53: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
|
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
|
# See https://github.com/NixOS/nixpkgs/issues/296179
|
|
|
|
|
#
|
|
|
|
|
# This is a quick hack to add `libnvrtc` to the runpath so that torch can find
|
|
|
|
|
# it when it is needed at runtime.
|
|
|
|
|
extraRunpaths = lib.optionals cudaSupport [ "${lib.getLib cudaPackages.cuda_nvrtc}/lib" ];
|
2024-09-26 11:04:55 +00:00
|
|
|
|
postPhases = lib.optionals stdenv.hostPlatform.isLinux [ "postPatchelfPhase" ];
|
2024-06-20 14:57:18 +00:00
|
|
|
|
postPatchelfPhase = ''
|
|
|
|
|
while IFS= read -r -d $'\0' elf ; do
|
|
|
|
|
for extra in $extraRunpaths ; do
|
|
|
|
|
echo patchelf "$elf" --add-rpath "$extra" >&2
|
|
|
|
|
patchelf "$elf" --add-rpath "$extra"
|
|
|
|
|
done
|
|
|
|
|
done < <(
|
|
|
|
|
find "''${!outputLib}" "$out" -type f -iname '*.so' -print0
|
|
|
|
|
)
|
|
|
|
|
'';
|
|
|
|
|
|
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 = {
|
2024-06-05 15:53:02 +00:00
|
|
|
|
inherit
|
|
|
|
|
cudaSupport
|
|
|
|
|
cudaPackages
|
|
|
|
|
rocmSupport
|
|
|
|
|
rocmPackages
|
|
|
|
|
;
|
2024-07-01 15:47:52 +00:00
|
|
|
|
cudaCapabilities = if cudaSupport then supportedCudaCapabilities else [ ];
|
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-10-09 19:29:22 +00:00
|
|
|
|
# To help debug when a package is broken due to CUDA support
|
|
|
|
|
inherit brokenConditions;
|
2024-07-01 15:47:52 +00:00
|
|
|
|
tests = callPackage ./tests.nix { };
|
2021-08-08 23:34:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
|
meta = {
|
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/";
|
2024-06-20 14:57:18 +00:00
|
|
|
|
license = lib.licenses.bsd3;
|
|
|
|
|
maintainers = with lib.maintainers; [
|
2024-06-05 15:53:02 +00:00
|
|
|
|
teh
|
|
|
|
|
thoughtpolice
|
|
|
|
|
tscholak
|
|
|
|
|
]; # tscholak esp. for darwin-related builds
|
2024-06-20 14:57:18 +00:00
|
|
|
|
platforms = with lib.platforms; linux ++ lib.optionals (!cudaSupport && !rocmSupport) darwin;
|
2023-10-09 19:29:22 +00:00
|
|
|
|
broken = builtins.any trivial.id (builtins.attrValues brokenConditions);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|