1{
2 lib,
3 stdenv,
4 addDriverRunpath,
5 autoPatchelfHook,
6 buildPythonPackage,
7 cudaPackages,
8 fetchurl,
9 pythonAtLeast,
10 pythonOlder,
11 pillow,
12 python,
13 torch-bin,
14}:
15
16let
17 pyVerNoDot = builtins.replaceStrings [ "." ] [ "" ] python.pythonVersion;
18 srcs = import ./binary-hashes.nix version;
19 unsupported = throw "Unsupported system";
20 version = "0.18.1";
21in
22buildPythonPackage {
23 inherit version;
24
25 pname = "torchvision";
26
27 format = "wheel";
28
29 src = fetchurl srcs."${stdenv.system}-${pyVerNoDot}" or unsupported;
30
31 disabled = (pythonOlder "3.8") || (pythonAtLeast "3.13");
32
33 # Note that we don't rely on config.cudaSupport here, because the Linux wheels all come built with CUDA support.
34 buildInputs =
35 with cudaPackages;
36 lib.optionals stdenv.isLinux [
37 # $out/${sitePackages}/torchvision/_C.so wants libcudart.so.11.0 but torchvision.libs only ships
38 # libcudart.$hash.so.11.0
39 cuda_cudart
40 ];
41
42 nativeBuildInputs = lib.optionals stdenv.isLinux [
43 autoPatchelfHook
44 addDriverRunpath
45 ];
46
47 dependencies = [
48 pillow
49 torch-bin
50 ];
51
52 # The wheel-binary is not stripped to avoid the error of `ImportError: libtorch_cuda_cpp.so: ELF load command address/offset not properly aligned.`.
53 dontStrip = true;
54
55 pythonImportsCheck = [ "torchvision" ];
56
57 preInstall = lib.optionalString stdenv.isLinux ''
58 addAutoPatchelfSearchPath "${torch-bin}/${python.sitePackages}/torch"
59 '';
60
61 meta = {
62 description = "PyTorch vision library";
63 homepage = "https://pytorch.org/";
64 changelog = "https://github.com/pytorch/vision/releases/tag/v${version}";
65 # Includes CUDA and Intel MKL, but redistributions of the binary are not limited.
66 # https://docs.nvidia.com/cuda/eula/index.html
67 # https://www.intel.com/content/www/us/en/developer/articles/license/onemkl-license-faq.html
68 license = lib.licenses.bsd3;
69 sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
70 platforms = [
71 "aarch64-darwin"
72 "x86_64-linux"
73 "aarch64-linux"
74 ];
75 maintainers = with lib.maintainers; [ junjihashimoto ];
76 };
77}