Skip to content

Commit

Permalink
Refactor CMakeLists.txt (pytorch#2501)
Browse files Browse the repository at this point in the history
Summary:
- Refactor CMakeLists.txt to support building experimental targets
- Add example folder to demonstrate building an experimental target

Pull Request resolved: pytorch#2501

Reviewed By: spcyppt

Differential Revision: D56265492

Pulled By: q10

fbshipit-source-id: c1b3778d78fda528bbe79b29ee2b44e7e1411a1c
  • Loading branch information
q10 authored and facebook-github-bot committed Apr 17, 2024
1 parent 3f4f98f commit a2fdc90
Show file tree
Hide file tree
Showing 21 changed files with 382 additions and 169 deletions.
29 changes: 22 additions & 7 deletions .github/scripts/fbgemm_gpu_test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ run_fbgemm_gpu_tests () {
done
}

test_all_fbgemm_gpu_modules () {
local env_name="$1"
local fbgemm_variant="$2"

local target_directories=(
fbgemm_gpu/test
fbgemm_gpu/experimental/example/test
)

for test_dir in "${target_directories[@]}"; do
cd "${test_dir}" || return 1
run_fbgemm_gpu_tests "${env_name}" "${fbgemm_variant}" || return 1
cd - || return 1
done
}


################################################################################
# FBGEMM_GPU Test Bulk-Combination Functions
Expand Down Expand Up @@ -292,9 +308,8 @@ test_fbgemm_gpu_build_and_install () {
cd ~/FBGEMM/ || return 1
install_fbgemm_gpu_wheel "${env_name}" fbgemm_gpu/dist/*.whl || return 1

cd ~/FBGEMM/fbgemm_gpu/test || return 1
run_fbgemm_gpu_tests "${env_name}" "${pytorch_variant_type}" || return 1
cd - || return 1
cd ~/FBGEMM/ || return 1
test_all_fbgemm_gpu_modules "${env_name}" "${pytorch_variant_type}" || return 1
}

test_fbgemm_gpu_setup_and_pip_install () {
Expand Down Expand Up @@ -323,11 +338,11 @@ test_fbgemm_gpu_setup_and_pip_install () {

local env_name="test_py${py_version}_pytorch_${pytorch_channel_version}_fbgemm_${fbgemm_gpu_channel_version}_${variant_type}/${variant_version}"
local env_name="${env_name//\//_}"
test_setup_conda_environment "${env_name}" 'no-compiler' "${py_version}" pip "${pytorch_channel_version}" "${variant_type}" "${variant_version}" || return 1
install_fbgemm_gpu_pip "${env_name}" "${fbgemm_gpu_channel_version}" "${variant_type}/${variant_version}" || return 1
cd ~/FBGEMM/fbgemm_gpu/test || return 1
test_setup_conda_environment "${env_name}" 'no-compiler' "${py_version}" pip "${pytorch_channel_version}" "${variant_type}" "${variant_version}" || return 1
install_fbgemm_gpu_pip "${env_name}" "${fbgemm_gpu_channel_version}" "${variant_type}/${variant_version}" || return 1
cd ~/FBGEMM || return 1

run_fbgemm_gpu_tests "${env_name}" "${variant_type}";
test_all_fbgemm_gpu_modules "${env_name}" "${variant_type}";
local retcode=$?

echo "################################################################################"
Expand Down
4 changes: 2 additions & 2 deletions .github/scripts/nova_postscript.bash
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ else
fi

$CONDA_RUN python3 -c "import torch; print('cuda.is_available() ', torch.cuda.is_available()); print ('device_count() ',torch.cuda.device_count());"
cd "${FBGEMM_REPO}/fbgemm_gpu/test" || { echo "[NOVA] Failed to cd to fbgemm_gpu/test from $(pwd)"; };
run_fbgemm_gpu_tests "${BUILD_ENV_NAME}" "${fbgemm_variant}"
cd "${FBGEMM_REPO}" || { echo "[NOVA] Failed to cd to ${FBGEMM_REPO} from $(pwd)"; };
test_all_fbgemm_gpu_modules "${BUILD_ENV_NAME}" "${fbgemm_variant}"

# Workaround EACCES: permission denied error at checkout step
chown -R 1000:1000 /__w/FBGEMM/FBGEMM/ || echo "Unable to chown 1000:1000 from $USER, uid: $(id -u)"
5 changes: 3 additions & 2 deletions .github/scripts/utils_base.bash
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ env_name_or_prefix () {
}

test_network_connection () {
wget -q --timeout 1 pypi.org -O /dev/null
exec_with_retries 3 wget -q --timeout 1 pypi.org -O /dev/null
local exit_status=$?

# https://man7.org/linux/man-pages/man1/wget.1.html
if [ $exit_status == 0 ]; then
echo "[CHECK] Network does not appear to be blocked."
else
echo "[CHECK] Network check exit status: ${exit_status}"
echo "[CHECK] Network appears to be blocked; please proxy the network connetions, i.e. re-run the command prefixed with 'with-proxy'."
echo "[CHECK] Network appears to be blocked or suffering from poor connection."
echo "[CHECK] Please remember to proxy the network connetions if needed, i.e. re-run the command prefixed with 'with-proxy'."
return 1
fi
}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fbgemm_gpu_ci_cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
- name: Test with PyTest
timeout-minutes: ${{ matrix.host-machine.timeout }}
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cpu
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cpu

- name: Push Wheel to PyPI
if: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to_pypi == 'true')) && matrix.compiler == 'gcc' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fbgemm_gpu_ci_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ jobs:

- name: Test with PyTest
timeout-minutes: 20
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cuda
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cuda

- name: Push Wheel to PyPI
if: ${{ (github.event_name == 'schedule' && matrix.cuda-version == matrix.cuda-version-publish) || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to_pypi == 'true' && matrix.cuda-version == matrix.cuda-version-publish) }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fbgemm_gpu_ci_rocm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,4 @@ jobs:

- name: Test with PyTest
timeout-minutes: 20
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV rocm
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV rocm
6 changes: 3 additions & 3 deletions .github/workflows/fbgemm_gpu_pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:

- name: Test with PyTest
timeout-minutes: ${{ matrix.host-machine.timeout }}
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cpu
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cpu


test_pypi_install_cuda:
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:

- name: Test with PyTest
timeout-minutes: 20
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cuda
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cuda


test_pypi_install_rocm:
Expand Down Expand Up @@ -225,4 +225,4 @@ jobs:

- name: Test with PyTest
timeout-minutes: 20
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV rocm
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV rocm
2 changes: 1 addition & 1 deletion .github/workflows/fbgemm_gpu_release_cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ jobs:
- name: Test with PyTest
timeout-minutes: ${{ matrix.host-machine.timeout }}
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cpu
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cpu

- name: Push FBGEMM_GPU (CPU version) Binary to PYPI
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to_pypi == 'true' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fbgemm_gpu_release_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ jobs:

- name: Test with PyTest
timeout-minutes: 20
run: . $PRELUDE; cd fbgemm_gpu/test; run_fbgemm_gpu_tests $BUILD_ENV cuda
run: . $PRELUDE; test_all_fbgemm_gpu_modules $BUILD_ENV cuda

- name: Push FBGEMM_GPU Binary to PYPI
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to_pypi == 'true' && matrix.cuda-version == github.event.inputs.cuda_version }}
Expand Down
28 changes: 28 additions & 0 deletions cmake/modules/CudaSetup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)


################################################################################
# CUDA Setup
################################################################################

# Set NVML_LIB_PATH if provided, or detect the default lib path
if(NOT NVML_LIB_PATH)
set(DEFAULT_NVML_LIB_PATH
"${CUDA_TOOLKIT_ROOT_DIR}/lib64/stubs/libnvidia-ml.so")

if(EXISTS ${DEFAULT_NVML_LIB_PATH})
message(STATUS "Setting NVML_LIB_PATH: \
${CUDA_TOOLKIT_ROOT_DIR}/lib64/stubs/libnvidia-ml.so")
set(NVML_LIB_PATH "${CUDA_TOOLKIT_ROOT_DIR}/lib64/stubs/libnvidia-ml.so")
endif()
endif()

if(NVML_LIB_PATH)
message(STATUS "Found NVML_LIB_PATH: ${NVML_LIB_PATH}")
endif()
83 changes: 83 additions & 0 deletions cmake/modules/CxxCompilerSetup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)


################################################################################
# CMake C++ Setup
################################################################################

# SET THE C AND C++ VERSIONS HERE
set(C_VERSION 17)
set(CXX_VERSION 20)

# Set the default C++ standard to CXX_VERSION if CMAKE_CXX_STANDARD is not
# supplied by CMake command invocation.
# Individual targets can have this value overridden; see
# https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html
# https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html
# https://cmake.org/cmake/help/latest/prop_tgt/HIP_STANDARD.html
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD ${CXX_VERSION})
set(CMAKE_HIP_STANDARD ${CXX_VERSION})
set(CXX_STANDARD ${CXX_VERSION})
set(HIP_STANDARD ${CXX_VERSION})
endif()
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(HIP_STANDARD_REQUIRED ON)

# Set the default C standard to C_VERSION if CMAKE_C_STANDARD is not supplied
# by CMake command invocation.
# Individual targets can have this value overridden; see
# https://cmake.org/cmake/help/latest/variable/CMAKE_C_STANDARD.html
# https://cmake.org/cmake/help/latest/prop_tgt/C_STANDARD.html
if(NOT CMAKE_C_STANDARD)
set(C_STANDARD ${C_VERSION})
set(CMAKE_C_STANDARD ${C_VERSION})
endif()
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(DEFINED GLIBCXX_USE_CXX11_ABI)
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
endif()

BLOCK_PRINT(
"Default C compiler flags"
"(values may be overridden by CMAKE_CXX_STANDARD and CXX_STANDARD):"
""
"${CMAKE_C_FLAGS}"
)

BLOCK_PRINT(
"Default C++ compiler flags"
"(values may be overridden by CMAKE_CXX_STANDARD and CXX_STANDARD):"
""
"${CMAKE_CXX_FLAGS}"
)

# Strip all symbols from the .SO file after building
add_link_options($<$<CONFIG:RELEASE>:-s>)

# Set flags for AVX2
set(AVX2_FLAGS "-mavx2;-mf16c;-mfma;-fopenmp")
if(NOT FBGEMM_CPU_ONLY AND WSL_MODE)
# NVCC in WSL complains about unknown -mavx options
# https://github.com/pytorch/FBGEMM/issues/2135
set(AVX2_FLAGS "-Xcompiler;-mavx;-Xcompiler;-mavx2;-Xcompiler;-mf16c;-Xcompiler;-mfma;-fopenmp")
endif()

# Set flags for AVX512
set(AVX512_FLAGS "-mavx2;-mf16c;-mfma;-mavx512f;-mavx512bw;-mavx512dq;-mavx512vl;-fopenmp")
if(NOT FBGEMM_CPU_ONLY AND WSL_MODE)
set(AVX512_FLAGS "-Xcompiler;-mavx2;-Xcompiler;-mf16c;-Xcompiler;-mfma;-Xcompiler;-mavx512f;-Xcompiler;-mavx512bw;-Xcompiler;-mavx512dq;-Xcompiler;-mavx512vl;-fopenmp")
endif()
1 change: 0 additions & 1 deletion cmake/modules/FindAVX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ MACRO(CHECK_SSE lang type flags)
ENDIF()

MARK_AS_ADVANCED(${lang}_${type}_FOUND ${lang}_${type}_FLAGS)

ENDMACRO()

CHECK_SSE(C "AVX" " ;-mavx;/arch:AVX")
Expand Down
26 changes: 26 additions & 0 deletions cmake/modules/PyTorchSetup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)


################################################################################
# PyTorch Dependencies Setup
################################################################################

find_package(Torch REQUIRED)

#
# Toch Cuda Extensions are normally compiled with the flags below. However we
# disabled -D__CUDA_NO_HALF_CONVERSIONS__ here as it caused "error: no suitable
# constructor exists to convert from "int" to "__half" errors in
# gen_embedding_forward_quantized_split_[un]weighted_codegen_cuda.cu
#

set(TORCH_CUDA_OPTIONS
--expt-relaxed-constexpr -D__CUDA_NO_HALF_OPERATORS__
# -D__CUDA_NO_HALF_CONVERSIONS__
-D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__)
38 changes: 38 additions & 0 deletions cmake/modules/RocmSetup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/Utilities.cmake)


################################################################################
# ROCm and HIPify Setup
################################################################################

if(USE_ROCM)
# Load CMake modules
list(APPEND CMAKE_MODULE_PATH
"${PROJECT_SOURCE_DIR}/cmake"
"${THIRDPARTY}/hipify_torch/cmake")
include(Hip)
include(Hipify)

# Configure compiler for HIP
list(APPEND HIP_HCC_FLAGS
" \"-Wno-#pragma-messages\" "
" \"-Wno-#warnings\" "
-Wno-cuda-compat
-Wno-deprecated-declarations
-Wno-format
-Wno-ignored-attributes
-Wno-unused-result)

BLOCK_PRINT(
"HIP found: ${HIP_FOUND}"
"HIPCC compiler flags:"
""
"${HIP_HCC_FLAGS}"
)
endif()
20 changes: 20 additions & 0 deletions cmake/modules/Utilities.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

################################################################################
# Utility Functions
################################################################################

function(BLOCK_PRINT)
message("")
message("")
message("================================================================================")
foreach(ARG IN LISTS ARGN)
message("${ARG}")
endforeach()
message("================================================================================")
message("")
endfunction()
Loading

0 comments on commit a2fdc90

Please sign in to comment.