|
| 1 | +{ config |
| 2 | +, stdenv |
| 3 | +, lib |
| 4 | +, fetchFromGitHub |
| 5 | +, abseil-cpp_202508 |
| 6 | +, cmake |
| 7 | +, cpuinfo |
| 8 | +, eigen |
| 9 | +, flatbuffers |
| 10 | +, glibcLocales |
| 11 | +, gtest |
| 12 | +, howard-hinnant-date |
| 13 | +, libpng |
| 14 | +, nlohmann_json |
| 15 | +, pkg-config |
| 16 | +, python3Packages |
| 17 | +, re2 |
| 18 | +, zlib |
| 19 | +, protobuf |
| 20 | +, microsoft-gsl |
| 21 | +, darwinMinVersionHook |
| 22 | +, pythonSupport ? true |
| 23 | +, cudaSupport ? config.cudaSupport |
| 24 | +, ncclSupport ? config.cudaSupport |
| 25 | +, cudaPackages ? { } |
| 26 | +, rocmSupport ? false |
| 27 | +, rcclSupport ? rocmSupport |
| 28 | +, rocmPackages ? { } |
| 29 | +, |
| 30 | +}@inputs: |
| 31 | + |
| 32 | +let |
| 33 | + version = "1.22.2"; |
| 34 | + |
| 35 | + src = fetchFromGitHub { |
| 36 | + owner = "microsoft"; |
| 37 | + repo = "onnxruntime"; |
| 38 | + tag = "v${version}"; |
| 39 | + fetchSubmodules = true; |
| 40 | + hash = "sha256-X8Pdtc0eR0iU+Xi2A1HrNo1xqCnoaxNjj4QFm/E3kSE="; |
| 41 | + }; |
| 42 | + |
| 43 | + stdenv = throw "Use effectiveStdenv instead"; |
| 44 | + effectiveStdenv = if cudaSupport then cudaPackages.backendStdenv else inputs.stdenv; |
| 45 | + |
| 46 | + cudaArchitecturesString = cudaPackages.flags.cmakeCudaArchitecturesString; |
| 47 | + |
| 48 | + mp11 = fetchFromGitHub { |
| 49 | + owner = "boostorg"; |
| 50 | + repo = "mp11"; |
| 51 | + tag = "boost-1.89.0"; |
| 52 | + hash = "sha256-6dbfae01358be88ebefcdfb7707a2a68ba914c39dc83fdd85f556761fe0fafb4"; |
| 53 | + }; |
| 54 | + |
| 55 | + safeint = fetchFromGitHub { |
| 56 | + owner = "dcleblanc"; |
| 57 | + repo = "safeint"; |
| 58 | + tag = "3.0.28a"; |
| 59 | + hash = "sha256-9e652d065a3cef80623287d5dc61edcf6a95ddab38a9dfeb34f155261fc9cef7"; |
| 60 | + }; |
| 61 | + |
| 62 | + onnx = fetchFromGitHub { |
| 63 | + owner = "onnx"; |
| 64 | + repo = "onnx"; |
| 65 | + tag = "v1.19.0"; |
| 66 | + hash = "sha256-2c2ac5a078b0350a0723fac606be8cd9e9e8cbd4c99bab1bffe2623b188fd236"; |
| 67 | + }; |
| 68 | + |
| 69 | + cutlass = fetchFromGitHub { |
| 70 | + owner = "NVIDIA"; |
| 71 | + repo = "cutlass"; |
| 72 | + tag = "v3.5.1"; |
| 73 | + hash = "sha256-sTGYN+bjtEqQ7Ootr/wvx3P9f8MCDSSj3qyCWjfdLEA="; |
| 74 | + }; |
| 75 | + |
| 76 | + dlpack = fetchFromGitHub { |
| 77 | + owner = "dmlc"; |
| 78 | + repo = "dlpack"; |
| 79 | + tag = "v1.1"; |
| 80 | + hash = "sha256-2e3b94b55825c240cc58e6721e15b449978cbae21a2a4caa23058b0157ee2fb3"; |
| 81 | + }; |
| 82 | + |
| 83 | + isCudaJetson = cudaSupport && cudaPackages.flags.isJetsonBuild; |
| 84 | +in |
| 85 | +effectiveStdenv.mkDerivation rec { |
| 86 | + pname = "onnxruntime"; |
| 87 | + inherit src version; |
| 88 | + |
| 89 | + patches = lib.optionals cudaSupport [ |
| 90 | + # We apply the referenced 1064.patch ourselves to our nix dependency. |
| 91 | + # FIND_PACKAGE_ARGS for CUDA was added in https://github.com/microsoft/onnxruntime/commit/87744e5 so it might be possible to delete this patch after upgrading to 1.17.0 |
| 92 | + ./nvcc-gsl.patch |
| 93 | + ]; |
| 94 | + |
| 95 | + nativeBuildInputs = [ |
| 96 | + cmake |
| 97 | + pkg-config |
| 98 | + python3Packages.python |
| 99 | + protobuf |
| 100 | + ] |
| 101 | + ++ lib.optionals pythonSupport ( |
| 102 | + with python3Packages; |
| 103 | + [ |
| 104 | + pip |
| 105 | + python |
| 106 | + pythonOutputDistHook |
| 107 | + setuptools |
| 108 | + wheel |
| 109 | + ] |
| 110 | + ) |
| 111 | + ++ lib.optionals cudaSupport [ |
| 112 | + cudaPackages.cuda_nvcc |
| 113 | + cudaPackages.cudnn-frontend |
| 114 | + ] |
| 115 | + ++ lib.optionals isCudaJetson [ |
| 116 | + cudaPackages.autoAddCudaCompatRunpath |
| 117 | + ] |
| 118 | + ++ lib.optionals rocmSupport [ |
| 119 | + rocmPackages.rocm-cmake |
| 120 | + rocmPackages.hipcc |
| 121 | + ]; |
| 122 | + |
| 123 | + buildInputs = [ |
| 124 | + eigen |
| 125 | + glibcLocales |
| 126 | + howard-hinnant-date |
| 127 | + libpng |
| 128 | + nlohmann_json |
| 129 | + microsoft-gsl |
| 130 | + zlib |
| 131 | + ] |
| 132 | + ++ lib.optionals (lib.meta.availableOn effectiveStdenv.hostPlatform cpuinfo) [ |
| 133 | + cpuinfo |
| 134 | + ] |
| 135 | + ++ lib.optionals pythonSupport ( |
| 136 | + with python3Packages; |
| 137 | + [ |
| 138 | + numpy |
| 139 | + pybind11 |
| 140 | + packaging |
| 141 | + ] |
| 142 | + ) |
| 143 | + ++ lib.optionals cudaSupport ( |
| 144 | + with cudaPackages; |
| 145 | + [ |
| 146 | + cuda_cccl # cub/cub.cuh |
| 147 | + libcublas # cublas_v2.h |
| 148 | + libcurand # curand.h |
| 149 | + libcusparse # cusparse.h |
| 150 | + libcufft # cufft.h |
| 151 | + cudnn # cudnn.h |
| 152 | + cuda_cudart |
| 153 | + ] |
| 154 | + ++ lib.optionals (cudaSupport && ncclSupport) ( |
| 155 | + with cudaPackages; |
| 156 | + [ |
| 157 | + nccl |
| 158 | + ] |
| 159 | + ) |
| 160 | + ) |
| 161 | + ++ lib.optionals rocmSupport ( |
| 162 | + with rocmPackages; |
| 163 | + [ |
| 164 | + rocm-core |
| 165 | + rocm-runtime |
| 166 | + hipblas |
| 167 | + rocblas |
| 168 | + miopen |
| 169 | + rocfft |
| 170 | + rocsparse |
| 171 | + ] |
| 172 | + ++ lib.optionals (rocmSupport && rcclSupport) [ |
| 173 | + rccl |
| 174 | + ] |
| 175 | + ) |
| 176 | + ++ lib.optionals effectiveStdenv.hostPlatform.isDarwin [ |
| 177 | + (darwinMinVersionHook "13.3") |
| 178 | + ]; |
| 179 | + |
| 180 | + nativeCheckInputs = [ |
| 181 | + gtest |
| 182 | + ] |
| 183 | + ++ lib.optionals pythonSupport ( |
| 184 | + with python3Packages; |
| 185 | + [ |
| 186 | + pytest |
| 187 | + sympy |
| 188 | + onnx |
| 189 | + ] |
| 190 | + ); |
| 191 | + |
| 192 | + # TODO: build server, and move .so's to lib output |
| 193 | + # Python's wheel is stored in a separate dist output |
| 194 | + outputs = [ |
| 195 | + "out" |
| 196 | + "dev" |
| 197 | + ] |
| 198 | + ++ lib.optionals pythonSupport [ "dist" ]; |
| 199 | + |
| 200 | + enableParallelBuilding = true; |
| 201 | + |
| 202 | + cmakeDir = "../cmake"; |
| 203 | + |
| 204 | + cmakeFlags = [ |
| 205 | + (lib.cmakeBool "ABSL_ENABLE_INSTALL" true) |
| 206 | + (lib.cmakeBool "FETCHCONTENT_FULLY_DISCONNECTED" true) |
| 207 | + (lib.cmakeBool "FETCHCONTENT_QUIET" false) |
| 208 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_ABSEIL_CPP" "${abseil-cpp_202508.src}") |
| 209 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_DLPACK" "${dlpack}") |
| 210 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_FLATBUFFERS" "${flatbuffers.src}") |
| 211 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_MP11" "${mp11}") |
| 212 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_ONNX" "${onnx}") |
| 213 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_RE2" "${re2.src}") |
| 214 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_SAFEINT" "${safeint}") |
| 215 | + (lib.cmakeFeature "FETCHCONTENT_TRY_FIND_PACKAGE_MODE" "ALWAYS") |
| 216 | + # fails to find protoc on darwin, so specify it |
| 217 | + (lib.cmakeFeature "ONNX_CUSTOM_PROTOC_EXECUTABLE" (lib.getExe protobuf)) |
| 218 | + (lib.cmakeBool "onnxruntime_BUILD_SHARED_LIB" true) |
| 219 | + (lib.cmakeBool "onnxruntime_BUILD_UNIT_TESTS" doCheck) |
| 220 | + (lib.cmakeBool "onnxruntime_USE_FULL_PROTOBUF" false) |
| 221 | + (lib.cmakeBool "onnxruntime_USE_CUDA" cudaSupport) |
| 222 | + (lib.cmakeBool "onnxruntime_USE_NCCL" (cudaSupport && ncclSupport)) |
| 223 | + (lib.cmakeBool "onnxruntime_USE_ROCM" rocmSupport) |
| 224 | + (lib.cmakeBool "onnxruntime_USE_RCCL" (rocmSupport && rcclSupport)) |
| 225 | + (lib.cmakeBool "onnxruntime_ENABLE_LTO" (!cudaSupport || cudaPackages.cudaOlder "12.8")) |
| 226 | + ] |
| 227 | + ++ lib.optionals pythonSupport [ |
| 228 | + (lib.cmakeBool "onnxruntime_ENABLE_PYTHON" true) |
| 229 | + ] |
| 230 | + ++ lib.optionals cudaSupport [ |
| 231 | + (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_CUTLASS" "${cutlass}") |
| 232 | + (lib.cmakeFeature "onnxruntime_CUDNN_HOME" "${cudaPackages.cudnn}") |
| 233 | + (lib.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaArchitecturesString) |
| 234 | + (lib.cmakeFeature "onnxruntime_NVCC_THREADS" "1") |
| 235 | + ] |
| 236 | + ++ lib.optionals rocmSupport [ |
| 237 | + (lib.cmakeFeature "ROCM_PATH" "${rocmPackages.rocm-core}") |
| 238 | + # Comprehensive AMD GPU architecture support: |
| 239 | + # MI Cards: gfx900 (MI25/Vega 10), gfx906 (MI50/MI60/Vega 20), gfx908 (MI100/CDNA), gfx90a (MI200/CDNA2), gfx942 (MI300/CDNA3) |
| 240 | + # RDNA Cards: gfx1010 (RX 5700/Navi 10), gfx1011 (Pro 5600M/Navi 12), gfx1012 (RX 5500/Navi 14), gfx1020 (Navi 21 early), gfx1030 (RX 6800/6900/Navi 21/22), gfx1100 (RX 7900/Navi 31) |
| 241 | + (lib.cmakeFeature "CMAKE_HIP_ARCHITECTURES" "gfx900;gfx906;gfx908;gfx90a;gfx942;gfx1010;gfx1011;gfx1012;gfx1020;gfx1030;gfx1100") |
| 242 | + (lib.cmakeFeature "HIP_COMPILER" "${rocmPackages.hipcc}/bin/hipcc") |
| 243 | + (lib.cmakeFeature "CMAKE_CXX_COMPILER" "${rocmPackages.hipcc}/bin/hipcc") |
| 244 | + ]; |
| 245 | + |
| 246 | + env = lib.optionalAttrs effectiveStdenv.cc.isClang { |
| 247 | + NIX_CFLAGS_COMPILE = "-Wno-error"; |
| 248 | + }; |
| 249 | + |
| 250 | + doCheck = |
| 251 | + !( |
| 252 | + cudaSupport |
| 253 | + || builtins.elem effectiveStdenv.buildPlatform.system [ |
| 254 | + # aarch64-linux fails cpuinfo test, because /sys/devices/system/cpu/ does not exist in the sandbox |
| 255 | + "aarch64-linux" |
| 256 | + # 1 - onnxruntime_test_all (Failed) |
| 257 | + # 4761 tests from 311 test suites ran, 57 failed. |
| 258 | + "loongarch64-linux" |
| 259 | + ] |
| 260 | + ); |
| 261 | + |
| 262 | + requiredSystemFeatures = lib.optionals cudaSupport [ "big-parallel" ]; |
| 263 | + |
| 264 | + hardeningEnable = lib.optionals (effectiveStdenv.hostPlatform.system == "loongarch64-linux") [ |
| 265 | + "nostrictaliasing" |
| 266 | + ]; |
| 267 | + |
| 268 | + postPatch = '' |
| 269 | + substituteInPlace cmake/libonnxruntime.pc.cmake.in \ |
| 270 | + --replace-fail '$'{prefix}/@CMAKE_INSTALL_ @CMAKE_INSTALL_ |
| 271 | + echo "find_package(cudnn_frontend REQUIRED)" > cmake/external/cudnn_frontend.cmake |
| 272 | +
|
| 273 | + # https://github.com/microsoft/onnxruntime/blob/c4f3742bb456a33ee9c826ce4e6939f8b84ce5b0/onnxruntime/core/platform/env.h#L249 |
| 274 | + substituteInPlace onnxruntime/core/platform/env.h --replace-fail \ |
| 275 | + "GetRuntimePath() const { return PathString(); }" \ |
| 276 | + "GetRuntimePath() const { return PathString(\"$out/lib/\"); }" |
| 277 | + '' |
| 278 | + + lib.optionalString (effectiveStdenv.hostPlatform.system == "aarch64-linux") '' |
| 279 | + # https://github.com/NixOS/nixpkgs/pull/226734#issuecomment-1663028691 |
| 280 | + rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc |
| 281 | + ''; |
| 282 | + |
| 283 | + postBuild = lib.optionalString pythonSupport '' |
| 284 | + ${python3Packages.python.interpreter} ../setup.py bdist_wheel |
| 285 | + ''; |
| 286 | + |
| 287 | + postInstall = '' |
| 288 | + # perform parts of `tools/ci_build/github/linux/copy_strip_binary.sh` |
| 289 | + install -m644 -Dt $out/include \ |
| 290 | + ../include/onnxruntime/core/framework/provider_options.h \ |
| 291 | + ../include/onnxruntime/core/providers/cpu/cpu_provider_factory.h \ |
| 292 | + ../include/onnxruntime/core/session/onnxruntime_*.h |
| 293 | + ''; |
| 294 | + |
| 295 | + passthru = { |
| 296 | + inherit cudaSupport cudaPackages; # for the python module |
| 297 | + inherit rocmSupport rocmPackages rcclSupport; # for the python module |
| 298 | + inherit protobuf; |
| 299 | + tests = lib.optionalAttrs pythonSupport { |
| 300 | + python = python3Packages.onnxruntime; |
| 301 | + }; |
| 302 | + }; |
| 303 | + |
| 304 | + meta = { |
| 305 | + description = "Cross-platform, high performance scoring engine for ML models"; |
| 306 | + longDescription = '' |
| 307 | + ONNX Runtime is a performance-focused complete scoring engine |
| 308 | + for Open Neural Network Exchange (ONNX) models, with an open |
| 309 | + extensible architecture to continually address the latest developments |
| 310 | + in AI and Deep Learning. ONNX Runtime stays up to date with the ONNX |
| 311 | + standard with complete implementation of all ONNX operators, and |
| 312 | + supports all ONNX releases (1.2+) with both future and backwards |
| 313 | + compatibility. |
| 314 | + ''; |
| 315 | + homepage = "https://github.com/microsoft/onnxruntime"; |
| 316 | + changelog = "https://github.com/microsoft/onnxruntime/releases/tag/v${version}"; |
| 317 | + # https://github.com/microsoft/onnxruntime/blob/master/BUILD.md#architectures |
| 318 | + platforms = lib.platforms.unix; |
| 319 | + license = lib.licenses.mit; |
| 320 | + maintainers = with lib.maintainers; [ |
| 321 | + puffnfresh |
| 322 | + ck3d |
| 323 | + ]; |
| 324 | + }; |
| 325 | +} |
0 commit comments