diff --git a/CMakeLists.txt b/CMakeLists.txt index 54374c2d01..bcc0fc53d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -395,6 +395,34 @@ if(VLLM_GPU_LANG STREQUAL "CUDA" OR VLLM_GPU_LANG STREQUAL "HIP") # # _C_stable_libtorch extension (ops registered via STABLE_TORCH_LIBRARY) # + if(Torch_VERSION VERSION_GREATER_EQUAL "2.13" AND + Torch_VERSION VERSION_LESS "2.14") + # hotfix for https://github.com/pytorch/pytorch/pull/190493 + # delete after we bump to 2.14 + list(GET TORCH_INCLUDE_DIRS 0 TORCH_INCLUDE_DIR) + set(_stable_hdr_rel "torch/csrc/stable/stableivalue_conversions.h") + file(READ "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}" + TORCH_STABLE_STRING_CONTENTS) + + if(TORCH_STABLE_STRING_CONTENTS MATCHES "auto strptr = new std::string") + set(TORCH_SHADOW_INCLUDE_DIR + "${CMAKE_CURRENT_BINARY_DIR}/torch_patched_headers") + + file(MAKE_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}/torch/csrc/stable") + file(COPY_FILE + "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}" + "${TORCH_SHADOW_INCLUDE_DIR}/${_stable_hdr_rel}") + + execute_process( + COMMAND patch --batch --forward -p1 + "--input=${CMAKE_CURRENT_LIST_DIR}/cmake/patches/pytorch_stable_string.patch" + WORKING_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}" + COMMAND_ERROR_IS_FATAL ANY) + + include_directories(BEFORE "${TORCH_SHADOW_INCLUDE_DIR}") + endif() + endif() + set(VLLM_STABLE_EXT_SRC "csrc/libtorch_stable/torch_bindings.cpp" "csrc/libtorch_stable/cuda_view.cu" diff --git a/cmake/patches/pytorch_stable_string.patch b/cmake/patches/pytorch_stable_string.patch new file mode 100644 index 0000000000..bcb3628571 --- /dev/null +++ b/cmake/patches/pytorch_stable_string.patch @@ -0,0 +1,15 @@ +diff --git a/torch/csrc/stable/stableivalue_conversions.h b/torch/csrc/stable/stableivalue_conversions.h +--- a/torch/csrc/stable/stableivalue_conversions.h ++++ b/torch/csrc/stable/stableivalue_conversions.h +@@ -807 +807 @@ +-// Returns a new std::string; the string in val is deleted. ++// Returns a std::string which owns its memory; the string in val is deleted. +@@ -819 +819 @@ +- auto strptr = new std::string(data, length); ++ const auto str = std::string(data, length); +@@ -821 +821 @@ +- // delete the old string before returning new string ++ // delete the old string +@@ -823 +823 @@ +- return *strptr; ++ return str;