def check_batch_state(state, max_len, pad_token):
    """Check batch of states and left pad or trim if necessary.

    Args:
        state (list): list of of L decoder states (B, ?, dec_dim)
        max_len (int): maximum length authorized
        pad_token (int): padding token id

    Returns:
        final (list): list of L decoder states (B, pred_len, dec_dim)

    """
    final_dims = (len(state), max_len, state[0].size(1))
    final = state[0].data.new(*final_dims).fill_(pad_token)

    for i, s in enumerate(state):
        curr_len = s.size(0)

        if curr_len < max_len:
            final[i, (max_len - curr_len) : max_len, :] = s
        else:
            final[i, :, :] = s[(curr_len - max_len) :, :]

    return final