File onnxruntime.spec of Package onnxruntime

#
# spec file for package onnxruntime
#
# Copyright (c) 2024 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

%define cpuinfo_ver ca67895
%define flatbuffers_ver 23.5.26
%define mp11_ver 1.86.0
%define nsync_ver 1.26.0
%define onnx_ver 1.16.1
%define protobuf_ver 21.12
%define safeint_ver 3.0.28
%define abseil_ver f46495e
%define eigen_ver e7248b26
%define re2_ver 20240702

%define so_uffix 1_19_2

%define _enable_tests 0

%{?sle15_python_module_pythons}
Name:           onnxruntime
Version:        1.19.2
Release:        0
Summary:        Cross-platform, high performance ML inferencing and training accelerator
License:        MIT AND Apache-2.0 AND MPL-2.0 AND BSD-3-Clause
URL:            https://github.com/microsoft/onnxruntime/
Source0:        %{name}-%{version}.tar.xz
Source1:        cpuinfo-%{cpuinfo_ver}.tar.xz
Source2:        flatbuffers-%{flatbuffers_ver}.tar.xz
Source3:        mp11-boost-%{mp11_ver}.tar.xz
Source4:        nsync-%{nsync_ver}.tar.xz
Source5:        onnx-%{onnx_ver}.tar.xz
Source6:        protobuf-%{protobuf_ver}.tar.xz
Source7:        safeint-%{safeint_ver}.tar.xz
Source8:        abseil-cpp-%{abseil_ver}.tar.xz
Source9:        eigen-%{eigen_ver}.tar.xz
# SLE 15 SP6 cmake build fails to successfully pull in re2-devel config
%if 0%{?suse_version} < 1600
Source10:       re2-%{re2_ver}.tar.xz
%endif
# ensure pybind11 integration doesn't allow code execution from stack
Patch0:         onnxruntime-pybind11_state-noexecstack.patch
# fully version the onnxruntime library's soname
Patch1:         onnxruntime-soname.patch
# add versioned soname support to providers library
Patch2:         versioned-onnxruntime_providers_shared.patch
BuildRequires:  %{python_module devel}
BuildRequires:  %{python_module numpy-devel}
BuildRequires:  %{python_module numpy}
BuildRequires:  %{python_module packaging}
BuildRequires:  %{python_module pip}
BuildRequires:  %{python_module pybind11-devel}
BuildRequires:  %{python_module pytest}
BuildRequires:  %{python_module setuptools}
BuildRequires:  %{python_module sympy}
BuildRequires:  %{python_module wheel}
BuildRequires:  hhdate-devel
BuildRequires:  cmake
BuildRequires:  fdupes
# gcc 13 required for SLE 15 SP6 as default gcc version (7) < 9
# gcc 13 required for Tumbleweed as default gcc version (14) is too high
BuildRequires:  gcc13-c++
BuildRequires:  libtool
BuildRequires:  ms-gsl-devel
BuildRequires:  nlohmann_json-devel
BuildRequires:  pkgconfig
BuildRequires:  python-rpm-macros
# Tumbleweed cmake builds successfully with re2-devel config
%if 0%{?suse_version} > 1600
BuildRequires:  re2-devel
%endif
BuildRequires:  pkgconfig(libpng)
BuildRequires:  pkgconfig(zlib)
Requires(post):   update-alternatives
Requires(postun):  update-alternatives
%python_subpackages

%description
ONNX Runtime is a performance-focused scoring engine for Open Neural Network
Exchange (ONNX) models.  For more information on ONNX Runtime, please see
<https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.

%package  -n lib%{name}%{so_uffix}
Summary:       Cross-platform, high performance ML inferencing and training accelerator

%description -n lib%{name}%{so_uffix}
ONNX Runtime is a performance-focused scoring engine for Open Neural Network
Exchange (ONNX) models.  For more information on ONNX Runtime, please see
<https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.

%package -n %{name}-devel
Summary:       Development files for %{name}
Requires:      lib%{name}%{so_uffix} = %{version}

%description -n %{name}-devel
Development headers and libraries for ONNX Runtime.  For more information on
ONNX Runtime, please see <https://aka.ms/onnxruntime/> or
<https://github.com/microsoft/onnxruntime/>.

%if 0%{?_enable_tests}
%package -n %{name}-test
Summary:       Test tools for %{name}
Requires:      %{name}-devel = %{version}

%description -n %{name}-test
Testing tools for ONNX Runtime  For more information on ONNX Runtime, please
see <https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.
%endif

%prep
%setup -q
%patch -P 0 -p1
%patch -P 1 -p1
%patch -P 2 -p1

sed -i -e '/FIND_PACKAGE_ARGS 20240116 NAMES absl/d' cmake/external/abseil-cpp.cmake

mkdir -p cmake/deps

tar xf %{SOURCE1} -C cmake/deps
tar xf %{SOURCE2} -C cmake/deps
tar xf %{SOURCE3} -C cmake/deps
tar xf %{SOURCE4} -C cmake/deps
tar xf %{SOURCE5} -C cmake/deps
tar xf %{SOURCE6} -C cmake/deps
tar xf %{SOURCE7} -C cmake/deps
tar xf %{SOURCE8} -C cmake/deps
tar xf %{SOURCE9} -C cmake/deps
%if 0%{?suse_version} < 1600
tar xf %{SOURCE10} -C cmake/deps
%endif

shebang_files=""
shebang_files+="onnxruntime/python/tools/quantization/calibrate.py "
shebang_files+="tools/python/util/convert_onnx_models_to_ort.py "
shebang_files+="tools/python/util/make_dynamic_shape_fixed.py "
shebang_files+="tools/python/util/optimize_onnx_model.py "
shebang_files+="tools/python/util/qdq_helpers/optimize_qdq_model.py "
shebang_files+="tools/python/util/update_onnx_opset.py "

for s in ${shebang_files}
do
    echo "[Removing shebang line for ${s}]"
    sed -i -e '/^#![/]usr[/]bin[/]env python3/d' ${s}
done

%build
pushd cmake
%global _lto_cflags %{_lto_cflags} -ffat-lto-objects

# construct cmake opts list
_cmake_opts=""

# cmake compiler overrides
_cmake_opts+="-DCMAKE_C_COMPILER=gcc-13 "
_cmake_opts+="-DCMAKE_CXX_COMPILER=g++-13 "

# cmake general settings
_cmake_opts+="-DBUILD_SHARED_LIBS:BOOL=OFF "

# FetchContent offline mode settings
_cmake_opts+="-DFETCHCONTENT_FULLY_DISCONNECTED=ON "
_cmake_opts+="-DFETCHCONTENT_QUIET=OFF "
_cmake_opts+="-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS "

# FetchContent Source Dir overrides
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_PYTORCH_CPUINFO=/%{_builddir}/%{name}-%{version}/cmake/deps/cpuinfo-%{cpuinfo_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_FLATBUFFERS=%{_builddir}/%{name}-%{version}/cmake/deps/flatbuffers-%{flatbuffers_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_MP11=%{_builddir}/%{name}-%{version}/cmake/deps/mp11-boost-%{mp11_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_GOOGLE_NSYNC=%{_builddir}/%{name}-%{version}/cmake/deps/nsync-%{nsync_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_ONNX=%{_builddir}/%{name}-%{version}/cmake/deps/onnx-%{onnx_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_PROTOBUF=%{_builddir}/%{name}-%{version}/cmake/deps/protobuf-%{protobuf_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_SAFEINT=%{_builddir}/%{name}-%{version}/cmake/deps/safeint-%{safeint_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_ABSEIL_CPP=%{_builddir}/%{name}-%{version}/cmake/deps/abseil-cpp-%{abseil_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_EIGEN=%{_builddir}/%{name}-%{version}/cmake/deps/eigen-%{eigen_ver} "
%if 0%{?suse_version} < 1600
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_RE2=%{_builddir}/%{name}-%{version}/cmake/deps/re2-%{re2_ver} "
%endif

# onnxruntime settings
_cmake_opts+="-Donnxruntime_DISABLE_ABSEIL=OFF "
_cmake_opts+="-Donnxruntime_ENABLE_CPUINFO=OFF "
_cmake_opts+="-Donnxruntime_ENABLE_PYTHON=ON "
_cmake_opts+="-Donnxruntime_BUILD_SHARED_LIB=ON "
_cmake_opts+="-Donnxruntime_BUILD_BENCHMARKS=OFF "
_cmake_opts+="-Donnxruntime_USE_FULL_PROTOBUF=ON "

# testing related options
%if 0%{?_enable_tests}
_cmake_opts+="-Donnxruntime_BUILD_UNIT_TESTS=ON "
_cmake_opts+="-Donnxruntime_INSTALL_UNIT_TESTS=ON "
%else
_cmake_opts+="-Donnxruntime_BUILD_UNIT_TESTS=OFF "
_cmake_opts+="-Donnxruntime_INSTALL_UNIT_TESTS=OFF "
%endif

# use eigen3 preinstalled sources - currently doesn't work 
#_cmake_opts+="-Donnxruntime_USE_PREINSTALLED_EIGEN=ON "
#_cmake_opts+="-Deigen_SOURCE_PATH=/usr/include/eigen3 "

%cmake ${_cmake_opts}
%cmake_build
popd

# must be called in this subdirectory
pushd cmake/build
%python_exec ../../setup.py build
popd

%install
# must be called in this subdirectory
pushd cmake
%cmake_install

pushd build
%python_exec ../../setup.py install -O1 --skip-build --force --root %{buildroot} --prefix %{_prefix}
%python_clone -a %{buildroot}%{_bindir}/onnxruntime_test
%python_expand %fdupes %{buildroot}%{$python_sitearch} %{buildroot}%{$python_sitelib}

%if 0%{?_enable_tests}
%check
pushd cmake/build
%pytest
%endif

%post
%python_install_alternative onnxruntime_test
%postun
%python_uninstall_alternative onnxruntime_test

%post -n lib%{name}%{so_uffix} -p /sbin/ldconfig
%postun -n lib%{name}%{so_uffix} -p /sbin/ldconfig

%files -n lib%{name}%{so_uffix}
%license LICENSE ThirdPartyNotices.txt
%doc README.md
%{_libdir}/libonnxruntime.so.%{version}
%{_libdir}/libonnxruntime_providers_shared.so.%{version}

%if 0%{?_enable_tests}
%files -n %{name}-test
%{_bindir}/onnx_test_runner
%endif

%files -n %{name}-devel
%dir %{_includedir}/onnxruntime/
%{_includedir}/onnxruntime/*
%{_libdir}/libonnxruntime.so
%{_libdir}/libonnxruntime_providers_shared.so
%dir %{_libdir}/cmake/onnxruntime/
%{_libdir}/cmake/onnxruntime/*.cmake
%{_libdir}/pkgconfig/libonnxruntime.pc

%files %{python_files}
%license LICENSE ThirdPartyNotices.txt
%doc README.md
%{python_sitearch}/onnxruntime*
%python_alternative %{_bindir}/onnxruntime_test

%changelog
openSUSE Build Service is sponsored by