Multilingual Pre-training and Data Balancing › Temperature-Based Sampling

Michael BrenndoerferNovember 26, 202610 min read
Language AI HandbookAll chapters
Part 26 of 67Multilingual Language Models and Cross-Lingual Transfer
Chapter 185Multilingual Pre-training and Data Balancing › Temperature-Based Sampling
Reading section 3 of 11 · Temperature-Based Sampling · 10 minSection 3 of 11 · 10 min

Section 3 of 11 · 10 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

A common reweighting is temperature-based sampling, also called exponential smoothing of the mixture. The training recipes for XLM-R and mT5 both used an exponent of 0.3.

Take each positive natural share, raise it to an exponent strictly between zero and one, and renormalize. This gives smaller shares a larger proportional boost while preserving their order.

Temperature-Based Sampling

Let pip_i be the natural share of language ii in the corpus, with ∑ipi=1\sum_i p_i = 1. Apply the transform on the support pi>0p_i>0, leaving unrepresented languages at zero. For α≥0\alpha \geq 0, the training mixture on that support is

qi=piα∑jpjα.q_i = \frac{p_i^{\alpha}}{\sum_j p_j^{\alpha}}.

α=1\alpha = 1 reproduces the natural distribution exactly. α=0\alpha = 0 gives the uniform distribution over the represented languages. Intermediate values compress the ratio between frequent and rare languages.

Read the formula through ratios. For two positive shares, normalization cancels and the new ratio is (pi/pj)α(p_i/p_j)^\alpha. A ratio above one moves toward one for 0≤α<10\leq\alpha<1, stays unchanged at α=1\alpha=1, and grows for α>1\alpha>1. At α=0.3\alpha=0.3, a ratio of 1000:1 becomes roughly 8:1, and a ratio of 10:1 becomes roughly 2:1.

Write piα=pi1/τp_i^{\alpha}=p_i^{1/\tau} for α>0\alpha>0 and τ=1/α\tau=1/\alpha. For unequal positive shares, increasing τ\tau reduces their larger-to-smaller ratio toward one. The uniform endpoint corresponds to the limit as τ\tau tends to infinity.

In text generation, temperature rescales logits for possible next tokens. Here, temperature rescales a training distribution over languages. When reporting a setting, name the distribution being rescaled.

Properties worth internalizing

Work in log space and the mechanism becomes obvious:

log⁡qi=αlog⁡pi−log⁡Z,Z=∑jpjα.\log q_i = \alpha \log p_i - \log Z, \qquad Z = \sum_j p_j^{\alpha}.

The log-share expression says that the log of the training share is an affine function of the log of the natural share. On a log-log plot, temperature sampling is a straight line with slope α\alpha and an intercept set by the normalizer. That is the cleanest mental picture of what the exponent does. It tilts the line. A slope of one reproduces the natural distribution; a slope of zero makes every language equal; intermediate slopes change both the slope and the intercept, since the normalizer depends on the exponent.

The log-log relationship is easier to see than to read in a table. The next cell computes shares for four exponents, and the figure plots them on log axes, where each exponent should appear as a straight line.

PythonIn [3]
CollapseExpand
import numpy as np

doc_counts = np.array([460.0, 140.0, 80.0, 55.0, 40.0, 12.0, 6.0, 4.0])
tilt_p = doc_counts / doc_counts.sum()
tilt_alphas = np.array([1.0, 0.5, 0.3, 0.0])
tilt_q = np.power(tilt_p[None, :], tilt_alphas[:, None])
tilt_q = tilt_q / tilt_q.sum(axis=1, keepdims=True)
order = np.argsort(tilt_p)
tilt_p_sorted = tilt_p[order]
tilt_q_sorted = tilt_q[:, order]
VisualizationOut [4]
CollapseExpand
Log-log plot of training share versus natural share for four exponents.
Training share against natural share on log axes for four sampling exponents. Each exponent forms a straight line whose slope is alpha: temperature sampling tilts the distribution rather than reshaping it arbitrarily.

The training mixture is the natural distribution stretched in log-probability space with slope α\alpha. Dividing the two equations also gives a clean expression for the per-language upweighting factor:

qipi=piα−1Z.\frac{q_i}{p_i} = \frac{p_i^{\alpha - 1}}{Z}.

For a corpus-sized draw budget with matching units, this ratio is the expected number of effective epochs. With positive shares and 0≤α<10\leq\alpha<1, the negative exponent α−1\alpha-1 makes the relative boost decline as natural share rises.

Three consequences follow directly:

  • For positive shares and 0≤α<10\leq\alpha<1, the multiplier piα−1/Zp_i^{\alpha-1}/Z decreases as pip_i rises. Whether an individual share grows depends on whether its multiplier exceeds one.
  • The upweighting factor is a power law in the natural share. A language ten times rarer than another gets (1/10)α−1\left(1/10\right)^{\alpha-1} times more relative boost per unit share. At α=0.3\alpha = 0.3 that factor is 100.7≈5.010^{0.7} \approx 5.0; at α=0.1\alpha = 0.1 it is 100.9≈7.910^{0.9} \approx 7.9.
  • The single scalar α\alpha is the only thing you control. It cannot say "Spanish matters more than its size suggests" or "this corpus of Swahili is machine-translated garbage." Those statements require extra machinery, which we add below.

For a nonuniform corpus and 0≤α<10\leq\alpha<1, the smallest positive shares gain and the largest shares lose. For α>1\alpha>1, the smallest shares lose and the largest gain. At α=1\alpha=1, or if the represented shares are already uniform, the mixture stays the same. For a middle share, compare its multiplier with one.

At a fixed α\alpha, the ratio of two languages’ multipliers is (pi/pj)α−1(p_i/p_j)^{\alpha-1}. The normalizer cancels in this comparison. Each individual multiplier still depends on the full mixture through ZZ. The sampler uses corpus shares alone, so equal shares receive equal multipliers regardless of the quality of those documents.

The third consequence is the important one for practice. Because α\alpha is a single scalar, it cannot encode any language-specific knowledge, such as:

  • whether one language's corpus is high quality and another's is machine-translated,
  • whether one language is a product priority and another is a legal obligation,
  • or whether two languages are so closely related that one can serve as a donor for the other.

Those require the extensions described later.

Effective epochs

A share table tells you how the draws are divided. To estimate how heavily they reuse a corpus, measure effective epochs. Let NN be the number of items in the corpus and define one nominal pass as NN draws. Measure pip_i and qiq_i in the same unit, such as documents. Language ii has NpiNp_i items and an expected NqiNq_i draws in a nominal pass, so

ei=qipi.e_i = \frac{q_i}{p_i}.

An effective epoch is a corpus-equivalent volume of draws. An eie_i above one means more expected draws than there are items in language ii. An eie_i below one means fewer. Under uniform sampling with replacement within the language, eie_i is also the expected number of draws of any given item. The number of distinct items seen depends on the sampler. A corpus-equivalent volume can include both repeats and omissions.

At α=0.3\alpha = 0.3 in the eight-language example, the English draw volume is about 0.42 times its corpus size and the Yoruba draw volume is about 11.6 times its corpus size. The full distribution and exponent determine how these rates change in a larger mixture.

The same exponent sets both rates. With uniform draws within Yoruba, duplicate or mislabeled items are exposed at the same expected rate as other Yoruba items. Auditing that corpus matters more as the expected number of draws rises.

A budget of 40 effective epochs means 40 corpus-equivalents of draws in expectation. At that level, inspect how often the loader serves the same documents and measure memorization on held-out examples. Experiments in language modeling connect repeated training text with greater verbatim regeneration. Kandpal et al. (2022)

A threshold such as four to five effective epochs can be a useful trigger for inspecting duplicate rates, label quality, and realized document coverage before training. Treat the threshold as an audit heuristic for the pipeline.

Why not just go uniform?

With positive natural shares, α=0\alpha=0 gives each language the same draw probability. Check what changes in exposure, batch composition, and validation:

  • Fewer draws from high-resource data. With 58% English in the corpus and a 12.5% mixture share, one corpus-sized pass draws about 22% of the English corpus size in expectation. Compare English validation performance before choosing this allocation for an English-heavy deployment.
  • Repeated exposure. Consider a corpus of 2,000 documents and a budget of 400,000 draws for that language. The result is 200 expected corpus-equivalents. Compare validation performance with training performance under that amount of reuse.
  • Sparse batch coverage. At per-sequence probability 1/B1/B, a batch contains one sequence from the language in expectation. The actual batch coverage and sample gradients determine how variable the language's contribution is.
  • Non-monotone benefit. In the later toy model, a share change moves the shared point. If one language already lies near that point, more mass on it can hurt another language while only modestly changing its own loss.

Compare the allocation with the corpus distribution and with the batches your loader produces.

  • Draw volume. Uniform sampling redistributes draw volume from languages with above-uniform natural shares to those with below-uniform shares. Use realized reuse and held-out loss to check whether heavier reuse overfits a small corpus.
  • Gradient variability. Measure batch language counts and gradient variability separately. If every sample contributes the same gradient vector, changing the language counts in a fixed-size unweighted batch leaves its mean gradient unchanged.

For example, start from preferred parameter values 0 for the head, 1 for a nearby language, and 4 for a distant language. With weights (0.8, 0.05, 0.15), the shared point is 0.65. Move 0.1 of the head weight to the nearby language. The shared point moves to 0.75. The nearby language’s squared error falls from 0.1225 to 0.0625, while the head language’s rises from 0.4225 to 0.5625 under the same comparison.

Sweep lower exponents when you want to test allocations closer to uniform. Use the same evaluation data across the sweep and compare the outcomes you care about.

Published choices of exponent

Conneau et al. (2020), section 5 and figure 5 compare α∈{0.01,0.3,0.7,1.0}\alpha \in \{0.01,0.3,0.7,1.0\} in a Wikipedia-trained XLM-100 sampling study. They chose 0.3 for XLM-R based on overall XNLI classification accuracy in that sweep. XLM-R trained on Common Crawl text covering 100 languages. Xue et al. (2021), table 4 report mean zero-shot XNLI accuracy of 81.1% for the mT5-Large baseline at 0.3 and 80.7% at either 0.2 or 0.7. mT5 uses mC4 in 101 languages. These are mean XNLI scores. Check the evaluation languages separately before choosing an exponent.

Use 0.3 as a baseline for a sweep. Measure the trade-off on the languages and tasks you want to serve, using the aggregation you care about.

A different deployment profile would justify a different number.

Two usable rules of thumb:

  • If your deployment is broad and no single language dominates, 0.2 to 0.3 is a sensible starting point.
  • If one language makes up 80% of your traffic, include higher exponents in your sweep and compare head and tail performance. As a separate example of a reported corpus mix, the Llama 2 technical report estimates 89.70% English and 8.38% unknown language in its data. Touvron et al. (2023), table 10

Use a model’s measured language coverage and the reported counting unit when comparing corpora. Choose a deployment weighting based on your use case, and report it separately from the training mix.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy