2024-02-29 20:09:43 +00:00
{ config
, stdenv
2022-08-21 13:32:41 +00:00
, lib
, fetchFromGitHub
2023-08-04 22:07:22 +00:00
, Foundation
2024-09-19 14:19:46 +00:00
, abseil-cpp_202401
2022-08-21 13:32:41 +00:00
, cmake
2024-07-27 06:49:29 +00:00
, cpuinfo
2024-02-29 20:09:43 +00:00
, eigen
2024-07-31 10:19:44 +00:00
, flatbuffers_23
2024-07-27 06:49:29 +00:00
, gbenchmark
, glibcLocales
2024-02-29 20:09:43 +00:00
, gtest
2022-08-21 13:32:41 +00:00
, libpng
, nlohmann_json
2022-09-30 11:47:45 +00:00
, nsync
2023-08-04 22:07:22 +00:00
, pkg-config
, python3Packages
, re2
, zlib
, microsoft-gsl
2024-05-15 15:35:15 +00:00
, libiconv
2023-11-16 04:20:00 +00:00
, protobuf_21
2023-08-04 22:07:22 +00:00
, pythonSupport ? true
2024-02-29 20:09:43 +00:00
, cudaSupport ? config . cudaSupport
, cudaPackages ? { }
} @ inputs :
2022-08-21 13:32:41 +00:00
2022-10-06 18:32:54 +00:00
2023-08-04 22:07:22 +00:00
let
2024-07-27 06:49:29 +00:00
version = " 1 . 1 8 . 1 " ;
2024-02-29 20:09:43 +00:00
2024-09-19 14:19:46 +00:00
abseil-cpp = abseil-cpp_202401 ;
2024-02-29 20:09:43 +00:00
stdenv = throw " U s e e f f e c t i v e S t d e n v i n s t e a d " ;
effectiveStdenv = if cudaSupport then cudaPackages . backendStdenv else inputs . stdenv ;
2024-07-01 15:47:52 +00:00
cudaArchitecturesString = cudaPackages . flags . cmakeCudaArchitecturesString ;
2024-02-29 20:09:43 +00:00
2023-08-04 22:07:22 +00:00
howard-hinnant-date = fetchFromGitHub {
owner = " H o w a r d H i n n a n t " ;
repo = " d a t e " ;
2024-07-27 06:49:29 +00:00
rev = " v 3 . 0 . 1 " ;
sha256 = " s h a 2 5 6 - Z S j e J K A c T 7 m P y m / 4 V i D v I R 9 n F M Q E B C S U t P E u M O 2 7 Z + I = " ;
2023-08-04 22:07:22 +00:00
} ;
mp11 = fetchFromGitHub {
owner = " b o o s t o r g " ;
repo = " m p 1 1 " ;
2024-07-27 06:49:29 +00:00
rev = " b o o s t - 1 . 8 2 . 0 " ;
hash = " s h a 2 5 6 - c L P v j k f 2 A u + B 1 9 P J N r U k T W / V P x y b i 1 M p P x n I l 4 o o 4 / o = " ;
2023-08-04 22:07:22 +00:00
} ;
safeint = fetchFromGitHub {
owner = " d c l e b l a n c " ;
repo = " s a f e i n t " ;
rev = " f f 1 5 c 6 a d a 1 5 0 a 5 0 1 8 c 5 e f 2 1 7 2 4 0 1 c b 4 5 2 9 e a c 9 c 0 " ;
2024-02-29 20:09:43 +00:00
hash = " s h a 2 5 6 - P K 1 c e 4 C 0 u C R 4 T z L F g + e l Z d S k 5 D d P C R h h w T 3 L v E w W n P U = " ;
2023-08-04 22:07:22 +00:00
} ;
2024-07-27 06:49:29 +00:00
pytorch_clog = effectiveStdenv . mkDerivation {
pname = " c l o g " ;
version = " 3 c 8 b 1 5 3 " ;
src = " ${ cpuinfo . src } / d e p s / c l o g " ;
nativeBuildInputs = [ cmake gbenchmark gtest ] ;
cmakeFlags = [
" - D U S E _ S Y S T E M _ G O O G L E B E N C H M A R K = O N "
" - D U S E _ S Y S T E M _ G O O G L E T E S T = O N "
" - D U S E _ S Y S T E M _ L I B S = O N "
# 'clog' tests set 'CXX_STANDARD 11'; this conflicts with our 'gtest'.
" - D C L O G _ B U I L D _ T E S T S = O F F "
] ;
2024-02-29 20:09:43 +00:00
} ;
onnx = fetchFromGitHub {
owner = " o n n x " ;
repo = " o n n x " ;
2024-07-27 06:49:29 +00:00
rev = " r e f s / t a g s / v 1 . 1 6 . 1 " ;
hash = " s h a 2 5 6 - I 1 w w f n 9 1 h d H 3 j O R I K n y 0 X c 7 3 q W 2 P 0 4 M j k V C g c a N n Q U E = " ;
2023-08-04 22:07:22 +00:00
} ;
2022-10-06 18:32:54 +00:00
2024-02-29 20:09:43 +00:00
cutlass = fetchFromGitHub {
owner = " N V I D I A " ;
repo = " c u t l a s s " ;
2024-07-27 06:49:29 +00:00
rev = " v 3 . 1 . 0 " ;
hash = " s h a 2 5 6 - m p a i C x i Y R 1 W a S S k c E P T z v c R E e n J W k l D + H R d T T 5 / p D 5 4 = " ;
} ;
2023-08-04 22:07:22 +00:00
in
2024-02-29 20:09:43 +00:00
effectiveStdenv . mkDerivation rec {
2022-08-21 13:32:41 +00:00
pname = " o n n x r u n t i m e " ;
2024-02-29 20:09:43 +00:00
inherit version ;
2022-08-21 13:32:41 +00:00
src = fetchFromGitHub {
owner = " m i c r o s o f t " ;
repo = " o n n x r u n t i m e " ;
2024-02-29 20:09:43 +00:00
rev = " r e f s / t a g s / v ${ version } " ;
2024-07-27 06:49:29 +00:00
hash = " s h a 2 5 6 - + z W t b L K e k G h w d B U 3 b m 1 u 2 F 7 r Y e j Q 6 2 e p E + H c H j 0 5 / 8 A = " ;
2022-08-21 13:32:41 +00:00
fetchSubmodules = true ;
} ;
2024-02-29 20:09:43 +00:00
patches = [
# If you stumble on these patches trying to update onnxruntime, check
# `git blame` and ping the introducers.
# Context: we want the upstream to
# - always try find_package first (FIND_PACKAGE_ARGS),
# - use MakeAvailable instead of the low-level Populate,
# - use Eigen3::Eigen as the target name (as declared by libeigen/eigen).
./0001-eigen-allow-dependency-injection.patch
2024-07-27 06:49:29 +00:00
# Incorporate a patch that has landed upstream which exposes new
# 'abseil-cpp' libraries & modifies the 're2' CMakeLists to fix a
# configuration error that around missing 'gmock' exports.
#
# TODO: Check if it can be dropped after 1.19.0
# https://github.com/microsoft/onnxruntime/commit/b522df0ae477e59f60acbe6c92c8a64eda96cace
./update-re2.patch
2024-02-29 20:09:43 +00:00
] ++ lib . optionals cudaSupport [
# We apply the referenced 1064.patch ourselves to our nix dependency.
# 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
./nvcc-gsl.patch
] ;
2022-08-21 13:32:41 +00:00
nativeBuildInputs = [
cmake
pkg-config
2022-09-30 11:47:45 +00:00
python3Packages . python
2023-11-16 04:20:00 +00:00
protobuf_21
2022-09-30 11:47:45 +00:00
] ++ lib . optionals pythonSupport ( with python3Packages ; [
pip
2024-02-29 20:09:43 +00:00
python
2022-10-06 18:32:54 +00:00
pythonOutputDistHook
2024-02-29 20:09:43 +00:00
setuptools
wheel
] ) ++ lib . optionals cudaSupport [
cudaPackages . cuda_nvcc
] ;
2022-08-21 13:32:41 +00:00
buildInputs = [
2024-07-27 06:49:29 +00:00
cpuinfo
2024-02-29 20:09:43 +00:00
eigen
2024-07-27 06:49:29 +00:00
glibcLocales
2022-08-21 13:32:41 +00:00
libpng
nlohmann_json
2023-08-04 22:07:22 +00:00
microsoft-gsl
2024-07-27 06:49:29 +00:00
pytorch_clog
zlib
2024-02-29 20:09:43 +00:00
] ++ lib . optionals pythonSupport ( with python3Packages ; [
numpy
pybind11
packaging
2024-09-26 11:04:55 +00:00
] ) ++ lib . optionals effectiveStdenv . hostPlatform . isDarwin [
2023-08-04 22:07:22 +00:00
Foundation
2024-05-15 15:35:15 +00:00
libiconv
2024-02-29 20:09:43 +00:00
] ++ lib . optionals cudaSupport ( with cudaPackages ; [
cuda_cccl # cub/cub.cuh
libcublas # cublas_v2.h
libcurand # curand.h
libcusparse # cusparse.h
libcufft # cufft.h
cudnn # cudnn.h
cuda_cudart
2024-07-27 06:49:29 +00:00
nccl
2024-02-29 20:09:43 +00:00
] ) ;
2022-08-21 13:32:41 +00:00
2024-04-21 15:54:59 +00:00
nativeCheckInputs = [
gtest
] ++ lib . optionals pythonSupport ( with python3Packages ; [
2023-08-04 22:07:22 +00:00
pytest
sympy
onnx
] ) ;
2022-08-21 13:32:41 +00:00
# TODO: build server, and move .so's to lib output
2022-10-06 18:32:54 +00:00
# Python's wheel is stored in a separate dist output
outputs = [ " o u t " " d e v " ] ++ lib . optionals pythonSupport [ " d i s t " ] ;
2022-08-21 13:32:41 +00:00
enableParallelBuilding = true ;
cmakeDir = " . . / c m a k e " ;
cmakeFlags = [
2023-10-09 19:29:22 +00:00
" - D A B S L _ E N A B L E _ I N S T A L L = O N "
2023-08-04 22:07:22 +00:00
" - D F E T C H C O N T E N T _ F U L L Y _ D I S C O N N E C T E D = O N "
" - D F E T C H C O N T E N T _ Q U I E T = O F F "
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ A B S E I L _ C P P = ${ abseil-cpp . src } "
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ D A T E = ${ howard-hinnant-date } "
2024-07-31 10:19:44 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ F L A T B U F F E R S = ${ flatbuffers_23 . src } "
2024-07-27 06:49:29 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ G O O G L E T E S T = ${ gtest . src } "
2023-08-04 22:07:22 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ G O O G L E _ N S Y N C = ${ nsync . src } "
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ M P 1 1 = ${ mp11 } "
2024-02-29 20:09:43 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ O N N X = ${ onnx } "
2023-10-09 19:29:22 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ R E 2 = ${ re2 . src } "
2023-08-04 22:07:22 +00:00
" - D F E T C H C O N T E N T _ S O U R C E _ D I R _ S A F E I N T = ${ safeint } "
" - D F E T C H C O N T E N T _ T R Y _ F I N D _ P A C K A G E _ M O D E = A L W A Y S "
2022-08-21 13:32:41 +00:00
" - D o n n x r u n t i m e _ B U I L D _ S H A R E D _ L I B = O N "
2024-04-21 15:54:59 +00:00
( lib . cmakeBool " o n n x r u n t i m e _ B U I L D _ U N I T _ T E S T S " doCheck )
2023-08-04 22:07:22 +00:00
" - D o n n x r u n t i m e _ E N A B L E _ L T O = O N "
" - D o n n x r u n t i m e _ U S E _ F U L L _ P R O T O B U F = O F F "
2024-02-29 20:09:43 +00:00
( lib . cmakeBool " o n n x r u n t i m e _ U S E _ C U D A " cudaSupport )
( lib . cmakeBool " o n n x r u n t i m e _ U S E _ N C C L " cudaSupport )
2022-09-30 11:47:45 +00:00
] ++ lib . optionals pythonSupport [
" - D o n n x r u n t i m e _ E N A B L E _ P Y T H O N = O N "
2024-02-29 20:09:43 +00:00
] ++ lib . optionals cudaSupport [
2024-04-21 15:54:59 +00:00
( lib . cmakeFeature " F E T C H C O N T E N T _ S O U R C E _ D I R _ C U T L A S S " " ${ cutlass } " )
( lib . cmakeFeature " o n n x r u n t i m e _ C U D N N _ H O M E " " ${ cudaPackages . cudnn } " )
2024-02-29 20:09:43 +00:00
( lib . cmakeFeature " C M A K E _ C U D A _ A R C H I T E C T U R E S " cudaArchitecturesString )
2024-04-21 15:54:59 +00:00
( lib . cmakeFeature " o n n x r u n t i m e _ N V C C _ T H R E A D S " " 1 " )
2022-08-21 13:32:41 +00:00
] ;
2024-02-29 20:09:43 +00:00
env = lib . optionalAttrs effectiveStdenv . cc . isClang {
2023-11-16 04:20:00 +00:00
NIX_CFLAGS_COMPILE = toString [
" - W n o - e r r o r = d e p r e c a t e d - d e c l a r a t i o n s "
2024-07-27 06:49:29 +00:00
" - W n o - e r r o r = d e p r e c a t e d - p r a g m a "
2023-11-16 04:20:00 +00:00
" - W n o - e r r o r = u n u s e d - b u t - s e t - v a r i a b l e "
] ;
} ;
2024-07-27 06:49:29 +00:00
# aarch64-linux fails cpuinfo test, because /sys/devices/system/cpu/ does not exist in the sandbox
doCheck = ! ( cudaSupport || effectiveStdenv . buildPlatform . system == " a a r c h 6 4 - l i n u x " ) ;
2024-02-29 20:09:43 +00:00
requiredSystemFeatures = lib . optionals cudaSupport [ " b i g - p a r a l l e l " ] ;
2022-08-21 13:32:41 +00:00
postPatch = ''
2022-09-30 11:47:45 +00:00
substituteInPlace cmake/libonnxruntime.pc.cmake.in \
2024-02-29 20:09:43 +00:00
- - replace-fail ' $ ' { prefix } / @ CMAKE_INSTALL_ @ CMAKE_INSTALL_
'' + l i b . o p t i o n a l S t r i n g ( e f f e c t i v e S t d e n v . h o s t P l a t f o r m . s y s t e m = = " a a r c h 6 4 - l i n u x " ) ''
2023-08-04 22:07:22 +00:00
# https://github.com/NixOS/nixpkgs/pull/226734#issuecomment-1663028691
rm - v onnxruntime/test/optimizer/nhwc_transformer_test.cc
2022-09-30 11:47:45 +00:00
'' ;
postBuild = lib . optionalString pythonSupport ''
2024-02-29 20:09:43 +00:00
$ { python3Packages . python . interpreter } ../setup.py bdist_wheel
2022-08-21 13:32:41 +00:00
'' ;
postInstall = ''
# perform parts of `tools/ci_build/github/linux/copy_strip_binary.sh`
install - m644 - Dt $ out/include \
../include/onnxruntime/core/framework/provider_options.h \
../include/onnxruntime/core/providers/cpu/cpu_provider_factory.h \
../include/onnxruntime/core/session/onnxruntime_ * . h
'' ;
2022-10-06 18:32:54 +00:00
passthru = {
2024-04-21 15:54:59 +00:00
inherit cudaSupport cudaPackages ; # for the python module
2023-11-16 04:20:00 +00:00
protobuf = protobuf_21 ;
2022-10-06 18:32:54 +00:00
tests = lib . optionalAttrs pythonSupport {
python = python3Packages . onnxruntime ;
} ;
} ;
2022-08-21 13:32:41 +00:00
meta = with lib ; {
description = " C r o s s - p l a t f o r m , h i g h p e r f o r m a n c e s c o r i n g e n g i n e f o r M L m o d e l s " ;
longDescription = ''
ONNX Runtime is a performance-focused complete scoring engine
for Open Neural Network Exchange ( ONNX ) models , with an open
extensible architecture to continually address the latest developments
in AI and Deep Learning . ONNX Runtime stays up to date with the ONNX
standard with complete implementation of all ONNX operators , and
supports all ONNX releases ( 1 .2 + ) with both future and backwards
compatibility .
'' ;
homepage = " h t t p s : / / g i t h u b . c o m / m i c r o s o f t / o n n x r u n t i m e " ;
changelog = " h t t p s : / / g i t h u b . c o m / m i c r o s o f t / o n n x r u n t i m e / r e l e a s e s / t a g / v ${ version } " ;
# https://github.com/microsoft/onnxruntime/blob/master/BUILD.md#architectures
platforms = platforms . unix ;
license = licenses . mit ;
2024-07-01 15:47:52 +00:00
maintainers = with maintainers ; [ puffnfresh ck3d cbourjau ] ;
2022-08-21 13:32:41 +00:00
} ;
}