gpt4 book ai didi

python - tensorflow tf.slice 会产生分配和/或内存复制吗?

转载 作者:行者123 更新时间:2023-11-28 19:02:44 25 4
gpt4 key购买 nike

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

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

然后呢

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

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

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

谢谢!

最佳答案

我对此也很好奇,并深入研究了 tf.slice 的内核实现。 .

唯一不分配内存的时间是切片是标识切片(因此没有变化),并且切片是维度 0 对齐的。

Identity Lines

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

关于python - tensorflow tf.slice 会产生分配和/或内存复制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779869/

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