Adding PyTorch + DNNL + AMD BLIS path (#54953)

Summary:
These changes provide the user with an additional option to choose the DNNL+BLIS path for PyTorch.

This assumes BLIS is already downloaded or built from source and the necessary library file is available at the location: $BLIS_HOME/lib/libblis.so and include files are available at: $BLIS_HOME/include/blis/blis.h and $BLIS_HOME/include/blis/cblas.h

Export the below variables to build PyTorch with MKLDNN+BLIS and proceed with the regular installation procedure as below:
$export BLIS_HOME=path-to-BLIS
$export PATH=$BLIS_HOME/include/blis:$PATH LD_LIBRARY_PATH=$BLIS_HOME/lib:$LD_LIBRARY_PATH
$export BLAS=BLIS USE_MKLDNN_CBLAS=ON WITH_BLAS=blis
$python setup.py install

CPU only Dockerfile to build PyTorch with AMD BLIS is available at : docker/cpu-blis/Dockerfile
Example command line to build using the Dockerfile:
sudo DOCKER_BUILDKIT=1 docker build . -t docker-image-repo-name
Example command line to run the built docker container:
sudo docker run --name container-name -it docker-image-repo-name

Fixes #{issue number}

Pull Request resolved: https://github.com/pytorch/pytorch/pull/54953

Reviewed By: glaringlee

Differential Revision: D27466799

Pulled By: malfet

fbshipit-source-id: e03bae9561be3a67429df3b1be95a79005c63050
This commit is contained in:
Shruti Ramesh
2021-03-31 10:37:04 -07:00
committed by Facebook GitHub Bot
parent a74b10def9
commit f1f3c8b0fa
4 changed files with 158 additions and 3 deletions

View File

@ -118,7 +118,7 @@ else()
set(AT_MKLDNN_ENABLED 0)
set(AT_MKL_ENABLED 0)
endif()
set_property(CACHE BLAS PROPERTY STRINGS "Eigen;ATLAS;OpenBLAS;MKL;vecLib;FLAME;Generic")
set_property(CACHE BLAS PROPERTY STRINGS "ATLAS;BLIS;Eigen;FLAME;Generic;MKL;OpenBLAS;vecLib")
message(STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS})
if(BLAS STREQUAL "Eigen")
@ -133,6 +133,10 @@ elseif(BLAS STREQUAL "OpenBLAS")
find_package(OpenBLAS REQUIRED)
include_directories(SYSTEM ${OpenBLAS_INCLUDE_DIR})
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS ${OpenBLAS_LIB})
elseif(BLAS STREQUAL "BLIS")
find_package(BLIS REQUIRED)
include_directories(SYSTEM ${BLIS_INCLUDE_DIR})
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS ${BLIS_LIB})
elseif(BLAS STREQUAL "MKL")
if(BLAS_SET_BY_USER)
find_package(MKL REQUIRED)
@ -171,7 +175,7 @@ if(NOT INTERN_BUILD_MOBILE)
set(AT_MKL_ENABLED 0)
set(AT_MKL_MT 0)
set(USE_BLAS 1)
if(NOT (ATLAS_FOUND OR OpenBLAS_FOUND OR MKL_FOUND OR VECLIB_FOUND OR GENERIC_BLAS_FOUND))
if(NOT (ATLAS_FOUND OR BLIS_FOUND OR GENERIC_BLAS_FOUND OR MKL_FOUND OR OpenBLAS_FOUND OR VECLIB_FOUND))
message(WARNING "Preferred BLAS (" ${BLAS} ") cannot be found, now searching for a general BLAS library")
find_package(BLAS)
if(NOT BLAS_FOUND)

View File

@ -20,7 +20,7 @@ SET(BLAS_INCLUDE_DIR)
SET(BLAS_INFO)
SET(BLAS_F2C)
SET(WITH_BLAS "" CACHE STRING "Blas type [mkl/open/goto/acml/atlas/accelerate/veclib/generic]")
SET(WITH_BLAS "" CACHE STRING "Blas type [accelerate/acml/atlas/blis/generic/goto/mkl/open/veclib]")
# Old FindBlas
INCLUDE(CheckCSourceRuns)
@ -105,6 +105,20 @@ if((NOT BLAS_LIBRARIES)
ENDIF(MKL_FOUND)
endif()
#BLIS?
if((NOT BLAS_LIBRARIES)
AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "blis")))
check_fortran_libraries(
BLAS_LIBRARIES
BLAS
sgemm
""
"blis")
if(BLAS_LIBRARIES)
set(BLAS_INFO "blis")
endif(BLAS_LIBRARIES)
endif()
# Apple BLAS library?
if((NOT BLAS_LIBRARIES)
AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "accelerate")))

View File

@ -0,0 +1,69 @@
# - Find BLIS library
#
# This module sets the following variables:
# BLIS_FOUND - set to true if a library implementing CBLAS interface is found.
# BLIS_INCLUDE_DIR - path to include dir.
# BLIS_LIB - list of libraries for BLIS.
#
# CPU only Dockerfile to build with AMD BLIS is available at the location
# pytorch/docker/pytorch/cpu-blis/Dockerfile
#
SET(BLIS_INCLUDE_SEARCH_PATHS
/usr/include/blis
/usr/local/include
/usr/local/include/blis
/opt/blis/include
$ENV{BLIS_HOME}
$ENV{BLIS_HOME}/include
)
SET(BLIS_LIB_SEARCH_PATHS
/lib/blis
/lib64/blis
/usr/lib/blis
/usr/lib64/blis
/usr/local/blis/lib
/opt/blis/lib
$ENV{BLIS_HOME}
$ENV{BLIS_HOME}/lib
)
FIND_PATH(BLIS_INCLUDE_DIR NAMES cblas.h blis.h
PATHS ${BLIS_INCLUDE_SEARCH_PATHS})
# Check include files
IF(NOT BLIS_INCLUDE_DIR)
SET(BLIS_FOUND OFF)
MESSAGE(WARNING "Could not find BLIS include. Turning BLIS_FOUND off")
RETURN()
ENDIF()
FIND_LIBRARY(BLIS_LIB NAMES blis PATHS ${BLIS_LIB_SEARCH_PATHS})
# Check libraries
IF(NOT BLIS_LIB)
SET(BLIS_FOUND OFF)
MESSAGE(WARNING "Could not find BLIS lib. Turning BLIS_FOUND off")
RETURN()
ENDIF()
SET(BLIS_FOUND ON)
IF(BLIS_FOUND)
IF(NOT BLIS_FIND_QUIETLY)
MESSAGE(STATUS "Found BLIS libraries: ${BLIS_LIB}")
MESSAGE(STATUS "Found BLIS include: ${BLIS_INCLUDE_DIR}")
ENDIF()
ELSE()
MESSAGE(FATAL_ERROR "Could not find BLIS")
ENDIF()
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(BLIS DEFAULT_MSG BLIS_INCLUDE_DIR BLIS_LIB)
MARK_AS_ADVANCED(
BLIS_INCLUDE_DIR
BLIS_LIB
blis
)

View File

@ -0,0 +1,68 @@
# syntax = docker/dockerfile:experimental
#
# NOTE: To build this you will need a docker version > 18.06 with
# experimental enabled and DOCKER_BUILDKIT=1
#
# For reference:
# https://docs.docker.com/develop/develop-images/build_enhancements/
#
# This Dockerfile will build Docker Image with PyTorch + DNNL + AMD BLIS and Torchvision installed for CPU only
#
# Example commandline to build PyTorch with AMD BLIS:
# sudo DOCKER_BUILDKIT=1 docker build . -t docker-image-repo-name
# Example commandline to run the built docker container:
# sudo docker run --name container-name -it docker-image-repo-name
ARG BASE_IMAGE=ubuntu:18.04
ARG PYTHON_VERSION=3.8
FROM ${BASE_IMAGE} as dev-base
CMD echo "Welcome to the PyTorch Docker Container!" && \
echo "Version of PyTorch Installed: " && python -c 'import torch; print(torch.__version__)' && \
echo "Version of Torchvision Installed: " && python -c 'import torchvision; print(torchvision.__version__)' && \
echo "LDD output showing successful linking with BLIS: " && ldd /opt/conda/lib/python3.8/site-packages/torch/_C.cpython-38-x86_64-linux-gnu.so && \
/bin/bash
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
apt-get update && apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
ccache \
cmake \
curl \
git \
libjpeg-dev \
libpng-dev \
vim \
wget && \
rm -rf /var/lib/apt/lists/*
RUN /usr/sbin/update-ccache-symlinks
RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
ENV PATH /opt/conda/bin:$PATH
FROM dev-base as conda
RUN wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda install -y python=${PYTHON_VERSION} conda-build && \
/opt/conda/bin/conda install -y nomkl pyyaml numpy ipython ninja setuptools cmake cffi typing future && \
/opt/conda/bin/conda clean -ya
RUN conda install typing_extensions
WORKDIR /root
ARG BLIS_URL=https://github.com/amd/blis.git
# Download, Build BLIS with multithreading support and place necessary library and include files at BLIS_HOME/lib and BLIS_HOME/include respectively
RUN git clone ${BLIS_URL} && cd blis && \
./configure --prefix=/root/BLISBuild --enable-cblas --enable-threading=openmp auto && make -j && make install && \
if [ ! -e /root/BLISBuild/lib/libblis.so ] ; then cp /root/BLISBuild/lib/libblis*.so /root/BLISBuild/lib/libblis.so ; fi
# Build PyTorch with DNNL+BLIS
RUN git clone https://github.com/pytorch/pytorch.git && cd pytorch && \
git submodule update --init --recursive && \
export PATH=/root/BLISBuild/include/blis:$PATH LD_LIBRARY_PATH=/root/BLISBuild/lib:$LD_LIBRARY_PATH && \
export BLIS_HOME=/root/BLISBuild BLAS=BLIS USE_MKLDNN_CBLAS=ON WITH_BLAS=blis && python setup.py install
# Build Torchvision
RUN git clone https://github.com/pytorch/vision.git && cd vision && \
python setup.py install