Remove c10::either (#109299)

We can replace it with std::variant.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/109299
Approved by: https://github.com/colesbury, https://github.com/ezyang
This commit is contained in:
cyy
2023-09-15 19:34:31 +00:00
committed by PyTorch MergeBot
parent cc03e3a892
commit 9d297cc773
10 changed files with 181 additions and 1620 deletions

View File

@ -14,12 +14,9 @@
using at::TypeKind;
using c10::Argument;
using c10::either;
using c10::FunctionSchema;
using c10::IValue;
using c10::ListType;
using c10::make_left;
using c10::make_right;
using c10::OperatorName;
using c10::OptionalType;
@ -36,13 +33,13 @@ struct SchemaParser {
Source::DONT_COPY)),
type_parser(L, /*parse_complete_tensor_types*/ false) {}
either<OperatorName, FunctionSchema> parseDeclaration() {
std::variant<OperatorName, FunctionSchema> parseDeclaration() {
OperatorName name = parseName();
// If there is no parentheses coming, then this is just the operator name
// without an argument list
if (L.cur().kind != '(') {
return make_left<OperatorName, FunctionSchema>(std::move(name));
return OperatorName(std::move(name));
}
std::vector<Argument> arguments;
@ -97,7 +94,7 @@ struct SchemaParser {
parseArgument(0, /*is_return=*/true, /*kwarg_only=*/false));
}
return make_right<OperatorName, FunctionSchema>(
return FunctionSchema(
std::move(name.name),
std::move(name.overload_name),
std::move(arguments),
@ -130,16 +127,16 @@ struct SchemaParser {
return {name, overload_name};
}
std::vector<either<OperatorName, FunctionSchema>> parseDeclarations() {
std::vector<either<OperatorName, FunctionSchema>> results;
std::vector<std::variant<OperatorName, FunctionSchema>> parseDeclarations() {
std::vector<std::variant<OperatorName, FunctionSchema>> results;
do {
results.push_back(parseDeclaration());
results.emplace_back(parseDeclaration());
} while (L.nextIf(TK_NEWLINE));
L.expect(TK_EOF);
return results;
}
either<OperatorName, FunctionSchema> parseExactlyOneDeclaration() {
std::variant<OperatorName, FunctionSchema> parseExactlyOneDeclaration() {
auto result = parseDeclaration();
L.nextIf(TK_NEWLINE);
L.expect(TK_EOF);
@ -368,7 +365,7 @@ struct SchemaParser {
};
} // namespace
either<OperatorName, FunctionSchema> parseSchemaOrName(
std::variant<OperatorName, FunctionSchema> parseSchemaOrName(
const std::string& schemaOrName) {
return SchemaParser(schemaOrName).parseExactlyOneDeclaration();
}
@ -376,17 +373,17 @@ either<OperatorName, FunctionSchema> parseSchemaOrName(
FunctionSchema parseSchema(const std::string& schema) {
auto parsed = parseSchemaOrName(schema);
TORCH_CHECK(
parsed.is_right(),
std::holds_alternative<FunctionSchema>(parsed),
"Tried to parse a function schema but only the operator name was given");
return std::move(parsed.right());
return std::get<FunctionSchema>(std::move(parsed));
}
OperatorName parseName(const std::string& name) {
auto parsed = parseSchemaOrName(name);
TORCH_CHECK(
parsed.is_left(),
std::holds_alternative<OperatorName>(parsed),
"Tried to parse an operator name but function schema was given");
return std::move(parsed.left());
return std::get<OperatorName>(std::move(parsed));
}
} // namespace torch::jit