捷徑

建置說明

注意: 最新的建置說明嵌入在 FBGEMM 儲存庫中 setup_env.bash 下的一組腳本中。

建置 FBGEMM_GPU 的一般步驟如下

  1. 設定隔離的建置環境。

  2. 設定工具鏈。

  3. 執行建置腳本。

FBGEMM 需求

硬體需求

建置和執行 FBGEMM 需要支援 AVX2 指令集或更高版本的 CPU。

一般來說,FBGEMM 不依賴 Intel MKL。然而,為了進行效能比較,某些基準測試會使用 MKL 函數。如果找到 MKL 或透過 INTEL_MKL_DIR 環境變數提供 MKL 路徑,則基準測試將使用 MKL 建置,並報告 MKL 函數的效能數字。否則,將不會建置此基準測試子集。

軟體依賴

所有三個依賴項都透過 FBGEMM 儲存庫的 git 子模組提供。但是,如果需要自訂版本,可以使用環境變數 ASMJIT_SRC_DIRCPUINFO_SRC_DIRGOOGLETEST_SOURCE_DIR 在建置中設定它們。

asmjit

對於內部核心,FBGEMM 採用「一種尺寸不適用於所有情況」的方法,因此實作動態生成高效的矩陣形狀特定向量化程式碼,使用名為 asmjit 的第三方函式庫。

cpuinfo

FBGEMM 在執行時使用 PyTorch 專案提供的 cpuinfo 函式庫偵測 CPU 指令集支援,並為偵測到的指令集分派最佳化的核心。

GoogleTest

GoogleTest 是建置和執行 FBGEMM 測試的必要條件。但是,如果您不想執行 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 在此處 提供的安裝說明進行操作。

其他建置工具

安裝其他必要的建置工具,例如 ninjacmake

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+ 的建置問題

截至撰寫本文時,由於 已知的編譯器回歸問題,在 GCC 12+ 上編譯 FBGEMM 將會失敗。為了解決此問題,請在執行 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"

請參閱 GitHub 問題 7793910941666 以了解更多詳細資訊。

在 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

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學

取得針對初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並獲得解答

檢視資源