Search code examples
torch

Torch - narrow() without memory copy


Is there any way of using :narrow in place and avoiding having to make a copy? I.e. :resize is the in place version of :reshape, is there an equivalent for narrow?


Solution

  • As stated in the docs, narrow does not perform a memory copy:

    For methods narrow, select and sub the returned tensor shares the same Storage as the original. Hence, any modification in the memory of the sub-tensor will have an impact on the primary tensor, and vice-versa. These methods are very fast, as they do not involve any memory copy.

    Example:

    th> x = torch.Tensor{{1, 2}, {3, 4}}
    
    th> y = x:narrow(1, 2, 1)
    
    th> print(x:storage():data())
    cdata<double *>: 0x0079f240
    
    th> print(y:storage():data())
    cdata<double *>: 0x0079f240
    

    They only return a new tensor, i.e. a new object that uses the same storage behind the scenes.

    If you really want to modify the original tensor in-place you can use set:

    th> x:set(y)
     3  4
    [torch.DoubleTensor of size 1x2]
    

    Or even simpler x = y.