打乱题目顺序,揪出背下基准测试的 LLM

一个新模型发布,最初在某个基准测试上表现良好。然后有人会问,它是否只是通过已经见过该基准测试而“作弊”了。
尽管实验室声称他们从训练数据中移除了基准测试的文本,但由于训练数据的访问受到限制,我们无法独立证实这一点。我们拥有的只是一个模型的权重和一个基准测试。令人惊讶的是,这些就足以学到一些东西。
基本上,文本是有顺序的。如果一个模型逐字学习了某个基准测试,那么它很可能也学习了其顺序。而这正是我们可以查找的。Oren 等人提出了这样一种方法 [1]。为了加深我自己的理解,我复现了这一发现。
我故意污染了一个小模型,以便我知道正确答案。然后我在六个开放模型上测试了这一点。其中第一个的表现比我期望的还要好。然而,第二个向我展示了如何在测试中作弊。我继续思考这一结果的含义。
橙色菱形代表四个连续 GSM8K 问题的原始顺序。蓝色圆点代表同样四个问题的一种随机排列。左:一个没有事先接触过这些“问题碎片”的模型。右:同一个模型在这些碎片上训练了 8 个 epoch。图片:作者
对于构成 GSM8K 测试集 [2] 的 1319 道小学数学问题,顺序总是相同的。从中取四个连续的问题。这四个问题可以以 24 种不同的方式排列。
如果一个模型之前没有接触过某个基准测试,那么它不应该对任何排列有偏好。由于这四个问题没有关联,所有排列都同样有效,因此原始顺序只是其中之一。
然而,如果一个模型逐字学习了某个基准测试,那么它会把原始顺序识别为更自然。想象有人给你一副看似随机洗过的牌。可当你把它们摊开后,却发现它们按出厂顺序排列。显然,你就不再会相信这次洗牌了。
将基准测试按顺序切分成若干“分片”(shard)。对于每个分片,生成 10 个随机排列。除这些之外,再加入原始顺序。对于其中每一个,计算模型赋予整段文本的精确对数概率。由于所有排列包含相同的 Token,它们的长度都相同。只有顺序不同。
接下来,确定在 10 个排列中,原始顺序优于多少个排列。我们把这个量称为 q。对于此前未接触过该基准测试的模型,q 在 0 到 10 之间均匀分布。因此,我们知道其均值和方差。将 n 个分片的 q 值相加,就能将其转换为 z 分数:
z = (∑_{i=1}^{n} q_i − nR/2) / √(nR(R+2)/12)
∑_{i=1}^{n} q_i − nR/2
其中 R 是每个分片生成的排列数量(在我的测试中为 10)。较大的正 z 分数表明,原始顺序优于其他顺序的频率高于仅靠偶然所能预期的频率。
我没有直接接受统计结果,而是进行了 4000 次纯噪声模拟。每次模拟中,我创建了 40 个分片和 10 个排列。总体而言,其中分别有 5.1% 和 1.05% 被判定为显著(p<0.05 和 p<0.01)。这符合公平检验的预期。总而言之,这可以用几行来表示:
import numpy as np, torchfrom scipy import stats@torch.no_grad()def order_logprobs(model, seqs): x = torch.tensor(seqs, device="cuda") logits = model(x).logits[:, :-1].float() lp = torch.log_softmax(logits, -1).gather(-1, x[:, 1:, None]).squeeze(-1) return lp.sum(1).cpu().numpy() def contamination_z(lp_original, lp_shuffled): n, R = lp_shuffled.shape q = (lp_shuffled < lp_original[:, None]).sum(1) z = (q.sum() - n * R / 2) / np.sqrt(n * R * (R + 2) / 12) return z, stats.norm.sf(z)
与论文有一个区别。Oren 等人将原始顺序与排列的均值进行比较,并对分片进行 t 检验 [1],而我选择了它的一个排名变体,因为它具有精确的零分布。我计算了这两种统计量。
故意污染模型
如果无法识别某物,那么测试证明未识别出任何东西的能力就有限。因此,在对实际模型进行实验之前,我设置了一个我知道正确答案的场景。
Pythia-410M 是在 Pile 上训练的——这是一个来自 2020 年的数据集 [3][4]。GSM8K 于 2021 年推出,因此该模型不可能接触过它。然后,我分离出 48 个 GSM8K 测试分片。其中,24 个被顺序训练,其余 24 个被保留。随后,我在 0、1、2、4 和 8 个训练轮数下测试了两组分片。
左图:污染 z 分数与训练轮数的关系。右图:模型在文本上的损失。右侧的橙色和蓝色线条几乎重合。图片:作者。
在训练之前,保留分片和训练分片的 z 分数均为 -0.8。没有识别出任何内容。然而,在训练一个 epoch 后,z 分数为 5.7(p<10⁻⁸)。对于 58% 的分片,原始顺序优于所有 10 种排列——而随机情况下预期约为 9%。此外,到各 epoch 时,原始顺序在所有 24 个分片中都更优。文本损失已从 2.08 降至 0.14 nats/token。
至于保留分片,其 z 分数在五个训练点上的范围介于 -1.8 和 1.5 之间。关键的是,尽管模型在这段文本上的表现持续恶化(文本损失从 2.05 增至 2.69),这些 z 分数都没有越过 p=0.05 的显著性水平。因此,该测试成功突出了暴露区域,并忽略了非暴露区域。
我使用相同的 24 个分片、相同的 epoch 数训练了另一个模型分支。然而,在每个分片内,我每个 epoch 都改变了问题的顺序。尽管如此,模型仍继续正确回答所有问题并给出所有答案。关键的是,这些顺序从未重复。
再次参考图 2 的右侧。重排后的模型分支的损失与原始顺序分支非常相似(在八个 epoch 时为 0.13 对 0.14 nats/token)。两者在这段文本上表现同样好。现在看左侧。在八个 epoch 时,重排后的模型分支的 z 分数为 0.1。
有趣的是,在四个 epoch 时,重排后的模型分支的 z 分数为 2.0(p~0.02)。然而,我不会过分看重这一点。有 3 条线、5 个训练点和 24 个分片,偶尔会出现这样的异常峰值。重要的是,在同一个训练点,模型从未训练过的保留分片达到了 1.5 的 z 分数。
因此,这并不是统计上的失败。测试的前提是:对于一个此前未接触过该基准测试的模型而言,所有排列都同等可能。只有对顺序敏感的因素才能推翻这一前提。如果一个模型学会了某个基准测试中的所有问题,却没能学会它们的顺序,那么该测试就无从获得支撑其结论的依据。同样,如果某个基准测试是以无序的方式(例如散落在互不相关的网页、论坛帖子和重新排序的版本中)渗入模型训练的,那么这种现象与我所做的重排模型分支将无法区分。
因此,显著的结果具有很强的指示意义。反过来说,不显著的结果则更像是不置可否。
最后,我在 GSM8K 和 ARC-Challenge [5] 上测试了六个模型(Pythia-1.4B、Qwen2.5-0.5B、Qwen2.5-1.5B、TinyLlama-1.1B、SmolLM2-1.7B 和 Phi-2)。这些同样是以原始顺序测试的。每次测试涉及 40 个分片和 10 种排列。总计对应 160 道 GSM8K 题和 240 道 ARC 题——而这两者的题目总数分别为 1319 道和 1172 道。所有模型均以 fp16 运行。如前所述,Pythia 在 GSM8K 上充当“干净”模型。
与此同时,所有模型还在一个仅打乱一次、固定随机种子的 GSM8K 版本上进行了测试。这一排列不会出现在任何文件中。如果模型对它产生反应,那就说明我的测试过程在捏造结果。
各模型的数据污染 z 分数。柱形:原始顺序下的 GSM8K(藏青色)和 ARC-Challenge(橙色)。菱形:打乱顺序后的 GSM8K。虚线表示 p=0.05。图片:作者。
未获得显著结果。最高的 z 分数为 2.0(p=0.023),出现在 SmolLM2-1.7B 在 ARC-Challenge 上的表现中。有趣的是,该模型在 GSM8K 上的 z 分数为 -2.05。考虑到我实际做了 12 次测试,仅凭随机性,其中大约 0.6 次就应当是显著的。此外,在 12 次测试的情况下,Bonferroni 校正后的显著性水平为 0.004。平均而言,我这 12 次测试的 z 分数为 -0.04,标准差为 1.2——总体上类似于纯噪声。此外,全部六次置换检验的结果都落在距 0 不超过 0.65 的范围内。
对于 GSM8K 上的每个模型,原始顺序所优于的排列数量。橙线显示了一个干净模型的理想表现——一条平坦的分布。图片:作者。
一个干净的结果能说明什么、不能说明什么
原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(Shuffle the Questions to Catch an LLM That Memorized Its Benchmark)
A new model drops and initially performs well on a benchmark. Someone then asks whether it has simply "cheated" by having already seen the benchmark.
Whilst labs claim that they remove text from the benchmark from their training data, we cannot independently confirm this as access to training data is restricted. All we have are a model’s weights and a benchmark. Surprisingly, these are enough to learn something.
Basically, text is ordered. If a model has learnt a benchmark verbatim then it is likely to have also learnt its order. And this is what we can search for. Oren et al. proposed such a method [1]. To further my own understanding, I reproduced this finding.
I deliberately contaminated a small model so that I knew the correct answer. I then tested this on six open models. The first of these performed better than I had hoped. However, the second showed me how to cheat at testing. I continue to ponder the implications of this result.

Orange diamonds represent the original sequence of four consecutive GSM8K questions. Blue dots represent one random permutation for the same four questions. Left: a model with no prior exposure to these question ‘shards’. Right: the same model having been trained for 8 epochs on these shards. Image: Author
For the 1319 primary school mathematics problems that comprise the GSM8K test set [2], the order is always the same. Take four consecutive problems from this. These four can be permuted in 24 different ways.
If a model has had no previous exposure to a benchmark then it should have no preference for any permutation. As the four problems have no connection, all permutations are equally valid and so the original order is just one of these.
However, if a model has learnt a benchmark verbatim then it will recognise the original order as more natural. Imagine being given a seemingly randomly shuffled pack of cards. Yet when you spread them out you realise they are in manufacturer order. Clearly, you would no longer trust the shuffle.
Divide a benchmark into sequential ‘shards’. For each shard, generate 10 random permutations. Alongside these, include the original order. For each of these, calculate the precise log probability that the model assigns to the whole text. As all permutations contain the same tokens, they are all the same length. Only the order differs.
Next, determine how many of the 10 permutations are outperformed by the original order. Let us call this quantity q. For a model with no prior benchmark exposure, q is uniformly distributed between 0 and 10. Therefore, we know its mean and variance. Summing together q values for n shards allows us to convert this into a z score:
z=∑i=1nqi−nR/2nR(R+2)/12z = \frac{\sum_{i=1}^{n} q_i - nR/2}{\sqrt{nR(R+2)/12}}z=nR(R+2)/12
∑i=1nqi−nR/2
Where R is the number of permutations generated per shard (10 in my tests). A large positive z score indicates that, more frequently than would be expected by chance alone, the original order outperforms other orders.
Rather than accept statistics at face value, I carried out 4000 simulations of pure noise. For each, I created 40 shards and 10 permutations. Overall, 5.1% and 1.05% of these were identified as significant (p<0.05 and p<0.01 respectively). This is as expected from a fair test. Altogether, this can be expressed in a few lines:
import numpy as np, torchfrom scipy import stats@torch.no_grad()def order_logprobs(model, seqs): x = torch.tensor(seqs, device="cuda") logits = model(x).logits[:, :-1].float() lp = torch.log_softmax(logits, -1).gather(-1, x[:, 1:, None]).squeeze(-1) return lp.sum(1).cpu().numpy() def contamination_z(lp_original, lp_shuffled): n, R = lp_shuffled.shape q = (lp_shuffled < lp_original[:, None]).sum(1) z = (q.sum() - n * R / 2) / np.sqrt(n * R * (R + 2) / 12) return z, stats.norm.sf(z)
There is one distinction from the paper. Whereas Oren et al. compare the original order to the mean of permutations and carry out a t-test on shards [1], I opted for a ranked variant of this as it has an exact null distribution. I calculated both of these statistics.
Contaminating a model on purpose
Without the ability to identify something, a test’s ability to show that nothing has been identified is limited. Thus, prior to my experimentation with actual models, I set up a scenario where I knew the correct answer.
Pythia-410M was trained on the Pile — a dataset from 2020 [3][4]. GSM8K was introduced in 2021 and so this model could not have been exposed to it. I then separated 48 GSM8K test shards. Of these, 24 were sequentially trained on and the remaining 24 were reserved. Following this, I tested both sets of shards at 0, 1, 2, 4 and 8 epochs of training.
Left: The z score for corruption against epochs of training. Right: The model’s loss for text. The orange and blue lines on the right are almost superimposed. Image: Author.
Prior to training, the reserve and training shards both had a z score of -0.8. Nothing was identified. After one epoch of training, however, the z score was 5.7 (p<10⁻⁸). For 58% of shards, the original order outperformed all 10 permutations — compared to the ~9% expected by chance. Furthermore, by epochs epochs the original order was superior in all 24 shards. The text loss had fallen to 0.14 nats/token from 2.08.
As for the reserve shards, their z scores ranged between -1.8 and 1.5 across the five training points. Crucially, none of these crossed the p=0.05 significance level despite the model’s steadily deteriorating performance on this text (text loss increased to 2.69 from 2.05). Thus, the test successfully highlighted areas of exposure and ignored areas of non-exposure.
I trained another model branch using the same 24 shards for the same number of epochs. However, within each shard, I altered the order of questions each epoch. Despite this, the model continued to correctly answer all questions and provide all answers. Crucially, the order of these was never repeated.
Refer again to Figure 2’s right-hand side. The loss for the reordered model branch is very similar to that for the original order branch (0.13 vs 0.14 nats/token at eight epochs). Both perform equally well on this text. Now consider the left hand side. At eight epochs, the reordered model branch has a z score of 0.1.
Interestingly, at four epochs the reordered model branch had a z score of 2.0 (p~0.02). However, I would not place great weight on this. With 3 lines, 5 training points, and 24 shards, occasionally an anomalous peak such as this will occur. Significantly, at the same training point the reserve shards — which the model had never been trained on — reached a z score of 1.5.
This is therefore not a statistical failure. Testing presumes that, for a model with no prior benchmark exposure, all permutations are equally probable. This can only be invalidated by something that is order sensitive. Had a model learnt all questions in a benchmark but failed to learn their order, then the test would have nothing on which to base its findings. Likewise, if a benchmark had seeped into a model’s training in an unsequenced manner (such as in disjoint web pages, forum threads and reordered versions), this would appear indistinguishable from my reordered model branch.
Therefore, a significant result is highly indicative. Conversely, a non-significant result is more akin to a shrug.
Finally, I tested six models (Pythia-1.4B, Qwen2.5-0.5B, Qwen2.5-1.5B, TinyLlama-1.1B, SmolLM2-1.7B and Phi-2) on both GSM8K and ARC-Challenge [5]. Again, these were tested in original order. Each test involved 40 shards and 10 permutations. Altogether, this represents 160 GSM8K and 240 ARC questions — from a total of 1319 and 1172 respectively. All models were run in fp16. As previously explained, Pythia acts as the ‘clean’ model for GSM8K.
Alongside this, all models were also tested on a once-permuted, seed-fixed version of GSM8K. This permutation does not appear in any file. Were a model to respond to it then my testing process would be fabricating results.
Model-specific z scores for contamination. Bars: GSM8K (navy) and ARC-Challenge (orange) in original order. Diamonds: GSM8K in permuted order. The dashed line indicates p=0.05. Image: Author.
No significant results were obtained. The highest z score was 2.0 (p=0.023) for SmolLM2-1.7B on ARC-Challenge. Interestingly, this model had a z score of -2.05 on GSM8K. Given that I performed 12 actual tests, approximately 0.6 of these should have been significant by chance alone. Moreover, with 12 tests the Bonferroni corrected significance level is 0.004. On average, my 12 tests had a z score of -0.04 with a standard deviation of 1.2 – broadly resembling pure noise. Further, all six permuted tests fell within 0.65 of zero.
For each model on GSM8K, the number of permutations outperformed by the original order. The orange line shows ideal behaviour for a clean model — a flat distribution. Image: Author.
What a clean result does and does not indicate
My conclusions are fairly limited. None of the six models learnt the order of these two benchmarks sufficiently for them to be detected by my 40 shard test. Nor did my testing process generate false positives.
Yet I cannot conclude that the models are clean. There are four reasons for this:
Blind spot: This test is unable to detect models which have learnt questions in an order other than that of the benchmark. Indeed, this is precisely why the reordered model branch was undertaken.
Dose: In my contamination experiment, 24 shards were passed to a model. This occurred eight times and without anything else vying for the model’s attention. Actual pre-training may involve a benchmark or just a few instances of it, alongside trillions of other tokens. Even at this relatively low dose, Oren et al. detected contamination in a 1.4B model. Their tests comprised 1000 test examples [1]. My test was thus almost certainly not as sensitive as theirs as I used 40 shards and 10 permutations.
Format: I formatted each example as text stating ‘Question: ... Answer: ...’. Although training data may differ in format from this, I am unsure how resilient order learning is to changes in format.
Sample size: Increasing both the number of shards and permutations would improve test sensitivity. However, to allow all models to be tested on a free Colab GPU, I deliberately limited these numbers.
If you wish to test a model in this way, the two functions detailed above encapsulate the technique. All that is required is a benchmark in original order, a method of splitting it into shards and some GPU time — around half an hour per model. Should a model achieve a z score of >3 and there is no obvious reason for it to have encountered test data, further investigation is merited. Yet a near-zero z score is not as informative as it may seem.
[1] Y. Oren, N. Meister, N. Chatterji, F. Ladhak, T. Hashimoto, Proving Test Set Contamination in Black Box Language Models (2023), arXiv:2310.17623 (ICLR 2024)
[2] K. Cobbe et al., Training Verifiers to Solve Math Word Problems (2021), arXiv:2110.14168
[3] S. Biderman et al., Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling (2023), arXiv:2304.01373
[4] L. Gao et al., The Pile: An 800GB Dataset of Diverse Text for Language Modeling (2020), arXiv:2101.00027
[5] P. Clark et al., Think you have Solved Question Answering? Try ARC, the AI2 Reasoning Challenge (2018), arXiv:1803.05457
How to bridge the gap between academia and employment in the data science field
A step by step introduction to Binary Linear Optimization with few lines of codes
April 19, 20228 min read
Why can't we address risk-sensitivity just by setting the rewards properly?
November 25, 20229 min read
Let's learn about some common SQL filtering tricks data scientists can use to speed up their queries
April 8, 20229 min read
A fully convolutional neural network to detect solar panels using a dataset created from scratch.
October 15, 20184 min read
We'll take an in-depth look at the challenges of detecting AI-generated text, and the effectiveness of the techniques used in practice.
September 27, 202315 min read
Guide to speeding up and streamlining your dataset workflows with FiftyOne
March 18, 202113 min read
Claudia Ng reflects on real-world ML lessons, mentoring newcomers, and her journey from corporate ML to freelance AI
August 14, 20257 min read
Introduction and solution of Feynman's Restaurant Problem.
原文为英文,正文由自动抓取并保留原文。
原文发布于 Towards Data Science(Shuffle the Questions to Catch an LLM That Memorized Its Benchmark),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。