%global pkgvers 0
%global scdate0 20241212
%global schash0 f7b1cfa8f7e10e0c157da6e55dc6f0237397faec
%global branch0 main
%global source0 https://github.com/pytorch/vision.git

%global sshort0 %{expand:%%{lua:print(('%{schash0}'):sub(1,8))}}

%global vcu_maj 12
%global vcu_min 6

Name:           torch-vision
Version:        %(curl -s "https://raw.githubusercontent.com/pytorch/vision/%{schash0}/version.txt" | sed 's|.[a-z,A-Z]||')
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}.cu%{vcu_maj}_%{vcu_min}%{?dist}
Summary:        Datasets, Transforms and Models specific to Computer Vision
License:        BSD

URL:            https://github.com/pytorch/vision

BuildRequires:  python3-devel python3-setuptools git
BuildRequires:  python3-numpy pytorch-devel pytorch-python3
BuildRequires:  ninja-build python3-typing-extensions glog-devel
BuildRequires:  libpng-devel libjpeg-turbo-devel pybind11-devel
BuildRequires:  libwebp-devel ffmpeg-free ffmpeg-free-devel libswscale-free-devel
BuildRequires:  libavutil-free-devel libavformat-free-devel libavcodec-free-devel
BuildRequires:  libavdevice-free-devel libavutil-free-devel libavfilter-free-devel
BuildRequires:  libswresample-free-devel
%if 0%{?rhel} == 8
BuildRequires:  python3-dataclasses
%endif

%define have_cuda 1
%define have_cuda_gcc 1
%define gpu_target_ptx "50"
%ifarch aarch64
%define gpu_target_bin 50,52,53,60,61,62,70,75,80,86,90
%else
%define gpu_target_bin 50,52,60,61,70,75,80,86,90
%endif

%if %{have_cuda}
%if %{have_cuda_gcc}
%if (0%{?fedora} > 34) || (0%{?rhel} > 8)
BuildRequires:  cuda-gcc-c++
%endif
%endif
BuildRequires:  cuda-nvcc-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvtx-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-cupti-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvprune-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-cudart-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvml-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvrtc-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-driver-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-profiler-api-%{vcu_maj}-%{vcu_min}
BuildRequires:  nvidia-driver-cuda-libs
BuildRequires:  libnpp-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcurand-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcusparse-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcublas-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcusolver-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcufft-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcurand-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnvjpeg-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnccl-devel
Requires:  cuda-cudart-%{vcu_maj}-%{vcu_min}
Requires:  cuda-nvrtc-%{vcu_maj}-%{vcu_min}
Requires:  cuda-nvtx-%{vcu_maj}-%{vcu_min}
%endif

%global _lto_cflags %{nil}
%undefine _hardened_build
%undefine _annotated_build
%global debug_package %{nil}
%undefine _find_debuginfo_dwz_opts
%undefine _missing_build_ids_terminate_build

%description
Datasets, Transforms and Models specific to Computer Vision.

%package        python3
Summary:        %{summary}
Provides:       torch-vision
Requires:       pytorch-python3 python3-numpy python3-requests
Requires:       python3-typing-extensions python3-pillow

%description    python3
Datasets, Transforms and Models specific to Computer Vision.


%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
git log --format=fuller
# ffmpeg
sed -i -e "s|ffmpeg_root, 'include'|ffmpeg_root, 'include/ffmpeg'|" setup.py
# nvcc
sed -i -e 's|^BUILD_CUDA_SOURCES = |BUILD_CUDA_SOURCES = True #|' setup.py
sed -i -e 's|nvcc_flags = nvcc_flags.split(" ")|nvcc_flags = os.getenv("NVCC_FLAGS", None).split(" ")|' setup.py

%build
set +x
gencode=""
for a in {%{gpu_target_bin}}; do
  gencode=$gencode"-gencode=arch=compute_$a,code=compute_$a "
done
for a in %{gpu_target_ptx}; do
  gencode=$gencode"-gencode=arch=compute_$a,code=sm_$a"
done

export NVCC_FLAGS="$gencode"
%if %{have_cuda_gcc}
%if (0%{?fedora} > 34) || (0%{?rhel} > 8)
export NVCC_FLAGS="$NVCC_FLAGS -ccbin %{_bindir}/cuda-gcc"
%endif
%endif
export NVCC_FLAGS="$NVCC_FLAGS --compiler-options -fPIC -Wno-deprecated-gpu-targets -allow-unsupported-compiler --fatbin-options -compress-all"
#export TORCH_CUDA_ARCH_LIST="5.2;6.0;6.1;7.0;7.5;8.0;8.6;9.0;5.0+PTX"

mkdir -p build
export FORCE_CUDA=1
export MAX_JOBS=%{_smp_build_ncpus}
export CUDA_HOME=/usr/local/cuda-%{vcu_maj}.%{vcu_min}
export LD_LIBRARY_PATH=/usr/local/cuda-%{vcu_maj}.%{vcu_min}/lib64/
%{__python3} setup.py build


%install
export FORCE_CUDA=1
export CUDA_HOME=/usr/local/cuda-%{vcu_maj}.%{vcu_min}
export LD_LIBRARY_PATH=/usr/local/cuda-%{vcu_maj}.%{vcu_min}/lib64/
%{__python3} setup.py install --skip-build --root %{buildroot}
%if 0%{?rhel} == 8
sed -i '/import annotations/d' %{buildroot}%{python3_sitearch}/torchvision/datasets/fgvc_aircraft.py
%endif
sed -i 's|[!<=>].*||g' %{buildroot}%{python3_sitearch}/*.egg-info/requires.txt


%files python3
%{python3_sitearch}/*
%doc README.md
%license LICENSE


%changelog
* Thu Mar 24 2016 Balint Cristian <cristian.balint@gmail.com>
- github build releases