Skip to content

Strings lexing, parsing, implementation in print #9324

Closed
eellison wants to merge 8 commits intopytorch:masterfrom
eellison:string_printing
Closed

Strings lexing, parsing, implementation in print #9324
eellison wants to merge 8 commits intopytorch:masterfrom
eellison:string_printing

Conversation

@eellison
Copy link
Contributor

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.

test/test_jit.py Outdated

This comment was marked as off-topic.

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.

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.

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.

This comment was marked as off-topic.

}
}
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.

@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.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

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.

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.

This comment was marked as off-topic.

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.

This comment was marked as off-topic.

@ezyang
Copy link
Contributor

ezyang commented Jul 11, 2018

@pytorchbot retest this please

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good -- I have a bunch of organizational suggestions to make the code easier to understand and maintain.

[](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.

}
}
auto filtered_inputs = getNamedValues(trees_, true, identity);
ensureTensors(ident.range(), toValues(filtered_inputs));

This comment was marked as off-topic.

}
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.

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.

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.

if (str[end] == '\n' && quote_len != 3) {
return false;
}
if (str[end] == '\\') {

This comment was marked as off-topic.

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.

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.

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.

next_start = str.find('\n', next_start);
}
next_start++;
}

This comment was marked as off-topic.

@eellison
Copy link
Contributor Author

Thanks adam and zach for the thorough review!!

@ezyang
Copy link
Contributor

ezyang commented Jul 14, 2018

@eellison I'll let you import this when you're ready

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@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.

@zou3519 zou3519 added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jul 23, 2018
@eellison eellison force-pushed the string_printing branch 9 times, most recently from 11c1f26 to 4cdb804 Compare August 1, 2018 01:05

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@zdevito zdevito dismissed apaszke’s stale review August 1, 2018 17:13

Changes addressed

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eellison has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@eellison
Copy link
Contributor Author

eellison commented Aug 2, 2018

@pytorchbot retest pr/caffe2-py2-gcc4.8-ubuntu14.04-test please

@eellison
Copy link
Contributor Author

eellison commented Aug 2, 2018

@pytorchbot retest this please

goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
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
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants