import torch


def padding_type(spatial, config):
    """
    Apply type padding in a convolution operation.
    Arguments:
        spatial (tensor): spatial dimensions.
        config (dict): module parameters.
            config['padding'] == 'same' -> padding should be such 
                that spatial dimensions remainthe same
            config['padding'] == 'valid' -> padding is 0
    """
    ret = None
    if 'padding' not in config:
        return 0
    elif isinstance(config['padding'], list):
        ret = torch.tensor(config['padding'])
    elif config['padding'] == 'same':

        k = torch.tensor(config['kernel_size'])
        s = torch.tensor(config['stride'])

        ret = (spatial*(s-1)-1+k)//2

    elif config['padding'] == 'valid':
        ret = torch.zeros(spatial.shape).long()
    else:
        raise ValueError('Pad type is invalid')
    return list(ret.numpy())