Strings lexing, parsing, implementation in print #9324
Strings lexing, parsing, implementation in print #9324eellison wants to merge 8 commits intopytorch:masterfrom
Conversation
b4e7cbd to
b2decb1
Compare
test/test_jit.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/register_prim_ops.cpp
Outdated
| size_t num_inputs = node->inputs().size(); | ||
| return [num_inputs](Stack& stack) { | ||
| std::vector<std::string> strings = node->ss(Symbol::attr("strings")); | ||
| std::vector<int64_t> indices = node->is(Symbol::attr("string_indices")); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/register_prim_ops.cpp
Outdated
| std::cout << at::Tensor(tensor_impl, true); | ||
| } else if (!i.defined()) { | ||
| std::cout << "<undefined tensor>"; | ||
| if (str_i < strings.size() && int(ten_i + str_i) == indices[str_i]) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| return emitTernaryIf(TernaryIf(tree)); | ||
| } break; | ||
| case TK_STRINGLITERAL: { | ||
| throw std::runtime_error("NYI: string literals are only supported as an argument to print\n"); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| } | ||
| } | ||
| auto filtered_inputs = getNamedValues(trees_, true, identity); | ||
| ensureTensors(ident.range(), toValues(filtered_inputs)); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/jit/frontend.py
Outdated
| @staticmethod | ||
| def build_Str(ctx, expr): | ||
| value = str(expr.s) | ||
| r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(value)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| return false; | ||
| int quote_len = isCharCount(quote, str, start, 3) ? 3 : 1; | ||
| size_t end = start + quote_len; | ||
| while(end < str.size() && !isCharCount(quote, str, end, quote_len)) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| if (str[end] == '\n' && quote_len != 3) { | ||
| return false; | ||
| } | ||
| if (str[end] == '\\') { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/parser.h
Outdated
| return start + len <= str.size() && std::count(str.begin() + start, str.begin() + start + len, c) == len; | ||
| } | ||
|
|
||
| std::string parseLexedString(const std::string &str, size_t start) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
@pytorchbot retest this please |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
zdevito
left a comment
There was a problem hiding this comment.
Looking good -- I have a bunch of organizational suggestions to make the code easier to understand and maintain.
torch/csrc/jit/register_prim_ops.cpp
Outdated
| [](Node* node) { | ||
| size_t num_inputs = node->inputs().size(); | ||
| return [num_inputs](Stack& stack) { | ||
| std::vector<std::string> strings = node->ss(Symbol::attr("strings")); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| } | ||
| } | ||
| auto filtered_inputs = getNamedValues(trees_, true, identity); | ||
| ensureTensors(ident.range(), toValues(filtered_inputs)); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| } | ||
| case TK_APPLY: { | ||
| auto apply = Apply(tree); | ||
| Ident ident = Var(apply.callee()).name(); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| return emitTernaryIf(TernaryIf(tree)); | ||
| } break; | ||
| case TK_STRINGLITERAL: { | ||
| throw std::runtime_error("NYI: string literals are only supported as an argument to print\n"); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/compiler.cpp
Outdated
| return emitTernaryIf(TernaryIf(tree)); | ||
| } break; | ||
| case TK_STRINGLITERAL: { | ||
| throw std::runtime_error("NYI: string literals are only supported as an argument to print\n"); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| if (str[end] == '\n' && quote_len != 3) { | ||
| return false; | ||
| } | ||
| if (str[end] == '\\') { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/parser.h
Outdated
| return start + len <= str.size() && std::count(str.begin() + start, str.begin() + start + len, c) == len; | ||
| } | ||
|
|
||
| std::string parseLexedString(const std::string &str, size_t start) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/parser.h
Outdated
| size_t next_start = end + quote_len; | ||
| //find start of next string | ||
| while(next_start < str.size() && str[next_start] != '\'' && str[next_start] != '\"') { | ||
| if (str[next_start] == '#') { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/parser.h
Outdated
| char quote = str[start]; | ||
| int quote_len = isCharCount(quote, str, start, 3) ? 3 : 1; | ||
| size_t end = start + quote_len; | ||
| while(!isCharCount(quote, str, end, quote_len)) { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/script/parser.h
Outdated
| next_start = str.find('\n', next_start); | ||
| } | ||
| next_start++; | ||
| } |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Thanks adam and zach for the thorough review!! |
ab23de7 to
b2decb1
Compare
|
@eellison I'll let you import this when you're ready |
apaszke
left a comment
There was a problem hiding this comment.
Haven't checked the lexer/parser. Generally looks ok, but we'll probably want to revise our strategy for handling string literals. I think it would be much cleaner to treat them as first class values in the compiler, and then have a string lowering pass that would eliminate them from the interpreter, and embed them into prim::Print nodes. We shouldn't really have any special casing here.
Also, this will need to be rebased on top of #9584 once it lands. It's a much bigger and more complicated PR, and it cleans up parts of the code you touch.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/register_prim_ops.cpp
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/csrc/jit/register_prim_ops.cpp
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/jit/frontend.py
Outdated
| @staticmethod | ||
| def build_Str(ctx, expr): | ||
| value = str(expr.s) | ||
| r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(value)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
7389ef6 to
928abfd
Compare
256eb20 to
76bd3eb
Compare
11c1f26 to
4cdb804
Compare
torch/csrc/jit/script/compiler.cpp
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
4cdb804 to
8c77c03
Compare
8c77c03 to
e905658
Compare
facebook-github-bot
left a comment
There was a problem hiding this comment.
eellison has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@pytorchbot retest pr/caffe2-py2-gcc4.8-ubuntu14.04-test please |
|
@pytorchbot retest this please |
Summary: This PR adds strings to the ast and implements them for print statements. Strings are lifted as attributes to the print node. They must be arguments to print itself, not as an argument for an object that is passed to print. If they are encountered elsewhere a NYI exception will be thrown. Pull Request resolved: pytorch#9324 Reviewed By: jramseyer Differential Revision: D8807128 Pulled By: eellison fbshipit-source-id: 984401ff458ed18d473c6d1bd86750e56c77d078
This PR adds strings to the ast and implements them for print statements. Strings are lifted as attributes to the print node. They must be arguments to print itself, not as an argument for an object that is passed to print. If they are encountered elsewhere a NYI exception will be thrown.