Reset THTensor_(narrow) operation

Is it possible to undo a narrow?

After checking, a narrow does:

if(firstIndex > 0) self->storageOffset += firstIndex*self->stride[dimension];
_ self->size[dimension] = size;_

Could restore the tensor by keeping the original size at dimension, call it s
Then:
self->size[dimension]=s;
self->storageOffset=0; (or offset before narrowing)

Release 0.5 and on won’t allow for this direct access to the tensor structure,
so I was wondering if there was a way to “un-narrow” without creating a separate structure.

Thanks

You can use Tensor.set_ from Python or C++ (ATen).

https://pytorch.org/docs/stable/tensors.html#torch.Tensor.set_

But if you can save the storage offset before narrowing, why not just save a reference to the un-narrowed tensor?

Thanks for the reply.

I’m trying to link the pytorch tensors and nn functions to another environment via a c interface.
e.g. allocate tensors via THTensor_… or THCTensor_… routines, pass the c pointer through a c-api to another interpreter.

No python, but i could probably start up higher with the c++ library routines if i could figure out how to pass the pointer through, e.g.
Tensor t=CPU(kFloat).ones({3, 4});
void *p=&t;

i don’t think i can use a pointer like the above since tensor t may be deleted out from under me as it goes out of scope.

so for now, i’ve been using the c-structure for a tensor.
processing through subsets of inputs is quick without the need to copy sizes & strides and the storage ptr to another tensor and incrementing the reference count.

origOffset = t->storageOffset;
origSize=t->size[dim];

then, subsets are quick:
t->size[dim]=n;
t->storageOffset = origOffset + index * t->stride[dim]