1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 cmake,
6 fetchFromGitHub,
7 fetchpatch,
8 gtest,
9 nbval,
10 numpy,
11 parameterized,
12 protobuf_21,
13 pybind11,
14 pytestCheckHook,
15 pythonOlder,
16 tabulate,
17 typing-extensions,
18 abseil-cpp,
19 google-re2,
20 pillow,
21 protobuf,
22}:
23
24let
25 gtestStatic = gtest.override { static = true; };
26in
27buildPythonPackage rec {
28 pname = "onnx";
29 version = "1.15.0";
30 format = "setuptools";
31
32 disabled = pythonOlder "3.8";
33
34 src = fetchFromGitHub {
35 owner = pname;
36 repo = pname;
37 rev = "refs/tags/v${version}";
38 hash = "sha256-Jzga1IiUO5LN5imSUmnbsjYtapRatTihx38EOUjm9Os=";
39 };
40
41 patches = [
42 ./1.15.0-CVE-2024-27318.patch
43 (fetchpatch {
44 name = "CVE-2024-27319.patch";
45 url = "https://github.com/onnx/onnx/commit/08a399ba75a805b7813ab8936b91d0e274b08287.patch";
46 hash = "sha256-9X92N9i/hpQjDGe4I/C+FwUcTUTtP2Nf7+pcTA2sXoA=";
47 })
48 ];
49
50 nativeBuildInputs = [
51 cmake
52 pybind11
53 ];
54
55 buildInputs = [
56 abseil-cpp
57 protobuf
58 google-re2
59 pillow
60 ];
61
62 propagatedBuildInputs = [
63 protobuf_21
64 protobuf
65 numpy
66 typing-extensions
67 ];
68
69 nativeCheckInputs = [
70 nbval
71 parameterized
72 pytestCheckHook
73 tabulate
74 ];
75
76 postPatch = ''
77 chmod +x tools/protoc-gen-mypy.sh.in
78 patchShebangs tools/protoc-gen-mypy.sh.in
79
80 substituteInPlace setup.py \
81 --replace 'setup_requires.append("pytest-runner")' ""
82
83 # prevent from fetching & building own gtest
84 substituteInPlace CMakeLists.txt \
85 --replace 'include(googletest)' ""
86 substituteInPlace cmake/unittest.cmake \
87 --replace 'googletest)' ')'
88 '';
89
90 preConfigure = ''
91 # Set CMAKE_INSTALL_LIBDIR to lib explicitly, because otherwise it gets set
92 # to lib64 and cmake incorrectly looks for the protobuf library in lib64
93 export CMAKE_ARGS="-DCMAKE_INSTALL_LIBDIR=lib -DONNX_USE_PROTOBUF_SHARED_LIBS=ON"
94 export CMAKE_ARGS+=" -Dgoogletest_STATIC_LIBRARIES=${gtestStatic}/lib/libgtest.a -Dgoogletest_INCLUDE_DIRS=${lib.getDev gtestStatic}/include"
95 export ONNX_BUILD_TESTS=1
96 '';
97
98 preBuild = ''
99 export MAX_JOBS=$NIX_BUILD_CORES
100 '';
101
102 # The executables are just utility scripts that aren't too important
103 postInstall = ''
104 rm -r $out/bin
105 '';
106
107 # The setup.py does all the configuration
108 dontUseCmakeConfigure = true;
109
110 preCheck = ''
111 export HOME=$(mktemp -d)
112
113 # detecting source dir as a python package confuses pytest
114 mv onnx/__init__.py onnx/__init__.py.hidden
115 '';
116
117 pytestFlagsArray = [
118 "onnx/test"
119 "onnx/examples"
120 ];
121
122 disabledTests =
123 [
124 # attempts to fetch data from web
125 "test_bvlc_alexnet_cpu"
126 "test_densenet121_cpu"
127 "test_inception_v1_cpu"
128 "test_inception_v2_cpu"
129 "test_resnet50_cpu"
130 "test_shufflenet_cpu"
131 "test_squeezenet_cpu"
132 "test_vgg19_cpu"
133 "test_zfnet512_cpu"
134 ]
135 ++ lib.optionals stdenv.isAarch64 [
136 # AssertionError: Output 0 of test 0 in folder
137 "test__pytorch_converted_Conv2d_depthwise_padded"
138 "test__pytorch_converted_Conv2d_dilated"
139 "test_dft"
140 "test_dft_axis"
141 # AssertionError: Mismatch in test 'test_Conv2d_depthwise_padded'
142 "test_xor_bcast4v4d"
143 # AssertionError: assert 1 == 0
144 "test_ops_tested"
145 ];
146
147 disabledTestPaths = [
148 # Unexpected output fields from running code: {'stderr'}
149 "onnx/examples/np_array_tensorproto.ipynb"
150 ];
151
152 __darwinAllowLocalNetworking = true;
153
154 postCheck = ''
155 # run "cpp" tests
156 .setuptools-cmake-build/onnx_gtests
157 '';
158
159 pythonImportsCheck = [ "onnx" ];
160
161 meta = with lib; {
162 description = "Open Neural Network Exchange";
163 homepage = "https://onnx.ai";
164 license = licenses.asl20;
165 maintainers = with maintainers; [ acairncross ];
166 };
167}