[Bugfix][Build] Patch stable string memleak fix from 2.14 for 2.13 (#51185)

Signed-off-by: Jane Xu <janeyx@meta.com> Signed-off-by: Shengqi Chen <harry-chen@outlook.com> Co-authored-by: Shengqi Chen <harry-chen@outlook.com>

Jane (Yuan) Xu committed Aug 9, 2026 at 00:12 UTC f8d03e77416bf90c49acbe50e233275722f02c4b
2 files changed +43
CMakeLists.txt
+28
@@ -395,6 +395,34 @@ if(VLLM_GPU_LANG STREQUAL "CUDA" OR VLLM_GPU_LANG STREQUAL "HIP")
395 #
396 # _C_stable_libtorch extension (ops registered via STABLE_TORCH_LIBRARY)
397 #
398 + if(Torch_VERSION VERSION_GREATER_EQUAL "2.13" AND
399 + Torch_VERSION VERSION_LESS "2.14")
400 + # hotfix for https://github.com/pytorch/pytorch/pull/190493
401 + # delete after we bump to 2.14
402 + list(GET TORCH_INCLUDE_DIRS 0 TORCH_INCLUDE_DIR)
403 + set(_stable_hdr_rel "torch/csrc/stable/stableivalue_conversions.h")
404 + file(READ "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}"
405 + TORCH_STABLE_STRING_CONTENTS)
406 +
407 + if(TORCH_STABLE_STRING_CONTENTS MATCHES "auto strptr = new std::string")
408 + set(TORCH_SHADOW_INCLUDE_DIR
409 + "${CMAKE_CURRENT_BINARY_DIR}/torch_patched_headers")
410 +
411 + file(MAKE_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}/torch/csrc/stable")
412 + file(COPY_FILE
413 + "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}"
414 + "${TORCH_SHADOW_INCLUDE_DIR}/${_stable_hdr_rel}")
415 +
416 + execute_process(
417 + COMMAND patch --batch --forward -p1
418 + "--input=${CMAKE_CURRENT_LIST_DIR}/cmake/patches/pytorch_stable_string.patch"
419 + WORKING_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}"
420 + COMMAND_ERROR_IS_FATAL ANY)
421 +
422 + include_directories(BEFORE "${TORCH_SHADOW_INCLUDE_DIR}")
423 + endif()
424 + endif()
425 +
426 set(VLLM_STABLE_EXT_SRC
427 "csrc/libtorch_stable/torch_bindings.cpp"
428 "csrc/libtorch_stable/cuda_view.cu"
cmake/patches/pytorch_stable_string.patch new
+15
@@ -0,0 +1,15 @@
1 +diff --git a/torch/csrc/stable/stableivalue_conversions.h b/torch/csrc/stable/stableivalue_conversions.h
2 +--- a/torch/csrc/stable/stableivalue_conversions.h
3 ++++ b/torch/csrc/stable/stableivalue_conversions.h
4 +@@ -807 +807 @@
5 +-// Returns a new std::string; the string in val is deleted.
6 ++// Returns a std::string which owns its memory; the string in val is deleted.
7 +@@ -819 +819 @@
8 +- auto strptr = new std::string(data, length);
9 ++ const auto str = std::string(data, length);
10 +@@ -821 +821 @@
11 +- // delete the old string before returning new string
12 ++ // delete the old string
13 +@@ -823 +823 @@
14 +- return *strptr;
15 ++ return str;