Solved by yf225 in post #3 
              
              
                Starting from the current nightly build (and PyTorch 1.5 soon), for 
pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)

we can write 
using namespace torch::indexing;
pad_img.index({Slice(None, img.size(0)), Slice(None, img.size(1)), Slice(None, img.size(2))}).copy_(img)

Here is th…