tensorflow AutoGraph 转换了这个函数:NameError: name 'Tuple' is not defined

ih99xse1  于 6个月前  发布在  其他
关注(0)|答案(5)|浏览(47)

问题类型

Bug

你是否在 TF nightly 中复现了这个 bug?

问题来源

source

Tensorflow 版本

2.11.0

自定义代码

OS 平台和发行版

Mac, Linux, Google Colab

移动设备

  • 无响应*

Python 版本

  • 无响应*

Bazel 版本

  • 无响应*

GCC/编译器版本

  • 无响应*

CUDA/cuDNN 版本

  • 无响应*

GPU 型号和内存

  • 无响应*

当前行为?

tf.function 在本地导入的类型注解使用时失败。我得到:

Cause: name 'Tuple' is not defined

注意:

  • 似乎重要的是 Tuple 是本地导入的。如果它在模块中全局导入,似乎没有问题。
  • 当我使用 from __future__ import annotations 时,也没有错误。但我认为这是因为它不会直接评估它,但它仍然缺少 Tuple 引用,尽管这可能并不重要。

重现问题的独立代码

import tensorflow as tf

print("TensorFlow:", tf.__version__)

tf.compat.v1.disable_eager_execution()

session = tf.compat.v1.Session()

# https://www.tensorflow.org/guide/function

def f(x: tf.Tensor):
  from typing import Tuple

  @tf.function
  def local_func(x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
    while tf.reduce_sum(x) > 1:
      tf.print(x)
      x = tf.tanh(x)
    return x, x

  return local_func(x)

session.run(f(tf.random.uniform([5])))

Colab 链接: https://colab.research.google.com/drive/1K69XH_RsU-Ux-RBfUd0B4eR8iJFTXoFM?usp=sharing

相关日志输出

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py", line 427, in converted_call
    converted_f = _convert_actual(target_entity, program_ctx)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py", line 269, in _convert_actual
    transformed, module, source_map = _TRANSPILER.transform(entity, program_ctx)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 282, in transform
    return self.transform_function(obj, user_context)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 490, in transform_function
    transformed_fn = factory.instantiate(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 213, in instantiate
    new_fn = bound_factory(**self._extra_locals)  # pylint:disable=not-callable
  File "/tmp/__autograph_generated_filem5lq6_hq.py", line 6, in inner_factory
    def tf__local_func(x: tf.Tensor) -> Tuple[(tf.Tensor, tf.Tensor)]:
NameError: name 'Tuple' is not defined
WARNING:tensorflow:AutoGraph could not transform <function f.<locals>.local_func at 0x7f15f5ff3af0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: name 'Tuple' is not defined
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
TensorFlow: 2.11.0
Converted call: <function f.<locals>.local_func at 0x7f15f5ff3af0>
    args: (<tf.Tensor 'x:0' shape=(5,) dtype=float32>,)
    kwargs: {}

<function f.<locals>.local_func at 0x7f15f5ff3af0> is not cached for subkey ConversionOptions[{}]
Source code of <function f.<locals>.local_func at 0x7f15f5ff3af0>:

@tf.function
def local_func(x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
  while tf.reduce_sum(x) > 1:
    tf.print(x)
    x = tf.tanh(x)
  return x, x

Transformed <function f.<locals>.local_func at 0x7f15f5ff3af0>:

# coding=utf-8
def tf__local_func(x: tf.Tensor) -> Tuple[(tf.Tensor, tf.Tensor)]:
    with ag__.FunctionScope('local_func', 'fscope', ag__.ConversionOptions(recursive=True, user_requested=True, optional_features=(), internal_convert_user_code=True)) as fscope:
        do_return = False
        retval_ = ag__.UndefinedReturnValue()

        def get_state():
            return (x,)

        def set_state(vars_):
            nonlocal x
            (x,) = vars_

        def loop_body():
            nonlocal x
            ag__.converted_call(ag__.ld(tf).print, (ag__.ld(x),), None, fscope)
            x = ag__.converted_call(ag__.ld(tf).tanh, (ag__.ld(x),), None, fscope)

        def loop_test():
            return (ag__.converted_call(ag__.ld(tf).reduce_sum, (ag__.ld(x),), None, fscope) > 1)
        ag__.while_stmt(loop_test, loop_body, get_state, set_state, ('x',), {})
        try:
            do_return = True
            retval_ = (ag__.ld(x), ag__.ld(x))
        except:
            do_return = False
            raise
        return fscope.ret(retval_, do_return)

Error transforming entity <function f.<locals>.local_func at 0x7f15f5ff3af0>
WARNING: AutoGraph could not transform <function f.<locals>.local_func at 0x7f15f5ff3af0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: name 'Tuple' is not defined
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
3ks5zfa0

3ks5zfa01#

你好@albertz,感谢你报告这个问题。
@tilakrayal,我能够在colab中使用TF v2.11和tf-nightly(2.13.0-dev20230228)复现这个问题。请查看以下的gists,分别是here-TF v2.11here-tf-nightly。谢谢!

7vux5j2d

7vux5j2d2#

你好,@albertz ,

看起来我们需要在函数外部导入元组。请参考附件中的 gist

将元组导入到全局级别后,它可以正常工作。

pod7payv

pod7payv3#

我知道。
但这是一个错误。这应该是不必要的。

pbwdgjma

pbwdgjma4#

你好,@albertz ,

你需要将整个函数 f()tf.function 装饰器包裹起来,然后在其中导入 Tuple 并使其生效。这适用于 1.x 和 2.x 版本。请参考附件 gist

每当我们用一些输入调用 f() 时,由于它在 tf.function 装饰器下有 local_func ,而当调用 tf.function 时,通常会有两个阶段。第一个阶段是跟踪创建 tf.Graph 的过程,第二个阶段是执行图(仅包含 TF 操作)。

如果我们将整个函数用 tf.function 包裹起来,那么这个工作就会按预期进行。

m4pnthwp

m4pnthwp5#

在我的情况下,我无法做到这一点。外部函数必须是常规的(非TensorFlow)Python函数。但无论如何,这仍然是一个TensorFlow的bug。不应该有必要使用这样的解决方法。

相关问题