File python-torch.spec of Package python-torch
#
# spec file for package python-torch
#
# 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 srcname pytorch
%define pname torch
%global flavor @BUILD_FLAVOR@%{nil}
%bcond_with gloo
%if "%{flavor}" == "rocm"
%bcond_without rocm
%global rocm_major 6
%global rocm_minor 2
%global rocm_release %{rocm_major}.%{rocm_minor}
%global pkg_suffix -rocm-%{rocm_major}-%{rocm_minor}
%endif
%if "%{flavor}" == "standard"
%bcond_with cuda
%endif
%if "%{flavor}" == "cuda-10-2"
%bcond_without cuda
%define cudaver 10-2
%endif
%bcond_with mpi
%if "%{flavor}" == "openmpi4"
%bcond_without mpi
%bcond_without openmpi4
%global mpi_flavor openmpi
%define mpi_ext 4
%endif
%if "%{flavor}" == "vulkan"
%bcond_without vulkan
%global pkg_suffix -vulkan
%endif
%if %{with mpi}
%global pkg_suffix %{?mpi_flavor:-%{mpi_flavor}%{?mpi_ext}}
%define pkg_prefix %{_libdir}/mpi/gcc/%{mpi_flavor}%{?mpi_ext}
%define pkg_bindir %{pkg_prefix}/bin/
%define pkg_libdir %{pkg_prefix}/%{_lib}/
%define pkg_incdir %{pkg_prefix}/include/
%define pkg_datadir %{pkg_prefix}/share/
%define pkg_sysconfdir %{pkg_prefix}/etc/
%define pkg_skeldir %{pkg_prefix}/etc/skel/
%define package_name %{pname}%{?pkg_suffix}
%endif
%define FP16_version 4dfe081
%define FXdiv_version b408327
%define XNNPACK_version fcbf55a
%define cpphttplib_version 3b6597b
%define cpuinfo_version fa1c679
%define flatbuffers 01834de
%define fmt_version e69e5f9
%define gemmlowp_version 3fb5c17
%define gloo_version 5354032
%define kineto d975313
%define libnop 910b558
%define onnx_version 3bf92c0
%define opentelemetrycpp_version a799f4a
%define pocketfft 9d3ab05
%define psimd_version 072586a
%define pthreadpool_version 4fe0e1e
%define pybind11_version 7c33cdc
%define sleef_version 60e76d2
%define tensorpipe 52791a2
%{?sle15_python_module_pythons}
Name: python-torch%{?pkg_suffix}
Version: 2.5.0
Release: 0
Summary: Deep learning framework aka pytorch/Caffe2
License: Apache-2.0 AND BSD-2-Clause AND BSD-3-Clause AND MIT AND Zlib AND BSL-1.0
Group: Development/Languages/Python
URL: https://pytorch.org
Source0: https://github.com/pytorch/pytorch/archive/v%{version}.tar.gz#/%{srcname}-%{version}.tar.gz
Source1: releases.html
# License10: BSD-3-Clause
Source10: https://github.com/facebookincubator/gloo/archive/%{gloo_version}.tar.gz#/gloo-%{gloo_version}.tar.gz
# License11: MIT
Source11: https://github.com/yhirose/cpp-httplib/archive/%{cpphttplib_version}.tar.gz#/cpp-httplib-%{cpphttplib_version}.tar.gz
# License12: BSD-2-Clause
Source12: https://github.com/pytorch/cpuinfo/archive/%{cpuinfo_version}.tar.gz#/cpuinfo-%{cpuinfo_version}.tar.gz
# License13: BSL-1.0
Source13: https://github.com/zdevito/sleef/archive/%{sleef_version}.tar.gz#/sleef-%{sleef_version}.tar.gz
# License14: BSD-3-Clause
Source14: https://github.com/pybind/pybind11/archive/%{pybind11_version}.tar.gz#/pybind11-%{pybind11_version}.tar.gz
# License15: MIT
Source15: https://github.com/onnx/onnx/archive/%{onnx_version}.tar.gz#/onnx-%{onnx_version}.tar.gz
#License16: BSD-2-Clause
Source16: https://github.com/Maratyszcza/pthreadpool/archive/%{pthreadpool_version}.tar.gz#/pthreadpool-%{pthreadpool_version}.tar.gz
# License17: MIT
Source17: https://github.com/Maratyszcza/FXdiv/archive/%{FXdiv_version}.tar.gz#/FXdiv-%{FXdiv_version}.tar.gz
# License18: MIT
Source18: https://github.com/Maratyszcza/psimd/archive/%{psimd_version}.tar.gz#/psimd-%{psimd_version}.tar.gz
# License19: MIT
Source19: https://github.com/Maratyszcza/FP16/archive/%{FP16_version}.tar.gz#/FP16-%{FP16_version}.tar.gz
# License20: Apache-2.0
Source20: https://github.com/google/gemmlowp/archive/%{gemmlowp_version}.tar.gz#/gemmlowp-%{gemmlowp_version}.tar.gz
# License22: Apache-2.0
Source22: https://github.com/open-telemetry/opentelemetry-cpp/archive/%{opentelemetrycpp_version}.tar.gz#/opentelemetry-cpp-%{opentelemetrycpp_version}.tar.gz
# License23: BSD-3-Clause
Source23: https://github.com/google/XNNPACK/archive/%{XNNPACK_version}.tar.gz#/XNNPACK-%{XNNPACK_version}.tar.gz
# License 25: MIT
Source25: https://github.com/fmtlib/fmt/archive/%{fmt_version}.tar.gz#/fmt-%{fmt_version}.tar.gz
# License 26: BSD-3-Clause
Source26: https://github.com/mreineck/pocketfft/archive/%{pocketfft}.tar.gz#/pocketfft-%{pocketfft}.tar.gz
# License 27: BSD-3-Clause
Source27: https://github.com/pytorch/kineto/archive/%{kineto}.tar.gz#/kineto-%{kineto}.tar.gz
# License 28: Apache-2.0
Source28: https://github.com/google/flatbuffers/archive/%{flatbuffers}.tar.gz#/flatbuffers-%{flatbuffers}.tar.gz
# License 29: BSD-3-Clause
Source29: https://github.com/pytorch/tensorpipe/archive/%{tensorpipe}.tar.gz#/tensorpipe-%{tensorpipe}.tar.gz
# License 30: Apache-2.0
Source30: https://github.com/google/libnop/archive/%{libnop}.tar.gz#/libnop-%{libnop}.tar.gz
Patch1: skip-third-party-check.patch
Patch2: fix-setup.patch
Patch3: pytorch-patch-onednn.patch
Patch4: pytorch-rocm-do-not-use-aotriton-if-not-required.patch
Patch5: pytorch-cuda-hip-signatures.patch
#Patch6: pytorch-optionally-use-hipblaslt.patch
Patch7: pytorch-use-any-hip.patch
Patch8: pytorch-amd-build-check-clang.patch
Patch9: pytorch-hipify_python-copy-attrs.patch
Patch11: pytorch-get-hip-compiler-env.patch
Patch13: pytorch-fix-hip-clang-build.patch
Patch14: pytorch-hipcc-env-cxx-flags.patch
# A python call to cmake fails with a return code of 1 on this arch, disable it for now.
# and 32-bit arm is not supported
ExcludeArch: %ix86 %{arm}
%if %{with gloo}
BuildRequires: %{python_module Gloo}
%endif
%ifarch x86_64
BuildRequires: %{python_module PeachPy}
%endif
BuildRequires: %{python_module PyYAML}
BuildRequires: %{python_module devel}
BuildRequires: %{python_module hypothesis}
BuildRequires: %{python_module numpy-devel}
BuildRequires: %{python_module opcodes}
BuildRequires: %{python_module pip}
BuildRequires: %{python_module protobuf}
BuildRequires: %{python_module psutil}
BuildRequires: %{python_module py-cpuinfo}
BuildRequires: %{python_module setuptools}
BuildRequires: %{python_module typing_extensions}
BuildRequires: %{python_module typing}
# Python 3.6 still need dataclasses
BuildRequires: %{python_module dataclasses if %python-base < 3.7}
BuildRequires: cmake >= 3.5
BuildRequires: eigen3-devel
BuildRequires: fdupes
%if 0%{?sle_version} == 150600
BuildRequires: gcc12-c++
BuildRequires: libprotobuf-c-devel-gcc12
BuildRequires: libstdc++6-devel-gcc12
BuildRequires: ocl-icd-devel
BuildRequires: protobuf-devel-gcc12
%else
BuildRequires: gcc-c++
BuildRequires: protobuf-c
BuildRequires: protobuf-devel
%endif
BuildRequires: glog-devel
BuildRequires: gtest
BuildRequires: leveldb-devel
BuildRequires: libnuma-devel
BuildRequires: libopenblas_pthreads-devel
BuildRequires: libuv-devel
BuildRequires: lmdb-devel
BuildRequires: ninja
BuildRequires: nlohmann_json-devel
%if %{without rocm}
%ifarch x86_64 aarch64 ppc64le
BuildRequires: onednn-devel
%endif
%endif
BuildRequires: openblas-devel
BuildRequires: opencv-devel
BuildRequires: openssl-devel
BuildRequires: python-rpm-macros
BuildRequires: snappy-devel
%if %{with rocm}
BuildRequires: binutils-gold
BuildRequires: patchelf
BuildRequires: rocm-cmake
BuildRequires: rocm-rpm-macros
BuildRequires: rocm-release(hip-devel) = %{rocm_release}
BuildRequires: cmake(COMgr-ROCm) = %{rocm_release}
BuildRequires: cmake(LLVM-ROCm) = %{rocm_release}
BuildRequires: cmake(rocm-runtime) = %{rocm_release}
BuildRequires: rocm-release(hipblas-devel) = %{rocm_release}
BuildRequires: rocm-release(hipblaslt-devel) = %{rocm_release}
BuildRequires: rocm-release(hipcub-headers) = %{rocm_release}
BuildRequires: rocm-release(hipfft-devel) = %{rocm_release}
BuildRequires: rocm-release(hiprand-devel) = %{rocm_release}
BuildRequires: rocm-release(hipsolver-devel) = %{rocm_release}
BuildRequires: rocm-release(hipsparse-devel) = %{rocm_release}
BuildRequires: rocm-release(rocblas-devel) = %{rocm_release}
BuildRequires: rocm-release(rocfft-devel) = %{rocm_release}
BuildRequires: rocm-release(rocm-core-devel) = %{rocm_release}
BuildRequires: rocm-release(rocprim-headers) = %{rocm_release}
BuildRequires: rocm-release(rocrand-devel) = %{rocm_release}
BuildRequires: rocm-release(miopen-devel) = %{rocm_release}
BuildRequires: rocm-release(rocthrust-headers) = %{rocm_release}
BuildRequires: rocm-release(roctracer-devel) = %{rocm_release}
Provides: rocm-release(python-torch) = %{rocm_release}
%endif
%if %{with cuda}
BuildRequires: cuda-compiler-%cudaver
BuildRequires: cuda-cudart-dev-%cudaver
BuildRequires: cuda-libraries-dev-%cudaver
BuildRequires: cuda-misc-headers-%cudaver
BuildRequires: cuda-nsight-%cudaver
BuildRequires: cuda-toolkit-%cudaver
BuildRequires: libcudnn7-devel
BuildRequires: libnccl-devel
%endif
%if %{with openmpi4}
BuildRequires: openmpi4-devel
%endif
%if %{with vulkan}
BuildRequires: VulkanMemoryAllocator-devel
BuildRequires: shaderc
BuildRequires: vulkan-devel
%endif
Requires: python-filelock
%if 0%{?python_version_nodots} <= 312
Requires: python-numpy >= 1.0.0
%else
Requires: python-numpy >= 2.0.0
%endif
Requires: python-protobuf
Requires: python-six
Requires: python-sympy
Requires: python-typing_extensions
Provides: python-caffe2%{?pkg_suffix} = %version
Conflicts: python-caffe2%{?pkg_suffix} < %version
Provides: python-pytorch%{?pkg_suffix} = %version
Conflicts: python-pytorch%{?pkg_suffix} < %version
%if %{with rocm} || %{with openmpi4} || %{with vulkan}
Provides: python-caffe2 = %version
Conflicts: python-caffe2 < %version
Provides: python-pytorch = %version
Conflicts: python-pytorch < %version
%endif
%if "%flavor" == ""
ExclusiveArch: do_not_build
%endif
Requires(post): update-alternatives
Requires(postun): update-alternatives
%python_subpackages
%description
PyTorch enables fast, flexible experimentation and efficient production through
a hybrid front-end, distributed training, and ecosystem of tools and libraries.
The library is developed by Facebook and other groups.
PyTorch provides two high-level features:
* Tensor computing (like NumPy) with strong acceleration via graphics
* processing units (GPU) Deep neural networks built on a tape-based autodiff
system
%package devel
Summary: Headers for C/C++, cmake build description and libraries needed for development
Group: Development/Languages/Python
Requires: %{name} = %{version}
%if %{with rocm} || %{with openmpi4} || %{with vulkan}
Provides: %{python3_dist torch-devel} = %version
Conflicts: %{python3_dist torch-devel} < %version
%endif
%description devel
Although the Python interface is more polished and the primary focus of
development, PyTorch also has a C++ frontend. This package contains the header
to access the C/C++ interface.
%package converters
Summary: Converters for onnx and caffe2
Group: Development/Languages/Python
BuildArch: noarch
Requires: python3-click
Requires: python3-onnx
Requires: python3-pip
Requires: python3-pname
%if %{with rocm} || %{with openmpi4} || %{with vulkan}
Provides: %{python3_dist torch-converters} = %version
Conflicts: %{python3_dist torch-converters} < %version
%endif
Requires(post): update-alternatives
Requires(postun): update-alternatives
%description converters
Converter from caffe2 to onnx and from caffe2 to onnx formated files.
%prep
%define make_depend_src() test -e $(basename %1| sed 's/-.*//') && rmdir %{?2}%{!?2:$(basename %1| sed 's/-.*//')}; tar xzf %1; mv $(basename %{1} | sed 's/\.tar\.gz//' )* %{?2}%{!?2:$(basename %1| sed 's/-.*//')}
%define make_depend_src_uppercase() rmdir -p $(basename %1| sed 's/-.*//'| tr '[:upper:]' '[:lower:]'); tar xzf %1; mv $(basename %1 | cut -f 1 -d '.' ) $(basename %1| sed 's/-.*//'| tr '[:upper:]' '[:lower:]')
%autosetup -p1 -n %{srcname}-%{version}
cp %{S:1} releases.html
%if %{with vulkan}
sed -i '/-Werror=return-type/d' CMakeLists.txt
%endif
cd third_party
rmdir python-peachpy/
rmdir eigen/
%make_depend_src %{SOURCE10}
%make_depend_src %{SOURCE11}
rmdir cpp-httplib && mv cpp cpp-httplib
%make_depend_src %{SOURCE12}
%make_depend_src %{SOURCE13}
%make_depend_src %{SOURCE14}
%make_depend_src %{SOURCE15}
%make_depend_src %{SOURCE16}
%make_depend_src %{SOURCE17}
%make_depend_src %{SOURCE18}
%make_depend_src %{SOURCE19}
%make_depend_src %{SOURCE20} gemmlowp/gemmlowp
%make_depend_src %{SOURCE22}
rmdir opentelemetry-cpp && mv opentelemetry opentelemetry-cpp
%make_depend_src %{SOURCE23}
%make_depend_src %{SOURCE25}
%make_depend_src %{SOURCE26}
%make_depend_src %{SOURCE27}
%make_depend_src %{SOURCE28}
%make_depend_src %{SOURCE29}
# getting the vendoring of the vendored source working, this is
# insanity at the next level. My onlu exuse is that libnop is header only
rmdir tensorpipe/third_party/libnop
%make_depend_src %{SOURCE30} tensorpipe/third_party/libnop
cd -
# Fix installation path
sed -i -e 's@DESTINATION ${PYTHON_LIB_REL_PATH}@DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON_LIB_REL_PATH}@' caffe2/CMakeLists.txt
# Unify stdc++ standard
find -name CMakeLists.txt | xargs sed -e 's|CXX_STANDARD 14|CXX_STANDARD 17|' -e 's|CXX_STANDARD 11|CXX_STANDARD 17|' -i
%if %{with rocm}
# hipify sources
python3.11 ./tools/amd_build/build_amd.py
%endif
%build
# enable sccache during build if available
if command -v ccache > /dev/null; then
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export CMAKE_C_COMPILER_LAUNCHER=ccache
%if %{with rocm}
export HIP_CLANG_LAUNCHER=ccache
%endif
fi
%define buildvars \
export CMAKE_INSTALL_RPATH_USE_LINK_PATH=OFF \
export PYTORCH_BUILD_VERSION="%{version}" \
export PYTORCH_BUILD_NUMBER="%(echo "%{release}" | tr -cd '[:digit:]')" \
export MAX_JOBS=%{?jobs} \
export BUILD_CUSTOM_PROTOBUF=OFF \
export BUILD_NVFUSER=OFF \
export BUILD_SHARED_LIBS=ON \
export BUILD_TEST=OFF \
export INTERN_BUILD_MOBILE=OFF \
export USE_DISTRIBUTED=OFF \
export USE_FAKELOWP=OFF \
export USE_XPU=OFF \
export USE_SYSTEM_LIBS=OFF \
export USE_FLASH_ATTENTION=OFF \
export USE_NNPACK=OFF \
export LC_ALL="C.UTF-8" \
%if 0%{?suse_version} > 1500 \
export CC=gcc-%gcc_version \
export CXX=g++-%gcc_version \
%else \
export CC=gcc-12 \
export CXX=g++-12 \
%endif \
export USE_KINETO=OFF \
export USE_FBGEMM=OFF \
export USE_MKLDNN=ON \
export TP_BUILD_LIBUV=OFF \
export USE_SYSTEM_BENCHMARK=ON \
export USE_SYSTEM_EIGEN_INSTALL=ON \
export USE_SYSTEM_PYBIND11=OFF \
export USE_OPENMP=OFF \
export USE_LAPACK=ON \
export BLAS=OpenBLAS \
%if %{with cuda} \
export USE_CUDA=ON \
export USE_CUDNN=ON \
export USE_SYSTEM_NCCL=ON \
export PATH="/usr/local/cuda-10.1/bin:$PATH" \
export CPLUS_INCLUDE_PATH="/usr/local/cuda-10.1/include" \
export C_INCLUDE_PATH="/usr/local/cuda-10.1/include" \
export LD_LIBRARY_PATH="/usr/local/cuda-10.1/lib" \
export NCCL_INCLUDE_DIR="/usr/include/" \
%else \
export USE_CUDA=OFF \
export USE_CUDNN=OFF \
export USE_NCCL=OFF \
%endif \
%if %{with rocm} \
export PYTORCH_ROCM_ARCH="%{list_sep rocm_gpu_list ;}" \
export USE_ROCM=ON \
export USE_MKLDNN=OFF \
export USE_MAGMA=OFF \
export USE_FLASH_ATTENTION=OFF \
export USE_MEM_EFF_ATTENTION=OFF \
export USE_GOLD_LINKER=ON \
export MAX_JOBS=$(nproc --ignore=1) \
export CC=clang-rocm \
export CXX=clang++-rocm \
export CFLAGS="-Wno-unknown-warning-option -Wno-unused-result -Wno-sign-compare -Wno-deprecated-declarations" \
export CXXFLAGS="$CFLAGS" \
export HIPCXX=hipcc \
export HIPCXXFLAGS="$CFLAGS -Wno-pass-failed" \
export HIP_PATH=`hipconfig -p` \
export ROCM_PATH=`hipconfig -R` \
export ROCM_INCLUDE_DIRS=${HIP_PATH}/include \
export HIP_CLANG_PATH=`hipconfig -l` \
export USE_LLVM=${ROCM_PATH}/ \
export LLVM_DIR=${ROCM_PATH}/%{_lib}/cmake/llvm \
export DEVICE_LIB_PATH="`${HIP_CLANG_PATH}/${CC} -print-resource-dir`/amdgcn/bitcode" \
%else \
export USE_ROCM=OFF \
%endif \
%if %{with mpi} \
export USE_DISTRIBUTED=ON \
export USE_MPI=ON \
export MPIEXEC_EXECUTABLE="%{pkg_bindir}/mpiexec" \
%else \
%if %{with gloo} \
export USE_DISTRIBUTED=ON \
export USE_GLOO=ON \
export USE_SYSTEM_GLOO=ON \
%endif \
%endif \
%if %{with vulkan} \
export USE_VULKAN=ON \
export CXXFLAGS="-I /usr/ -Wno-error=return-type" \
%endif \
%{nil}
%buildvars
%python_build
%install
%buildvars
%python_install -q
%python_expand %fdupes %{buildroot}%{$python_sitearch}
%if %{with rocm}
%{python_expand find %{buildroot}%{$python_sitearch} -wholename '*/bin/*' -o -name '*.so' | xargs -n 1 patchelf --debug --shrink-rpath --allowed-rpath-prefixes '$ORIGIN'}
%endif
%if %{with gloo} || %{with mpi}
%python_clone -a %{buildroot}%{_bindir}/torchfrtrace
%endif
%python_clone -a %{buildroot}%{_bindir}/torchrun
%python_clone -a %{buildroot}%{_bindir}/convert-caffe2-to-onnx
%python_clone -a %{buildroot}%{_bindir}/convert-onnx-to-caffe2
%post
%if %{with gloo} || %{with mpi}
%{python_install_alternative torchrun torchfrtrace}
%else
%python_install_alternative torchrun
%endif
%end
%postun
%python_uninstall_alternative torchrun
%end
%post converters
%{python_install_alternative convert-caffe2-to-onnx convert-onnx-to-caffe2}
%end
%postun converters
%python_uninstall_alternative convert-caffe2-to-onnx
%end
%files %{python_files}
%defattr(-,root,root)
%doc README.md NOTICE releases.html
%license LICENSE
%{python_sitearch}/torch*
%{python_sitearch}/torchgen/
%{python_sitearch}/functorch/
%exclude %{python_sitearch}/torch/share
%exclude %{python_sitearch}/torch/include
%exclude %{python_sitearch}/torch/_inductor/codegen
%exclude %{python_sitearch}/torch/utils/benchmark/utils/
%exclude %{python_sitearch}/torchgen/packaged/ATen/templates
%exclude %{python_sitearch}/torchgen/packaged/autograd/templates
%if %{with gloo} || %{with mpi}
%python_alternative %{_bindir}/torchfrtrace
%endif
%python_alternative %{_bindir}/torchrun
%files %{python_files devel}
%{python_sitearch}/torch/share
%{python_sitearch}/torch/include
%{python_sitearch}/torch/_inductor/codegen
%{python_sitearch}/torch/utils/benchmark/utils/
%{python_sitearch}/torchgen/packaged/ATen/templates
%{python_sitearch}/torchgen/packaged/autograd/templates
%files %{python_files converters}
%python_alternative %{_bindir}/convert-caffe2-to-onnx
%python_alternative %{_bindir}/convert-onnx-to-caffe2
%changelog