def _truncate_reference_sections(references, model_tokenizer):
    """
    Truncate target sections so that the length of tokenized section <= 512

    *Note*: generations are at most 512 tokens in tokenized format (max_target_length = 512).
    For evaluations with word-overlap metrics (bleu, rouge), we need to also tokenize the reference sections
    in order to have comparable length.

    :param references: list of target sections
    :param model_tokenizer: HuggingFace Tokenizer
    :return: list of target truncated sections
    """

    # keep the count of truncated sections
    COUNT_SECTIONS_TRUNCATED = 0

    # save the truncated references in list
    truncated_references = []

    # for each section in references
    for ref_section in references:

        # tokenize with <model_tokenizer>
        ref_section_tokenized = model_tokenizer(ref_section)
        input_ids = ref_section_tokenized['input_ids']

        # truncate the tokenized ref_section if its length > 512
        if len(input_ids) > 512:

            COUNT_SECTIONS_TRUNCATED += 1

            # choose only <= 512
            input_ids = input_ids[:512]

            # decode back
            truncated_ref_text = model_tokenizer.decode(input_ids)

            truncated_references.append(truncated_ref_text)

        # otherwise just leave section as it is
        else:
            truncated_references.append(ref_section)

    print("Num. of sections truncated: ", COUNT_SECTIONS_TRUNCATED)

    return truncated_references