tensorflow tf.slice是否会导致分配和/或内存复制? [英] does tensorflow tf.slice incur allocation and/or memory copy?

查看:117
本文介绍了tensorflow tf.slice是否会导致分配和/或内存复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b = tf.slice(a, [...], [...])

分配一个新的内存缓冲区,然后从a的缓冲区复制?还是a和b共享相同的缓冲区?

allocate a new memory buffer and then copy from a's buffer? or do a and b share the same buffer?

... = tf.nn.convolution(tf.slice(a, [...], [...]), ...)

在这种情况下,未命名切片.这会导致分配和复制吗?

In this case, the slice is not being named. Does this incur an allocation and copy?

通常,是否有一些资源可用于学习这些内存管理详细信息?

In general, is there some resource for learning these memory management details?

谢谢!

推荐答案

我也对此感到很好奇,并进入了

I was curious of this as well and went into the kernel implementation of tf.slice.

唯一的一次不分配内存的时间是切片是标识切片(因此没有任何更改),并且切片是0维对齐的.

The only time memory is not allocated is when the slice is the identity slice (so there are no changes), and when the slice is dimension 0 aligned.

身份行

  if (is_identity) {
    VLOG(1) << "Slice identity";
    context->set_output(0, input);
    *done = true;
    return;
  }

Dim0Aligned

  if (slice_dim0 &&
      IsDim0SliceAligned<T>(input.shape(), (*begin)[0], (*size)[0])) {
    VLOG(1) << "Slice dim 0: " << input.shape().DebugString();
    CHECK_GE(input.dims(), 1);  // Otherwise, is_identity should be true.
    context->set_output(0, input.Slice((*begin)[0], (*begin)[0] + (*size)[0]));
    *done = true;
    return;
  }

否则,以下将被称为

  OP_REQUIRES_OK(context, context->allocate_output(0, *output_shape, result));

这篇关于tensorflow tf.slice是否会导致分配和/或内存复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆