構建說明¶
注意: 最新的構建說明嵌入在 FBGEMM 倉庫中 setup_env.bash 下捆綁的一組指令碼中。
構建 FBGEMM_GPU 的一般步驟如下
設定隔離的構建環境。
設定工具鏈。
執行構建指令碼。
FBGEMM 要求¶
硬體要求¶
構建和執行 FBGEMM 需要支援 AVX2 指令集或更高版本的 CPU。
通常,FBGEMM 不依賴於 Intel MKL。然而,為了進行效能比較,一些基準測試使用了 MKL 函式。如果找到了 MKL 或透過 INTEL_MKL_DIR 環境變數提供了 MKL 路徑,則將使用 MKL 構建這些基準測試,並報告 MKL 函式的效能資料。否則,這部分基準測試將不會構建。
軟體依賴項¶
所有這三個依賴項都透過 FBGEMM 倉庫的 Git 子模組提供。但是,如果需要自定義版本,可以在構建時使用環境變數 ASMJIT_SRC_DIR、CPUINFO_SRC_DIR 和 GOOGLETEST_SOURCE_DIR 進行設定。
asmjit¶
對於內部核心,FBGEMM 採取“一刀切行不通”的方法,因此其實現會使用名為 asmjit 的第三方庫動態生成針對特定矩陣形狀的高效向量化程式碼。
cpuinfo¶
FBGEMM 在執行時使用 PyTorch 專案提供的 cpuinfo 庫檢測 CPU 指令集支援,並針對檢測到的指令集分派最佳化過的核心。
GoogleTest¶
構建和執行 FBGEMM 的測試需要 GoogleTest。但是,如果您不想執行 FBGEMM 測試,則不需要 GoogleTest。預設情況下,測試與庫一起構建;要關閉此功能,只需設定 FBGEMM_BUILD_TESTS=0。
設定隔離的構建環境¶
請按照 設定隔離的構建環境 中的說明設定 Conda 環境。
安裝構建工具¶
C/C++ 編譯器¶
對於 Linux 和 macOS 平臺,請按照 C/C++ 編譯器 (GCC) 中的說明安裝 GCC 工具鏈。對於基於 Clang 的構建,請按照 C/C++ 編譯器 (Clang) 中的說明安裝 Clang 工具鏈。
對於 Windows 機器上的構建,推薦使用 Microsoft Visual Studio 2019 或更高版本。請按照 Microsoft 此處 提供的安裝說明進行操作。
其他構建工具¶
安裝其他必要的構建工具,例如 ninja、cmake 等
conda install -n ${env_name} -c conda-forge --override-channels -y \
bazel \
cmake \
doxygen \
make \
ninja \
openblas
請注意,bazel 包僅用於 Bazel 構建,而 ninja 包僅用於 Windows 構建。
構建 FBGEMM 庫¶
準備構建¶
克隆倉庫及其子模組
# !! Run inside the Conda environment !!
# Clone the repo and its submodules
git clone --recurse-submodules https://github.com/pytorch/FBGEMM.git
cd FBGEMM
在 Linux 和 macOS 上構建 (CMake + GCC)¶
假設已安裝所有工具的 Conda 環境,CMake 構建過程非常簡單
# !! Run inside the Conda environment !!
# Create a build directory
mkdir build
cd build
# Set CMake build arguments
build_args=(
-DUSE_SANITIZER=address
-DFBGEMM_LIBRARY_TYPE=shared
-DPYTHON_EXECUTABLE=`which python3`
# OPTIONAL: Set to generate Doxygen documentation
-DFBGEMM_BUILD_DOCS=ON
)
# Set up the build
cmake ${build_args[@]} ..
# Build the library
make -j VERBOSE=1
# Run all tests
make test
# Install the library
make install
GCC 12+ 的構建問題¶
截至撰寫本文時,由於 已知的編譯器迴歸問題,FBGEMM 在 GCC 12+ 上的編譯將會失敗。要解決此問題,請在執行 CMake 之前新增以下匯出變數
# !! Run inside the Conda environment !!
export CFLAGS+=" -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict"
export CXXFLAGS+=" -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict"
在 Linux 和 macOS 上構建 (CMake + Clang)¶
使用 Clang 構建 FBGEMM 的步驟與使用 GCC 構建完全相同。但是,需要在呼叫 CMake 時新增額外的構建引數,以指定 Clang 路徑、基於 LLVM 的 C++ 標準庫 (libc++) 和基於 LLVM 的 OpenMP 實現 (libomp)
# !! Run inside the Conda environment !!
# Locate Clang
cc_path=$(which clang)
cxx_path=$(which clang++)
# Append to the CMake build arguments
build_args+=(
-DCMAKE_C_COMPILER="${cc_path}"
-DCMAKE_CXX_COMPILER="${cxx_path}"
-DCMAKE_C_FLAGS=\"-fopenmp=libomp -stdlib=libc++ -I $CONDA_PREFIX/include\"
-DCMAKE_CXX_FLAGS=\"-fopenmp=libomp -stdlib=libc++ -I $CONDA_PREFIX/include\"
)
在 Linux 上構建 (Bazel)¶
同樣,使用 Bazel 進行構建也非常簡單
# !! Run inside the Conda environment !!
# Build the library
bazel build -s :*
# Run all tests
bazel test -s :*
在 Windows 上構建¶
# Specify the target architecture to bc x64
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
# Create a build directory
mkdir %BUILD_DIR%
cd %BUILD_DIR%
cmake -G Ninja -DFBGEMM_BUILD_BENCHMARKS=OFF -DFBGEMM_LIBRARY_TYPE=${{ matrix.library-type }} -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" ..
ninja -v all