Revert "Use std::string_view in torchgen (#157050)"

This reverts commit 064288cbab.

Reverted https://github.com/pytorch/pytorch/pull/157050 on behalf of https://github.com/jeanschmidt due to Seems to have broken internal builds, more details on D77449943. @ezyang may I count on your help to get those changes merged? ([comment](https://github.com/pytorch/pytorch/pull/157050#issuecomment-3020222668))
This commit is contained in:
PyTorch MergeBot 2025-06-30 18:08:54 +00:00
parent efbf07e7ea
commit d5e6f42094
8 changed files with 12 additions and 16 deletions

View File

@ -675,6 +675,8 @@ def generate_tensor_like_override_tests(cls):
return None return None
elif arg_type == "ScalarType": elif arg_type == "ScalarType":
return torch.float32 return torch.float32
elif arg_type == "c10::string_view":
return ""
elif arg_type in ("std::string_view", "::std::string_view"): elif arg_type in ("std::string_view", "::std::string_view"):
return "" return ""
elif arg_type == "SymInt": elif arg_type == "SymInt":

View File

@ -969,7 +969,7 @@ def saved_variables(
if nctype.type == OptionalCType(BaseCType(stringT)): if nctype.type == OptionalCType(BaseCType(stringT)):
formula = re.sub( formula = re.sub(
rf"\b{name}\b", rf"\b{name}\b",
f"{name}.has_value() ? std::optional<::std::string_view>({name}.value()) : std::nullopt", f"{name}.has_value() ? std::optional<std::string_view>({name}.value()) : std::nullopt",
formula, formula,
) )

View File

@ -46,6 +46,7 @@ static std::unordered_map<std::string, ParameterType> type_map = {
{"DeviceIndex", ParameterType::INT64}, {"DeviceIndex", ParameterType::INT64},
{"Stream", ParameterType::STREAM}, {"Stream", ParameterType::STREAM},
{"std::string", ParameterType::STRING}, {"std::string", ParameterType::STRING},
{"c10::string_view", ParameterType::STRING},
{"std::string_view", ParameterType::STRING}, {"std::string_view", ParameterType::STRING},
{"::std::string_view", ParameterType::STRING}, {"::std::string_view", ParameterType::STRING},
{"Dimname", ParameterType::DIMNAME}, {"Dimname", ParameterType::DIMNAME},

View File

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

View File

@ -52,7 +52,7 @@ float8_e5m2fnuzT = BaseCppType("at", "Float8_e5m2fnuz")
float8_e4m3fnT = BaseCppType("at", "Float8_e4m3fn") float8_e4m3fnT = BaseCppType("at", "Float8_e4m3fn")
float8_e4m3fnuzT = BaseCppType("at", "Float8_e4m3fnuz") float8_e4m3fnuzT = BaseCppType("at", "Float8_e4m3fnuz")
float8_e8m0fnuT = BaseCppType("at", "Float8_e8m0fnu") float8_e8m0fnuT = BaseCppType("at", "Float8_e8m0fnu")
stringT = BaseCppType("::std", "string_view") stringT = BaseCppType("c10", "string_view")
generatorT = BaseCppType("at", "Generator") generatorT = BaseCppType("at", "Generator")
scalarTypeT = BaseCppType("at", "ScalarType") scalarTypeT = BaseCppType("at", "ScalarType")
tensorT = BaseCppType("at", "Tensor") tensorT = BaseCppType("at", "Tensor")

View File

@ -256,11 +256,7 @@ class GenLazyIR(ABC):
[ [
# This code is just special casing the mapping from string_view -> strings # 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)" f"{a.name}({a.name}.has_value() ? ::std::make_optional(std::string(*{a.name})) : ::std::nullopt)"
if a.lazy_type.cpp_type() if a.lazy_type.cpp_type() == "::std::optional<c10::string_view>"
in (
"::std::optional<::std::string_view>",
"::std::optional<std::string_view>",
)
else f"{a.name}({a.name})" else f"{a.name}({a.name})"
for a in scalar_args for a in scalar_args
] ]
@ -270,13 +266,9 @@ class GenLazyIR(ABC):
scalar_decls = "\n ".join( scalar_decls = "\n ".join(
[ [
f"std::string {a.name};" f"std::string {a.name};"
if a.lazy_type.cpp_type() in ("::std::string_view", "std::string_view") if a.lazy_type.cpp_type() == "c10::string_view"
else f"::std::optional<std::string> {a.name};" else f"::std::optional<std::string> {a.name};"
if a.lazy_type.cpp_type() if a.lazy_type.cpp_type() == "::std::optional<c10::string_view>"
in (
"::std::optional<::std::string_view>",
"::std::optional<std::string_view>",
)
else f"{a.lazy_type.cpp_type()} {a.name};" else f"{a.lazy_type.cpp_type()} {a.name};"
for a in scalar_args for a in scalar_args
] ]

View File

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

View File

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