import torch


def parse_conv(conv, weights, offset):
    """
    Args:
        conv (nn.Conv2d): pytorch convolution module.
        weights (numpy.ndarray): pretrained weights data.
        offset (int): current position in the weights file.
    Returns:
        offset (int): new position in the weights file.
    """

    # bias
    if hasattr(conv, 'bias') and conv.bias is not None:
        param_length = conv.bias.numel()
        param = torch.from_numpy(weights[offset:offset + param_length])
        conv.bias.data.copy_(param.view_as(conv.bias))
        offset += param_length

    # conv
    param_length = conv.weight.numel()
    param = torch.from_numpy(weights[offset:offset + param_length])
    conv.weight.data.copy_(param.view_as(conv.weight))
    offset += param_length
    return offset