From 46f15b74b761291ea9a8d9b71aa26f273972d0ed Mon Sep 17 00:00:00 2001 From: Mikhail Zolotukhin Date: Thu, 21 Feb 2019 14:18:21 -0800 Subject: [PATCH] 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 --- torch/csrc/jit/ir.cpp | 19 ++++++++++++++++--- torch/csrc/jit/ir.h | 6 ++++-- torch/csrc/jit/irparser.cpp | 12 ++++-------- 3 files changed, 24 insertions(+), 13 deletions(-) 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 @@ std::string Value::uniqueNameBase() const { 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 @@ struct Value { 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 @@ struct Graph { TORCH_API Node* createUndefined(); TORCH_API Node* createFusionGroup(); TORCH_API Node* createDifferentiableSubgraph(); - TORCH_API Node* createTuple(at::ArrayRef values, c10::OptNameList field_names=c10::nullopt); + TORCH_API Node* createTuple( + at::ArrayRef 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 @@ struct Graph { const std::function& 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 @@ void IRParser::parseBlocks(Node* parentNode) { 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::parseOperator(Block* b) { 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)); }); -- 2.7.4