Remove dead or duplicate code.
PiperOrigin-RevId: 304414674 Change-Id: Ie1bcd80b4d3236172be62fdf98d2a72dceca6e0f
This commit is contained in:
parent
56e44425b6
commit
cf69aea282
@ -36,11 +36,11 @@ from tensorflow.python.autograph.pyct import templates
|
|||||||
from tensorflow.python.autograph.pyct import transformer
|
from tensorflow.python.autograph.pyct import transformer
|
||||||
|
|
||||||
|
|
||||||
|
# TODO(mdan): Replace with naming.Namer.
|
||||||
class DummyGensym(object):
|
class DummyGensym(object):
|
||||||
"""A dumb gensym that suffixes a stem by sequential numbers from 1000."""
|
"""A dumb gensym that suffixes a stem by sequential numbers from 1000."""
|
||||||
|
|
||||||
def __init__(self, ctx):
|
def __init__(self):
|
||||||
del ctx
|
|
||||||
# A proper implementation needs to account for:
|
# A proper implementation needs to account for:
|
||||||
# * ctx.info.namespace
|
# * ctx.info.namespace
|
||||||
# * all the symbols defined in the AST
|
# * all the symbols defined in the AST
|
||||||
@ -105,14 +105,12 @@ class AnfTransformer(transformer.Base):
|
|||||||
# processing the `body` and the `orelse` need to be kept together with them,
|
# processing the `body` and the `orelse` need to be kept together with them,
|
||||||
# and not accidentally lifted out of the `if`.
|
# and not accidentally lifted out of the `if`.
|
||||||
|
|
||||||
def __init__(self, ctx, config, gensym_source=None):
|
def __init__(self, ctx, config):
|
||||||
"""Creates an ANF transformer.
|
"""Creates an ANF transformer.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
ctx: transformer.Context
|
ctx: transformer.Context
|
||||||
config: Configuration
|
config: Configuration
|
||||||
gensym_source: An optional object with the same interface as `DummyGensym`
|
|
||||||
for generating unique names
|
|
||||||
"""
|
"""
|
||||||
super(AnfTransformer, self).__init__(ctx)
|
super(AnfTransformer, self).__init__(ctx)
|
||||||
if config is None:
|
if config is None:
|
||||||
@ -137,10 +135,7 @@ class AnfTransformer(transformer.Base):
|
|||||||
(ASTEdgePattern(ANY, ANY, gast.expr), REPLACE)]
|
(ASTEdgePattern(ANY, ANY, gast.expr), REPLACE)]
|
||||||
else:
|
else:
|
||||||
self._overrides = config
|
self._overrides = config
|
||||||
if gensym_source is None:
|
self._gensym = DummyGensym()
|
||||||
self._gensym = DummyGensym(ctx)
|
|
||||||
else:
|
|
||||||
self._gensym = gensym_source(ctx)
|
|
||||||
self._pending_statements = []
|
self._pending_statements = []
|
||||||
|
|
||||||
def _consume_pending_statements(self):
|
def _consume_pending_statements(self):
|
||||||
@ -529,7 +524,7 @@ def _is_trivial(node):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def transform(node, ctx, config=None, gensym_source=None):
|
def transform(node, ctx, config=None):
|
||||||
"""Converts the given node to A-normal form (ANF).
|
"""Converts the given node to A-normal form (ANF).
|
||||||
|
|
||||||
The general idea of A-normal form: https://en.wikipedia.org/wiki/A-normal_form
|
The general idea of A-normal form: https://en.wikipedia.org/wiki/A-normal_form
|
||||||
@ -605,7 +600,5 @@ def transform(node, ctx, config=None, gensym_source=None):
|
|||||||
argument provide?
|
argument provide?
|
||||||
config: Optional ANF configuration. If omitted, ANF replaces all expression
|
config: Optional ANF configuration. If omitted, ANF replaces all expression
|
||||||
expect literal constants.
|
expect literal constants.
|
||||||
gensym_source: An optional object with the same interface as `DummyGensym`
|
|
||||||
for generating unique names.
|
|
||||||
"""
|
"""
|
||||||
return AnfTransformer(ctx, config, gensym_source=gensym_source).visit(node)
|
return AnfTransformer(ctx, config).visit(node)
|
||||||
|
@ -29,30 +29,8 @@ from tensorflow.python.autograph.pyct.common_transformers import anf
|
|||||||
from tensorflow.python.platform import test
|
from tensorflow.python.platform import test
|
||||||
|
|
||||||
|
|
||||||
class DummyGensym(object):
|
# TODO(mdan): These two functions no longer need to be at the top level.
|
||||||
"""A dumb gensym that suffixes a stem by sequential numbers from 1000."""
|
# TODO(mdan): Don't use exec.
|
||||||
|
|
||||||
def __init__(self, ctx):
|
|
||||||
del ctx
|
|
||||||
# A proper implementation needs to account for:
|
|
||||||
# * ctx.info.namespace
|
|
||||||
# * all the symbols defined in the AST
|
|
||||||
# * the symbols generated so far
|
|
||||||
self._idx = 0
|
|
||||||
|
|
||||||
def new_name(self, stem='tmp'):
|
|
||||||
self._idx += 1
|
|
||||||
return stem + '_' + str(1000 + self._idx)
|
|
||||||
|
|
||||||
|
|
||||||
# These two test functions have to be top-level, not nested, for compatibility
|
|
||||||
# with some unknown version of Python 2.7 preceding 2.7.15. Why? Because
|
|
||||||
# `exec` and nested function definitions _incompatibly_ change the
|
|
||||||
# representation of local variables, such that `exec` inside a nested function
|
|
||||||
# definition is a syntax error in that version. The tuple form of `exec` fixes
|
|
||||||
# this problem, but apparently that was introduced in some unknown version of
|
|
||||||
# Python that's more recent than at least one version that we wish to be
|
|
||||||
# compatible with.
|
|
||||||
def exec_test_function():
|
def exec_test_function():
|
||||||
# The point is to test A-normal form conversion of exec
|
# The point is to test A-normal form conversion of exec
|
||||||
# pylint: disable=exec-used
|
# pylint: disable=exec-used
|
||||||
@ -88,9 +66,7 @@ class AnfTestBase(test.TestCase):
|
|||||||
# statements.
|
# statements.
|
||||||
exp_node, _ = parser.parse_entity(expected_fn, future_features=())
|
exp_node, _ = parser.parse_entity(expected_fn, future_features=())
|
||||||
node, _ = parser.parse_entity(test_fn, future_features=())
|
node, _ = parser.parse_entity(test_fn, future_features=())
|
||||||
node = anf.transform(
|
node = anf.transform(node, self._simple_context(), config=config)
|
||||||
node, self._simple_context(),
|
|
||||||
config=config, gensym_source=DummyGensym)
|
|
||||||
exp_name = exp_node.name
|
exp_name = exp_node.name
|
||||||
# Ignoring the function names in the result because they can't be
|
# Ignoring the function names in the result because they can't be
|
||||||
# the same (because both functions have to exist in the same scope
|
# the same (because both functions have to exist in the same scope
|
||||||
@ -98,8 +74,7 @@ class AnfTestBase(test.TestCase):
|
|||||||
node.name = exp_name
|
node.name = exp_name
|
||||||
self.assert_same_ast(exp_node, node)
|
self.assert_same_ast(exp_node, node)
|
||||||
# Check that ANF is idempotent
|
# Check that ANF is idempotent
|
||||||
node_repeated = anf.transform(
|
node_repeated = anf.transform(node, self._simple_context())
|
||||||
node, self._simple_context(), gensym_source=DummyGensym)
|
|
||||||
self.assert_same_ast(node_repeated, node)
|
self.assert_same_ast(node_repeated, node)
|
||||||
|
|
||||||
|
|
||||||
@ -466,9 +441,7 @@ class AnfNonTransformationTest(AnfTransformerTest):
|
|||||||
orig_source = parser.unparse(node, indentation=' ')
|
orig_source = parser.unparse(node, indentation=' ')
|
||||||
orig_str = textwrap.dedent(orig_source).strip()
|
orig_str = textwrap.dedent(orig_source).strip()
|
||||||
config = [(anf.ANY, anf.LEAVE)] # Configuration to transform nothing
|
config = [(anf.ANY, anf.LEAVE)] # Configuration to transform nothing
|
||||||
node = anf.transform(
|
node = anf.transform(node, self._simple_context(), config=config)
|
||||||
node, self._simple_context(),
|
|
||||||
config=config, gensym_source=DummyGensym)
|
|
||||||
new_source = parser.unparse(node, indentation=' ')
|
new_source = parser.unparse(node, indentation=' ')
|
||||||
new_str = textwrap.dedent(new_source).strip()
|
new_str = textwrap.dedent(new_source).strip()
|
||||||
self.assertEqual(orig_str, new_str)
|
self.assertEqual(orig_str, new_str)
|
||||||
|
Loading…
Reference in New Issue
Block a user