From deb7ebe0a399b982ee48001713320a0bac792261 Mon Sep 17 00:00:00 2001 From: PyTorch MergeBot Date: Sat, 13 Sep 2025 07:52:50 +0000 Subject: [PATCH] Revert "[Reland] Use std::string_view in torchgen (#158625)" This reverts commit 972e409829343cc2062aeee0994a9c1c735d216a. Reverted https://github.com/pytorch/pytorch/pull/158625 on behalf of https://github.com/huydhn due to Sorry for reverting your change but it seems to break a couple of ExecuTorch tests for Vulkan backend ([comment](https://github.com/pytorch/pytorch/pull/158625#issuecomment-3287754275)) --- test/test_overrides.py | 2 ++ tools/autograd/load_derivatives.py | 2 +- torch/csrc/utils/python_arg_parser.cpp | 1 + torchgen/api/python.py | 2 +- torchgen/api/types/types.py | 2 +- torchgen/api/types/types_base.py | 2 -- torchgen/dest/lazy_ir.py | 6 +++--- torchgen/static_runtime/generator.py | 3 ++- 8 files changed, 11 insertions(+), 9 deletions(-) diff --git a/test/test_overrides.py b/test/test_overrides.py index 2a4f244bad11..8454677856d0 100644 --- a/test/test_overrides.py +++ b/test/test_overrides.py @@ -940,6 +940,8 @@ def generate_tensor_like_override_tests(cls): return None elif arg_type == "ScalarType": return torch.float32 + elif arg_type == "c10::string_view": + return "" elif arg_type in ("std::string_view", "::std::string_view"): return "" elif arg_type == "SymInt": diff --git a/tools/autograd/load_derivatives.py b/tools/autograd/load_derivatives.py index 28fddf0fb851..f61226f25fb9 100644 --- a/tools/autograd/load_derivatives.py +++ b/tools/autograd/load_derivatives.py @@ -969,7 +969,7 @@ def saved_variables( if nctype.type == OptionalCType(BaseCType(stringT)): formula = re.sub( rf"\b{name}\b", - f"{name}.has_value() ? std::optional<::std::string_view>({name}.value()) : std::nullopt", + f"{name}.has_value() ? std::optional({name}.value()) : std::nullopt", formula, ) diff --git a/torch/csrc/utils/python_arg_parser.cpp b/torch/csrc/utils/python_arg_parser.cpp index d801c7f730b0..613657e03b92 100644 --- a/torch/csrc/utils/python_arg_parser.cpp +++ b/torch/csrc/utils/python_arg_parser.cpp @@ -46,6 +46,7 @@ static std::unordered_map type_map = { {"DeviceIndex", ParameterType::INT64}, {"Stream", ParameterType::STREAM}, {"std::string", ParameterType::STRING}, + {"c10::string_view", ParameterType::STRING}, {"std::string_view", ParameterType::STRING}, {"::std::string_view", ParameterType::STRING}, {"Dimname", ParameterType::DIMNAME}, diff --git a/torchgen/api/python.py b/torchgen/api/python.py index 0c5b9ad5e7b4..dbfa73060163 100644 --- a/torchgen/api/python.py +++ b/torchgen/api/python.py @@ -683,7 +683,7 @@ def argument_type_str( elif t.name == BaseTy.float: return "double" elif t.name == BaseTy.str: - return "std::string_view" + return "c10::string_view" elif t.name in [ BaseTy.Tensor, BaseTy.bool, diff --git a/torchgen/api/types/types.py b/torchgen/api/types/types.py index 97724384c2a2..41c05653fffd 100644 --- a/torchgen/api/types/types.py +++ b/torchgen/api/types/types.py @@ -52,7 +52,7 @@ float8_e5m2fnuzT = BaseCppType("at", "Float8_e5m2fnuz") float8_e4m3fnT = BaseCppType("at", "Float8_e4m3fn") float8_e4m3fnuzT = BaseCppType("at", "Float8_e4m3fnuz") float8_e8m0fnuT = BaseCppType("at", "Float8_e8m0fnu") -stringT = BaseCppType("std", "string_view") +stringT = BaseCppType("c10", "string_view") generatorT = BaseCppType("at", "Generator") scalarTypeT = BaseCppType("at", "ScalarType") tensorT = BaseCppType("at", "Tensor") diff --git a/torchgen/api/types/types_base.py b/torchgen/api/types/types_base.py index 2288ebce7183..08085fa0fa2b 100644 --- a/torchgen/api/types/types_base.py +++ b/torchgen/api/types/types_base.py @@ -81,8 +81,6 @@ class BaseCType(CType): type: BaseCppType def cpp_type(self, *, strip_ref: bool = False) -> str: - if self.type.ns == "std": - return "::" + str(self.type) return str(self.type) def remove_const_ref(self) -> CType: diff --git a/torchgen/dest/lazy_ir.py b/torchgen/dest/lazy_ir.py index 6231a36d5d46..b912b8f2427f 100644 --- a/torchgen/dest/lazy_ir.py +++ b/torchgen/dest/lazy_ir.py @@ -256,7 +256,7 @@ class GenLazyIR(ABC): [ # This code is just special casing the mapping from string_view -> strings f"{a.name}({a.name}.has_value() ? ::std::make_optional(std::string(*{a.name})) : ::std::nullopt)" - if a.lazy_type.cpp_type() == "::std::optional<::std::string_view>" + if a.lazy_type.cpp_type() == "::std::optional" else f"{a.name}({a.name})" for a in scalar_args ] @@ -266,9 +266,9 @@ class GenLazyIR(ABC): scalar_decls = "\n ".join( [ f"std::string {a.name};" - if a.lazy_type.cpp_type() == "::std::string_view" + if a.lazy_type.cpp_type() == "c10::string_view" else f"::std::optional {a.name};" - if a.lazy_type.cpp_type() == "::std::optional<::std::string_view>" + if a.lazy_type.cpp_type() == "::std::optional" else f"{a.lazy_type.cpp_type()} {a.name};" for a in scalar_args ] diff --git a/torchgen/static_runtime/generator.py b/torchgen/static_runtime/generator.py index a9814bd4dee1..8ad2fd3c4588 100644 --- a/torchgen/static_runtime/generator.py +++ b/torchgen/static_runtime/generator.py @@ -323,7 +323,8 @@ def ivalue_type_conversion_method( ), BaseTy.str: ( (False, "toStringView()"), - (False, "toOptional()"), + (False, "toOptional()"), + (False, "toOptional<::std::string_view>()"), ), }