在图形模式下运行 "defun"。

问题描述 投票:0回答:1

我在TF中看到这样的代码。

from tensorflow.python.eager import function

...

class _PerDeviceGenerator(dataset_ops.DatasetV2):
  """A `dummy` generator dataset."""

  def __init__(self, shard_num, multi_device_iterator_resource, incarnation_id,
               source_device, element_spec):

    ...

    # TODO(b/124254153): Enable autograph once the overhead is low enough.
    @function.defun(autograph=False)  # Pure graph code.
    def _remote_init_func():
      return functional_ops.remote_call(
          target=source_device,
          args=init_func_concrete.captured_inputs,
          Tout=[dtypes.string],
          f=init_func_concrete)

    self._init_func = _remote_init_func._get_concrete_function_internal()  # pylint: disable=protected-access

    ...

    variant_tensor = gen_dataset_ops.generator_dataset(
        self._init_captured_args,
        self._next_captured_args,
        self._finalize_captured_args,
        init_func=self._init_func,
        next_func=self._next_func,
        finalize_func=self._finalize_func,
        **self._flat_structure)
    super(_PerDeviceGenerator, self).__init__(variant_tensor)

(这段代码来自TF 1. 15. 0)

我试图理解这段代码。

更具体地说,我想知道的是 defun 在这里。我想 defun 是用于急切模式的,但在这里,这段代码似乎同时用于急切模式和图形模式。

但在这里,这段代码似乎是同时用于急切模式和图模式。还是说这是错误的,这只在急切模式下有效?MultiDeviceIterator,它有这样的检查 if context.executing_eagerly() 而后用于 _PerDeviceGenerator 对于急切模式和图形模式。还是图模式也被破坏了?为什么检查 executing_eagerly 然后呢?)

什么是 defun 在图形模式下怎么办?

_get_concrete_function_internal 是一些内部API?

tensorflow
1个回答
0
投票

defun_get_concrete_function_internal 是内部API。你应该更喜欢使用 tf.function 尽可能 def_function.function 在处理内部代码时)。) defun 是一个旧的API,主要重复了 function,将来很可能会被删除。

话虽如此,但。defun 不仅仅是急切的模式(也不是 function). 它们都创建了一个TF函数,在图模式下,有 "函数调用 "操作,让你调用这些函数。在急切模式下,它只是让你调用一个TF图。但是在图模式下,它们让你减少图的大小,就像你通过将普通代码因子到函数中来减少普通代码的大小一样。

© www.soinside.com 2019 - 2024. All rights reserved.