does tensorflow tf.slice incur allocation and/or memory copy?

后端 未结 1 414
旧时难觅i
旧时难觅i 2021-01-20 22:00

Does

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

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

相关标签:
1条回答
  • 2021-01-20 22:55

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

    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.

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

    Otherwise the following will be called

      OP_REQUIRES_OK(context, context->allocate_output(0, *output_shape, result));
    
    0 讨论(0)
提交回复
热议问题