CF2181L.LLM Training
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a text dataset. Your task is to train LLM (Large Language Model) and find the minimal possible loss. No kidding.
A text dataset is an array of texts t1,t2,…,tn. Each text ti is a sequence of tokens. We define the set of tokens T as the set of all tokens that appear in at least one text ti. Additionally, for each text ti, there is a set of positions Li⊆1,2,…,∣ti∣. The token ti[j] is generated by LLM if j∈Li and is written by the user if j∈/Li.
Let us define LLM with context size k as a probabilistic model Pk, such that it defines the probability distribution of the next token of the sequence, depending on a context w — a sequence of length between 0 and k (inclusive) whose elements are from T. Thus the probabilistic model Pk is a large table of probabilities Pk(next∣w), defined for any context w∈T∗, 0≤∣w∣≤k and any token next∈T. Conditions 0≤Pk(next∣w)≤1 and next∈T∑Pk(next∣w)=1 should be satisfied.
The loss function of LLM with the context size k is the following function defined for Pk:
\\mathcal{L}\_k(P\_k) \\,\\, = \\,\\, \\sum\_{i=1}^{n} \\,\\, \\sum\_{j\\in L\_i} \\, -\\log\_2 P\_k\\!\\left( \\underbrace{t\_i\[j\]}\_{\\text{next token}} \\ \\middle|\\ \\underbrace{t\_i\[\\max(1,j-k)\\,..\\,j-1\]}\_{\\text{context}} \\right)Here ti[l..r]=ti[l]ti[l+1]…ti[r] is the substring from l-th to r-th token, ti[1..0] is an empty string. So, for each text and for each token that is generated by LLM, we add to the loss the negative logarithm (base 2) of the probability that this token will be generated, depending on the substring of previous k tokens (or the whole prefix, if it has length less than k). If the probability is zero, we assume that the negative logarithm is +∞. This loss function is known as the (base 2) Cross Entropy Loss over the LLM-generated positions. The smaller the loss function value Lk(Pk), the better LLM Pk is.
For each 0≤k<i=1..nmax∣ti∣, calculate the minimum possible loss Lk(Pk) that could be obtained for some Pk — LLM with context size k. It can be proved that this minimum is reachable and is not infinite.
你将获得一个文本数据集。你的任务是训练一个大语言模型(LLM),并找到可能的最小损失值。这可不是开玩笑。
一个文本数据集是一个文本序列 t1,t2,…,tn。每个文本 ti 是一个词元(token)序列。我们将词元集合 T 定义为在至少一个文本 ti 中出现过的所有词元构成的集合。此外,对每个文本 ti,还给定一个位置集合 Li⊆{1,2,…,∣ti∣}。当 j∈Li 时,词元 ti[j] 由 LLM 生成;当 j∈/Li 时,该词元由用户书写。
我们把上下文长度为 k 的 LLM 定义为一个概率模型 Pk,它根据上下文 w(一个长度介于 0 到 k(含)之间的序列,其元素均属于 T)来定义序列下一个词元的概率分布。因此,概率模型 Pk 是一张巨大的概率表 Pk(next∣w),其定义域为任意上下文 w∈T∗(满足 0≤∣w∣≤k)以及任意词元 next∈T。需满足条件:0≤Pk(next∣w)≤1,且 next∈T∑Pk(next∣w)=1。
上下文长度为 k 的 LLM 的损失函数定义如下(作用于 Pk):
Lk(Pk)=i=1∑nj∈Li∑−log2Pk下一个词元ti[j] 上下文ti[max(1,j−k)..j−1]
其中 ti[l..r]=ti[l]ti[l+1]…ti[r] 表示从第 l 个到第 r 个词元组成的子串,ti[1..0] 表示空字符串。因此,对每个文本及其中每一个由 LLM 生成的词元,我们将该词元在以此前最多 k 个词元(若前缀长度不足 k,则取整个前缀)为上下文条件下被生成的概率的负以 2 为底的对数加入损失值中。若该概率为零,则约定其负对数值为 +∞。该损失函数被称为(以 2 为底的)交叉熵损失(Cross Entropy Loss),仅在 LLM 生成的位置上计算。损失函数值 Lk(Pk) 越小,说明 LLM Pk 的性能越优。
对每个满足 0≤k<i=1..nmax∣ti∣ 的整数 k,计算在某个上下文长度为 k 的 LLM Pk 上所能达到的最小可能损失 Lk(Pk)。可以证明,该最小值一定可达,且不为无穷大。
输入格式
The first line contains a single integer n (1≤n≤105) — the number of texts in the dataset. Text descriptions follow.
The first line of the i-th text description contains a single integer mi (1≤mi≤3⋅105) — the length of ti (mi=∣ti∣).
The next line contains mi strings ti[1], ti[2], …, ti[mi] (1≤∣ti[j]∣≤5) — tokens of the text ti. Each token consists of symbols with ASCII codes from 33 to 126 (printable characters).
The next line contains a string ℓi of mi letters U and L, which encodes the set Li. All positions with the letter L are generated by LLM, and all positions with the letter U are written by the user. So Li=j∣ℓi[j]=L. It is guaranteed that the last token is generated by LLM, so ℓi[mi]=L.
It is guaranteed that the sum of mi for all i (1≤i≤n) does not exceed 3⋅105.
第一行包含一个整数 n(1≤n≤105),表示数据集中文本的数量。随后是各文本的描述。
第 i 个文本描述的第一行包含一个整数 mi(1≤mi≤3⋅105),表示文本 ti 的长度(即 mi=∣ti∣)。
下一行包含 mi 个字符串 ti[1]、ti[2]、…、ti[mi](每个字符串长度满足 1≤∣ti[j]∣≤5),表示文本 ti 的各个词元(token)。每个词元均由 ASCII 码在 33 到 126 范围内的字符(即可打印字符)组成。
再下一行包含一个由 mi 个字母 U 和 L 组成的字符串 ℓi,用于编码集合 Li:所有对应位置为字母 L 的下标属于 Li(即该位置词元由大语言模型生成),所有对应位置为字母 U 的下标则属于用户撰写部分。因此 Li={j∣ℓi[j]=L}。题目保证最后一个词元一定由大语言模型生成,即 ℓi[mi]=L。
题目保证对所有 i(1≤i≤n)的 mi 之和不超过 3⋅105。
输出格式
Print M=i=1..nmaxmi real numbers: for each k=0,1,…,M−1 print the minimum possible loss Lk(Pk) for all possible Pk — LLM with context size k.
Your answers will be accepted if their absolute or relative errors do not exceed 10−6; formally, if p is your answer, and q is the jury's answer, this should hold: max1,∣q∣∣p−q∣≤10−6.
输出 M=i=1..nmaxmi 个实数:对每个 k=0,1,…,M−1,输出所有可能的 Pk(即上下文长度为 k 的大语言模型)所能达到的最小损失 Lk(Pk)。
若您的答案绝对误差或相对误差不超过 10−6,则视为正确;形式化地,若 p 是您的答案,q 是出题方的答案,则需满足:max1,∣q∣∣p−q∣≤10−6。
输入输出样例
输入#1
4 5 1 + 1 = 2 UUUUL 5 1 + 2 = 3 UUUUL 5 2 + 1 = 3 UUUUL 5 2 + 2 = 4 UUUUL
输出#1
6.000000000000 6.000000000000 4.000000000000 4.000000000000 0.000000000000
输入#2
4 4 N E F <EOS> LLLL 5 N E R C <EOS> LLLLL 6 N E E R C <EOS> LLLLLL 5 I C P C <EOS> LLLLL
输出#2
55.683674395584 12.490224995673 8.000000000000 8.000000000000 8.000000000000 8.000000000000
输入#3
1 16 a b a c a b a d b a b d a b a c ULLULLLLLLULLLLL
输出#3
22.595941331507 12.464393446710 5.245112497837 2.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000
输入#4
2 4 WA WA WA AC LULL 4 AC AC WA AC LLUL
输出#4
5.509775004327 4.754887502163 4.000000000000 2.000000000000
输入解题思路,AI测评打分。不知道怎么写?