import torch


def flipx4_forward(model, inp):
    """Flip testing with X4 self ensemble, i.e., normal, flip H, flip W, flip H and W
    Args:
        model (PyTorch model)
        inp (Tensor): inputs defined by the model

    Returns:
        output (Tensor): outputs of the model. float, in CPU
    """
    with torch.no_grad():
        # normal
        output_f = model(inp)

        # flip W
        output = model(torch.flip(inp, (-1, )))
        output_f = output_f + torch.flip(output, (-1, ))
        # flip H
        output = model(torch.flip(inp, (-2, )))
        output_f = output_f + torch.flip(output, (-2, ))
        # flip both H and W
        output = model(torch.flip(inp, (-2, -1)))
        output_f = output_f + torch.flip(output, (-2, -1))

        output_f = output_f / 4

    return output_f