Exemplo n.º 1
0
def node_to_graph(node, namer, namespace, value_hints):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    namer: A naming.Namer object.
    namespace: Dict mapping symbol names to their corresponding live objects.
    value_hints: A dict containing value hints for symbols like function
        parameters.

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of object
            dependencies that this node has.
  """
  # TODO(mdan): Get rid of this.
  node = gradients_function.transform(node)

  node = access.resolve(node)
  node = live_values.resolve(node, namespace, config.PYTHON_LITERALS)
  node = type_info.resolve(node, value_hints)

  # TODO(mdan): Factor out common elements.
  # These include:
  #   * keeping track of symbols that have been created
  #   * marking nodes (e.g. py_func wrappers) to suppress further processing

  node = print_functions.transform(node)
  node = call_trees.transform(node, namer, config.DEFAULT_UNCOMPILED_MODULES)
  node = control_flow.transform(node, namer)
  node = logical_expressions.transform(node)
  node = side_effect_guards.transform(node, namer)

  return node
Exemplo n.º 2
0
  def test_uncompiled_modules(self):

    def test_fn(a):
      a = math_ops.multiply(a, constant_op.constant(2))
      a = math_ops.add(a, constant_op.constant(1))
      return a

    node = self._parse_and_analyze(test_fn, {
        'math_ops': math_ops,
        'constant_op': constant_op
    })
    node = call_trees.transform(node, TestNamer(), {},
                                set(((math_ops.__name__,),
                                     (constant_op.__name__,))), ())
    result = compiler.ast_to_object(node)
    setattr(result, 'math_ops', math_ops)
    setattr(result, 'constant_op', constant_op)

    with self.test_session() as sess:
      # Not renamed, because the converter doesn't rename the definition itself.
      # (the caller is responsible for that).
      result_tensor = result.test_fn(constant_op.constant(1))
      result_val = sess.run(result_tensor)

    self.assertEquals(3, result_val)
Exemplo n.º 3
0
def node_to_graph(node, namer, namespace, value_hints):
    """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    namer: A naming.Namer object.
    namespace: Dict mapping symbol names to their corresponding live objects.
    value_hints: A dict containing value hints for symbols like function
        parameters.

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of object
            dependencies that this node has.
  """
    node = access.resolve(node)
    node = live_values.resolve(node, namespace, config.PYTHON_LITERALS)
    node = type_info.resolve(node, value_hints)

    # TODO(mdan): Factor out common elements.
    # These include:
    #   * keeping track of symbols that have been created
    #   * marking nodes (e.g. py_func wrappers) to suppress further processing

    node = print_functions.transform(node)
    node = call_trees.transform(node, namer, config.DEFAULT_UNCOMPILED_MODULES)
    node = control_flow.transform(node, namer)
    node = logical_expressions.transform(node)
    node = side_effect_guards.transform(node, namer)

    return node
Exemplo n.º 4
0
def node_to_graph(node, namer, namespace, value_hints, nocompile_decorators):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    namer: A naming.Namer object.
    namespace: Dict mapping symbol names to their corresponding live objects.
    value_hints: A dict containing value hints for symbols like function
        parameters.
    nocompile_decorators: A tuple containing decorators to be stripped from
        functions during conversion.

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of object
            dependencies that this node has.
  """
  # TODO(mdan): Verify arguments for correctness.

  # TODO(mdan): Factor out common elements.
  # These include:
  #   * keeping track of symbols that have been created
  #   * marking nodes (e.g. py_func wrappers) to suppress further processing
  #   * code move between blocks
  #   * insertion of new global references
  #   * visiting blocks in transformers

  # Certain steps, especially canonicalization, insert new symbols into the
  # tree, which must be accounted. Although less efficient, it is most robust
  # to re-run the analysis.

  node = _static_analysis_pass(node, namespace, value_hints)
  node = decorators.transform(node, nocompile_decorators)
  node = break_canonicalization.transform(node, namer)

  # Note: sequencing continue canonicalization before for loop one avoids
  # dealing with the extra loop increment operation that the for
  # canonicalization creates.
  node = continue_canonicalization.transform(node, namer)
  namespace['len'] = len

  node = _static_analysis_pass(node, namespace, value_hints)
  node = for_canonicalization.transform(node, namer)
  # for_canonicalization may insert new global references.
  node = builtin_functions.transform(node)
  # builtin_functions may insert new global references.
  namespace['print'] = print

  node = _static_analysis_pass(node, namespace, value_hints)
  node = print_functions.transform(node)
  node = call_trees.transform(node, namer, namespace,
                              config.DEFAULT_UNCOMPILED_MODULES,
                              nocompile_decorators)
  node = control_flow.transform(node, namer)
  node = logical_expressions.transform(node)
  node = side_effect_guards.transform(node, namer)

  return node
Exemplo n.º 5
0
def node_to_graph(node, namer, namespace, value_hints):
    """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    namer: A naming.Namer object.
    namespace: Dict mapping symbol names to their corresponding live objects.
    value_hints: A dict containing value hints for symbols like function
        parameters.

  Returns:
    A tuple (node, deps):
        * node: A Python ast node, representing the converted code.
        * deps: A set of strings, the fully qualified names of object
            dependencies that this node has.
  """
    node = access.resolve(node)
    node = live_values.resolve(node, namespace, config.PYTHON_LITERALS)
    node = type_info.resolve(node, value_hints)

    # TODO(mdan): Factor out common elements.
    # These include:
    #   * keeping track of symbols that have been created
    #   * marking nodes (e.g. py_func wrappers) to suppress further processing

    node = for_canonicalization.transform(node, namer)
    node = builtin_functions.transform(node)

    # The transformation steps above insert new variables. Although less
    # efficient, it is most robust to re-run the analysis.
    # We also need to ensure the namespace contains any new references that may
    # have been created.
    namespace['len'] = len
    namespace['print'] = print

    node = access.resolve(node)
    node = live_values.resolve(node, namespace, config.PYTHON_LITERALS)
    node = type_info.resolve(node, value_hints)

    node = print_functions.transform(node)
    node = call_trees.transform(node, namer, config.DEFAULT_UNCOMPILED_MODULES)
    node = control_flow.transform(node, namer)
    node = logical_expressions.transform(node)
    node = side_effect_guards.transform(node, namer)

    return node
Exemplo n.º 6
0
  def test_basic(self):

    def test_fn_1(_):
      raise ValueError('This should not be called in the compiled verison.')

    def renamed_test_fn_1(a):
      return a + 1

    def test_fn_2(a):
      return test_fn_1(a) + 1

    node = self._parse_and_analyze(test_fn_2, {'test_fn_1': test_fn_1})
    node = call_trees.transform(node, TestNamer(), set())
    result = compiler.ast_to_object(node)
    # Only test_fn_2 is transformed, so we'll insert renamed_test_fn_1 manually.
    setattr(result, 'renamed_test_fn_1', renamed_test_fn_1)

    self.assertEquals(3, result.renamed_test_fn_2(1))
Exemplo n.º 7
0
  def test_basic(self):

    def test_fn_1(_):
      raise ValueError('This should not be called in the compiled verison.')

    def renamed_test_fn_1(a):
      return a + 1

    def test_fn_2(a):
      return test_fn_1(a) + 1

    node = self._parse_and_analyze(test_fn_2, {'test_fn_1': test_fn_1})
    node = call_trees.transform(node, TestNamer(), {}, (), ())
    result = compiler.ast_to_object(node)
    # Only test_fn_2 is transformed, so we'll insert renamed_test_fn_1 manually.
    setattr(result, 'renamed_test_fn_1', renamed_test_fn_1)

    self.assertEquals(3, result.test_fn_2(1))
Exemplo n.º 8
0
  def test_uncompiled_modules(self):

    def test_fn(a):
      a = math_ops.multiply(a, constant_op.constant(2))
      a = math_ops.add(a, constant_op.constant(1))
      return a

    node = self._parse_and_analyze(test_fn, {
        'math_ops': math_ops,
        'constant_op': constant_op
    })
    node = call_trees.transform(node, TestNamer(),
                                set((math_ops.__name__, constant_op.__name__)))
    result = compiler.ast_to_object(node)
    setattr(result, 'math_ops', math_ops)
    setattr(result, 'constant_op', constant_op)

    with self.test_session() as sess:
      result_tensor = result.renamed_test_fn(constant_op.constant(1))
      result_val = sess.run(result_tensor)

    self.assertEquals(3, result_val)
Exemplo n.º 9
0
  def test_uncompiled_modules(self):

    def test_fn(a):
      a = math_ops.multiply(a, constant_op.constant(2))
      a = math_ops.add(a, constant_op.constant(1))
      return a

    node = self._parse_and_analyze(test_fn, {
        'math_ops': math_ops,
        'constant_op': constant_op
    })
    node = call_trees.transform(node, TestNamer(),
                                set(((math_ops.__name__,),
                                     (constant_op.__name__,))))
    result = compiler.ast_to_object(node)
    setattr(result, 'math_ops', math_ops)
    setattr(result, 'constant_op', constant_op)

    with self.test_session() as sess:
      result_tensor = result.renamed_test_fn(constant_op.constant(1))
      result_val = sess.run(result_tensor)

    self.assertEquals(3, result_val)