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.7.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 [
40 # We need to patch lib/torio/_torio_ffmpeg6
41 ffmpeg_6.dev
42 sox
43 ]
44 ++ lib.optionals stdenv.hostPlatform.isLinux (
45 with cudaPackages;
46 [
47 # $out/${sitePackages}/torchaudio/lib/libtorchaudio*.so wants libcudart.so.11.0 but torch/lib only ships
48 # libcudart.$hash.so.11.0
49 cuda_cudart
50
51 # $out/${sitePackages}/torchaudio/lib/libtorchaudio*.so wants libnvToolsExt.so.2 but torch/lib only ships
52 # libnvToolsExt-$hash.so.1
53 cuda_nvtx
54 ]
55 );
56
57 nativeBuildInputs = lib.optionals stdenv.hostPlatform.isLinux [
58 autoPatchelfHook
59 addDriverRunpath
60 ];
61
62 dependencies = [ torch-bin ];
63
64 preInstall = lib.optionals stdenv.hostPlatform.isLinux ''
65 addAutoPatchelfSearchPath "${torch-bin}/${python.sitePackages}/torch"
66 '';
67
68 preFixup = ''
69 # TorchAudio loads the newest FFmpeg that works, so get rid of the
70 # old ones.
71 rm $out/${python.sitePackages}/torio/lib/{lib,_}torio_ffmpeg{4,5}.*
72 '';
73
74 # The wheel-binary is not stripped to avoid the error of `ImportError: libtorch_cuda_cpp.so: ELF load command address/offset not properly aligned.`.
75 dontStrip = true;
76
77 pythonImportsCheck = [ "torchaudio" ];
78
79 meta = {
80 description = "PyTorch audio library";
81 homepage = "https://pytorch.org/";
82 changelog = "https://github.com/pytorch/audio/releases/tag/v${version}";
83 # Includes CUDA and Intel MKL, but redistributions of the binary are not limited.
84 # https://docs.nvidia.com/cuda/eula/index.html
85 # https://www.intel.com/content/www/us/en/developer/articles/license/onemkl-license-faq.html
86 license = lib.licenses.bsd3;
87 sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
88 platforms = [
89 "aarch64-linux"
90 "x86_64-linux"
91 "aarch64-darwin"
92 ];
93 maintainers = with lib.maintainers; [
94 GaetanLepage
95 junjihashimoto
96 ];
97 };
98}