Skip to content

MAGMA wheel and source torch build integration #2233

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

Open
wants to merge 12 commits into
base: rocm7.0_internal_testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions .ci/docker/manywheel/Dockerfile_2_28
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ FROM base as intel
# MKL
ADD ./common/install_mkl.sh install_mkl.sh
RUN bash ./install_mkl.sh && rm install_mkl.sh
ENV MKLROOT /opt/intel

FROM base as magma
ARG BASE_CUDA_VERSION=10.2
Expand Down Expand Up @@ -158,9 +159,6 @@ RUN python3 -m pip install --upgrade pip && \
python3 -mpip install cmake==3.28.4
ADD ./common/install_rocm_drm.sh install_rocm_drm.sh
RUN bash ./install_rocm_drm.sh && rm install_rocm_drm.sh
ENV MKLROOT /opt/intel
ADD ./common/install_rocm_magma.sh install_rocm_magma.sh
RUN bash ./install_rocm_magma.sh && rm install_rocm_magma.sh
ADD ./common/install_miopen.sh install_miopen.sh
RUN bash ./install_miopen.sh ${ROCM_VERSION} && rm install_miopen.sh

Expand Down
5 changes: 1 addition & 4 deletions .ci/docker/ubuntu-rocm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ARG ANACONDA_PYTHON_VERSION
ARG BUILD_ENVIRONMENT
ENV ANACONDA_PYTHON_VERSION=$ANACONDA_PYTHON_VERSION
ENV PATH /opt/conda/envs/py_$ANACONDA_PYTHON_VERSION/bin:/opt/conda/bin:$PATH
ENV MKLROOT=/opt/conda/envs/py_$ANACONDA_PYTHON_VERSION
ARG CONDA_CMAKE
COPY requirements-ci.txt /opt/conda/requirements-ci.txt
COPY ./common/install_conda.sh install_conda.sh
Expand Down Expand Up @@ -63,9 +64,6 @@ ARG ROCM_VERSION
COPY ./common/install_rocm.sh install_rocm.sh
RUN bash ./install_rocm.sh
RUN rm install_rocm.sh
COPY ./common/install_rocm_magma.sh install_rocm_magma.sh
RUN bash ./install_rocm_magma.sh
RUN rm install_rocm_magma.sh
ADD ./common/install_miopen.sh install_miopen.sh
RUN bash ./install_miopen.sh ${ROCM_VERSION} && rm install_miopen.sh
ENV ROCM_PATH /opt/rocm
Expand All @@ -74,7 +72,6 @@ ENV PATH /opt/rocm/hcc/bin:$PATH
ENV PATH /opt/rocm/hip/bin:$PATH
ENV PATH /opt/rocm/opencl/bin:$PATH
ENV PATH /opt/rocm/llvm/bin:$PATH
ENV MAGMA_HOME /opt/rocm/magma
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ endif()
list(APPEND ATen_CUDA_DEPENDENCY_LIBS torch::magma)
endif(USE_CUDA AND NOT BUILD_LAZY_CUDA_LINALG)
if(USE_ROCM)
list(APPEND ATen_HIP_DEPENDENCY_LIBS torch::magma)
list(APPEND ATen_HIP_DEPENDENCY_LIBS __rocm_magma)
endif(USE_ROCM)
if(MSVC)
if($ENV{TH_BINARY_BUILD})
Expand Down
7 changes: 6 additions & 1 deletion caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ if(USE_ROCM)
target_compile_definitions(torch_hip PRIVATE USE_NCCL)
endif()


if(USE_PRECOMPILED_HEADERS)
target_precompile_headers(torch_hip PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:ATen/core/ATen_pch.h>")
Expand Down Expand Up @@ -1002,7 +1003,11 @@ elseif(USE_CUDA)
# `torch::magma` should be mentioned before other CUDA
# to transitively include all symbols present in torch_cuda/torch_cpu
if(USE_MAGMA)
target_link_libraries(torch_cuda_linalg PRIVATE torch::magma)
if(USE_CUDA)
target_link_libraries(torch_cuda_linalg PRIVATE torch::magma)
elseif(USE_ROCM)
target_link_libraries(torch_cuda_linalg PRIVATE __rocm_magma)
endif()
# CUDAHooks reports version of MAGMA PyTorch was compiled against, i.e. needs to be able to include magma headers
get_target_property(HOOKS_INCLUDE_DIRECTORIES torch_cuda INCLUDE_DIRECTORIES)
if(NOT "${MAGMA_INCLUDE_DIR}" IN_LIST HOOKS_INCLUDE_DIRECTORIES)
Expand Down
21 changes: 13 additions & 8 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1432,15 +1432,20 @@ if(NOT INTERN_BUILD_MOBILE)

if(USE_CUDA OR USE_ROCM)
if(USE_MAGMA)
find_package(MAGMA)
if(MAGMA_FOUND)
message(STATUS "Compiling with MAGMA support")
message(STATUS "MAGMA INCLUDE DIRECTORIES: ${MAGMA_INCLUDE_DIR}")
message(STATUS "MAGMA LIBRARIES: ${MAGMA_LIBRARIES}")
message(STATUS "MAGMA V2 check: ${MAGMA_V2}")
if(USE_ROCM)
message(STATUS "Installing MAGMA from magma.cmake")
include(${CMAKE_CURRENT_LIST_DIR}/External/magma.cmake)
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS magma)
else()
message(STATUS "MAGMA not found. Compiling without MAGMA support")
caffe2_update_option(USE_MAGMA OFF)
find_package(MAGMA)
if(MAGMA_FOUND)
message(STATUS "Compiling with MAGMA support")
message(STATUS "MAGMA INCLUDE DIRECTORIES: ${MAGMA_INCLUDE_DIR}")
message(STATUS "MAGMA LIBRARIES: ${MAGMA_LIBRARIES}")
message(STATUS "MAGMA V2 check: ${MAGMA_V2}")
else()
caffe2_update_option(USE_MAGMA OFF)
endif()
endif()
endif()
elseif(USE_MAGMA)
Expand Down
148 changes: 148 additions & 0 deletions cmake/External/magma.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
if(NOT __MAGMA_INCLUDED)

set(__MAGMA_INCLUDED TRUE)

set(__MAGMA_INSTALL_DIR "${PROJECT_SOURCE_DIR}/torch")
add_library(__rocm_magma INTERFACE)

# MAGMA package information from GitHub Release Pages
set(__MAGMA_VER "2.9.0")
set(__MAGMA_MANYLINUX_LIST
"manylinux_2_28" # rocm6.3
"manylinux_2_28" # rocm6.4
"manylinux_2_28" # rocm7.0
)
set(__MAGMA_ROCM_LIST
"rocm6.3.0"
"rocm6.4.0"
"rocm7.0.0"
)
set(__MAGMA_CI_COMMIT "")
set(__MAGMA_EXT ".whl")

file(MAKE_DIRECTORY ${__MAGMA_INSTALL_DIR}/include/magma)

# If it is INSTALLED
if(DEFINED ENV{MAGMA_HOME})

# Install from /opt/rocm/magma
ExternalProject_Add(magma_external
SOURCE_DIR $ENV{MAGMA_HOME}
INSTALL_DIR ${__MAGMA_INSTALL_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include/ <INSTALL_DIR>/include/magma
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/lib/libmagma.so <INSTALL_DIR>/lib
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/libmagma.so
BUILD_BYPRODUCTS <INSTALL_DIR>/include/magma
)
add_dependencies(__rocm_magma magma_external)

message(STATUS "Installing Preinstalled MAGMA from $ENV{MAGMA_HOME} to ${__MAGMA_INSTALL_DIR}\n"
"MAGMA headers installed to ${__MAGMA_INSTALL_DIR}/include/magma")

# Installing from WHEEL
elseif($ENV{CI})

if(DEFINED ENV{MAGMA_WHEEL_URL})
set(__MAGMA_WHEEL_URL $ENV{MAGMA_WHEEL_URL})
else()
set(__MAGMA_WHEEL_URL "repo.radeon.com/rocm/magma")
endif()

set(__MAGMA_SYSTEM_ROCM "${ROCM_VERSION_DEV_MAJOR}.${ROCM_VERSION_DEV_MINOR}")
list(GET __MAGMA_ROCM_LIST 0 __MAGMA_ROCM_DEFAULT_STR)

# Find ROCm version
string(SUBSTRING ${__MAGMA_ROCM_DEFAULT_STR} 4 -1 __MAGMA_ROCM)
foreach(MAGMA_ROCM_BUILD_STR IN LISTS __MAGMA_ROCM_LIST)
string(SUBSTRING ${MAGMA_ROCM_BUILD_STR} 4 -1 MAGMA_ROCM_BUILD)
if(MAGMA_ROCM_BUILD VERSION_GREATER __MAGMA_SYSTEM_ROCM)
break()
endif()
set(__MAGMA_ROCM ${MAGMA_ROCM_BUILD})
endforeach()

list(FIND __MAGMA_ROCM_LIST "rocm${__MAGMA_ROCM}" __MAGMA_ROCM_INDEX)
list(GET __MAGMA_SHA256_LIST ${__MAGMA_ROCM_INDEX} __MAGMA_SHA256)
list(GET __MAGMA_MANYLINUX_LIST ${__MAGMA_ROCM_INDEX} __MAGMA_MANYLINUX)
set(__MAGMA_ARCH "manylinux_2_28_x86_64")

string(CONCAT __MAGMA_FILE "magma-"
"${__MAGMA_VER}+rocm${__MAGMA_ROCM}"
"-py3-none-${__MAGMA_ARCH}"
"${__MAGMA_EXT}")

string(CONCAT __MAGMA_URL "${__MAGMA_WHEEL_URL}/"
"${__MAGMA_FILE}")

message(STATUS "MAGMA full URL=" ${__MAGMA_URL})

message(STATUS "Installing MAGMA from wheel file to ${__MAGMA_INSTALL_DIR}\n"
"MAGMA headers in ${__MAGMA_INSTALL_DIR}/include/magma")

ExternalProject_Add(magma_external
URL ${__MAGMA_URL}
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/magma_src"
INSTALL_DIR ${__MAGMA_INSTALL_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/magma/lib/libmagma.so <INSTALL_DIR>/lib/
COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/magma/include <INSTALL_DIR>/include/magma
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/libmagma.so
BUILD_BYPRODUCTS <INSTALL_DIR>/include/magma
)
add_dependencies(__rocm_magma magma_external)

else()
message(STATUS "No MAGMA installation found. Installing MAGMA from source.")

set(MAGMA_VERSION "2.9.0")
set(MAGMA_REPOSITORY "https://github.com/ROCm/utk-magma.git")
set(MAGMA_GIT_TAG "05caf6482768e8ec1445dba4765d5d174c2aa531")

# Install MKL if not installed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MKL can be assumed as a per-requsite of pytorch.

if(DEFINED ENV{MKLROOT})
set(MKLROOT $ENV{MKLROOT})
message(STATUS "Attempting to install MAGMA using MKL found in $ENV{MKLROOT}.")
else()
message(FATAL_ERROR "MAGMA install from source: No MKL installation detected. \n"
"Please install MKL using 'pip install mkl-static mkl-include' \n"
"And set MKLROOT appropriately (Usually to your env location).' \n"
"Or set USE_MAGMA=OFF to build without MAGMA.")
endif()

set(__MAGMA_EXTERN_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/magma")
set(__MAGMA_INSTALL_DIR "${PROJECT_SOURCE_DIR}/torch")

string(REPLACE ";" "" MAGMA_GFX_ARCH $ENV{PYTORCH_ROCM_ARCH}) # We avoid using lists due to cmake adding spaces between list items in the cmake command
message("Building MAGMA for gfx architectures: ${MAGMA_GFX_ARCH}")

cmake_host_system_information(RESULT N_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES)

ExternalProject_Add(magma_external
SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/magma_src
PREFIX ${__MAGMA_EXTERN_PREFIX}
INSTALL_DIR ${__MAGMA_INSTALL_DIR}
GIT_REPOSITORY ${MAGMA_REPOSITORY}
GIT_TAG ${MAGMA_GIT_TAG}
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/make.inc-examples/make.inc.hip-gcc-mkl <SOURCE_DIR>/make.inc
COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> make -f make.gen.hipMAGMA -j ${N_LOGICAL_CORES}
BUILD_COMMAND ${CMAKE_COMMAND} -E env MKLROOT=${MKLROOT}
${CMAKE_COMMAND} -E chdir <SOURCE_DIR> make lib/libmagma.so -j ${N_LOGICAL_CORES} MKLROOT=${MKLROOT} GPU_TARGET=${MAGMA_GFX_ARCH}
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/lib/libmagma.so <INSTALL_DIR>/lib/
COMMAND ${CMAKE_COMMAND} -E copy_directory <SOURCE_DIR>/include <INSTALL_DIR>/include/magma
USES_TERMINAL_DOWNLOAD TRUE
USES_TERMINAL_CONFIGURE TRUE
USES_TERMINAL_BUILD TRUE
USES_TERMINAL_INSTALL TRUE
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/libmagma.so
BUILD_BYPRODUCTS <INSTALL_DIR>/include/magma
)
add_dependencies(__rocm_magma magma_external)

endif()
target_link_libraries(__rocm_magma INTERFACE ${__MAGMA_INSTALL_DIR}/lib/libmagma.so)
target_include_directories(__rocm_magma INTERFACE ${__MAGMA_INSTALL_DIR}/include/magma)
set(MAGMA_FOUND TRUE)
endif() # __MAGMA_INCLUDED
12 changes: 6 additions & 6 deletions cmake/public/LoadHIP.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ else()
endif()

# MAGMA_HOME
if(NOT DEFINED ENV{MAGMA_HOME})
set(MAGMA_HOME ${ROCM_PATH}/magma)
set(ENV{MAGMA_HOME} ${ROCM_PATH}/magma)
else()
set(MAGMA_HOME $ENV{MAGMA_HOME})
endif()
#if(NOT DEFINED ENV{MAGMA_HOME})
# set(MAGMA_HOME ${ROCM_PATH}/magma)
# set(ENV{MAGMA_HOME} ${ROCM_PATH}/magma)
#else()
# set(MAGMA_HOME $ENV{MAGMA_HOME})
#endif()

# MIOpen isn't a part of HIP-SDK for Windows and hence, may have a different
# installation directory.
Expand Down