Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

## 编译与安装

`FastPT` 的 wheel 会构建集成的 `torch-mocker`,因此构建环境必须同时具备 DTK、已安装的目标 HIP torch package 和同版本 HIP torch 源码。`TORCH_PATH` 必须指向干净的 HIP torch 安装目录,不能是已经被 FastPT `-C` 覆盖过的 torch。
`FastPT` 的 wheel 会构建集成的 `torch-mocker`,因此构建环境必须具备 DTK、已安装的目标 HIP torch package,以及 CMake、GNU Make 和可用的编译器。使用的 torch 应为干净的目标 HIP torch package,不能是已经被 FastPT `-C` 覆盖过的 torch。

构建前确认:

- FastPT 分支、已安装 torch 和 `HIP_TORCH_PATH` 对应同一 PyTorch 版本;
- FastPT 分支与已安装 torch 对应同一 PyTorch 版本;
- 已安装 `wheel`、CMake、GNU Make 和可用的 DTK 编译环境;
- 当前环境未安装过 `FastPT wheel`。检查torch的安装路径下, torch/lib 不存在 libtorch_cuda.so、libc10_cuda.so、libtorch_mocker.so,并确认`torch/utils/cpp_extension.py` 不含 USE_FASTPT_CUDA 等 FastPT 路由。

Expand All @@ -21,16 +21,11 @@
cd /path/to/fastpt
source /opt/dtk/cuda/env.sh

# 必须是干净的目标 HIP torch package,
export TORCH_PATH="$(python3 -c 'import os, torch; print(os.path.dirname(torch.__file__))')"
# /path/to/pytorch: 为HCU适配的pytorch的源码,注意版本分支。
export HIP_TORCH_PATH=/path/to/pytorch

git submodule update --init --recursive
rm -rf build dist fastpt.egg-info torch-mocker/build

# FASTPT_CTEST可以控制CTest部分的编译,默认为 1:构建 torch-mocker 和已注册的 CTest 可执行文件,不需要Ctest可以设置为0.
python3 setup.py bdist_wheel
# FASTPT_CTEST 默认为 1;设置为 0 可跳过 CTest 可执行文件构建。
FASTPT_CTEST=1 python3 setup.py bdist_wheel
```

构建产物位于 `dist/fastpt-*.whl`。若仅需要构建发布 wheel,且已通过后文的独立 CTest,可设置 `FASTPT_CTEST=0` 跳过测试可执行文件构建。
Expand All @@ -44,7 +39,7 @@ cd /path/to/fastpt
FASTPT_HIPIFY_ONLY=1 python3 setup.py bdist_wheel
```

该模式不需要设置 `TORCH_PATH` 或 `HIP_TORCH_PATH`,但仍需要可导入的匹配 HCU/HIP torch,以生成包版本元数据。它不构建或打包 `libc10_cuda.so`、`libtorch_cuda.so`、`libtorch_mocker.so`、torch overlay 和 `fastpt -C/-E` 环境脚本,只能用于源码/CMake 转码, `CUDAExtension`、`CppExtension`的扩展编译,不能用于基于GPUFusion的不转码 编译或运行环境初始化。
该模式不需要设置 `TORCH_PATH`,但仍需要可导入的匹配 HCU/HIP torch,以生成包版本元数据。它不构建或打包 `libc10_cuda.so`、`libtorch_cuda.so`、`libtorch_mocker.so`、torch overlay 和 `fastpt -C/-E` 环境脚本,只能用于源码/CMake 转码, `CUDAExtension`、`CppExtension`的扩展编译,不能用于基于GPUFusion的不转码 编译或运行环境初始化。

### 安装与验证

Expand All @@ -59,17 +54,18 @@ fastpt.hipify.file --help

### CTest 测试

`FASTPT_CTEST`不设置时默认为1,会编译CTest测试文件,可通过以下方式进行`torch-mocker`部分的测试:
`FASTPT_CTEST` 不设置时默认为 1,会构建 `torch-mocker` 的 CTest 可执行文件。wheel 构建后可使用以下命令运行测试:

```bash
export LD_LIBRARY_PATH="$PWD/torch-mocker/build:$TORCH_PATH/lib:${LD_LIBRARY_PATH:-}"
TORCH_LIB_DIR="$(python3 -c 'import os, torch; print(os.path.join(os.path.dirname(torch.__file__), "lib"))')"
export LD_LIBRARY_PATH="$PWD/torch-mocker/build:$TORCH_LIB_DIR:${LD_LIBRARY_PATH:-}"
ctest --test-dir torch-mocker/build --output-on-failure -j1
```

使用 `ctest --test-dir torch-mocker/build_ctest -N` 可列出已注册测试。定位单个失败时,使用测试名筛选,例如:
使用 `ctest --test-dir torch-mocker/build -N` 可列出已注册测试。定位单个失败时,使用测试名筛选,例如:

```bash
ctest --test-dir torch-mocker/build_ctest --output-on-failure \
ctest --test-dir torch-mocker/build --output-on-failure \
-R '^torch_mocker\.c10\.cuda_functions$'
```

Expand Down
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
try:
import torch
torch_install_path = os.path.dirname(torch.__file__)
# CMake consumes TORCH_PATH from its environment. Derive it from the
# imported torch package so callers do not need to export it manually.
os.environ["TORCH_PATH"] = torch_install_path
except ImportError as e:
raise("Torch is required")
raise RuntimeError("Torch is required") from e

torch_version = torch.__version__

Expand Down Expand Up @@ -49,12 +52,9 @@ def run(self):
"from the fastpt repository root before building."
)

HIP_TORCH_PATH = os.environ.get("HIP_TORCH_PATH", "")
if len(HIP_TORCH_PATH) == 0:
raise("Environment variable HIP_TORCH_PATH is required, please set the torch source dir such as: export HIP_TORCH_PATH=/path/to/pytorch ")
torch_lib_path = os.environ.get("TORCH_PATH", "")
if len(torch_lib_path) == 0:
raise("Environment variable TORCH_PATH is required, please set the torch source dir such as: export TORCH_PATH=/path/to/site-packages/torch ")
torch_lib_path = torch_install_path
if not os.path.isdir(torch_lib_path):
raise RuntimeError(f"Torch installation directory does not exist: {torch_lib_path}")
cmake_args = os.environ.get("TORCH_MOCKER_CMAKE_ARGS", "")
make_args = "-j8 VERBOSE=1"
make_args_env = os.environ.get("TORCH_MOCKER_MAKE_ARGS", "")
Expand Down
56 changes: 41 additions & 15 deletions torch-mocker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ project(Torch-Mocker-Test LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(CUDA REQUIRED)
find_package(Python3 COMPONENTS Interpreter REQUIRED)

set(TORCH_PATH "" CACHE PATH "Path to the installed torch package")
if(NOT TORCH_PATH)
if(DEFINED ENV{TORCH_PATH} AND NOT "$ENV{TORCH_PATH}" STREQUAL "")
set(TORCH_PATH "$ENV{TORCH_PATH}")
else()
execute_process(
COMMAND "${Python3_EXECUTABLE}" -c
"import os, torch; print(os.path.dirname(torch.__file__))"
RESULT_VARIABLE torch_path_result
OUTPUT_VARIABLE TORCH_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE torch_path_error
)
if(NOT torch_path_result EQUAL 0)
message(FATAL_ERROR
"Unable to determine the installed torch path: ${torch_path_error}")
endif()
endif()
endif()

get_filename_component(TORCH_PATH "${TORCH_PATH}" ABSOLUTE)
set(TORCH_PATH "${TORCH_PATH}" CACHE PATH "Path to the installed torch package" FORCE)
if(NOT EXISTS "${TORCH_PATH}/include")
message(FATAL_ERROR
"Invalid TORCH_PATH '${TORCH_PATH}': torch headers were not found")
endif()
set(ENV{TORCH_PATH} "${TORCH_PATH}")
message(STATUS "Using torch package: ${TORCH_PATH}")

# fmt is a transitive dependency of torch_cuda. Configure the vendored
# project before this legacy build sets nvcc as the host C++ compiler.
Expand Down Expand Up @@ -51,12 +81,12 @@ include(GNUInstallDirs)
find_package(Python3 COMPONENTS Development REQUIRED)
include_directories(${Python3_INCLUDE_DIRS})
link_directories(${Python3_LIBRARY_DIRS})
find_package(Python3 COMPONENTS Interpreter REQUIRED)
file(GLOB_RECURSE Torch_CUDA_SRCS "aten/src/ATen/*.cpp")
list(FILTER Torch_CUDA_SRCS EXCLUDE REGEX "aten/src/ATen/cuda/nvrtc_stub/*ATenNVRTC.cpp")
file(GLOB NVRTC_SRCS "aten/src/ATen/cuda/nvrtc_stub/ATenNVRTC.cpp")
file(GLOB_RECURSE Torch_CUDA_SRCS2 "torch/csrc/*.cpp" "torch/csrc/*.cu")
list(APPEND Torch_CUDA_SRCS ${Torch_CUDA_SRCS} ${Torch_CUDA_SRCS2})

file(GLOB C10_CUDA_SRCS1 "c10/cuda/*.cpp")
file(GLOB C10_CUDA_SRCS2 "c10/cuda/impl/*.cpp")
file(GLOB C10_CUDA_SRCS3 "c10/util/*.cpp")
Expand All @@ -67,12 +97,10 @@ file(GLOB Torch_MOCKER_VLOG "torch_mocker/utils/*.cpp")
# build libcaffe2_nvrtc.so
#add_library(caffe2_nvrtc SHARED ${NVRTC_SRCS})
#target_link_libraries(caffe2_nvrtc PRIVATE hiprtc)
#target_include_directories(caffe2_nvrtc PRIVATE $ENV{HIP_TORCH_PATH})
#target_compile_definitions(caffe2_nvrtc PRIVATE "-D__HIP_PLATFORM_AMD__")

add_library(torch_mocker SHARED ${Torch_MOCKER_SRCS})
add_library(torch_cuda SHARED ${Torch_CUDA_SRCS})
target_include_directories(torch_cuda PRIVATE $ENV{HIP_TORCH_PATH})
add_library(c10_cuda SHARED ${C10_CUDA_SRCS} ${Torch_MOCKER_VLOG})

# Encode DTK MAJOR.MINOR.PATCH as MAJOR * 10000 + MINOR * 100 + PATCH.
Expand Down Expand Up @@ -119,11 +147,11 @@ add_dependencies(torch_cuda torch_mocker c10_cuda)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include/aten/src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/aten/src)
include_directories(SYSTEM $ENV{TORCH_PATH}/include)
include_directories(SYSTEM ${TORCH_PATH}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/torch_mocker)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/cudnn-frontend/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/cudnn-frontend/include/cudnn_frontend/thirdparty)
include_directories(SYSTEM $ENV{TORCH_PATH}/include/torch/csrc/api/include)
include_directories(SYSTEM ${TORCH_PATH}/include/torch/csrc/api/include)

add_definitions(-DTORCH_USE_CUDA_DSA)
add_definitions(-DPYTORCH_C10_DRIVER_API_SUPPORTED)
Expand All @@ -132,24 +160,22 @@ add_definitions(-DPYTORCH_C10_DRIVER_API_SUPPORTED)
#add_definitions("-DC10_USING_CUSTOM_GENERATED_MACROS")
target_compile_definitions(torch_mocker PRIVATE "-D__CUDA_HIP_PLATFORM_AMD__")
target_compile_definitions(torch_mocker PRIVATE "-D__HIP_PLATFORM_AMD__")
target_link_libraries(torch_mocker PUBLIC $ENV{TORCH_PATH}/lib/libtorch_hip.so)
target_link_libraries(torch_mocker PUBLIC ${TORCH_PATH}/lib/libtorch_hip.so)
target_compile_definitions(torch_cuda PUBLIC "-DC10_USING_CUSTOM_GENERATED_MACROS")
#target_include_directories(torch_cuda PRIVATE $ENV{TORCH_PATH}/include)
target_include_directories(c10_cuda PRIVATE $ENV{HIP_TORCH_PATH})
target_compile_definitions(c10_cuda PRIVATE "-D__CUDA_HIP_PLATFORM_AMD__")
target_compile_definitions(c10_cuda PRIVATE "-D__HIP_PLATFORM_AMD__")
target_compile_definitions(c10_cuda PUBLIC "-DC10_USING_CUSTOM_GENERATED_MACROS")

target_link_libraries(torch_cuda PUBLIC torch_mocker)
message("$ENV{TORCH_PATH}/lib/libc10_hip.so")
target_link_libraries(c10_cuda PUBLIC $ENV{TORCH_PATH}/lib/libc10_hip.so cublasLt torch_mocker)
message("${TORCH_PATH}/lib/libc10_hip.so")
target_link_libraries(c10_cuda PUBLIC ${TORCH_PATH}/lib/libc10_hip.so cublasLt torch_mocker)

target_include_directories(torch_cuda PRIVATE $ENV{HIP_TORCH_PATH})
target_compile_definitions(torch_cuda PRIVATE "-D__HIP_PLATFORM_AMD__")
target_compile_definitions(torch_cuda PUBLIC "-DC10_USING_CUSTOM_GENERATED_MACROS")
if(USE_GLOG)
target_compile_definitions(torch_cuda PUBLIC "-DC10_USE_GLOG")
set(TORCH_LIBS_DIR $ENV{TORCH_PATH}/../torch.libs)
set(TORCH_LIBS_DIR ${TORCH_PATH}/../torch.libs)
if(EXISTS ${TORCH_LIBS_DIR})
file(GLOB GLOG_LIB "${TORCH_LIBS_DIR}/libglog-*.so*")
if(NOT GLOG_LIB)
Expand All @@ -163,14 +189,14 @@ if(USE_GLOG)
endif()
endif()
target_link_libraries(torch_cuda PUBLIC torch_mocker)
target_link_libraries(torch_cuda PUBLIC $ENV{TORCH_PATH}/lib/libtorch_hip.so)
target_link_libraries(torch_cuda PUBLIC $ENV{TORCH_PATH}/lib/libtorch_python.so)
target_link_libraries(torch_cuda PUBLIC ${TORCH_PATH}/lib/libtorch_hip.so)
# target_link_libraries(torch_cuda PUBLIC ${TORCH_PATH}/lib/libtorch_python.so)
target_link_directories(torch_cuda PUBLIC ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
target_link_libraries(torch_cuda PUBLIC cuda nvrtc cudnn_cnn_infer cublas cublasLt cufft cusparse cudnn cudnn_cnn_infer cudnn_adv_infer nvToolsExt fmt)
#target_link_libraries(torch_cuda PUBLIC cuda nvrtc cudnn_cnn_infer cublas cublasLt cufft cusparse cudnn cudnn_cnn_infer cudnn_adv_infer)
target_link_libraries(torch_cuda PUBLIC c10_cuda)
target_link_libraries(torch_cuda PUBLIC ${Python3_LIBRARIES})
message("$ENV{TORCH_PATH}/lib/libtorch_cuda.so")
# target_link_libraries(torch_cuda PUBLIC ${Python3_LIBRARIES})
message("${TORCH_PATH}/lib/libtorch_cuda.so")

set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++20")
include(CTest)
Expand Down
Loading