gpt4 book ai didi

c++ - TensorFlow 操作 ` IsExpensive()` 的含义?

转载 作者:IT老高 更新时间:2023-10-28 23:01:07 30 4
gpt4 key购买 nike

有一个method in OpKernel

 // Returns true iff this op kernel is considered "expensive". The
// runtime may use this flag to optimize graph execution for example
// to "inline" inexpensive kernels.
virtual bool IsExpensive() { return expensive_; }

似乎默认情况下 GPU are considered as inexpensive 上的所有操作而 CPU、SYSL 则被标记为昂贵。

expensive的定义和作用有点难以理解。没有information in the guide .

  1. IsExpensive 应为 false, true 时是否有任何具体指导?
  2. 如果某项操作被标记为昂贵,会有什么影响?到目前为止,我只能说,主动分析使用这个 just as a hint?唯一查询此属性的地方是 in the scheduler但没有解释 inline 是什么意思。
  3. 与“1”连用。我应该在我的自定义 Ops 中关心它吗?
  4. 虽然有道理,但任何 AsyncOp(如 RemoteFusedGraphExecuteOp )都很昂贵,MPIAllgatherOp似乎被定义为不昂贵。这不是矛盾吗?

我在问,因为 IdentityOp被明确标记为便宜。我想知道,我是否也应该在我的自定义操作中重写此方法,因为每个 CPU 版本(甚至 任何 自定义代码)都被标记为昂贵。

XLA 的整个逻辑似乎是关于 an instruction is expensive或不。因此,这可能是需要考虑的重要部分。因此,关于真/假的抛硬币可能不是在我的自定义操作中决定返回值的最佳方式。

最佳答案

在回答您的问题之前,我认为值得尝试了解 TensorFlow 如何使用线程来完成您的工作。为此,我建议您阅读 this related and very good SO post .

您会发现 TensorFlow 使用线程池来完成工作。昂贵的 Ops 被安排在线程池上执行,而廉价的 Ops 是“内联”执行的,这意味着由安排任务的同一线程执行(旁注:从您链接的源文件中,您只发现一个异常,即当inline_ready 队列是空的,线程可以自己执行最后一个昂贵的操作。)。

考虑到这一点,让我们尝试回答您的问题。

  1. Is there any specific guideline when IsExpensive should be false, true?

我在 TensorFlow 手册中找不到具体的指导方针,但是,从我们上面讨论的内部结构来看,当将任务调度到线程池的偏移量与需要执行任务的时间。

  1. What's the effect if an operation is flagged as expensive? So far I can only tell, that active profiling uses this just as a hint ? The only place querying this property is in the scheduler but without explaining what being inline means.

效果如下,每次Ops的IsExpensive方法返回false可能会被插入inline_ready队列,可能阻塞线程从执行进一步的任务,从而停止你的程序。相反,如果 Ops IsExpensive 方法返回 true,它将被调度到线程池上执行,并且调度线程可以自由地继续在进程循环中执行其任务。

  1. In conjunction with "1." should I care about it in my custom Ops?

我认为您应该关心并尝试尽可能多地推理您 Op. 的执行时间。然后决定如何实现 IsExpensive 方法。

  1. While it makes sense, that any AsyncOp (like RemoteFusedGraphExecuteOp) is expensive, MPIAllgatherOp seems to be defined as not expensive. Isn't this a contradiction?

不,这并不矛盾。如果您阅读MPIAllgatherOp的评论,您会发现以下内容:

// Although this op is handled asynchronously, the ComputeAsync call is
// very inexpensive. It only sets up a CollectiveOpRecord and places it
// in the table for the background thread to handle. Thus, we do not need
// a TF pool thread to perform the op.

其中明确指出,为线程池调度此任务几乎只是开销。因此,内联执行它很有意义。

关于c++ - TensorFlow 操作 ` IsExpensive()` 的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51397757/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com