快捷方式

构建说明

注意: 最新的构建说明嵌入在 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

构建和运行 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 此处 提供的安装说明进行操作。

其他构建工具

安装其他必要的构建工具,例如 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 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源