Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe for pytorch (C++ interface only) #8388

Merged
merged 38 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dd2f95c
Initial commit of py3-torch recipe
iarspider Mar 14, 2023
15e8919
Merge branch 'IB/CMSSW_13_1_X/master' into add-py3-torch
iarspider Mar 15, 2023
b1f0dca
Fix the recipe
iarspider Mar 15, 2023
0181b0c
Add py3-torch to python_tools
iarspider Mar 15, 2023
317acbd
Add relocation
iarspider Mar 16, 2023
b05c5d3
Remove pthreadpoool - only needed for nnpack or mobile
iarspider Mar 16, 2023
a164d27
Fix recipe
iarspider Mar 16, 2023
121ea50
Update requirements.txt
iarspider Mar 16, 2023
d3b06e8
Update torch.file
iarspider Mar 16, 2023
4bf963b
Changes from review
iarspider Mar 17, 2023
58e945e
Merge branch 'add-py3-torch' of github.com:cms-sw/cmsdist into add-py…
iarspider Mar 17, 2023
be3430f
Fix recipe
iarspider Mar 17, 2023
60cf195
Update torch.file
iarspider Mar 20, 2023
a39c221
Update torch.file
iarspider Mar 20, 2023
3a92bea
Create symlinks to lib and include
iarspider Mar 23, 2023
758ba7e
Add toolfiles
iarspider Mar 23, 2023
43bbdbb
Fix toolfile
iarspider Mar 23, 2023
afe2130
Update toolfiles again
iarspider Mar 23, 2023
0f79019
Update torch-interface.xml
iarspider Apr 4, 2023
89ae3c3
Update torch.xml
iarspider Apr 4, 2023
4b706d1
Update toolfiles
iarspider Apr 5, 2023
74f2a58
Merge branch 'IB/CMSSW_13_3_X/master' into add-py3-torch
smuzaffar Oct 21, 2023
b2688d3
Test without CUDA
iarspider Oct 23, 2023
a6562b7
Update pytorch to 2.1 (but still keep both python and C++ parts)
iarspider Nov 7, 2023
61d29b6
Add patch to ignore difference between CUDA and CUDAToolkit include dirs
iarspider Nov 7, 2023
350bb60
Build only C++ part of pytorch
Nov 14, 2023
795093c
Delete py3-torch-cpp-externsion-ppc64.patch
iarspider Nov 14, 2023
f95f7af
Delete pip/py3-torch-ignore-different-cuda-include-dir.patch
iarspider Nov 14, 2023
c1a644d
Update python_tools.spec
iarspider Nov 14, 2023
2b6a5ef
Update toolfiles
iarspider Nov 14, 2023
4c38b6a
Add missing file; add pytorch to cmssw-tool-conf
iarspider Nov 14, 2023
238e271
Add patch to ignore difference between CUDA and CUDAToolkit include dirs
iarspider Nov 7, 2023
dfb07d3
Add patch to fix missing-braces error
iarspider Nov 15, 2023
ee99c9a
Update toolfiles
iarspider Nov 15, 2023
099f60a
Make CUDA optional
iarspider Nov 16, 2023
f29deb0
Use external eigen and fmt
iarspider Nov 27, 2023
2040f9f
Fix relocation
iarspider Nov 28, 2023
cbd56e4
Cleanup pytorch.spec
iarspider Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions FindEigen3.cmake.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# - Try to find Eigen3 lib
#
# This module supports requiring a minimum version, e.g. you can do
# find_package(Eigen3 3.1.2)
# to require version 3.1.2 or newer of Eigen3.
#
# Once done this will define
#
# EIGEN3_FOUND - system has eigen lib with correct version
# EIGEN3_INCLUDE_DIR - the eigen include directory
# EIGEN3_VERSION - eigen version

# Copyright (c) 2006, 2007 Montel Laurent, <[email protected]>
# Copyright (c) 2008, 2009 Gael Guennebaud, <[email protected]>
# Copyright (c) 2009 Benoit Jacob <[email protected]>
# Redistribution and use is allowed according to the terms of the 2-clause BSD license.
#
# Modified by Andreas Schuh to add Eigen3_DIR/EIGEN3_DIR to the PATHS of
# the find_path command (instead of HINGS). Use further PATH_SUFFIXES.
# Also, removed the separate branches for whether or not EIGEN3_INCLUDE_DIR
# is set already. The find_path command will not repeat the search in
# this case anyway.

if(NOT Eigen3_FIND_VERSION)
if(NOT Eigen3_FIND_VERSION_MAJOR)
set(Eigen3_FIND_VERSION_MAJOR 2)
endif(NOT Eigen3_FIND_VERSION_MAJOR)
if(NOT Eigen3_FIND_VERSION_MINOR)
set(Eigen3_FIND_VERSION_MINOR 91)
endif(NOT Eigen3_FIND_VERSION_MINOR)
if(NOT Eigen3_FIND_VERSION_PATCH)
set(Eigen3_FIND_VERSION_PATCH 0)
endif(NOT Eigen3_FIND_VERSION_PATCH)

set(Eigen3_FIND_VERSION "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}")
endif(NOT Eigen3_FIND_VERSION)

macro(_eigen3_check_version)
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header)

string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}")
set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match "${_eigen3_version_header}")
set(EIGEN3_MAJOR_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match "${_eigen3_version_header}")
set(EIGEN3_MINOR_VERSION "${CMAKE_MATCH_1}")

set(EIGEN3_VERSION ${EIGEN3_WORLD_VERSION}.${EIGEN3_MAJOR_VERSION}.${EIGEN3_MINOR_VERSION})
if(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
set(EIGEN3_VERSION_OK FALSE)
else(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
set(EIGEN3_VERSION_OK TRUE)
endif(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})

if(NOT EIGEN3_VERSION_OK)
message(STATUS "Eigen3 version ${EIGEN3_VERSION} found in ${EIGEN3_INCLUDE_DIR}, "
"but at least version ${Eigen3_FIND_VERSION} is required")
endif(NOT EIGEN3_VERSION_OK)
endmacro(_eigen3_check_version)

find_path(EIGEN3_INCLUDE_DIR
NAMES
signature_of_eigen3_matrix_library
PATHS
${Eigen3_DIR}
${EIGEN3_DIR}
${CMAKE_INSTALL_PREFIX}
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES
include
include/eigen3
include/eigen
eigen3
eigen
)

if (EIGEN3_INCLUDE_DIR)
_eigen3_check_version()
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Eigen3 DEFAULT_MSG EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)

mark_as_advanced(EIGEN3_INCLUDE_DIR)
29 changes: 29 additions & 0 deletions FindFMT.cmake.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Find FMT
#
# Find the FMT includes
#
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH
#
# This module defines
# FMT_INCLUDE_DIRS, where to find header, etc.
# FMT_FOUND, If false, do not try to use FMT.

# only look in default directories
find_path(
FMT_INCLUDE_DIR
NAMES fmt/core.h
DOC "FMT include dir"
HINTS ${FMT_ROOT}/include
)

if (NOT FMT_INCLUDE_DIR)
MESSAGE(FATAL_ERROR "Error, FMT include dir not found. Did you set FMT_ROOT variable?")
endif()

set(FMT_INCLUDE_DIRS ${FMT_INCLUDE_DIR})

# handle the QUIETLY and REQUIRED arguments and set FMT_FOUND to TRUE
# if all listed variables are TRUE, hide their existence from configuration view
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FMT DEFAULT_MSG FMT_INCLUDE_DIR)
mark_as_advanced (FMT_FOUND FMT_INCLUDE_DIR)
1 change: 1 addition & 0 deletions cmssw-tool-conf.spec
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Requires: oracle-fake
Requires: xtensor
Requires: xtl
Requires: xgboost
Requires: pytorch

## INCLUDE cmssw-vectorization
## INCLUDE cmssw-drop-tools
Expand Down
14 changes: 14 additions & 0 deletions fxdiv.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### RPM external fxdiv 2020-04-17

%define commit b408327ac2a15ec3e43352421954f5b1967701d1

Source0: git+https://github.com/Maratyszcza/FXdiv.git?obj=master/%{commit}&export=%{n}-%{realversion}&output=/%{n}-%{realversion}.tgz

%prep
%setup -n %{n}-%{realversion}

%build

%install
mkdir -p %{i}/include
cp -a include/fxdiv.h %{i}/include/
22 changes: 22 additions & 0 deletions psimd.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### RPM external psimd 2020-05-17
%define commit 072586a71b55b7f8c584153d223e95687148a900

Source0: git+https://github.com/Maratyszcza/psimd.git?obj=master/%{commit}&export=%{n}-%{realversion}&output=/%{n}-%{realversion}.tgz

BuildRequires: gmake cmake

%prep
%setup -n %{n}-%{realversion}

%build
cd %{_builddir}
rm -rf build
mkdir build && cd build

cmake ../%{n}-%{realversion} \
-DCMAKE_INSTALL_PREFIX=%{i} \
-DCMAKE_BUILD_TYPE=Release

%install
cd %{_builddir}/build
make install
16 changes: 16 additions & 0 deletions pytorch-ignore-different-cuda-include-dir.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- pytorch-2.1.0/cmake/public/cuda.cmake.orig 2023-11-07 13:17:24.113086707 +0100
+++ pytorch-2.1.0/cmake/public/cuda.cmake 2023-11-07 13:17:36.295966731 +0100
@@ -60,13 +60,6 @@

cmake_policy(POP)

-if(NOT CMAKE_CUDA_COMPILER_VERSION STREQUAL CUDAToolkit_VERSION OR
- NOT CUDA_INCLUDE_DIRS STREQUAL CUDAToolkit_INCLUDE_DIR)
- message(FATAL_ERROR "Found two conflicting CUDA installs:\n"
- "V${CMAKE_CUDA_COMPILER_VERSION} in '${CUDA_INCLUDE_DIRS}' and\n"
- "V${CUDAToolkit_VERSION} in '${CUDAToolkit_INCLUDE_DIR}'")
-endif()
-
if(NOT TARGET CUDA::nvToolsExt)
message(FATAL_ERROR "Failed to find nvToolsExt")
endif()
21 changes: 21 additions & 0 deletions pytorch-missing-braces.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/torch/csrc/profiler/events.h b/torch/csrc/profiler/events.h
index a1a956f1327..a4e02c795d9 100644
--- a/torch/csrc/profiler/events.h
+++ b/torch/csrc/profiler/events.h
@@ -11,7 +11,7 @@ namespace profiler {
using perf_counters_t = std::vector<uint64_t>;

/* Standard list of performance events independent of hardware or backend */
-constexpr std::array<const char*, 2> ProfilerPerfEvents = {
+constexpr std::array<const char*, 2> ProfilerPerfEvents = {{
/*
* Number of Processing Elelement (PE) cycles between two points of interest
* in time. This should correlate positively with wall-time. Measured in
@@ -25,6 +25,6 @@ constexpr std::array<const char*, 2> ProfilerPerfEvents = {
* (i.e. work). Across repeat executions, the number of instructions should
* be more or less invariant. Measured in uint64_t. PE can be non cpu.
*/
- "instructions"};
+ "instructions" }};
} // namespace profiler
} // namespace torch
57 changes: 57 additions & 0 deletions pytorch-system-fmt.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/c10/CMakeLists.txt b/c10/CMakeLists.txt
index feebad7cbb7..8a2ad840286 100644
--- a/c10/CMakeLists.txt
+++ b/c10/CMakeLists.txt
@@ -87,6 +87,7 @@ endif()
if(${USE_GLOG})
target_link_libraries(c10 PUBLIC glog::glog)
endif()
+find_package(fmt REQUIRED)
target_link_libraries(c10 PRIVATE fmt::fmt-header-only)

find_package(Backtrace)
diff --git a/caffe2/CMakeLists.txt b/caffe2/CMakeLists.txt
index 74d0d557190..ff3e94b1c4b 100644
--- a/caffe2/CMakeLists.txt
+++ b/caffe2/CMakeLists.txt
@@ -1363,6 +1363,8 @@ if($ENV{TH_BINARY_BUILD})
endif()
endif()

+find_package(fmt REQUIRED)
+
target_link_libraries(torch_cpu PUBLIC c10)
target_link_libraries(torch_cpu PUBLIC ${Caffe2_PUBLIC_DEPENDENCY_LIBS})
target_link_libraries(torch_cpu PRIVATE ${Caffe2_DEPENDENCY_LIBS})
diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
index c3abce52e4c..5e89d3eb43a 100644
--- a/cmake/Dependencies.cmake
+++ b/cmake/Dependencies.cmake
@@ -1832,9 +1832,13 @@ endif()
#
# End ATen checks
#
-set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
-set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
-add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
+find_package(FMT)
+if(FMT_FOUND)
+ message(STATUS "Found system FMT at " ${FMT_INCLUDE_DIR})
+else()
+ message(STATUS "Did not find system FMT. Using third party subdirectory.")
+ add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
+endif()

# Disable compiler feature checks for `fmt`.
#
@@ -1843,10 +1847,8 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
# CMAKE_CXX_FLAGS in ways that break feature checks. Since we already know
# `fmt` is compatible with a superset of the compilers that PyTorch is, it
# shouldn't be too bad to just disable the checks.
-set_target_properties(fmt-header-only PROPERTIES INTERFACE_COMPILE_FEATURES "")

list(APPEND Caffe2_DEPENDENCY_LIBS fmt::fmt-header-only)
-set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)

# ---[ Kineto
# edge profiler depends on KinetoProfiler but it only does cpu
92 changes: 92 additions & 0 deletions pytorch.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
### RPM external pytorch 2.1.1
## INCLUDE cuda-flags

%define cuda_arch_float $(echo %{cuda_arch} | tr ' ' '\\n' | sed -E 's|([0-9])$|.\\1|' | tr '\\n' ' ')
%define tag v%{realversion}
%define branch release/2.1

Source: git+https://github.com/pytorch/pytorch.git?obj=%{branch}/%{tag}&export=%{n}-%{realversion}&submodules=1&output=/%{n}-%{realversion}.tgz
Source1: FindEigen3.cmake
Source2: FindFMT.cmake
Patch0: pytorch-ignore-different-cuda-include-dir
Patch1: pytorch-missing-braces
Patch2: pytorch-system-fmt

BuildRequires: cmake ninja
Requires: eigen fxdiv numactl openmpi protobuf psimd python3 py3-PyYAML
Requires: cuda cudnn OpenBLAS zlib protobuf fmt py3-pybind11

%prep
%setup -n %{n}-%{realversion}
%patch0 -p1
%patch1 -p1
%patch2 -p1

%build
cp %{_sourcedir}/FindEigen3.cmake %{_sourcedir}/FindFMT.cmake cmake/Modules/
rm -rf ../build && mkdir ../build && cd ../build

USE_CUDA=OFF
%if "%{cmsos}" != "slc7_aarch64"
if [ "%{cuda_gcc_support}" = "true" ] ; then
USE_CUDA=ON
fi
%endif

cmake ../%{n}-%{realversion} \
-G Ninja \
-DCMAKE_INSTALL_PREFIX=%{i} \
-DCMAKE_INSTALL_LIBDIR=lib \
-DBUILD_TEST=OFF \
-DBUILD_BINARY=OFF \
-DBUILD_PYTHON=OFF \
-DUSE_CUDA=${USE_CUDA} \
-DTORCH_CUDA_ARCH_LIST="%{cuda_arch_float}" \
-DCUDNN_INCLUDE_DIR=${CUDNN_ROOT}/include \
-DCUDNN_LIBRARY=${CUDNN_ROOT}/lib64/libcudnn.so \
-DUSE_NCCL=OFF \
-DUSE_FBGEMM=OFF \
-DUSE_KINETO=OFF \
-DUSE_MAGMA=OFF \
-DUSE_METAL=OFF \
-DUSE_MPS=OFF \
-DUSE_NNPACK=OFF \
-DUSE_QNNPACK=OFF \
-DUSE_PYTORCH_QNNPACK=OFF \
-DUSE_XNNPACK=OFF \
-DUSE_NUMA=ON \
-DNUMA_ROOT_DIR=${NUMACTL_ROOT} \
-DUSE_NUMPY=OFF \
-DUSE_OPENMP=ON \
-DUSE_QNNPACK=OFF \
-DUSE_VALGRIND=OFF \
-DUSE_XNNPACK=OFF \
-DUSE_MKLDNN=OFF \
-DUSE_DISTRIBUTED=OFF \
-DUSE_MPI=ON \
-DUSE_GLOO=OFF \
-DUSE_TENSORPIPE=OFF \
-DONNX_ML=ON \
-DBLAS=OpenBLAS \
-DBUILD_CUSTOM_PROTOBUF=OFF \
-DUSE_SYSTEM_EIGEN_INSTALL=ON \
-DUSE_SYSTEM_PSIMD=ON \
-DUSE_SYSTEM_FXDIV=ON \
-DUSE_SYSTEM_PYBIND11=ON \
-DUSE_SYSTEM_BENCHMARK=ON \
-DCMAKE_PREFIX_PATH="%{cmake_prefix_path}" \
-DPYTHON_EXECUTABLE=${PYTHON3_ROOT}/bin/python3

ninja -v %{makeprocesses}

%install
cd ../build
ninja -v %{makeprocesses} install

%post
%{relocateConfig}include/caffe2/core/macros.h
%{relocateConfig}share/cmake/ATen/ATenConfig.cmake

# For ROCm, pre-build
# NOTICE: can't build with both cuda and rocm
# python @{_builddir}/tools/amd_build/build_amd.py
6 changes: 6 additions & 0 deletions scram-tools.file/tools/pytorch/pytorch-cuda.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tool name="pytorch-cuda" version="@TOOL_VERSION@">
<lib name="torch_cuda"/>
<lib name="c10_cuda"/>
<use name="cuda"/>
<use name="torch-interface"/>
</tool>
6 changes: 6 additions & 0 deletions scram-tools.file/tools/pytorch/pytorch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tool name="pytorch" version="@TOOL_VERSION@">
<lib name="torch"/>
<lib name="torch_cpu"/>
<lib name="c10"/>
<use name="torch-interface"/>
</tool>
10 changes: 10 additions & 0 deletions scram-tools.file/tools/pytorch/torch-interface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<tool name="torch-interface" version="@TOOL_VERSION@">
<client>
<environment name="TORCH_INTERFACE_BASE" default="@TOOL_ROOT@"/>
<environment name="INCLUDE" default="$TORCH_INTERFACE_BASE/include"/>
<environment name="INCLUDE" default="$TORCH_INTERFACE_BASE/include/torch/csrc/api/include"/>
<environment name="LIBDIR" default="$TORCH_INTERFACE_BASE/lib"/>
</client>
<use name="protobuf"/>
<use name="zlib"/>
</tool>