Package: rankgen
RankGen - Improving Text Generation with Large Ranking ModelsThis is the official repository for our preprint,RankGen - Improving Text Generation with Large Ranking Models. RankGen is a 1.2 billion encoder model which maps prefixes and generations from any pretrained English language model to a shared vector space. RankGen can be used to rerank multiple full-length samples from an LM, and it can also be incorporated as a scoring function into beam search to significantly improve generation quality (0.85 vs 0.77MAUVE, 75% preference according to humans annotators who are English writers). RankGen can also be used like a dense retriever, and achieves state-of-the-art performance onliterary retrieval.This repository contains human evaluation data, links to HuggingFace-compatible model checkpoints, and code to integrate RankGen in beam search on HuggingFace models. RankGen is trained by fine-tuning the T5-XL encoder using theT5X library.Updates(July 2022) RankGen is now aPyPI package, just runpip install rankgento use it!(July 2022) RankGen checkpoints are now available on the HuggingFace Model Hub (link)!Model checkpointsAll RankGen checkpoints are available on the HuggingFace Model Hub -linkWe recommend usingRankGen-XL-all.CheckpointSizeHub Model NameHF Hub LinkRankGen-base-all0.1Bkalpeshk2011/rankgen-t5-base-alllinkRankGen-large-all0.3Bkalpeshk2011/rankgen-t5-large-alllinkRankGen-XL-all1.2Bkalpeshk2011/rankgen-t5-xl-alllinkRankGen-XL-PG191.2Bkalpeshk2011/rankgen-t5-xl-pg19linkOlder versions of the checkpoints:RankGen XL checkpoints compatible withT5XEmbeddingGeneratorLegacy-hereT5X JAX checkpoints (base, large, XL) -hereSetupRequirementsPython 3.7+,torch(CUDA recommended),transformersInstallation(from PyPI)python3.7 -m virtualenv rankgen-venv
source rankgen-venv/bin/activate
pip install rankgen(from source)python3.7 -m virtualenv rankgen-venv
source rankgen-venv/bin/activate
git clone https://github.com/martiansideofthemoon/rankgen
cd rankgen
pip install --editable .Data Download / TestGet the datahereand place folder in root directory. Alternatively, usegdownas shown below,gdown --folder https://drive.google.com/drive/folders/1DRG2ess7fK3apfB-6KoHb_azMuHbsIv4Run the test script to make sure the RankGen checkpoint has loaded correctly,python -m rankgen.test_rankgen_encoder --model_path kalpeshk2011/rankgen-t5-base-all

### Expected output
0.0009239262409127233
0.0011521980725477804Using RankGenLoading RankGen is simple using the HuggingFace APIs, but we suggest usingRankGenEncoder, which is a small wrapper around the HuggingFace APIs for correctly preprocessing data and doing tokenization automatically. Please seerankgen/test_rankgen_encoder.pyfor an example of the usage or see below.from rankgen import RankGenEncoder, RankGenGenerator

rankgen_encoder = RankGenEncoder("kalpeshk2011/rankgen-t5-xl-all")Encoding text to prefix/suffix vectorsprefix_vectors = rankgen_encoder.encode(["This is a prefix sentence."], vectors_type="prefix")
suffix_vectors = rankgen_encoder.encode(["This is a suffix sentence."], vectors_type="suffix")Generating text# use a HuggingFace compatible language model
generator = RankGenGenerator(rankgen_encoder=rankgen_encoder, language_model="gpt2-medium")

inputs = ["Whatever might be the nature of the tragedy it would be over with long before this, and those moving black spots away yonder to the west, that he had discerned from the bluff, were undoubtedly the departing raiders. There was nothing left for Keith to do except determine the fate of the unfortunates, and give their bodies decent burial. That any had escaped, or yet lived, was altogether unlikely, unless, perchance, women had been in the party, in which case they would have been borne away prisoners."]

# Baseline nucleus sampling
print(generator.generate_single(inputs, top_p=0.9)[0][0])
# Over-generate and re-rank
print(generator.overgenerate_rerank(inputs, top_p=0.9, num_samples=10)[0][0])
# Beam search
print(generator.beam_search(inputs, top_p=0.9, num_samples=10, beam_size=2)[0][0])Running beam search with RankGen (reproducing experiments in the paper)The main file isrankgen/rankgen_beam_search.py. To execute it,python rankgen/rankgen_beam_search.py \
    --dataset rankgen_data/wiki.jsonl \
    --rankgen_encoder kalpeshk2011/rankgen-t5-xl-all \
    --num_tokens 20 --num_samples 10 --beam_size 2 \
    --output_file outputs_beam/wiki_t5_xl_beam_2_tokens_20_samples_10.jsonlEvaluating using MAUVE (make sure JSONL file has several thousand generations for intuitive MAUVE scores, 7713 in our experiments),python rankgen/score_multi_beam.py --dataset outputs_beam/wiki_t5_xl_beam_2_tokens_10_samples_10.jsonlHuman evaluation dataWe conducted our human evaluation on Upwork, hiring English teachers and writers. We performed blind A/B testing between RankGen and nucleus sampling. We also asked our annotators to provide a 1-3 sentence explanation. You can find all the 600 annotations across two files inhuman-eval-data. To compute the evaluation scores run,python rankgen/score_ab_text.pyCitation InformationIf you use RankGen, please cite it as follows:@article{krishna2022rankgen,
  title={RankGen: Improving Text Generation with Large Ranking Models},
  author={Kalpesh Krishna and Yapei Chang and John Wieting and Mohit Iyyer},
  journal={arXiv preprint arXiv:2205.09726},
  year={2022}
}