wikillm

Paper · 2022-06-09 · cs.CL, cs.AI, cs.CY, cs.LG

Factuality Enhanced Language Models for Open-Ended Text Generation

Measures and reduces nonfactual text from open-ended LMs: FactualityPrompts with Wikipedia-based NE and NLI metrics, factual-nucleus sampling (top-p with a per-sentence decay and floor), and continued Wikipedia training with a title prefix and sentence-completion loss. Author-reported: 530B named-entity error on factual prompts falls from 33.3% (top-p) to 14.5% with both changes.

Hallucination mitigationHallucination detectionNucleus (top-p) samplingLanguage model pretrainingfactualitydecodingcontinued-pretrainingevaluation

arXivPDFCodeMarkdown14 min read

AI-compiled by claude-haiku-5-5 on 2026-10-18, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

Large language models write fluent text, but they often state things that are not true, such as a wrong year or the wrong author of a film. This paper first builds a way to measure that: it takes sentence starts drawn from fact-checking claims and checks whether the model's continuation names things that do not appear in the matching Wikipedia article.

The authors then find two causes that can be fixed. The first is the sampling step: the usual "top-p" way of picking the next word adds randomness at every word, and that randomness drifts away from facts. Their fix lowers the randomness as a sentence goes on and resets it at each new sentence, so the start of a sentence stays varied and its end stays anchored to what the model knows. The second is training: the model learns facts badly when a Wikipedia article is cut into chunks, so each sentence is given its article title as a prefix, and the training loss focuses on the later half of each sentence, where the facts are.

Together these changes reduce made-up names and facts in the authors' tests, at some cost in variety of wording.

TL;DR

  • Introduces FactualityPrompts, a benchmark of factual and nonfactual prompts from FEVER, with Wikipedia-based metrics for named-entity error and entailment §3.
  • Factual-nucleus sampling makes the top-p cut-off shrink within each sentence (decay λ), resets at sentence starts, and stops at a floor ω §5.1.
  • Continued Wikipedia training with a title prefix ("TopicPrefix") and a sentence-completion loss that scores only the later part of each sentence §6.
  • Author-reported: with both changes, 530B named-entity error on factual prompts drops from 33.3% to 14.5%, and entailment rises from 11.8% to 25.5% tab. 5.
  • Trade-off: diversity and repetition get worse than top-p sampling §5.2 tab. 4.

Problem

Pretrained LMs produce nonfactual continuations when used for open-ended generation, and this matters for safe deployment §1. Earlier factuality work targets fine-tuned models on tasks such as summarisation, where a source text is given; the authors focus on pretrained LMs without retrieval §1 §2. Open-ended text has no reference answer, so measuring factuality needs Wikipedia as a ground-truth source §3. Top-p sampling is popular for diversity but is reported to hurt factuality §4 §5.1. Continued training on Wikipedia alone does not reliably improve factuality §1 §6.3. Retrieval-augmented models are a known alternative but add complexity and cost §1.

Method

Benchmark (FactualityPrompts). Prompts are FEVER validation claims: the SUPPORTED claims form the factual set and the REFUTED claims the nonfactual set. Very short claims are removed. Each set has 8,000 prompts, averaging 9.77 and 9.48 tokens §3.1 app. D.

Ground truth. For each prompt, the document-level reference is the Wikipedia article that FEVER annotates for the claim. The sentence-level reference is found in that article in two ways: the sentence with the highest TF-IDF cosine to the generated text, and the sentence with the highest similarity in a sentence-embedding space §3.2.

Check-worthy filter. Before scoring, a continuation's first sentence is skipped if it has no named entity, if it contains first-person pronouns (I, we, us), or if it contains a question mark app. B.

Named-entity error (NE error). For each scored continuation, the share of its named entities that do not appear in the document-level reference. Multi-word entities count as matched on partial n-gram overlap, ignoring stopwords §3.3.

Entailment ratio. A RoBERTa model fine-tuned on MNLI classifies each pair of (reference sentence, generated claim). A generation counts as factual only when it is classified as entailed; neutral does not count §3.3.

Quality. Fluency is the mean perplexity under a 1.3B LM. Diversity is the mean number of distinct 4-grams, normalised by text length, over 10 generations per prompt (160,000 generations per setting). Repetition uses the metric code of Holtzman et al. §3.3.

Human check. 200 continuations were labelled against Wikipedia by two crowd workers and one expert; majority vote is used, and the expert labels are reported separately §3.4 tab. 2.

Decoding analysis. Models of 126M to 530B parameters are compared under greedy decoding and top-p 0.9 §4 tab. 3.

Factual-nucleus sampling. The nucleus probability for the t-th token of a sentence is

p_{t} = \max\{\omega,\ p \times \lambda^{t-1}\} §5.1

with three parts: λ-decay shrinks p at each step; p-reset restores the default p at the start of each new sentence, detected when the previous token was a full stop; and the ω-bound stops p from falling below a floor §5.1. Algorithm:

Pseudocode
# Factual-nucleus sampling for one continuation  [§5.1]
# Inputs: prompt tokens; p, lam, omega (best reported: 0.9, 0.9, 0.3)
# Output: generated tokens, stopping at end-of-text or max_len (150)

def factual_nucleus_generate(model, prompt, p, lam, omega, max_len):
    tokens = list(prompt)
    # position of the next token inside the current sentence  [§5.1]
    position = 1
    while len(tokens) < max_len:
        # the nucleus shrinks with position but never below omega  [§5.1]
        p_t = max(omega, p * lam ** (position - 1))
        # sample from the smallest set whose mass reaches p_t  [§5.1]
        next_token = sample_top_p(model.next_token_probs(tokens), p_t)
        tokens.append(next_token)
        if next_token == END_OF_TEXT:
            break
        # restart the schedule after a full stop, a new sentence  [§5.1]
        if next_token == FULL_STOP:
            position = 1
        else:
            position += 1
    return tokens

Factuality-enhanced training. Two changes are applied to continued pretraining on Wikipedia §6.

  1. TopicPrefix. Each sentence is prefixed with its article title, written as "Title ==> sentence" in the paper's example §6.1 app. C.
  2. Sentence-completion loss. For each sentence a pivot is chosen, and token losses before the pivot are zeroed out. Training stays autoregressive. Three pivot strategies are compared: half (0.5 × sentence length), random (uniform between 0.25 and 0.75 × sentence length), and root (the position of the dependency root) §6.2. The paper recommends half as the default §6.2.
Pseudocode
# Sentence-completion loss over one training document  [§6.2]
# Sentences were already prefixed with their article title  [§6.1]
# Normalisation of the mean is not stated in the paper

def sentence_completion_loss(model, sentences, strategy):
    scored_losses = []
    for sentence in sentences:
        tokens = tokenize(sentence)
        # one negative log-likelihood per token  # (T,)
        token_losses = next_token_loss(model, tokens)
        # first token that counts toward the loss  [§6.2]
        start = pivot_token_index(sentence, strategy)
        # earlier tokens get zero weight; decoding stays autoregressive  [§6.2]
        scored_losses.extend(token_losses[start:])
    return mean(scored_losses)
Pseudocode
# Where the loss starts inside one sentence  [§6.2]

def pivot_token_index(sentence, strategy):
    words = dependency_words(sentence)
    if strategy == "half":
        # the middle word  [§6.2]
        word_index = len(words) // 2
    elif strategy == "random":
        # uniform between 25% and 75% of the sentence  [§6.2]
        word_index = int(len(words) * uniform(0.25, 0.75))
    else:
        # the dependency root  [§6.2]
        word_index = index_of_root(words)
    # map the chosen word to its first subword token
    return first_token_of_word(sentence, word_index)

Figures: fig. 1 shows the evaluation pipeline (continuation, check-worthy filter, evidence retrieval, metrics). fig. 2 plots diversity and repetition against NE error for top-p sampling and factual-nucleus sampling across several settings; the caption gives 0.767 as the diversity of 5,000 random Wikipedia documents for reference. tab. 1 gives example continuations under greedy, top-p and factual-nucleus decoding.

Hyperparameters

NameValueWhereNotes
Factual-nucleus p (initial)0.9§5.1 §5.2Same as the top-p baseline
λ (decay factor)0.9 or 0.5§5.2 tab. 40.9 used in the reported best setting
ω (lower bound)0.3§5.2 tab. 4Also tested: 0.2 and 0.7
p-resetat each sentence start§5.1Sentence start = previous token is a full stop
Max generation length150 subwordsapp. CInference only
Model 126M12 layers, hidden 768, 12 headsapp. CArchitecture table
Model 357M24 layers, hidden 1024, 16 headsapp. C
Model 1.3B24 layers, hidden 2048, 32 headsapp. CUsed for decoding and training ablations
Model 8.3B40 layers, hidden 4096, 64 headsapp. C
Model 530B105 layers, hidden 20480, 128 headsapp. C
Continued training, 1.3BLR 2e-6; batch 64; max seq 2048app. C
Continued training, 530BLR 1e-5; batch 512; max seq 2048app. C
Sentence-completion pivot, half0.5 × sentence length§6.2Recommended default
Sentence-completion pivot, randomuniform [0.25, 0.75] × length§6.2
Sentence-completion pivot, rootposition of dependency ROOT§6.2
Evidence retrievaltop-1 by TF-IDF, top-1 by sentence embedding§3.2Embedding model not named in the paper
Prompts8,000 factual, 8,000 nonfactualapp. DFrom FEVER validation
Generations per prompt10 seeds (greedy: 1)§3.3 README.md
Diversity n-gram order4§3.3
Entailment modelRoBERTa fine-tuned on MNLI§3.3Official code loads roberta.large.mnli factuality_metric.py
Fluency LM1.3B§3.3
Human annotations200 continuations; 2 crowd + 1 expert§3.4Majority vote
Optimiser, warm-up, LR schedule, weight decaynot stated
Training steps and corpus sizenot stated
Hardware for main runsnot statedThe only hardware note sits in commented-out LaTeX

Results

Author-reported. Setup: FactualityPrompts (8,000 factual and 8,000 nonfactual prompts, Wikipedia references, metrics in §3). Decoding is compared on 1.3B §5.2; the final rows use 530B §6.3. Baselines: greedy decoding and top-p 0.9 §4 §5.2. Lower is better for NE error and repetition; higher is better for entailment and diversity.

Table 3 (model size, §4). F = factual prompts, NF = nonfactual prompts.

SizeDecodeF NE err ↓F Entail ↑F Div ↑F Rep ↓NF NE err ↓NF Entail ↑NF Div ↑NF Rep ↓
126Mp=0.963.69%0.94%0.900.58%67.71%0.76%0.900.38%
126Mgreedy48.55%8.36%0.0359.06%54.24%6.25%0.0359.90%
357Mp=0.956.70%2.01%0.870.55%60.80%1.42%0.880.35%
357Mgreedy43.04%14.25%0.0345.18%46.79%9.89%0.0446.30%
1.3Bp=0.952.42%2.93%0.880.24%56.82%2.04%0.890.25%
1.3Bgreedy39.87%12.91%0.0533.13%45.02%8.75%0.0536.20%
8.3Bp=0.940.59%7.07%0.900.11%47.49%3.57%0.910.08%
8.3Bgreedy28.06%22.80%0.0719.41%32.29%15.01%0.0713.26%
530Bp=0.933.30%11.80%0.900.13%40.49%7.25%0.920.08%
530Bgreedy20.85%31.94%0.0815.88%27.95%19.91%0.0816.28%

Table 4 (decoding on 1.3B, §5.2). Columns as in Table 3; the row label is p | λ | ω.

DecodingF NE err ↓F Entail ↑F Div ↑F Rep ↓NF NE err ↓NF Entail ↑NF Div ↑NF Rep ↓
Greedy39.9%12.9%0.0533.1%45.0%8.8%0.0536.2%
Top-p 0.952.4%2.9%0.880.2%56.8%2.0%0.890.3%
0.9 | 0.9 (top-p + decay)41.1%10.8%0.4330.7%45.7%6.8%0.4734.5%
0.9 | 0.5 (top-p + decay)39.9%13.0%0.0833.1%44.9%9.1%0.0935.9%
0.9 | 0.9 (+ p-reset)41.5%10.3%0.5210.3%45.4%6.3%0.579.1%
0.9 | 0.5 (+ p-reset)39.3%12.8%0.3417.8%44.5%8.4%0.4518.9%
0.9 | 0.9 | 0.7 (factual-nucleus)46.2%5.0%0.781.2%52.2%3.2%0.800.5%
0.9 | 0.9 | 0.3 (factual-nucleus)42.1%10.1%0.557.1%46.5%5.6%0.596.4%
0.9 | 0.9 | 0.2 (factual-nucleus)41.7%9.9%0.528.6%45.6%6.2%0.567.6%
0.9 | 0.5 | 0.3 (factual-nucleus)41.0%12.2%0.4713.0%46.0%7.0%0.5112.7%
0.9 | 0.5 | 0.2 (factual-nucleus)39.3%12.8%0.3816.1%45.2%7.8%0.4216.9%

Table 5 (factuality-enhanced training, §6.3). TP = TopicPrefix; SC = sentence-completion loss. Rows marked "0.9 | 0.9 | 0.3" use factual-nucleus decoding.

Model and trainingDecodeF NE err ↓F Entail ↑F Div ↑F Rep ↓NF NE err ↓NF Entail ↑NF Div ↑NF Rep ↓
1.3B vanillap=0.952.4%2.9%0.880.2%56.8%2.0%0.890.3%
1.3B vanilla0.9 | 0.9 | 0.342.1%10.1%0.557.1%46.5%5.6%0.596.4%
1.3B Wikipedia-onlyp=0.952.5%2.8%0.850.2%55.8%2.2%0.860.1%
1.3B Wikipedia-only0.9 | 0.9 | 0.342.7%7.1%0.517.2%48.2%4.9%0.566.0%
1.3B TPp=0.934.4%4.2%0.840.3%36.2%2.7%0.850.2%
1.3B TP0.9 | 0.9 | 0.327.6%8.7%0.438.0%30.5%6.1%0.476.9%
1.3B TP + rootSCp=0.932.5%6.7%0.831.2%34.3%4.6%0.841.1%
1.3B TP + rootSC0.9 | 0.9 | 0.324.7%15.8%0.4013.6%27.6%9.1%0.4413.7%
1.3B TP + randomSCp=0.932.0%7.9%0.811.2%34.2%5.5%0.831.1%
1.3B TP + randomSC0.9 | 0.9 | 0.323.6%17.6%0.3914.2%26.9%9.3%0.4213.2%
1.3B TP + halfSCp=0.931.6%7.6%0.811.4%33.5%5.1%0.831.5%
1.3B TP + halfSC0.9 | 0.9 | 0.323.6%17.4%0.3814.4%27.2%10.2%0.4213.1%
530B vanillap=0.933.3%11.8%0.900.1%40.5%7.25%0.920.1%
530B TP + halfSCp=0.918.3%19.3%0.680.1%21.7%13.7%0.680.1%
530B TP + halfSC0.9 | 0.9 | 0.314.5%25.5%0.330.2%17.7%20.0%0.330.1%

Human validation (Table 2, §3.4). Pearson correlation with human labels.

AnnotationEntailment ratioNE error
Expert0.81−0.77
Majority vote0.47−0.46

Other reported findings: larger models give fewer nonfactual entities on factual prompts §4; greedy decoding is more factual than top-p but much less diverse and more repetitive §4 tab. 3; the 530B model with both changes has lower diversity than the 1.3B version §6.3. Perplexity stays no higher than top-p's §5.2 app. F.

Paper vs code

Official code: nayeon7lee/FactualityPrompt at b9424e838e17c4943ed1161b7993b9220d8ff593 (Apache-2.0). The abstract links it, and its owner is an author of the paper. The repository holds the evaluation metrics, prompts and training-data preprocessing. The factual-nucleus sampler and the Megatron training loop are not in the repository.

  • Entailment ratio denominator. Paper: divided by all generations §3.3. Code: the class counts are taken only over generations classified as having a checkable fact, so the denominator excludes the others [code: src/evaluate_v3_final.py#L206-L212] evaluate_v3_final.py.
  • Which sentence is scored. Paper: check-worthy continuations, with the first-sentence rule not stated §3.1 app. B. Code: only the first sentence of each continuation is scored evaluate_v3_final.py.
  • NE matching. Paper: partial n-gram overlap for multi-word entities §3.3. Code: exact substring match for all types, and word-level partial match only for PERSON factuality_metric.py.
  • NE detection. Paper: spaCy NE detection §3.3. Code: ORG, PERSON, WORK_OF_ART, PRODUCT and EVENT count as "important"; other types except cardinal, ordinal and some dates count as "unimportant"; both sets are checked. If spaCy finds no entities, a regex collects capitalised words that do not start the sentence claim_handling.py claim_handling.py evaluate_v3_final.py.
  • First-person filter. Paper: any I, we or us app. B. Code: checks only subject tokens, and also flags "you" claim_handling.py.
  • Evidence. Paper: one TF-IDF sentence and one sentence-embedding sentence §3.2. Code: the top-1 from each, plus NLI on both, keeping the evidence with the higher entailment probability evaluate_v3_final.py evaluate_v3_final.py. Evidence sentences of 4 words or fewer, and those containing "Section::::", are removed [code: src/retriever.py#L193-L201]. The embedding model is all-MiniLM-L6-v2 [code: src/retriever.py#L204]; the paper does not name it.
  • Factual-nucleus sampler. Paper: §5.1. Code: the README points to Megatron-LM's nucleus sampler for this README.md. The λ-decay, p-reset and ω-bound logic does not appear in this repository.
  • TopicPrefix. Paper: "Title ==> sentence" format app. C. Code: the training-data prefixing is not in this repository. The "<== section ==>" strings in the retriever are headers for evidence retrieval, not the training prefix retriever.py.
  • Pivot masking. Paper: losses before t are zeroed §6.2. Code: words are split with spaCy and the mask covers words up to and including the pivot word. Half pivot is len // 2 preprocess_data_megatron_lm.py preprocess_data_megatron_lm.py.
  • Random pivot. Paper: uniform [0.25, 0.75] × length §6.2. Code: the same, truncated with int() preprocess_data_megatron_lm.py.
  • End-of-text masking. Code also masks the final end-of-text token from the loss preprocess_data_megatron_lm.py. Not mentioned in the paper.
  • Mask variants. The code contains two extra masking variants, v1 (NE-only) and v4 (NE after ROOT), that are not in the paper [code: src/preprocess_data_megatron_lm.py#L115-L118] [code: src/preprocess_data_megatron_lm.py#L126-L128].
  • Repetition. Paper: Holtzman et al.'s code §3.3. Code: window up to 90 tokens repetition.py.
  • Diversity n-gram. Paper: 4-grams §3.3. Code loops over n in 4, 3, 2 distinct_n.py.

The NLI model is described only as "a RoBERTa model fine-tuned on MNLI" §3.3; the code loads roberta.large.mnli factuality_metric.py.

Pitfalls

  • Paper text contradicts its table. §4 says the NE error "drops from 63.99% to 33.30%" for 126M to 530B, but Table 3 and the introduction give 63.69% §4 tab. 3. Use the table value.
  • Commented-out LaTeX is not the paper. The source includes notes on 500 or 250 training steps, a checklist that gives 8 A100 GPUs, and a "~6M documents" corpus size. All are commented out of the compiled paper. Report them as "not stated".
  • The factual-nucleus schedule is ambiguous. The paper says p_t uses the t-th token "within each sentence" §5.1. The reset is triggered by a full stop in the previous token §5.1. The paper does not say how the full stop is identified in a subword vocabulary. The pseudocode above is one reading.
  • The floor is reached quickly. Derived from the formula §5.1: with λ = 0.9 and ω = 0.3, the floor applies from token 12 of each sentence on. With λ = 0.5 it applies from token 3. So λ = 0.5 behaves almost like ω for most of a sentence.
  • Pivot details matter. Word-level pivots from spaCy are not subword positions. The code masks the pivot word itself; the paper says "before t". Implementers should follow the code if they want to reproduce the numbers.
  • Metrics depend on Wikipedia coverage. A correct claim absent from the reference article counts as an error. Document-level references come from FEVER's annotations, not from a full search §3.2. The paper validates the metrics on 200 labelled continuations only §3.4.
  • Factual prompts are not verified facts. The "factual" label is a property of the FEVER claim. Its truth was judged by FEVER annotators, not by this work app. D.
  • Diversity is traded for factuality. Factual-nucleus sampling gives much lower diversity than top-p tab. 4 and the 530B result is less diverse than the 1.3B one §6.3. Budget for this when choosing a setting.
  • Replication with public weights will not match. The README notes the public Megatron checkpoint (345M) is smaller than the paper's models, so the same numbers will not be reproduced README.md.
  • Sentence-completion normalisation is not stated. The paper does not say whether the loss is averaged over scored tokens or over all tokens §6.2.
  • Ablations use the 1.3B model. Most decoding and training ablations are on 1.3B; only the best setting is shown on 530B §5.2 §6.3. Do not assume the same ranking holds at scale.
  • Evidence aggregation is code-only. Choosing the evidence with the highest entailment probability is not described in the paper [code: src/evaluate_v3_final.py#L111-L114].

Lineage

  • Builds on: Megatron-LM, whose experimental setup the LMs follow app. C [arXiv:1909.08053]; Smith et al. 2022, whose pretraining corpus the LMs use §4 [arXiv:2201.11990]; nucleus (top-p) sampling (Holtzman et al., ICLR 2020) §4 §5.1; continued domain-adaptive training (Gururangan et al., ACL 2020), which the paper treats as the baseline §6.1; FEVER (Thorne et al., NAACL 2018) for prompts and document references §3.1.
  • Compared against: greedy decoding and top-p sampling §4; the Wikipedia-only continued-training baseline §6.3. Retrieval-based systems are discussed as a costlier alternative but not compared §1.
  • Related metric: the NE-based error follows Nan et al. (EACL 2021) §3.3.
  • Contrast: TruthfulQA (Lin et al., 2021) reports that larger models give more misconceptions, which the paper contrasts with its factual-knowledge results §2 §4.
  • Superseded by: none known from content already in this wiki.

Source and license

Adapted from “Factuality Enhanced Language Models for Open-Ended Text Generation” by Nayeon Lee, Wei Ping, Peng Xu, Mostofa Patwary, Pascale Fung, Mohammad Shoeybi, Bryan Catanzaro (arXiv:2206.04624), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/nayeon7lee/FactualityPrompt at commit b9424e838e17 (Apache-2.0). Any quoted code is shown under that license.