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