summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/paddlepaddle/default.nix
blob: fe0f6519409fd9f03e5844089cd04e86f8f1deab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  config,
  lib,
  stdenv,
  buildPythonPackage,
  fetchurl,
  fetchPypi,
  python,
  pythonOlder,
  pythonAtLeast,
  autoPatchelfHook,
  bash,
  zlib,
  setuptools,
  cudaSupport ? config.cudaSupport or false,
  cudaPackages,
  addDriverRunpath,
  # runtime dependencies
  httpx,
  numpy,
  protobuf,
  pillow,
  decorator,
  astor,
  opt-einsum,
  rdma-core,
  safetensors,
  typing-extensions,
}:

let
  pname = "paddlepaddle" + lib.optionalString cudaSupport "-gpu";
  sources = import ./sources.nix;
  version = sources.version;
  format = "wheel";
  pyShortVersion = "cp${builtins.replaceStrings [ "." ] [ "" ] python.pythonVersion}";
  cudaVersion = "cu${lib.replaceStrings [ "." ] [ "" ] cudaPackages.cudatoolkit.version}";

  throwSystem = throw "Unsupported system: ${stdenv.hostPlatform.system}";
  systemSources = sources."${stdenv.hostPlatform.system}" or throwSystem;

  supportedCudaVersions = lib.concatStringsSep ", " (builtins.attrNames systemSources.gpu);
  throwCuda = throw "Unsupported CUDA version: ${cudaVersion}. Supported versions: ${supportedCudaVersions}";
  platformSources =
    if cudaSupport then systemSources.gpu.${cudaVersion} or throwCuda else systemSources.cpu;

  throwPython = throw "Unsupported python version: ${pyShortVersion}";
  hash = platformSources.${pyShortVersion} or throwPython;

  platform = sources.${stdenv.hostPlatform.system}.platform;

  src =
    if cudaSupport then
      (fetchurl {
        url = "https://paddle-whl.bj.bcebos.com/stable/${cudaVersion}/paddlepaddle-gpu/paddlepaddle_gpu-${version}-${pyShortVersion}-${pyShortVersion}-linux_x86_64.whl";
        inherit hash;
      })
    else
      (fetchPypi {
        inherit
          version
          format
          hash
          platform
          ;
        pname = "paddlepaddle";
        dist = pyShortVersion;
        python = pyShortVersion;
        abi = pyShortVersion;
      });
in
buildPythonPackage {
  inherit
    pname
    version
    format
    src
    ;

  disabled = pythonOlder "3.12" || pythonAtLeast "3.14";

  nativeBuildInputs = [
    addDriverRunpath
  ]
  ++ lib.optionals cudaSupport [ autoPatchelfHook ];

  buildInputs = lib.optionals cudaSupport [ rdma-core ];

  dependencies = [
    setuptools
    httpx
    numpy
    protobuf
    pillow
    decorator
    astor
    opt-einsum
    safetensors
    typing-extensions
  ];

  # Segmentation fault in darwin sandbox
  pythonImportsCheck = lib.optionals stdenv.hostPlatform.isLinux [ "paddle" ];

  # no tests
  doCheck = false;

  postFixup =
    lib.optionalString stdenv.hostPlatform.isLinux (
      let
        libraryPath = lib.makeLibraryPath (
          [
            zlib
            (lib.getLib stdenv.cc.cc)
          ]
          ++ lib.optionals cudaSupport (
            with cudaPackages;
            [
              cudatoolkit.lib
              cudatoolkit.out
              cudnn
            ]
          )
        );
      in
      ''
        function fixRunPath {
          patchelf --add-rpath ${libraryPath} $1
          ${lib.optionalString cudaSupport ''
            addDriverRunpath $1
          ''}
        }
        fixRunPath $out/${python.sitePackages}/paddle/base/libpaddle.so
        fixRunPath $out/${python.sitePackages}/paddle/libs/lib*.so
      ''
    )
    + ''
      substituteInPlace $out/bin/paddle \
        --replace-fail "/bin/bash" "${lib.getExe bash}" \
        --replace-fail "python -"  "${lib.getExe (python.withPackages (ps: with ps; [ distutils ]))} -"
      sed -i '/# Check python lib installed or not./,/^fi$/d' $out/bin/paddle
      sed -i 's/^INSTALLED_VERSION=.*/INSTALLED_VERSION="${version}"/' $out/bin/paddle
    '';

  passthru.updateScript = ./update.sh;

  meta = {
    description = "Machine Learning Framework from Industrial Practice";
    homepage = "https://github.com/PaddlePaddle/Paddle";
    license = lib.licenses.asl20;
    maintainers = with lib.maintainers; [ happysalada ];
    platforms = [
      "x86_64-linux"
    ]
    ++ lib.optionals (!cudaSupport) [
      "aarch64-linux"
      "aarch64-darwin"
    ];
    sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
  };
}