Overriding a Tensor at a given index using `data_ptr`

Hi everyone.

I’m using C++ libtorch for performance reasons. I have the following tensor representing a batch of images [128, 3, 224, 224]. I would like to replace the image at the given index k as follows:

auto nbytes = tensor[0].nbytes();
void* ptr = tensor.data_ptr() + k * nbytes; // I think this is fine as PyTorch is indexing row-wise
*ptr = ...

Does that look correct to you?

The reason I’m not using the .index_put_ method of the tensor is because the latter increases its version counter of the tensor marking it as ‘dirty’, which I do not want.

Additional question: how do I make sure to free the overwritten memory in the above example?