首页 > 解决方案 > tensorflow tf.slice 会导致分配和/或内存复制吗?

问题描述

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

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

而且,怎么样

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

在这种情况下,切片没有被命名。这会导致分配和复制吗?

一般来说,是否有一些资源可以学习这些内存管理细节?

谢谢!

标签: pythontensorflowmemory-management

解决方案


我对此也很好奇,并进入了tf.slice.

唯一不分配内存的情况是切片是标识切片(因此没有更改),并且切片是维度 0 对齐的。

身份线

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

暗淡0对齐

  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));

推荐阅读