File miopen.spec of Package miopen

%global upstreamname MIOpen

%global rocm_release 6.2
%global rocm_patch 0
%global rocm_version %{rocm_release}.%{rocm_patch}

# MIOpen version
%define so_major 1
%define so_version %{so_major}.0
%define so_name libMIOpen
%define libname %{so_name}%{so_major}
%define miopen_version 3.2.0

%global toolchain clang
# hipcc does not support some clang flags
%global build_cxxflags %(echo %{optflags} | sed -e 's/-fstack-protector-strong/-Xarch_host -fstack-protector-strong/' -e 's/-fcf-protection/-Xarch_host -fcf-protection/') -fPIE -Wdeprecated-declarations

%bcond_with debug
%if %{with debug}
%global build_type DEBUG
%else
%global build_type RelWithDebInfo
%endif

# Option to build the test suite
%bcond_with test
# Option to run the test suite for testing on real HW:
%bcond_with check

Name:           miopen
Version:        %{rocm_version}
Release:        0
Summary:        AMD's Machine Intelligence Library
Url:            https://github.com/ROCm/%{upstreamname}
License:        MIT

Source0:        %{url}/archive/rocm-%{version}.tar.gz#/%{upstreamname}-rocm-%{version}.tar.gz
Source1:        FindZstd.cmake
Patch0:         miopen-fix-build.patch
BuildRequires:  cmake
BuildRequires:  ninja
BuildRequires:  libboost_filesystem-devel >= 1.79.0
BuildRequires:  pkgconfig(bzip2)
BuildRequires:  pkgconfig(libzstd)
BuildRequires:  pkgconfig(eigen3)
BuildRequires:  pkgconfig(sqlite3)
BuildRequires:  cmake(fplus)
BuildRequires:  cmake(fdeep)
BuildRequires:  cmake(nlohmann_json)
BuildRequires:  libzstd-devel
BuildRequires:  half-headers

BuildRequires:  rocm-cmake
BuildRequires:  rocm-comgr-devel
BuildRequires:  rocm-hip-devel
BuildRequires:  rocm-runtime-devel
BuildRequires:  rocm-rpm-macros
BuildRequires:  rocm-llvm-rpm-macros
BuildRequires:  rocm-release(roctracer-devel) = %{rocm_release}
BuildRequires:  rocm-release(rocblas-devel) = %{rocm_release}

%if 0%{?sle_version} == 150600
BuildRequires:  gcc12-c++
BuildRequires:  libstdc++6-devel-gcc12
%else
BuildRequires:  libstdc++-devel
%endif

%if %{with check} || %{with test}
BuildRequires:  googletest-devel
BuildRequires:  googlemock-devel
BuildRequires:  rocm-release(rocrand-devel) = %{rocm_release}
%endif

Requires:       %{libname}%{?_isa} = %{miopen_version}
Provides:       rocm-release(%{name}) = %{rocm_release}

# Only x86_64 works right now
ExclusiveArch:  x86_64

%description
AMD's library for high performance machine learning primitives.

%package     -n %{libname}
Summary:        AMD's Machine Intelligence Library
Group:          System/Libraries/C and C++
Provides:       %{libname}%{?_isa} = %{miopen_version}

%description -n %{libname}
AMD's library for high performance machine learning primitives.

%package        devel
Summary:        The %{upstreamname} development package
Group:          System/Libraries/C and C++
Requires:       %{name}%{?_isa} = %{version}-%{release}
Requires:       %{libname}%{?_isa} = %{miopen_version}
Provides:       rocm-release(%{name}-devel) = %{rocm_release}

%description    devel
The %{upstreamname} development package.

%if %{with test}
%package        test
Summary:        Tests for %{name}
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description    test
This package contains the test files to be run on real hardware.
%endif

%prep
%autosetup -n %{upstreamname}-rocm-%{version} -p 1

# move find module to cmake dir
cp %{S:1} cmake/FindZstd.cmake
sed -i 's/ZSTD/zstd/g' cmake/FindZstd.cmake


# Readme has executable bit
chmod 644 README.md

# clang-tidy is brittle and not needed for rebuilding from a tarball
sed -i -e 's@clang-tidy@true@' cmake/ClangTidy.cmake

# workaround error on finding lbunzip2
sed -i -e 's@lbunzip2 bunzip2@bunzip2@' CMakeLists.txt

# https://github.com/ROCm/MIOpen/issues/2672
sed -i -e 's@find_path(HALF_INCLUDE_DIR half/half.hpp)@#find_path(HALF_INCLUDE_DIR half/half.hpp)@' CMakeLists.txt
# #include <half/half.hpp> -> <half.hpp>
for f in `find . -type f -name '*.hpp' -o -name '*.cpp' `; do
    sed -i -e 's@#include <half/half.hpp>@#include <half.hpp>@' $f
done

# Tries to download its own googletest
# No good knob to turn it off so hack the cmake
%if %{without test}
sed -i -e 's@add_subdirectory(test)@#add_subdirectory(test)@' CMakeLists.txt
sed -i -e 's@add_subdirectory(speedtests)@#add_subdirectory(speedtests)@' CMakeLists.txt
%endif

# Our use of modules confuse install locations
# The db is not installed relative to the lib dir.
# Hardcode its location
sed -i -e 's@GetLibPath().parent_path() / "share/miopen/db"@"/usr/share/miopen/db"@' src/db_path.cpp.in

# Unsupported compiler flags
sed -i -e 's@opts.push_back("-fno-offload-uniform-block");@//opts.push_back("-fno-offload-uniform-block");@' src/comgr.cpp

# Paths to clang for V2 paths
sed -i \
  -e 's@MIOPEN_CLANG_REL_PATH "llvm/bin/clang"@MIOPEN_CLANG_REL_PATH "bin/clang-%{rocmllvm_major}"@' \
  -e 's@MIOPEN_CPPCLANG_REL_PATH "llvm/bin/clang++"@MIOPEN_CPPCLANG_REL_PATH "bin/clang++-%{rocmllvm_major}"@' \
  -e 's@MIOPEN_OCL_REL_PATH "bin/clang"@MIOPEN_OCL_REL_PATH "bin/clang-%{rocmllvm_major}"@' \
  -e 's@MIOPEN_OFFLOADBUNDLER_REL_PATH "llvm/bin/clang-offload-bundler"@MIOPEN_OFFLOADBUNDLER_REL_PATH "bin/clang-offload-bundler-%{rocmllvm_major}"@' \
  src/hip/hip_build_utils.cpp

%build
# enable ccache during build if available
if command -v ccache > /dev/null; then
  export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  export CMAKE_C_COMPILER_LAUNCHER=ccache
fi

%define __builder ninja
%cmake \
    -DBUILD_FILE_REORG_BACKWARD_COMPATIBILITY=OFF \
%if %{with test}
    -DBUILD_TESTING=ON \
%endif
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_WITH_SPARSE_DEFAULT=ON \
    -DCMAKE_BUILD_TYPE=%build_type \
    -DCMAKE_CXX_FLAGS="%build_cxxflags" \
    -DCMAKE_PREFIX_PATH="%rocm_path" \
    -DCMAKE_SKIP_INSTALL_RPATH=ON \
    -DCMAKE_INSTALL_LIBDIR=%{_lib} \
    -DBoost_USE_STATIC_LIBS=OFF \
    -DMIOPEN_BACKEND=HIP \
    -DMIOPEN_BUILD_DRIVER=OFF \
    -DMIOPEN_ENABLE_AI_IMMED_MODE_FALLBACK=OFF \
    -DMIOPEN_ENABLE_AI_KERNEL_TUNING=OFF \
    -DMIOPEN_USE_MLIR=OFF \
    -DMIOPEN_USE_COMPOSABLEKERNEL=OFF \
    -DAMDGPU_TARGETS="%{list_sep rocm_gpu_list ;}" \
    %rocmllvm_cmake_opts
%cmake_build

%install
%cmake_install

# we don't nned the installed docs
rm -rf %{buildroot}%{_docdir}/%{name}

%if %{with test}
# move tests to libexec
mkdir -p %{buildroot}%{_libexecdir}/%{name}-test
mv %{buildroot}%{_bindir}/* %{buildroot}%{_libexecdir}/%{name}-test/
%endif

%check
%if %{with check}
%ctest
%endif

%ldconfig_scriptlets -n %{libname}

%files
%doc README.md
%license LICENSE.txt
%dir %_datadir/%{name}
%dir %_datadir/%{name}/db
%_datadir/%{name}/db/gfx*.db.txt
%_datadir/%{name}/db/gfx*.fdb.txt
%exclude %_bindir/install_precompiled_kernels.sh

%files -n %{libname}
%{_libdir}/%{so_name}.so.%{so_major}
%{_libdir}/%{so_name}.so.%{so_version}

%files devel
%{_includedir}/%{name}
%{_libdir}/cmake/%{name}
%{_libdir}/%{so_name}.so

%if %{with test}
%files test
%{_libexecdir}/%{name}-test
%endif

%changelog
openSUSE Build Service is sponsored by