1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 python,
6 fetchurl,
7 pythonOlder,
8 pythonAtLeast,
9
10 # buildInputs
11 cudaPackages,
12 ffmpeg_6,
13 sox,
14
15 # nativeBuildInputs
16 addDriverRunpath,
17 autoPatchelfHook,
18
19 # dependencies
20 torch-bin,
21}:
22
23buildPythonPackage rec {
24 pname = "torchaudio";
25 version = "2.8.0";
26 format = "wheel";
27
28 src =
29 let
30 pyVerNoDot = lib.replaceStrings [ "." ] [ "" ] python.pythonVersion;
31 unsupported = throw "Unsupported system";
32 srcs = (import ./binary-hashes.nix version)."${stdenv.system}-${pyVerNoDot}" or unsupported;
33 in
34 fetchurl srcs;
35
36 disabled = (pythonOlder "3.9") || (pythonAtLeast "3.14");
37
38 buildInputs = [
39 # We need to patch lib/torio/_torio_ffmpeg6
40 ffmpeg_6.dev
41 sox
42 ]
43 ++ lib.optionals stdenv.hostPlatform.isLinux (
44 with cudaPackages;
45 [
46 # $out/${sitePackages}/torchaudio/lib/libtorchaudio*.so wants libcudart.so.11.0 but torch/lib only ships
47 # libcudart.$hash.so.11.0
48 cuda_cudart
49
50 # $out/${sitePackages}/torchaudio/lib/libtorchaudio*.so wants libnvToolsExt.so.2 but torch/lib only ships
51 # libnvToolsExt-$hash.so.1
52 cuda_nvtx
53 ]
54 );
55
56 nativeBuildInputs = lib.optionals stdenv.hostPlatform.isLinux [
57 autoPatchelfHook
58 addDriverRunpath
59 ];
60
61 dependencies = [ torch-bin ];
62
63 preInstall = lib.optionals stdenv.hostPlatform.isLinux ''
64 addAutoPatchelfSearchPath "${torch-bin}/${python.sitePackages}/torch"
65 '';
66
67 preFixup = ''
68 # TorchAudio loads the newest FFmpeg that works, so get rid of the
69 # old ones.
70 rm $out/${python.sitePackages}/torio/lib/{lib,_}torio_ffmpeg{4,5}.*
71 '';
72
73 # The wheel-binary is not stripped to avoid the error of `ImportError: libtorch_cuda_cpp.so: ELF load command address/offset not properly aligned.`.
74 dontStrip = true;
75
76 pythonImportsCheck = [ "torchaudio" ];
77
78 meta = {
79 description = "PyTorch audio library";
80 homepage = "https://pytorch.org/";
81 changelog = "https://github.com/pytorch/audio/releases/tag/v${version}";
82 # Includes CUDA and Intel MKL, but redistributions of the binary are not limited.
83 # https://docs.nvidia.com/cuda/eula/index.html
84 # https://www.intel.com/content/www/us/en/developer/articles/license/onemkl-license-faq.html
85 license = lib.licenses.bsd3;
86 sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
87 platforms = [
88 "aarch64-linux"
89 "x86_64-linux"
90 "aarch64-darwin"
91 ];
92 maintainers = with lib.maintainers; [
93 GaetanLepage
94 junjihashimoto
95 ];
96 };
97}