From 1dcf2ea0963ece881cd95841e1db8ac920544e15 Mon Sep 17 00:00:00 2001 From: David Riazati Date: Fri, 21 Dec 2018 15:59:29 -0800 Subject: [PATCH] Add self to Python printer reserved words (#15318) 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 | 4 ++-- torch/csrc/jit/passes/python_print.cpp | 15 +++++++++------ torch/jit/__init__.py | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/test/onnx/expect/TestOperators.test_norm.expect b/test/onnx/expect/TestOperators.test_norm.expect index ec9a7e6..56e57e3 100644 --- a/test/onnx/expect/TestOperators.test_norm.expect +++ b/test/onnx/expect/TestOperators.test_norm.expect @@ -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 diff --git a/torch/csrc/jit/passes/python_print.cpp b/torch/csrc/jit/passes/python_print.cpp index a478bd8..638ff95 100644 --- a/torch/csrc/jit/passes/python_print.cpp +++ b/torch/csrc/jit/passes/python_print.cpp @@ -142,22 +142,22 @@ void createTensorToParameterNameMap( // they are keywords or namespaces used in the output const static std::unordered_set 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", diff --git a/torch/jit/__init__.py b/torch/jit/__init__.py index fe90ab1..a59c980 100644 --- a/torch/jit/__init__.py +++ b/torch/jit/__init__.py @@ -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 -- 2.7.4