Add self to Python printer reserved words (#15318)
authorDavid Riazati <davidriazati@fb.com>
Fri, 21 Dec 2018 23:59:29 +0000 (15:59 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 22 Dec 2018 00:02:07 +0000 (16:02 -0800)
Summary:
This adds `self` to the list of reserved words and also sorts the lines and prevents the tracer from naming values 'self' (which happens in torch/tensor.py)

Fixes #15240
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15318

Differential Revision: D13540192

Pulled By: driazati

fbshipit-source-id: 46ae02e51b1b31d5c62110fa83ba258ea6bada27

test/onnx/expect/TestOperators.test_norm.expect
torch/csrc/jit/passes/python_print.cpp
torch/jit/__init__.py

index ec9a7e6..56e57e3 100644 (file)
@@ -3,7 +3,7 @@ producer_name: "pytorch"
 producer_version: "0.4"
 graph {
   node {
-    input: "self"
+    input: "0"
     output: "1"
     op_type: "ReduceL2"
     attribute {
@@ -19,7 +19,7 @@ graph {
   }
   name: "torch-jit-export"
   input {
-    name: "self"
+    name: "0"
     type {
       tensor_type {
         elem_type: 1
index a478bd8..638ff95 100644 (file)
@@ -142,22 +142,22 @@ void createTensorToParameterNameMap(
   // they are keywords or namespaces used in the output
   const static std::unordered_set<std::string> reserved_names = {
     // identifiers in the environment while parsing
+    "_", // avoid the confusing unnamed _
     "aten",
-    "ops",
+    "attribute",
     "CONSTANTS",
     "fork",
-    "attribute",
     "getattr",
-    "_", // avoid the confusing unnamed _
     "inf",
     "nan",
+    "ops",
+    "self",
     // the python keywords
-    "False",
-    "None",
-    "True",
     "and",
     "as",
     "assert",
+    "async",
+    "await",
     "break",
     "class",
     "continue",
@@ -166,6 +166,7 @@ void createTensorToParameterNameMap(
     "elif",
     "else",
     "except",
+    "False",
     "finally",
     "for",
     "from",
@@ -175,12 +176,14 @@ void createTensorToParameterNameMap(
     "in",
     "is",
     "lambda",
+    "None",
     "nonlocal",
     "not",
     "or",
     "pass",
     "raise",
     "return",
+    "True",
     "try",
     "while",
     "with",
index fe90ab1..a59c980 100644 (file)
@@ -223,10 +223,10 @@ def _create_interpreter_name_lookup_fn(frames_up=1):
 
         for k, v in f_locals.items():
             if isinstance(v, torch.Tensor) and var is v:
-                return k
+                return k if k != 'self' else ''
         for k, v in f_globals.items():
             if isinstance(v, torch.Tensor) and var is v:
-                return k
+                return k if k != 'self' else ''
         return ''
     return _get_interpreter_name_for_var