Add Value::isValidName method. (#17372)
Summary:
The method will be used in IRParser and in NetDef converter.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17372
Differential Revision: D14172494
Pulled By: ZolotukhinM
fbshipit-source-id: 96cae8422bc73c3c2eb27524f44ec1ee8cae92f3
diff --git a/torch/csrc/jit/ir.cpp b/torch/csrc/jit/ir.cpp
index 069d2d8..54f31fd 100644
--- a/torch/csrc/jit/ir.cpp
+++ b/torch/csrc/jit/ir.cpp
@@ -653,10 +653,23 @@
return name_base;
}
+bool Value::isValidName(const std::string& name) {
+ // Empty strings are legal
+ if (!name.size()) {
+ return true;
+ }
+
+ // Numbers are not legal
+ if (name.find_first_not_of("0123456789") == std::string::npos) {
+ return false;
+ }
+
+ return true;
+}
+
Value* Value::setUniqueName(const std::string& name) {
- if (name.size() > 0 &&
- name.find_first_not_of("0123456789") == std::string::npos) {
- throw std::runtime_error("names may not be integers: " + name);
+ if (!isValidName(name)) {
+ throw std::runtime_error("Invalid name: '" + name + "'");
}
auto& names = node()->owningGraph()->unique_names_;
diff --git a/torch/csrc/jit/ir.h b/torch/csrc/jit/ir.h
index f2ed570..ea5a5d9 100644
--- a/torch/csrc/jit/ir.h
+++ b/torch/csrc/jit/ir.h
@@ -177,6 +177,7 @@
bool hasUniqueName() const {
return !unique_name_.empty();
}
+ static bool isValidName(const std::string& name);
TORCH_API Value* setUniqueName(const std::string& name);
std::string uniqueName() const {
if (hasUniqueName()) {
@@ -1043,7 +1044,9 @@
TORCH_API Node* createUndefined();
TORCH_API Node* createFusionGroup();
TORCH_API Node* createDifferentiableSubgraph();
- TORCH_API Node* createTuple(at::ArrayRef<Value*> values, c10::OptNameList field_names=c10::nullopt);
+ TORCH_API Node* createTuple(
+ at::ArrayRef<Value*> values,
+ c10::OptNameList field_names = c10::nullopt);
TORCH_API Node* createTupleUnpack(Value* v);
TORCH_API Node* createTupleIndex(Value* tup, int64_t index);
TORCH_API Node* createTupleSlice(Value* tup, int64_t beg, int64_t end);
@@ -1072,7 +1075,6 @@
const std::function<Value*(Value*)>& value_map,
bool copy_blocks = true);
-
// Insert constant IValue into the graph. If the type cannot be fully deduced
// from the ivalue, as with a None that is set to t?, use result_type
TORCH_API Value* insertConstant(
diff --git a/torch/csrc/jit/irparser.cpp b/torch/csrc/jit/irparser.cpp
index f3341d9..24e23b1 100644
--- a/torch/csrc/jit/irparser.cpp
+++ b/torch/csrc/jit/irparser.cpp
@@ -262,15 +262,11 @@
L.expect(TK_DEDENT);
}
-static bool isNumber(const std::string& s) {
- return s.find_first_not_of("0123456789") == std::string::npos;
-}
-
void IRParser::parseBlockInputs(Block* b) {
parseList('(', ',', ')', [&] {
VarWithType v = parseVarWithType();
- // If the name is a number, don't use it
- std::string uniq_name = isNumber(v.name) ? "" : v.name;
+ // If the name isn't valid, don't use it
+ std::string uniq_name = Value::isValidName(v.name) ? v.name : "";
vmap[v.name] = b->addInput(uniq_name);
vmap[v.name]->setType(parseType(v.type));
});
@@ -365,8 +361,8 @@
void IRParser::parseGraphInputs() {
parseList('(', ',', ')', [&] {
VarWithType v = parseVarWithType();
- // If the name is a number, don't use it
- std::string uniq_name = isNumber(v.name) ? "" : v.name;
+ // If the name isn't valid, don't use it
+ std::string uniq_name = Value::isValidName(v.name) ? v.name : "";
vmap[v.name] = g->addInput(uniq_name);
vmap[v.name]->setType(parseType(v.type));
});