Use std::string_view in torchgen (#157050)

Let the generated code use std::sv

Pull Request resolved: https://github.com/pytorch/pytorch/pull/157050
Approved by: https://github.com/ezyang
This commit is contained in:
cyy
2025-06-27 06:36:06 +00:00
committed by PyTorch MergeBot
parent cc3ea2d840
commit 064288cbab
8 changed files with 16 additions and 12 deletions

View File

@ -683,7 +683,7 @@ def argument_type_str(
elif t.name == BaseTy.float:
return "double"
elif t.name == BaseTy.str:
return "c10::string_view"
return "std::string_view"
elif t.name in [
BaseTy.Tensor,
BaseTy.bool,

View File

@ -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("c10", "string_view")
stringT = BaseCppType("::std", "string_view")
generatorT = BaseCppType("at", "Generator")
scalarTypeT = BaseCppType("at", "ScalarType")
tensorT = BaseCppType("at", "Tensor")

View File

@ -256,7 +256,11 @@ 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<c10::string_view>"
if a.lazy_type.cpp_type()
in (
"::std::optional<::std::string_view>",
"::std::optional<std::string_view>",
)
else f"{a.name}({a.name})"
for a in scalar_args
]
@ -266,9 +270,13 @@ class GenLazyIR(ABC):
scalar_decls = "\n ".join(
[
f"std::string {a.name};"
if a.lazy_type.cpp_type() == "c10::string_view"
if a.lazy_type.cpp_type() in ("::std::string_view", "std::string_view")
else f"::std::optional<std::string> {a.name};"
if a.lazy_type.cpp_type() == "::std::optional<c10::string_view>"
if a.lazy_type.cpp_type()
in (
"::std::optional<::std::string_view>",
"::std::optional<std::string_view>",
)
else f"{a.lazy_type.cpp_type()} {a.name};"
for a in scalar_args
]

View File

@ -54,7 +54,7 @@ base_type_to_aten_type = {
BaseTy.SymInt: "c10::SymInt",
BaseTy.Scalar: "c10::Scalar",
BaseTy.float: "double",
BaseTy.str: "::std::string_view",
BaseTy.str: "std::string_view",
BaseTy.DeviceIndex: "c10::DeviceIndex",
BaseTy.Layout: "c10::Layout",
BaseTy.MemoryFormat: "c10::MemoryFormat",

View File

@ -323,8 +323,7 @@ def ivalue_type_conversion_method(
),
BaseTy.str: (
(False, "toStringView()"),
(False, "toOptional<c10::string_view>()"),
(False, "toOptional<::std::string_view>()"),
(False, "toOptional<std::string_view>()"),
),
}