コード例 #1
0
ファイル: conversion.py プロジェクト: wdma/tensorflow
def node_to_graph(node, context):
    """Convert Python code to equivalent TF graph mode code.

  Args:
    node: AST, the code to convert.
    context: converter.EntityContext

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of entity
            dependencies that this node has.
  """
    # TODO(mdan): Insert list_comprehensions somewhere.

    node = converter.standard_analysis(node, context, is_initial=True)
    # Past this point, line numbers are no longer accurate so we ignore the
    # source.
    # TODO(mdan): Is it feasible to reconstruct intermediate source code?
    context.info.source_code = None
    node = converter.apply_(node, context, arg_defaults)
    node = converter.apply_(node, context, directives)
    node = converter.apply_(node, context, break_statements)
    node = converter.apply_(node, context, asserts)
    # Note: sequencing continue canonicalization before for loop one avoids
    # dealing with the extra loop increment operation that the for
    # canonicalization creates.
    node = converter.apply_(node, context, continue_statements)
    node = converter.apply_(node, context, return_statements)
    if context.program.options.uses(converter.Feature.LISTS):
        node = converter.apply_(node, context, lists)
        node = converter.apply_(node, context, slices)
    node = converter.apply_(node, context, builtin_functions)
    node = converter.apply_(node, context, call_trees)
    node = converter.apply_(node, context, control_flow)
    node = converter.apply_(node, context, conditional_expressions)
    node = converter.apply_(node, context, logical_expressions)
    if context.program.options.uses(converter.Feature.AUTO_CONTROL_DEPS):
        node = converter.apply_(node, context, side_effect_guards)
    # TODO(mdan): If function scopes ever does more, the toggle will need moving.
    if context.program.options.uses(converter.Feature.NAME_SCOPES):
        node = converter.apply_(node, context, function_scopes)
    if context.program.options.uses(converter.Feature.ERROR_REWRITING):
        node = converter.apply_(node, context, error_handlers)
    return node
コード例 #2
0
def node_to_graph(node, context):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: AST, the code to convert.
    context: converter.EntityContext

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of entity
            dependencies that this node has.
  """
  # TODO(mdan): Insert list_comprehensions somewhere.

  node = converter.standard_analysis(node, context, is_initial=True)
  # Past this point, line numbers are no longer accurate so we ignore the
  # source.
  # TODO(mdan): Is it feasible to reconstruct intermediate source code?
  context.info.source_code = None

  if context.program.options.uses(converter.Feature.DECORATORS):
    node = converter.apply_(node, context, decorators)
  node = converter.apply_(node, context, arg_defaults)
  node = converter.apply_(node, context, directives)
  node = converter.apply_(node, context, break_statements)
  node = converter.apply_(node, context, asserts)
  # Note: sequencing continue canonicalization before for loop one avoids
  # dealing with the extra loop increment operation that the for
  # canonicalization creates.
  node = converter.apply_(node, context, continue_statements)
  node = converter.apply_(node, context, return_statements)
  if context.program.options.uses(converter.Feature.LISTS):
    node = converter.apply_(node, context, lists)
    node = converter.apply_(node, context, slices)
  node = converter.apply_(node, context, builtin_functions)
  node = converter.apply_(node, context, call_trees)
  node = converter.apply_(node, context, control_flow)
  node = converter.apply_(node, context, conditional_expressions)
  node = converter.apply_(node, context, logical_expressions)
  if context.program.options.uses(converter.Feature.AUTO_CONTROL_DEPS):
    node = converter.apply_(node, context, side_effect_guards)
  # TODO(mdan): If function scopes ever does more, the toggle will need moving.
  if context.program.options.uses(converter.Feature.NAME_SCOPES):
    node = converter.apply_(node, context, function_scopes)
  if context.program.options.uses(converter.Feature.ERROR_REWRITING):
    node = converter.apply_(node, context, error_handlers)
  return node
コード例 #3
0
def node_to_graph(node, context):
    """Convert Python code to equivalent TF graph mode code.

  Args:
    node: AST, the code to convert.
    context: converter.EntityContext

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of entity
            dependencies that this node has.
  """
    # TODO(mdan): Insert list_comprehensions somewhere.
    unsupported_features_checker.verify(node)

    node = converter.standard_analysis(node, context, is_initial=True)
    node = converter.apply_(node, context, arg_defaults)
    node = converter.apply_(node, context, directives)
    node = converter.apply_(node, context, break_statements)
    if context.program.options.uses(converter.Feature.ASSERT_STATEMENTS):
        node = converter.apply_(node, context, asserts)
    # Note: sequencing continue canonicalization before for loop one avoids
    # dealing with the extra loop increment operation that the for
    # canonicalization creates.
    node = converter.apply_(node, context, continue_statements)
    node = converter.apply_(node, context, return_statements)
    if context.program.options.uses(converter.Feature.LISTS):
        node = converter.apply_(node, context, lists)
        node = converter.apply_(node, context, slices)
    node = converter.apply_(node, context, call_trees)
    node = converter.apply_(node, context, control_flow)
    node = converter.apply_(node, context, conditional_expressions)
    if context.program.options.uses(converter.Feature.LOGICAL_EXPRESSIONS):
        node = converter.apply_(node, context, logical_expressions)
    if context.program.options.uses(converter.Feature.AUTO_CONTROL_DEPS):
        node = converter.apply_(node, context, side_effect_guards)
    # TODO(mdan): If function scopes ever does more, the toggle will need moving.
    if context.program.options.uses(converter.Feature.NAME_SCOPES):
        node = converter.apply_(node, context, function_scopes)
    return node
コード例 #4
0
def node_to_graph(node, context, rewrite_errors=True):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: AST, the code to convert.
    context: converter.EntityContext
    rewrite_errors: Boolean, whether or not to rewrite the error traceback.

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of entity
            dependencies that this node has.
  """
  # TODO(mdan): Insert list_comprehensions somewhere.

  node = converter.standard_analysis(node, context, is_initial=True)
  # Past this point, line numbers are no longer accurate so we ignore the
  # source.
  # TODO(mdan): Is it feasible to reconstruct intermediate source code?
  context.info.source_code = None

  node = converter.apply_(node, context, decorators)
  node = converter.apply_(node, context, directives)
  node = converter.apply_(node, context, break_statements)
  node = converter.apply_(node, context, asserts)
  # Note: sequencing continue canonicalization before for loop one avoids
  # dealing with the extra loop increment operation that the for
  # canonicalization creates.
  node = converter.apply_(node, context, continue_statements)
  context.info.namespace['len'] = len
  node = converter.apply_(node, context, return_statements)
  node = converter.apply_(node, context, lists)
  node = converter.apply_(node, context, slices)
  node = converter.apply_(node, context, builtin_functions)
  node = converter.apply_(node, context, call_trees)
  node = converter.apply_(node, context, control_flow)
  node = converter.apply_(node, context, conditional_expressions)
  node = converter.apply_(node, context, logical_expressions)
  node = converter.apply_(node, context, side_effect_guards)
  node = converter.apply_(node, context, name_scopes)
  if rewrite_errors:
    node = converter.apply_(node, context, error_handlers)
  return node
コード例 #5
0
ファイル: conversion.py プロジェクト: adit-chandra/tensorflow
def node_to_graph(node, context):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: AST, the code to convert.
    context: converter.EntityContext

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of entity
            dependencies that this node has.
  """
  # TODO(mdan): Insert list_comprehensions somewhere.
  unsupported_features_checker.verify(node)

  node = converter.standard_analysis(node, context, is_initial=True)
  node = converter.apply_(node, context, arg_defaults)
  node = converter.apply_(node, context, directives)
  node = converter.apply_(node, context, break_statements)
  if context.program.options.uses(converter.Feature.ASSERT_STATEMENTS):
    node = converter.apply_(node, context, asserts)
  # Note: sequencing continue canonicalization before for loop one avoids
  # dealing with the extra loop increment operation that the for
  # canonicalization creates.
  node = converter.apply_(node, context, continue_statements)
  node = converter.apply_(node, context, return_statements)
  if context.program.options.uses(converter.Feature.LISTS):
    node = converter.apply_(node, context, lists)
    node = converter.apply_(node, context, slices)
  node = converter.apply_(node, context, call_trees)
  node = converter.apply_(node, context, control_flow)
  node = converter.apply_(node, context, conditional_expressions)
  if context.program.options.uses(converter.Feature.LOGICAL_EXPRESSIONS):
    node = converter.apply_(node, context, logical_expressions)
  if context.program.options.uses(converter.Feature.AUTO_CONTROL_DEPS):
    node = converter.apply_(node, context, side_effect_guards)
  # TODO(mdan): If function scopes ever does more, the toggle will need moving.
  if context.program.options.uses(converter.Feature.NAME_SCOPES):
    node = converter.apply_(node, context, function_scopes)
  if context.program.options.uses(converter.Feature.ERROR_REWRITING):
    node = converter.apply_(node, context, error_handlers)
  return node
コード例 #6
0
    def transform_ast(self, node, ctx):
        # TODO(mdan): Insert list_comprehensions somewhere.
        unsupported_features_checker.verify(node)

        node = converter.standard_analysis(node, ctx, is_initial=True)
        node = converter.apply_(node, ctx, functions)
        node = converter.apply_(node, ctx, directives)
        node = converter.apply_(node, ctx, break_statements)
        if ctx.user.options.uses(converter.Feature.ASSERT_STATEMENTS):
            node = converter.apply_(node, ctx, asserts)
        # Note: sequencing continue canonicalization before for loop one avoids
        # dealing with the extra loop increment operation that the for
        # canonicalization creates.
        node = converter.apply_(node, ctx, continue_statements)
        node = converter.apply_(node, ctx, return_statements)
        if ctx.user.options.uses(converter.Feature.LISTS):
            node = converter.apply_(node, ctx, lists)
            node = converter.apply_(node, ctx, slices)
        node = converter.apply_(node, ctx, call_trees)
        node = converter.apply_(node, ctx, control_flow)
        node = converter.apply_(node, ctx, conditional_expressions)
        node = converter.apply_(node, ctx, logical_expressions)
        return node