def METHOD_NAME(
    models, sample, prefix_tokens, src_dict, remove_bpe, has_langtok=False
):
    model = EnsembleModel(models)
    # model.forward normally channels prev_output_tokens into the decoder
    # separately, but SequenceGenerator directly calls model.encoder
    encoder_input = {
        k: v for k, v in sample["net_input"].items() if k != "prev_output_tokens"
    }
    # compute the encoder output for each beam
    encoder_outs = model.forward_encoder(encoder_input)
    np_encoder_outs = encoder_outs[0].encoder_out.cpu().numpy().astype(np.float32)
    encoder_mask = 1 - encoder_outs[0].encoder_padding_mask.cpu().numpy().astype(
        np.float32
    )
    encoder_mask = np.expand_dims(encoder_mask.T, axis=2)
    if has_langtok:
        encoder_mask = encoder_mask[1:, :, :]
        np_encoder_outs = np_encoder_outs[1, :, :]
    masked_encoder_outs = encoder_mask * np_encoder_outs
    avg_pool = (masked_encoder_outs / encoder_mask.sum(axis=0)).sum(axis=0)
    return avg_pool