I thought of this approach, but I think the padding in the conv1d function should depend of the group; some inputs have the right result but others don’t. Is there a way of doing that?
import torch
import torch.nn as nn
import torch.nn.functional as F

def create_kernal(kernel_size, largest_kernel):
    #return torch.cat([torch.ones(kernel_size), torch.zeros(kernel_size - 1 + max(largest_kernel + (largest_kernel - 1) - (kernel_size + (kernel_size - 1)), 0))])
    pad = kernel_size - 1 + max(largest_kernel + (largest_kernel - 1) - (kernel_size + (kernel_size - 1)), 0)
    offset = int(pad % 2 == 1)
    pad = pad // 2
    return F.pad(torch.ones(kernel_size), (pad + offset, pad), mode='constant', value=0)

inputs1 = [0 for _ in range(20)]
inputs1[8] = 1
inputs2 = [0 for _ in range(20)]
inputs2[13] = 1
inputs3 = [0 for _ in range(20)]
inputs3[1] = 1
inputs4 = [0 for _ in range(20)]
inputs4[0] = 1
inputs5 = [0 for _ in range(20)]
inputs5[18] = 1
inputs = torch.FloatTensor([inputs1, inputs2, inputs3, inputs4, inputs5])

kernel_size1 = 6
kernel_size2 = 4
kernel_size3 = 5
kernel_size4 = 8
kernel_size5 = 4
largest_kernel = max(kernel_size1, kernel_size2, kernel_size3, kernel_size4, kernel_size5)

ks = kernel_size1 - 1 + max(largest_kernel + (largest_kernel - 1) - (kernel_size1 + (kernel_size1 - 1)), 0)
kernels = torch.cat([create_kernal(kernel_size, largest_kernel).unsqueeze(0) for kernel_size in (kernel_size1, kernel_size2, kernel_size3, kernel_size4, kernel_size5)], axis=0)

inputs = inputs.unsqueeze(0) # Inverse channel and batch
kernels = kernels.unsqueeze(1)
print('Predicted')
conv_res = F.conv1d(inputs, kernels, padding=largest_kernel + 1, groups=inputs.size(1)).long()[:, :, :inputs.size(-1)]
for x in conv_res.squeeze().tolist():
    print('({})'.format(len(x)), x)
print('Expected')
print('(20) [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]')
print('(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]')
print('(20) [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]')
print('(20) [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]')
print('(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]')

print()

Predicted
(20) [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
(20) [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(20) [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
Expected
(20) [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
(20) [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(20) [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(20) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]