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
, abseil-cpp
2022-08-21 13:32:41 +00:00
, cmake
2024-02-29 20:09:43 +00:00
, eigen
, 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-02-29 20:09:43 +00:00
version = " 1 . 1 6 . 3 " ;
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 " ;
rev = " v 2 . 4 . 1 " ;
sha256 = " s h a 2 5 6 - B Y L 7 w x s Y R I 4 5 l 8 C 3 V w x Y I I o c n 5 T z J n B t U 0 U Z 9 p H w w Z w = " ;
} ;
mp11 = fetchFromGitHub {
owner = " b o o s t o r g " ;
repo = " m p 1 1 " ;
rev = " b o o s t - 1 . 7 9 . 0 " ;
2024-02-29 20:09:43 +00:00
hash = " s h a 2 5 6 - Z x g P D L v p I S r j p E H K p L G B o w R K G f S w T f 6 T B f J D 1 8 y w + L M = " ;
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
} ;
pytorch_cpuinfo = fetchFromGitHub {
owner = " p y t o r c h " ;
repo = " c p u i n f o " ;
# There are no tags in the repository
rev = " 5 9 1 6 2 7 3 f 7 9 a 2 1 5 5 1 8 9 0 f d 3 d 5 6 f c 5 3 7 5 a 7 8 d 1 5 9 8 d " ;
2024-02-29 20:09:43 +00:00
hash = " s h a 2 5 6 - n X B n l o V T u B + A V X 5 9 V D U / W c + D s x 9 4 o 9 2 Y Q u H p 3 j o w x 2 A = " ;
2023-08-04 22:07:22 +00:00
} ;
flatbuffers = fetchFromGitHub {
owner = " g o o g l e " ;
repo = " f l a t b u f f e r s " ;
rev = " v 1 . 1 2 . 0 " ;
2024-02-29 20:09:43 +00:00
hash = " s h a 2 5 6 - L 1 B 5 Y / c 8 9 7 J g 9 f G w T 2 J 3 + v a X s Z + l f X n s k p 8 G t o 1 p / T g = " ;
} ;
onnx = fetchFromGitHub {
owner = " o n n x " ;
repo = " o n n x " ;
rev = " r e f s / t a g s / v 1 . 1 4 . 1 " ;
hash = " s h a 2 5 6 - Z V S d k 6 L e A i Z p Q r r z L x p h M b c 1 b 3 r N U M p c x c X P P 8 s / 5 t 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 " ;
rev = " v 3 . 0 . 0 " ;
sha256 = " s h a 2 5 6 - Y P D 5 S y 6 S v B y j I c G t g e G H 8 0 T E K g 2 B t q J W S g 4 6 R v n J C h Y = " ;
} ;
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 } " ;
hash = " s h a 2 5 6 - b T W 9 P c 3 r v H + c 8 V I l D D E t A X y A 3 s a j V y Y 5 A q r 6 + S x a M F 4 = " ;
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
] ++ 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-02-29 20:09:43 +00:00
eigen
2022-08-21 13:32:41 +00:00
libpng
zlib
nlohmann_json
2023-08-04 22:07:22 +00:00
microsoft-gsl
2024-02-29 20:09:43 +00:00
] ++ lib . optionals pythonSupport ( with python3Packages ; [
numpy
pybind11
packaging
] ) ++ lib . optionals effectiveStdenv . 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
] ) ;
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 } "
" - 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 } "
" - 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-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 _ P Y T O R C H _ C P U I N F O = ${ pytorch_cpuinfo } "
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 "
" - 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-02-29 20:09:43 +00:00
doCheck = ! cudaSupport ;
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
} ;
}