Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[FEATURE] Enable dynamic linking with MKL and compiler based OpenMP #20474

Merged
merged 16 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[WIP] Adding github runner for MAC OS to check MKL specific changes
This is a temporary change to check if adding MKL runtime support
won't crash MacOS.
  • Loading branch information
akarbown committed Oct 12, 2021
commit ae6686b5d0d1d4cec0243532e577b595114fbb9d
57 changes: 57 additions & 0 deletions .github/workflows/os_x_mklbuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: continuous build

on: [push, pull_request]

jobs:
macosx-x86_64:
runs-on: macos-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Compilation cache
uses: actions/cache@v2
with:
path: ~/.ccache
# We include the commit sha in the cache key, as new cache entries are
# only created if there is no existing entry for the key yet.
key: ${{ runner.os }}-ccache-${{ github.sha }}
# Restore any ccache cache entry, if none for
# ${{ runner.os }}-ccache-${{ github.sha }} exists
restore-keys: |
${{ runner.os }}-ccache

- name: Setup python
uses: actions/setup-python@v2
with:
python-version: '3.6'
architecture: x64

- name: Install Dependencies
run: |
brew install nasm automake ninja libtool cmake pkgconfig protobuf hdf5 zlib ccache
ccache -M 500M # Limit the ccache size; Github's overall cache limit is 5GB
python -m pip install -r ci/docker/install/requirements
shell: bash

- name: Build project
run: |
./tools/staticbuild/build.sh cpu mkl

- name: Setup Python
run: |
python -m pip install --user -e python

- name: Build with Cython
run: |
cd python
python setup.py build_ext --inplace --with-cython

- name: Test project
env:
MXNET_ENFORCE_CYTHON: 1
run: |
python3 -m pytest -n 4 --durations=50 --verbose tests/python/unittest/ -k 'not test_operator and not (test_subgraph or test_custom_op or test_external_op or test_recordimage_dataset_with_data_loader_multiworker or test_multi_worker or test_multi_worker_shape or test_multi_worker_forked_data_loader or test_multi_worker_dataloader_release_pool)' -m 'not serial'
MXNET_ENGINE_TYPE=NaiveEngine python3 -m pytest -n 4 --durations=50 --verbose tests/python/unittest/ -k 'test_operator and not (test_subgraph or test_custom_op or test_external_op or test_recordimage_dataset_with_data_loader_multiworker or test_multi_worker or test_multi_worker_shape or test_multi_worker_forked_data_loader or test_multi_worker_dataloader_release_pool)' -m 'not serial'
python3 -m pytest --durations=50 --verbose tests/python/unittest/ -k 'not (test_subgraph or test_custom_op or test_external_op or test_recordimage_dataset_with_data_loader_multiworker or test_multi_worker or test_multi_worker_shape or test_multi_worker_forked_data_loader or test_multi_worker_dataloader_release_pool)' -m 'serial'
python3 -m pytest -n 4 --durations=50 --verbose tests/python/mkl
33 changes: 33 additions & 0 deletions config/distribution/darwin_cpu_mkl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")

set(USE_BLAS "mkl" CACHE STRING "BLAS Vendor")

set(USE_CUDA OFF CACHE BOOL "Build with CUDA support")
set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
set(USE_OPENMP OFF CACHE BOOL "Build with Openmp support")
set(USE_ONEDNN ON CACHE BOOL "Build with ONEDNN support")
set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
set(USE_LIBJPEG_TURBO ON CACHE BOOL "Build with libjpeg-turbo")
set(USE_DIST_KVSTORE ON CACHE BOOL "Build with DIST_KVSTORE support")
32 changes: 32 additions & 0 deletions config/distribution/linux_cpu_mkl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")

set(USE_BLAS "mkl" CACHE STRING "BLAS Vendor")
set(USE_CUDA OFF CACHE BOOL "Build with CUDA support")
set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
set(USE_ONEDNN ON CACHE BOOL "Build with ONEDNN support")
set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
set(USE_LIBJPEG_TURBO ON CACHE BOOL "Build with libjpeg-turbo")
set(USE_DIST_KVSTORE ON CACHE BOOL "Build with DIST_KVSTORE support")
2 changes: 1 addition & 1 deletion src/initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void LibraryInitializer::lib_close(void* handle) {
* \param func function pointer that gets output address
* \param name function name to be fetched
*/
void LibraryInitializer::get_sym(void* handle, void** func, char* name) {
void LibraryInitializer::get_sym(void* handle, void** func, const char* name) {
#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
*func = GetProcAddress((HMODULE)handle, name);
if (!(*func)) {
Expand Down
8 changes: 6 additions & 2 deletions tools/dependencies/make_shared_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ download () {
fi
}

if [[ ! $PLATFORM == 'darwin' ]]; then
source ${DIR}/openblas.sh
if [[ ! $PLATFORM == 'darwin' ]] && [[ ! $BLAS == 'mkl' ]]; then
source ${DIR}/openblas.sh
fi
source $DIR/libz.sh
source $DIR/libturbojpeg.sh
Expand All @@ -64,6 +64,10 @@ source $DIR/protobuf.sh
source $DIR/cityhash.sh
source $DIR/zmq.sh
source $DIR/lz4.sh
if [[ $BLAS == 'mkl' ]]; then
source ${DIR}/mkl.sh
fi


export LIBRARY_PATH=${LIBRARY_PATH}:$(dirname $(find $DEPS_PATH -type f -name 'libprotoc*' | grep protobuf | head -n 1)):$DEPS_PATH/lib:$DEPS_PATH/lib64
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(dirname $(find $DEPS_PATH -type f -name 'libprotoc*' | grep protobuf | head -n 1)):$DEPS_PATH/lib:$DEPS_PATH/lib64
50 changes: 50 additions & 0 deletions tools/dependencies/mkl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# This script downloads OneMKL
# TODO @akarbown: Get more general link to download the OneMKL (instead of the generated one)
barry-jin marked this conversation as resolved.
Show resolved Hide resolved
set -ex
INTEL_MKL="2021.3.0"
if [[ (! -e /opt/intel/oneapi/mkl/)]]; then
>&2 echo "Downloading mkl..."

if [[ $PLATFORM == 'darwin' ]]; then
download \
https://registrationcenter-download.intel.com/akdlm/irc_nas/17960/m_onemkl_p_${INTEL_MKL}.517_offline.dmg \
${DEPS_PATH}/m_onemkl_p_${INTEL_MKL}.517_offline.dmg
hdiutil attach ${DEPS_PATH}/m_onemkl_p_${INTEL_MKL}.517_offline.dmg
pushd /Volumes/m_onemkl_p_${INTEL_MKL}.517_offline/bootstrapper.app/Contents/MacOS/
./install.sh --silent --eula accept
popd
elif [[ $PLATFORM == 'linux' ]]; then
# use wget to fetch the Intel repository public key
download \
https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
${DEPS_PATH}/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
# add to your apt sources keyring so that archives signed with this key will be trusted.
apt-key add ${DEPS_PATH}/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
# remove the public key
rm ${DEPS_PATH}/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
add-apt-repository "deb https://apt.repos.intel.com/oneapi all main"
apt-get update && \
apt install -y intel-oneapi-mkl-${INTEL_MKL} intel-oneapi-mkl-common-${INTEL_MKL} intel-oneapi-mkl-devel
else
>&2 echo "Not available"
fi
fi
2 changes: 1 addition & 1 deletion tools/dependencies/opencv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
set -ex
OPENCV_VERSION=3.4.2
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
if [[ $PLATFORM == 'linux' ]]; then
if [[ $PLATFORM == 'linux' ]] && [[ ! $BLAS == 'mkl' ]]; then
OPENCV_LAPACK_OPTIONS=" \
-D OpenBLAS_HOME=$DEPS_PATH \
-D OpenBLAS_INCLUDE_DIR=$DEPS_PATH/include \
Expand Down
7 changes: 6 additions & 1 deletion tools/staticbuild/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@
# under the License.

if [ $# -lt 1 ]; then
>&2 echo "Usage: build.sh <VARIANT>"
>&2 echo "Usage: build.sh <VARIANT> <BLAS>"
fi

export CURDIR=$PWD
export DEPS_PATH=$PWD/staticdeps
export VARIANT=$(echo $1 | tr '[:upper:]' '[:lower:]')
export PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
export BLAS=$(echo $2 | tr '[:upper:]' '[:lower:]')

if [[ $VARIANT == darwin* ]]; then
export VARIANT="darwin"
fi

if [[ ! $BLAS ]]; then
export BLAS="open"
fi

NUM_PROC=1
if [[ ! -z $(command -v nproc) ]]; then
NUM_PROC=$(nproc)
Expand Down
11 changes: 10 additions & 1 deletion tools/staticbuild/build_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
set -eo pipefail

# This script builds the libraries of mxnet.
if [[ ! $BLAS ]] || [[ $BLAS == 'open' ]]; then
cmake_config=${CURDIR}/config/distribution/${PLATFORM}_${VARIANT}.cmake
else
cmake_config=${CURDIR}/config/distribution/${PLATFORM}_${VARIANT}_${BLAS}.cmake
fi
if [[ ! -f $cmake_config ]]; then
>&2 echo "Couldn't find cmake config $make_config for the current settings."
exit 1
Expand All @@ -36,13 +40,18 @@ cmake -GNinja -C $cmake_config \
-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13 \
..
ninja
if [[ $BLAS == 'mkl' ]]; then
patchelf --set-rpath "/opt/intel/oneapi/mkl/${INTEL_MKL}/lib/intel64/" --force-rpath libmxnet.so
fi
cd -

# Move to lib
rm -rf lib; mkdir lib;
if [[ $PLATFORM == 'linux' ]]; then
cp -L build/libmxnet.so lib/libmxnet.so
cp -L $(ldd lib/libmxnet.so | grep libgfortran | awk '{print $3}') lib/
if [[ $BLAS == 'open' ]]; then
cp -L $(ldd lib/libmxnet.so | grep libgfortran | awk '{print $3}') lib/
fi
elif [[ $PLATFORM == 'darwin' ]]; then
cp -L build/libmxnet.dylib lib/libmxnet.dylib
fi
Expand Down