Address jittering issues in python_print (#14064)
authorZachary DeVito <zdevito@fb.com>
Wed, 21 Nov 2018 14:36:26 +0000 (06:36 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 21 Nov 2018 14:38:29 +0000 (06:38 -0800)
commit788d2e87bdf6df26e7fd94878677235c5d6300fd
treeff52c96f0046ecd50eabfb64a5e294cab43aafd6
parentaf82396f7f6bcfbdfbffb4e1bc970688749d6a68
Address jittering issues in python_print (#14064)

Summary:
export - print a method with python_print
import - import a method with import_method

We want to ensure:

    export(g) == export(import(export(g)))

That is after after exporting/importing once, the graph will stay exactly
the same. This is less strict that g == import(export(g)) which would
require us to maintain a lot more information about the structure of the
IR and about the names of debug symbols.

This PR addresses this with the following fixes:
* print out double-precision numbers with high enough precision such
  that they always parse in the same way
* when creating loop-carried dependencies, sort them
  by variable name, ensuring a consistent order
* parse nan correctly
* DCE: remove unused outputs of if statements, and loop-carried dependencies
  in loops that are dead both after the loop and inside the body of the
  loop.
* Do not set uniqueName for variables whose names are _[0-9]+, these
  are probably rare in user code, and we need a way to communicate
  that we do not care about a variable name when re-parsing the graph.
  Otherwise temporary variable names will jitter around.
* Expand the definition of a constant in printing code to None,
  and family.
* Allow re-treeing to work as long as the only thing in its way is a
  constant node. These do not have side effects but are sometimes
  inserted in a different order when tracing compared to how we print them.
* Print all constant nodes out first in the order in which they are used_val
 (or, if they are inlined, ensure they get assigned CONSTANT.cX number
  in a consistent order). Cleanup tuples (this is done in the compiler,
  but not in the tracer, leading to some tuple indexing jitter if not
  done).
* use strtod_l, not std::stod which can throw exceptions

Other:
* Add REL_WITH_DEB_INFO to setup.py. It already existed for the
  cmake files. Threading it into setup.py allows us to turn on
  debug symbols with optimization everywhere.
* enable round trip testing for all generated graphs. This only adds
  ~6 seconds to total build time but tests printing for every graph.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14064

Differential Revision: D13094637

Pulled By: zdevito

fbshipit-source-id: 0a1c6912194d965f15d6b0c6cf838ccc551f161d
21 files changed:
aten/src/ATen/core/ivalue.cpp
setup.py
test/expect/TestBatched.test_if_else.expect
test/expect/TestBatched.test_if_else_with_scalar.expect
test/expect/TestBatched.test_if_noelse.expect
test/expect/TestBatched.test_if_noelse_with_scalar.expect
test/expect/TestBatched.test_while.expect
test/expect/TestJit.test_pretty_printer-loop_use_test.expect
test/expect/TestJit.test_pretty_printer-while_if_test.expect
test/expect/TestJit.test_pretty_printer-while_test.expect
test/expect/TestScript.test_constant_pooling.expect
test/expect/TestScript.test_onnx_export_script_module_loop.expect
test/test_jit.py
torch/csrc/jit/import_method.cpp
torch/csrc/jit/ir.h
torch/csrc/jit/passes/dead_code_elimination.cpp
torch/csrc/jit/passes/python_print.cpp
torch/csrc/jit/python_tracer.cpp
torch/csrc/jit/script/compiler.cpp
torch/csrc/jit/script/lexer.h
torch/csrc/jit/script/tree_views.h