Duplicate elements based on a kernel

Hello,

Is there a known function able to duplicate values following this behavior?

x = torch.tensor([[1., 2.], 
                  [0., 3.]])

some_function(x, kernel=(2, 2))
>> [[1., 1., 2., 2.], 
    [1., 1., 2., 2.],
    [0., 0., 3., 3.],
    [0., 0., 3., 3.]]

some_function(x, kernel=(3, 2))
>> [[1., 1., 2., 2.], 
    [1., 1., 2., 2.],
    [1., 1., 2., 2.],
    [0., 0., 3., 3.],
    [0., 0., 3., 3.],
    [0., 0., 3., 3.]]

Thank you

I don’t think there is a single operator that does what you want, but it’s possible to achieve the behavior via some broadcasting and view logic:

import torch
x = torch.tensor([[1., 2.], [3., 4.]])
kernel = (3, 2)
x.view(2, 1, 2, 1).expand(2, kernel[0], 2, kernel[1]).contiguous().view(2 * kernel[0], 2 * kernel[1])

gives

tensor([[1., 1., 2., 2.],
        [1., 1., 2., 2.],
        [1., 1., 2., 2.],
        [3., 3., 4., 4.],
        [3., 3., 4., 4.],
        [3., 3., 4., 4.]])

Thank you
Can be solved using Kronecker product but not added to the main branch yet (torch.kron — PyTorch master documentation).