Exemplo n.º 1
0
    def test_basic_name(self):
        def test_fn(l):
            a = 5
            l += a
            return l

        node = self.parse_and_analyze(test_fn, {})
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            result_op = result.test_fn(constant_op.constant([1, 2, 3]))
            self.assertIn('test_fn/', result_op.op.name)
Exemplo n.º 2
0
  def test_basic_name(self):

    def test_fn(l):
      a = 5
      l += a
      return l

    node = self.parse_and_analyze(test_fn, {})
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.test_fn(constant_op.constant([1, 2, 3]))
      self.assertIn('test_fn/', result_op.op.name)
    def test_basic(self):
        def test_fn(l):
            """This should stay here."""
            a = 5
            l += a
            return l

        node = self.parse_and_analyze(test_fn, {})
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            result_op = result.test_fn(constant_op.constant(1))
            self.assertIn('test_fn/', result_op.op.name)

            self.assertEqual('This should stay here.', result.test_fn.__doc__)
Exemplo n.º 4
0
  def test_basic(self):

    def test_fn(l):
      """This should stay here."""
      a = 5
      l += a
      return l

    node = self.parse_and_analyze(test_fn, {})
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.test_fn(constant_op.constant(1))
      self.assertIn('test_fn/', result_op.op.name)

      self.assertEqual('This should stay here.', result.test_fn.__doc__)
    def test_long_docstring(self):
        def test_fn(l):
            """Multi-line docstring.

      Args:
        l: A thing.
      Returns:
        l
      """
            return l

        node = self.parse_and_analyze(test_fn, {})
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            self.assertIn('Multi-line', result.test_fn.__doc__)
            self.assertIn('Returns:', result.test_fn.__doc__)
    def test_nested_functions(self):
        def test_fn(l):
            def inner_fn(i):
                return i**2

            l += 4
            return inner_fn(l)

        node = self.parse_and_analyze(test_fn, {})
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            result_op = result.test_fn(constant_op.constant(1))
            first_result_input_name = result_op.op.inputs[0].name
            second_result_input_name = result_op.op.inputs[1].name
            self.assertIn('test_fn/', first_result_input_name)
            self.assertNotIn('inner_fn', first_result_input_name)
            self.assertIn('test_fn/inner_fn/', second_result_input_name)
Exemplo n.º 7
0
  def test_long_docstring(self):

    def test_fn(l):
      """Multi-line docstring.

      Args:
        l: A thing.
      Returns:
        l
      """
      return l

    node = self.parse_and_analyze(test_fn, {})
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      self.assertIn('Multi-line', result.test_fn.__doc__)
      self.assertIn('Returns:', result.test_fn.__doc__)
Exemplo n.º 8
0
    def test_nested_name(self):
        def test_fn(l):
            def body(i):
                return i**2

            l += [4]
            return body(l)

        node = self.parse_and_analyze(test_fn, {})
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            result_op = result.test_fn(constant_op.constant([1, 2, 3]))
            first_result_input_name = result_op.op.inputs[0].name
            second_result_input_name = result_op.op.inputs[1].name
            self.assertIn('test_fn/', first_result_input_name)
            self.assertNotIn('body/', first_result_input_name)
            self.assertIn('test_fn/body/', second_result_input_name)
Exemplo n.º 9
0
    def test_method(self):
        class TestClass(object):
            def test_fn(self, l):
                def inner_fn(i):
                    return i + 1

                l += 1
                return l, inner_fn(l)

        ns = {'TestClass': TestClass}
        node, ctx = self.prepare(TestClass, ns, owner_type=TestClass)
        node = name_scopes.transform(node, ctx)

        with self.compiled(node, {}, ops.name_scope) as result:
            first, second = result.TestClass().test_fn(constant_op.constant(1))
            self.assertIn('TestClass/test_fn/', first.op.name)
            self.assertNotIn('inner_fn', first.op.name)
            self.assertIn('TestClass/test_fn/inner_fn/', second.op.name)
Exemplo n.º 10
0
  def test_nested_name(self):

    def test_fn(l):

      def body(i):
        return i**2

      l += [4]
      return body(l)

    node = self.parse_and_analyze(test_fn, {})
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.test_fn(constant_op.constant([1, 2, 3]))
      first_result_input_name = result_op.op.inputs[0].name
      second_result_input_name = result_op.op.inputs[1].name
      self.assertIn('test_fn/', first_result_input_name)
      self.assertNotIn('body/', first_result_input_name)
      self.assertIn('test_fn/body/', second_result_input_name)
Exemplo n.º 11
0
  def test_nested_functions(self):

    def test_fn(l):

      def inner_fn(i):
        return i ** 2

      l += 4
      return inner_fn(l)

    node = self.parse_and_analyze(test_fn, {})
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.test_fn(constant_op.constant(1))
      first_result_input_name = result_op.op.inputs[0].name
      second_result_input_name = result_op.op.inputs[1].name
      self.assertIn('test_fn/', first_result_input_name)
      self.assertNotIn('inner_fn', first_result_input_name)
      self.assertIn('test_fn/inner_fn/', second_result_input_name)
Exemplo n.º 12
0
  def test_method(self):

    class TestClass(object):

      def test_fn(self, l):

        def inner_fn(i):
          return i + 1

        l += 1
        return l, inner_fn(l)

    ns = {'TestClass': TestClass}
    node, ctx = self.prepare(TestClass, ns, owner_type=TestClass)
    node = name_scopes.transform(node, ctx)

    with self.compiled(node, {}, ops.name_scope) as result:
      first, second = result.TestClass().test_fn(constant_op.constant(1))
      self.assertIn('TestClass/test_fn/', first.op.name)
      self.assertNotIn('inner_fn', first.op.name)
      self.assertIn('TestClass/test_fn/inner_fn/', second.op.name)
    def test_method(self):
        class TestClass(object):
            def test_fn(self, l):
                def inner_fn(i):
                    return i**2

                l += 4
                return inner_fn(l)

        # Note that 'TestClass' was needed in the namespace here.
        node = self.parse_and_analyze(TestClass, {'TestClass': TestClass},
                                      owner_type=TestClass)
        node = name_scopes.transform(node, self.ctx)

        with self.compiled(node, ops.name_scope) as result:
            result_op = result.TestClass().test_fn(constant_op.constant(1))
            first_result_input_name = result_op.op.inputs[0].name
            second_result_input_name = result_op.op.inputs[1].name
            self.assertIn('TestClass/test_fn/', first_result_input_name)
            self.assertNotIn('inner_fn', first_result_input_name)
            self.assertIn('TestClass/test_fn/inner_fn/',
                          second_result_input_name)
Exemplo n.º 14
0
  def test_class_name(self):

    class TestClass(object):

      def test_fn(self, l):

        def body(i):
          return i**2

        l += [4]
        return body(l)

    # Note that 'TestClass' was needed in the namespace here.
    node = self.parse_and_analyze(
        TestClass, {'TestClass': TestClass}, owner_type=TestClass)
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.TestClass().test_fn(constant_op.constant([1, 2, 3]))
      first_result_input_name = result_op.op.inputs[0].name
      second_result_input_name = result_op.op.inputs[1].name
      self.assertIn('TestClass/test_fn/', first_result_input_name)
      self.assertNotIn('body/', first_result_input_name)
      self.assertIn('TestClass/test_fn/body/', second_result_input_name)
Exemplo n.º 15
0
  def test_operator(self):

    class TestClass(object):

      def __call__(self, l):

        def inner_fn(i):
          return i ** 2

        l += 4
        return inner_fn(l)

    # Note that 'TestClass' was needed in the namespace here.
    node = self.parse_and_analyze(
        TestClass.__call__, {'TestClass': TestClass}, owner_type=TestClass)
    node = name_scopes.transform(node, self.ctx)

    with self.compiled(node, ops.name_scope) as result:
      result_op = result.__call__(TestClass(), constant_op.constant(1))
      first_result_input_name = result_op.op.inputs[0].name
      second_result_input_name = result_op.op.inputs[1].name
      self.assertIn('call__/', first_result_input_name)
      self.assertNotIn('inner_fn', first_result_input_name)
      self.assertIn('call__/inner_fn/', second_result_input_name)
def node_to_graph(node, ctx, nocompile_decorators):
    """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    ctx: An EntityContext object.
    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 entity
            dependencies that this node has.
  """
    # TODO(mdan): Verify arguments for correctness.

    # TODO(mdan): Factor out common elements.
    # These include:
    #   * code move between blocks
    #   * 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, ctx)

    # TODO(mdan): Clean this up.
    # Some intermediate analyses are not required, and some comments got orphaned.

    # Past this point, line numbers are no longer accurate so we ignore the
    # source.
    # TODO(mdan): Is it feasible to reconstruct intermediate source code?
    ctx.source_code = None
    node = ifexp.transform(node, ctx)
    node, deps = decorators.transform(node, nocompile_decorators)
    node = break_statements.transform(node, ctx)
    node = asserts.transform(node, ctx)

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

    node = _static_analysis_pass(node, ctx)
    node = single_return.transform(node, ctx)

    node = _static_analysis_pass(node, ctx)
    node = lists.transform(node, ctx)
    node = builtin_functions.transform(node, ctx)

    node = _static_analysis_pass(node, ctx)
    node = call_trees.transform(node, ctx, config.DEFAULT_UNCOMPILED_MODULES,
                                nocompile_decorators)
    node = control_flow.transform(node, ctx)

    # control_flow may create new symbols and change scopes.
    node = _static_analysis_pass(node, ctx)
    node = logical_expressions.transform(node, ctx)
    node = side_effect_guards.transform(node, ctx)
    node = name_scopes.transform(node, ctx)

    return node, deps
Exemplo n.º 17
0
def node_to_graph(node, ctx, nocompile_decorators):
  """Convert Python code to equivalent TF graph mode code.

  Args:
    node: A Python AST node representing the code to convert.
    ctx: An EntityContext object.
    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 entity
            dependencies that this node has.
  """
  # TODO(mdan): Verify arguments for correctness.

  # TODO(mdan): Factor out common elements.
  # These include:
  #   * code move between blocks
  #   * 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, ctx)

  # TODO(mdan): Clean this up.
  # Some intermediate analyses are not required, and some comments got orphaned.

  # Past this point, line numbers are no longer accurate so we ignore the
  # source.
  # TODO(mdan): Is it feasible to reconstruct intermediate source code?
  ctx.source_code = None
  node = ifexp.transform(node, ctx)
  node, deps = decorators.transform(node, nocompile_decorators)
  node = break_statements.transform(node, ctx)
  node = asserts.transform(node, ctx)

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

  node = _static_analysis_pass(node, ctx)
  node = single_return.transform(node, ctx)

  node = _static_analysis_pass(node, ctx)
  node = lists.transform(node, ctx)
  node = builtin_functions.transform(node, ctx)

  node = _static_analysis_pass(node, ctx)
  node = call_trees.transform(node, ctx, config.DEFAULT_UNCOMPILED_MODULES,
                              nocompile_decorators)
  node = control_flow.transform(node, ctx)

  # control_flow may create new symbols and change scopes.
  node = _static_analysis_pass(node, ctx)
  node = logical_expressions.transform(node, ctx)
  node = side_effect_guards.transform(node, ctx)
  node = name_scopes.transform(node, ctx)

  return node, deps