Skip to content

Commit fac3541

Browse files
committed
[fbgemm_gpu] Refactor setup.py to accomodate build targets and variants
- Refactor setup.py to accomodate build targets and variants - Update docs to include building GenAI package for ROCm
1 parent 0f00a8a commit fac3541

File tree

6 files changed

+159
-105
lines changed

6 files changed

+159
-105
lines changed

.github/scripts/fbgemm_gpu_build.bash

+7-8
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ __configure_fbgemm_gpu_build_cpu () {
141141
# Update the package name and build args depending on if CUDA is specified
142142
echo "[BUILD] Setting CPU-only build args ..."
143143
build_args=(
144-
--package_variant=cpu
144+
--build-variant=cpu
145145
)
146146
}
147147

148148
__configure_fbgemm_gpu_build_docs () {
149149
# Update the package name and build args depending on if CUDA is specified
150150
echo "[BUILD] Setting CPU-only (docs) build args ..."
151151
build_args=(
152-
--package_variant=docs
152+
--build-variant=docs
153153
)
154154
}
155155

@@ -206,7 +206,7 @@ __configure_fbgemm_gpu_build_rocm () {
206206
# https://rocm.docs.amd.com/en/docs-6.1.1/reference/rocmcc.html
207207
echo "[BUILD] Setting ROCm build args ..."
208208
build_args=(
209-
--package_variant=rocm
209+
--build-variant=rocm
210210
# HIP_ROOT_DIR now required for HIP to be correctly detected by CMake
211211
-DHIP_ROOT_DIR=/opt/rocm
212212
# ROCm CMake complains about missing AMDGPU_TARGETS, so we explicitly set this
@@ -284,7 +284,7 @@ __configure_fbgemm_gpu_build_cuda () {
284284

285285
echo "[BUILD] Setting CUDA build args ..."
286286
build_args=(
287-
--package_variant=cuda
287+
--build-variant=cuda
288288
--nvml_lib_path="${nvml_lib_path}"
289289
--nccl_lib_path="${nccl_lib_path}"
290290
# Pass to PyTorch CMake
@@ -303,10 +303,9 @@ __configure_fbgemm_gpu_build_genai () {
303303

304304
__configure_fbgemm_gpu_build_cuda "$fbgemm_variant_targets" || return 1
305305

306-
# Replace the package_variant flag, since GenAI is also a CUDA-type build
307-
for i in "${!build_args[@]}"; do
308-
build_args[i]="${build_args[i]/--package_variant=cuda/--package_variant=genai}"
309-
done
306+
build_args+=(
307+
--build-target=genai
308+
)
310309
}
311310

312311
# shellcheck disable=SC2120

.github/scripts/fbgemm_gpu_install.bash

+10-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ __install_fetch_version_and_variant_info () {
3939

4040
echo "[CHECK] Printing out the FBGEMM-GPU version ..."
4141
# shellcheck disable=SC2086,SC2155
42-
installed_fbgemm_gpu_version=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(fbgemm_gpu.__version__)")
42+
installed_fbgemm_target=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(fbgemm_gpu.__target__)")
4343
# shellcheck disable=SC2086,SC2155
44-
installed_fbgemm_gpu_variant=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(fbgemm_gpu.__variant__)")
44+
installed_fbgemm_variant=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(fbgemm_gpu.__variant__)")
45+
# shellcheck disable=SC2086,SC2155
46+
installed_fbgemm_version=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(fbgemm_gpu.__version__)")
4547
echo "################################################################################"
46-
echo "[CHECK] The installed VERSION of FBGEMM_GPU is: ${installed_fbgemm_gpu_version}"
47-
echo "[CHECK] The installed VARIANT of FBGEMM_GPU is: ${installed_fbgemm_gpu_variant}"
48+
echo "[CHECK] The installed FBGEMM TARGET is: ${installed_fbgemm_target}"
49+
echo "[CHECK] The installed FBGEMM VARIANT is: ${installed_fbgemm_variant}"
50+
echo "[CHECK] The installed FBGEMM VERSION is: ${installed_fbgemm_version}"
4851
echo "################################################################################"
4952
echo ""
5053
}
@@ -53,7 +56,7 @@ __install_check_subpackages () {
5356
# shellcheck disable=SC2086,SC2155
5457
local fbgemm_gpu_packages=$(conda run ${env_prefix} python -c "import fbgemm_gpu; print(dir(fbgemm_gpu))")
5558

56-
if [ "$installed_fbgemm_gpu_variant" == "cuda" ] || [ "$installed_fbgemm_gpu_variant" == "genai" ]; then
59+
if [ "$installed_fbgemm_target" == "genai" ]; then
5760
# shellcheck disable=SC2086,SC2155
5861
local experimental_packages=$(conda run ${env_prefix} python -c "import fbgemm_gpu.experimental; print(dir(fbgemm_gpu.experimental))")
5962
fi
@@ -74,7 +77,7 @@ __install_check_subpackages () {
7477
"fbgemm_gpu.tbe.cache"
7578
)
7679

77-
if [ "$installed_fbgemm_gpu_variant" != "genai" ]; then
80+
if [ "$installed_fbgemm_target" != "genai" ]; then
7881
subpackages+=(
7982
"fbgemm_gpu.split_embedding_codegen_lookup_invokers"
8083
"fbgemm_gpu.tbe.ssd"
@@ -89,7 +92,7 @@ __install_check_subpackages () {
8992

9093
__install_check_operator_registrations () {
9194
echo "[INSTALL] Check for operator registrations ..."
92-
if [ "$installed_fbgemm_gpu_variant" == "genai" ]; then
95+
if [ "$installed_fbgemm_target" == "genai" ]; then
9396
local test_operators=(
9497
"torch.ops.fbgemm.nccl_init"
9598
"torch.ops.fbgemm.gqa_attn_splitk"

fbgemm_gpu/docs/src/fbgemm_genai/development/BuildInstructions.rst

+49-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ toolchains have been properly installed.
147147
148148
# Build the wheel artifact only
149149
python setup.py bdist_wheel \
150-
--package_variant=genai \
150+
--build-target=genai \
151+
--build-variant=cuda \
151152
--python-tag="${python_tag}" \
152153
--plat-name="${python_plat_name}" \
153154
--nvml_lib_path=${NVML_LIB_PATH} \
@@ -156,11 +157,57 @@ toolchains have been properly installed.
156157
157158
# Build and install the library into the Conda environment
158159
python setup.py install \
159-
--package_variant=genai \
160+
--build-target=genai \
161+
--build-variant=cuda \
160162
--nvml_lib_path=${NVML_LIB_PATH} \
161163
--nccl_lib_path=${NCCL_LIB_PATH} \
162164
-DTORCH_CUDA_ARCH_LIST="${cuda_arch_list}"
163165
166+
.. _fbgemm-gpu.build.process.rocm:
167+
168+
ROCm Build
169+
----------
170+
171+
For ROCm builds, ``ROCM_PATH`` and ``PYTORCH_ROCM_ARCH`` need to be specified.
172+
The presence of a ROCm device, however, is not required for building
173+
the package.
174+
175+
Similar to CUDA builds, building with Clang + ``libstdc++`` can be enabled by
176+
appending ``--cxxprefix=$CONDA_PREFIX`` to the build command, presuming the
177+
toolchains have been properly installed.
178+
179+
.. code:: sh
180+
181+
# !! Run in fbgemm_gpu/ directory inside the Conda environment !!
182+
183+
export ROCM_PATH=/path/to/rocm
184+
185+
# [OPTIONAL] Enable verbose HIPCC logs
186+
export HIPCC_VERBOSE=1
187+
188+
# Build for the target architecture of the ROCm device installed on the machine (e.g. 'gfx908,gfx90a,gfx942')
189+
# See https://rocm.docs.amd.com/en/latest/reference/gpu-arch-specs.html for list
190+
export PYTORCH_ROCM_ARCH=$(${ROCM_PATH}/bin/rocminfo | grep -o -m 1 'gfx.*')
191+
192+
# Build the wheel artifact only
193+
python setup.py bdist_wheel \
194+
--build-target=genai \
195+
--build-variant=rocm \
196+
--python-tag="${python_tag}" \
197+
--plat-name="${python_plat_name}" \
198+
-DAMDGPU_TARGETS="${PYTORCH_ROCM_ARCH}" \
199+
-DHIP_ROOT_DIR="${ROCM_PATH}" \
200+
-DCMAKE_C_FLAGS="-DTORCH_USE_HIP_DSA" \
201+
-DCMAKE_CXX_FLAGS="-DTORCH_USE_HIP_DSA"
202+
203+
# Build and install the library into the Conda environment
204+
python setup.py install \
205+
--build-target=genai \
206+
--build-variant=rocm \
207+
-DAMDGPU_TARGETS="${PYTORCH_ROCM_ARCH}" \
208+
-DHIP_ROOT_DIR="${ROCM_PATH}" \
209+
-DCMAKE_C_FLAGS="-DTORCH_USE_HIP_DSA" \
210+
-DCMAKE_CXX_FLAGS="-DTORCH_USE_HIP_DSA"
164211
165212
Post-Build Checks (For Developers)
166213
----------------------------------

fbgemm_gpu/docs/src/fbgemm_gpu/development/BuildInstructions.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -542,16 +542,16 @@ For CPU-only builds, the ``--cpu_only`` flag needs to be specified.
542542
543543
# Build the wheel artifact only
544544
python setup.py bdist_wheel \
545-
--package_variant=cpu \
545+
--build-variant=cpu \
546546
--python-tag="${python_tag}" \
547547
--plat-name="${python_plat_name}"
548548
549549
# Build and install the library into the Conda environment (GCC)
550550
python setup.py install \
551-
--package_variant=cpu
551+
--build-variant=cpu
552552
553553
# NOTE: To build the package as part of generating the documentation, use
554-
# `--package_variant=docs` flag instead!
554+
# `--build-variant=docs` flag instead!
555555
556556
To build using Clang + ``libstdc++`` instead of GCC, simply append the
557557
``--cxxprefix`` flag:
@@ -562,14 +562,14 @@ To build using Clang + ``libstdc++`` instead of GCC, simply append the
562562
563563
# Build the wheel artifact only
564564
python setup.py bdist_wheel \
565-
--package_variant=cpu \
565+
--build-variant=cpu \
566566
--python-tag="${python_tag}" \
567567
--plat-name="${python_plat_name}" \
568568
--cxxprefix=$CONDA_PREFIX
569569
570570
# Build and install the library into the Conda environment (Clang)
571571
python setup.py install \
572-
--package_variant=cpu
572+
--build-variant=cpu
573573
--cxxprefix=$CONDA_PREFIX
574574
575575
Note that this presumes the Clang toolchain is properly installed along with the
@@ -642,7 +642,7 @@ toolchains have been properly installed.
642642
643643
# Build the wheel artifact only
644644
python setup.py bdist_wheel \
645-
--package_variant=cuda \
645+
--build-variant=cuda \
646646
--python-tag="${python_tag}" \
647647
--plat-name="${python_plat_name}" \
648648
--nvml_lib_path=${NVML_LIB_PATH} \
@@ -651,7 +651,7 @@ toolchains have been properly installed.
651651
652652
# Build and install the library into the Conda environment
653653
python setup.py install \
654-
--package_variant=cuda \
654+
--build-variant=cuda \
655655
--nvml_lib_path=${NVML_LIB_PATH} \
656656
--nccl_lib_path=${NCCL_LIB_PATH} \
657657
-DTORCH_CUDA_ARCH_LIST="${cuda_arch_list}"
@@ -684,7 +684,7 @@ presuming the toolchains have been properly installed.
684684
685685
# Build the wheel artifact only
686686
python setup.py bdist_wheel \
687-
--package_variant=rocm \
687+
--build-variant=rocm \
688688
--python-tag="${python_tag}" \
689689
--plat-name="${python_plat_name}" \
690690
-DAMDGPU_TARGETS="${PYTORCH_ROCM_ARCH}" \
@@ -694,7 +694,7 @@ presuming the toolchains have been properly installed.
694694
695695
# Build and install the library into the Conda environment
696696
python setup.py install \
697-
--package_variant=rocm \
697+
--build-variant=rocm \
698698
-DAMDGPU_TARGETS="${PYTORCH_ROCM_ARCH}" \
699699
-DHIP_ROOT_DIR="${ROCM_PATH}" \
700700
-DCMAKE_C_FLAGS="-DTORCH_USE_HIP_DSA" \

fbgemm_gpu/fbgemm_gpu/__init__.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def _load_library(filename: str, no_throw: bool = False) -> None:
3131

3232
try:
3333
# Export the version string from the version file auto-generated by setup.py
34-
from fbgemm_gpu.docs.version import __variant__, __version__ # noqa: F401, E402
34+
from fbgemm_gpu.docs.version import __target__, __variant__, __version__ # noqa: F401, E402
3535
except Exception:
3636
__variant__: str = "INTERNAL"
3737
__version__: str = "INTERNAL"
38+
__target__: str = "INTERNAL"
3839

3940
fbgemm_gpu_libraries = [
4041
"fbgemm_gpu_config",
@@ -52,7 +53,7 @@ def _load_library(filename: str, no_throw: bool = False) -> None:
5253
"fbgemm_gpu_py",
5354
]
5455

55-
fbgemm_gpu_genai_libraries = [
56+
fbgemm_genai_libraries = [
5657
"experimental/gen_ai/fbgemm_gpu_experimental_gen_ai",
5758
]
5859

@@ -64,17 +65,14 @@ def _load_library(filename: str, no_throw: bool = False) -> None:
6465
# .SO file for the ROCm case, so that clients can import
6566
# fbgemm_gpu.experimental.gemm without triggering an error.
6667
if torch.cuda.is_available() and torch.version.hip:
67-
fbgemm_gpu_genai_libraries = []
68+
fbgemm_genai_libraries = []
6869

6970
libraries_to_load = {
70-
"cpu": fbgemm_gpu_libraries,
71-
"docs": fbgemm_gpu_libraries,
72-
"cuda": fbgemm_gpu_libraries,
73-
"genai": fbgemm_gpu_genai_libraries,
74-
"rocm": fbgemm_gpu_libraries,
71+
"default": fbgemm_gpu_libraries,
72+
"genai": fbgemm_genai_libraries,
7573
}
7674

77-
for library in libraries_to_load.get(__variant__, []):
75+
for library in libraries_to_load.get(__target__, []):
7876
# NOTE: In all cases, we want to throw an error if we cannot load the
7977
# library. However, this appears to break the OSS documentation build,
8078
# where the Python documentation doesn't show up in the generated docs.

0 commit comments

Comments
 (0)