24 , pythonSupport ? true
25 , cudaSupport ? config.cudaSupport
26 , ncclSupport ? config.cudaSupport
34 abseil-cpp = abseil-cpp_202401;
36 stdenv = throw "Use effectiveStdenv instead";
37 effectiveStdenv = if cudaSupport then cudaPackages.backendStdenv else inputs.stdenv;
39 cudaArchitecturesString = cudaPackages.flags.cmakeCudaArchitecturesString;
41 howard-hinnant-date = fetchFromGitHub {
42 owner = "HowardHinnant";
45 sha256 = "sha256-ZSjeJKAcT7mPym/4ViDvIR9nFMQEBCSUtPEuMO27Z+I=";
48 mp11 = fetchFromGitHub {
52 hash = "sha256-cLPvjkf2Au+B19PJNrUkTW/VPxybi1MpPxnIl4oo4/o=";
55 safeint = fetchFromGitHub {
58 rev = "ff15c6ada150a5018c5ef2172401cb4529eac9c0";
59 hash = "sha256-PK1ce4C0uCR4TzLFg+elZdSk5DdPCRhhwT3LvEwWnPU=";
62 pytorch_clog = effectiveStdenv.mkDerivation {
65 src = "${cpuinfo.src}/deps/clog";
67 nativeBuildInputs = [ cmake gbenchmark gtest ];
69 "-DUSE_SYSTEM_GOOGLEBENCHMARK=ON"
70 "-DUSE_SYSTEM_GOOGLETEST=ON"
71 "-DUSE_SYSTEM_LIBS=ON"
72 # 'clog' tests set 'CXX_STANDARD 11'; this conflicts with our 'gtest'.
73 "-DCLOG_BUILD_TESTS=OFF"
77 onnx = fetchFromGitHub {
80 rev = "refs/tags/v1.16.1";
81 hash = "sha256-I1wwfn91hdH3jORIKny0Xc73qW2P04MjkVCgcaNnQUE=";
84 cutlass = fetchFromGitHub {
88 hash = "sha256-mpaiCxiYR1WaSSkcEPTzvcREenJWklD+HRdTT5/pD54=";
91 effectiveStdenv.mkDerivation rec {
92 pname = "onnxruntime";
95 src = fetchFromGitHub {
98 rev = "refs/tags/v${version}";
99 hash = "sha256-+zWtbLKekGhwdBU3bm1u2F7rYejQ62epE+HcHj05/8A=";
100 fetchSubmodules = true;
104 # If you stumble on these patches trying to update onnxruntime, check
105 # `git blame` and ping the introducers.
107 # Context: we want the upstream to
108 # - always try find_package first (FIND_PACKAGE_ARGS),
109 # - use MakeAvailable instead of the low-level Populate,
110 # - use Eigen3::Eigen as the target name (as declared by libeigen/eigen).
111 ./0001-eigen-allow-dependency-injection.patch
112 # Incorporate a patch that has landed upstream which exposes new
113 # 'abseil-cpp' libraries & modifies the 're2' CMakeLists to fix a
114 # configuration error that around missing 'gmock' exports.
116 # TODO: Check if it can be dropped after 1.19.0
117 # https://github.com/microsoft/onnxruntime/commit/b522df0ae477e59f60acbe6c92c8a64eda96cace
119 ] ++ lib.optionals cudaSupport [
120 # We apply the referenced 1064.patch ourselves to our nix dependency.
121 # 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
125 nativeBuildInputs = [
128 python3Packages.python
130 ] ++ lib.optionals pythonSupport (with python3Packages; [
136 ]) ++ lib.optionals cudaSupport [
137 cudaPackages.cuda_nvcc
149 ] ++ lib.optionals pythonSupport (with python3Packages; [
153 ]) ++ lib.optionals effectiveStdenv.hostPlatform.isDarwin [
156 ] ++ lib.optionals cudaSupport (with cudaPackages; [
157 cuda_cccl # cub/cub.cuh
158 libcublas # cublas_v2.h
160 libcusparse # cusparse.h
164 ] ++ lib.optionals (cudaSupport && ncclSupport) (with cudaPackages; [
168 nativeCheckInputs = [
170 ] ++ lib.optionals pythonSupport (with python3Packages; [
176 # TODO: build server, and move .so's to lib output
177 # Python's wheel is stored in a separate dist output
178 outputs = [ "out" "dev" ] ++ lib.optionals pythonSupport [ "dist" ];
180 enableParallelBuilding = true;
182 cmakeDir = "../cmake";
185 "-DABSL_ENABLE_INSTALL=ON"
186 "-DFETCHCONTENT_FULLY_DISCONNECTED=ON"
187 "-DFETCHCONTENT_QUIET=OFF"
188 "-DFETCHCONTENT_SOURCE_DIR_ABSEIL_CPP=${abseil-cpp.src}"
189 "-DFETCHCONTENT_SOURCE_DIR_DATE=${howard-hinnant-date}"
190 "-DFETCHCONTENT_SOURCE_DIR_FLATBUFFERS=${flatbuffers_23.src}"
191 "-DFETCHCONTENT_SOURCE_DIR_GOOGLETEST=${gtest.src}"
192 "-DFETCHCONTENT_SOURCE_DIR_GOOGLE_NSYNC=${nsync.src}"
193 "-DFETCHCONTENT_SOURCE_DIR_MP11=${mp11}"
194 "-DFETCHCONTENT_SOURCE_DIR_ONNX=${onnx}"
195 "-DFETCHCONTENT_SOURCE_DIR_RE2=${re2.src}"
196 "-DFETCHCONTENT_SOURCE_DIR_SAFEINT=${safeint}"
197 "-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS"
198 "-Donnxruntime_BUILD_SHARED_LIB=ON"
199 (lib.cmakeBool "onnxruntime_BUILD_UNIT_TESTS" doCheck)
200 "-Donnxruntime_ENABLE_LTO=ON"
201 "-Donnxruntime_USE_FULL_PROTOBUF=OFF"
202 (lib.cmakeBool "onnxruntime_USE_CUDA" cudaSupport)
203 (lib.cmakeBool "onnxruntime_USE_NCCL" (cudaSupport && ncclSupport))
204 ] ++ lib.optionals pythonSupport [
205 "-Donnxruntime_ENABLE_PYTHON=ON"
206 ] ++ lib.optionals cudaSupport [
207 (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_CUTLASS" "${cutlass}")
208 (lib.cmakeFeature "onnxruntime_CUDNN_HOME" "${cudaPackages.cudnn}")
209 (lib.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaArchitecturesString)
210 (lib.cmakeFeature "onnxruntime_NVCC_THREADS" "1")
213 env = lib.optionalAttrs effectiveStdenv.cc.isClang {
214 NIX_CFLAGS_COMPILE = toString [
215 "-Wno-error=deprecated-declarations"
216 "-Wno-error=deprecated-pragma"
217 "-Wno-error=unused-but-set-variable"
221 # aarch64-linux fails cpuinfo test, because /sys/devices/system/cpu/ does not exist in the sandbox
222 doCheck = !(cudaSupport || effectiveStdenv.buildPlatform.system == "aarch64-linux");
224 requiredSystemFeatures = lib.optionals cudaSupport [ "big-parallel" ];
227 substituteInPlace cmake/libonnxruntime.pc.cmake.in \
228 --replace-fail '$'{prefix}/@CMAKE_INSTALL_ @CMAKE_INSTALL_
229 '' + lib.optionalString (effectiveStdenv.hostPlatform.system == "aarch64-linux") ''
230 # https://github.com/NixOS/nixpkgs/pull/226734#issuecomment-1663028691
231 rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
234 postBuild = lib.optionalString pythonSupport ''
235 ${python3Packages.python.interpreter} ../setup.py bdist_wheel
239 # perform parts of `tools/ci_build/github/linux/copy_strip_binary.sh`
240 install -m644 -Dt $out/include \
241 ../include/onnxruntime/core/framework/provider_options.h \
242 ../include/onnxruntime/core/providers/cpu/cpu_provider_factory.h \
243 ../include/onnxruntime/core/session/onnxruntime_*.h
247 inherit cudaSupport cudaPackages; # for the python module
248 protobuf = protobuf_21;
249 tests = lib.optionalAttrs pythonSupport {
250 python = python3Packages.onnxruntime;
255 description = "Cross-platform, high performance scoring engine for ML models";
257 ONNX Runtime is a performance-focused complete scoring engine
258 for Open Neural Network Exchange (ONNX) models, with an open
259 extensible architecture to continually address the latest developments
260 in AI and Deep Learning. ONNX Runtime stays up to date with the ONNX
261 standard with complete implementation of all ONNX operators, and
262 supports all ONNX releases (1.2+) with both future and backwards
265 homepage = "https://github.com/microsoft/onnxruntime";
266 changelog = "https://github.com/microsoft/onnxruntime/releases/tag/v${version}";
267 # https://github.com/microsoft/onnxruntime/blob/master/BUILD.md#architectures
268 platforms = platforms.unix;
269 license = licenses.mit;
270 maintainers = with maintainers; [ puffnfresh ck3d cbourjau ];