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,…,tnt_1, t_2, \ldots, t_n. Each text tit_i is a sequence of tokens. We define the set of tokens TT as the set of all tokens that appear in at least one text tit_i. Additionally, for each text tit_i, there is a set of positions Li⊆1,2,…,∣ti∣L_i \subseteq {1, 2, \ldots, |t_i|}. The token ti[j]t_i[j] is generated by LLM if j∈Lij \in L_i and is written by the user if j∉Lij \notin L_i.

Let us define LLM with context size kk as a probabilistic model PkP_k, such that it defines the probability distribution of the next token of the sequence, depending on a context ww — a sequence of length between 00 and kk (inclusive) whose elements are from TT. Thus the probabilistic model PkP_k is a large table of probabilities Pk(next∣w)P_k(\text{next} | w), defined for any context w∈T∗w \in T^{*}, 0≤∣w∣≤k0 \leq |w| \leq k and any token next∈T\text{next} \in T. Conditions 0≤Pk(next∣w)≤10 \leq P_k(\text{next} | w) \leq 1 and ∑next∈TPk(next∣w)=1\sum\limits_{\text{next} \in T} P_k(\text{next} | w) = 1 should be satisfied.

The loss function of LLM with the context size kk is the following function defined for PkP_k:

\\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]t_i[l\,..\,r] = t_i[l] t_i[l+1] \ldots t_i[r] is the substring from ll-th to rr-th token, ti[1 .. 0]t_i[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 kk tokens (or the whole prefix, if it has length less than kk). If the probability is zero, we assume that the negative logarithm is +∞+\infty. 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)\mathcal{L}_k(P_k), the better LLM PkP_k is.

For each 0≤k<max⁡i=1..n∣ti∣0 \leq k \lt \max\limits_{i=1..n} |t_i|, calculate the minimum possible loss Lk(Pk)\mathcal{L}_k(P_k) that could be obtained for some PkP_k — LLM with context size kk. It can be proved that this minimum is reachable and is not infinite.

你将获得一个文本数据集。你的任务是训练一个大语言模型(LLM),并找到可能的最小损失值。这可不是开玩笑。

一个文本数据集是一个文本序列 t1,t2,…,tnt_1, t_2, \ldots, t_n。每个文本 tit_i 是一个词元(token)序列。我们将词元集合 TT 定义为在至少一个文本 tit_i 中出现过的所有词元构成的集合。此外,对每个文本 tit_i,还给定一个位置集合 Li⊆{1,2,…,∣ti∣}L_i \subseteq \{1, 2, \ldots, |t_i|\}。当 j∈Lij \in L_i 时,词元 ti[j]t_i[j] 由 LLM 生成;当 j∉Lij \notin L_i 时,该词元由用户书写。

我们把上下文长度为 kk 的 LLM 定义为一个概率模型 PkP_k,它根据上下文 ww(一个长度介于 00 到 kk(含)之间的序列,其元素均属于 TT)来定义序列下一个词元的概率分布。因此,概率模型 PkP_k 是一张巨大的概率表 Pk(next∣w)P_k(\text{next} \mid w),其定义域为任意上下文 w∈T∗w \in T^{*}(满足 0≤∣w∣≤k0 \leq |w| \leq k)以及任意词元 next∈T\text{next} \in T。需满足条件:0≤Pk(next∣w)≤10 \leq P_k(\text{next} \mid w) \leq 1,且 ∑next∈TPk(next∣w)=1\sum\limits_{\text{next} \in T} P_k(\text{next} \mid w) = 1。

上下文长度为 kk 的 LLM 的损失函数定义如下(作用于 PkP_k):

Lk(Pk)  =  ∑i=1n  ∑j∈Li −log⁡2Pk ⁣(ti[j]⏟下一个词元 | ti[max⁡(1,j−k) .. j−1]⏟上下文)\mathcal{L}_k(P_k) \,\, = \,\, \sum_{i=1}^{n} \,\, \sum_{j\in L_i} \, -\log_2 P_k\!\left( \underbrace{t_i[j]}_{\text{下一个词元}} \ \middle|\ \underbrace{t_i[\max(1,j-k)\,..\,j-1]}_{\text{上下文}} \right)

其中 ti[l .. r]=ti[l]ti[l+1]…ti[r]t_i[l\,..\,r] = t_i[l] t_i[l+1] \ldots t_i[r] 表示从第 ll 个到第 rr 个词元组成的子串,ti[1 .. 0]t_i[1\,..\,0] 表示空字符串。因此,对每个文本及其中每一个由 LLM 生成的词元,我们将该词元在以此前最多 kk 个词元(若前缀长度不足 kk,则取整个前缀)为上下文条件下被生成的概率的负以 2 为底的对数加入损失值中。若该概率为零,则约定其负对数值为 +∞+\infty。该损失函数被称为(以 2 为底的)交叉熵损失(Cross Entropy Loss),仅在 LLM 生成的位置上计算。损失函数值 Lk(Pk)\mathcal{L}_k(P_k) 越小,说明 LLM PkP_k 的性能越优。

对每个满足 0≤k<max⁡i=1..n∣ti∣0 \leq k < \max\limits_{i=1..n} |t_i| 的整数 kk,计算在某个上下文长度为 kk 的 LLM PkP_k 上所能达到的最小可能损失 Lk(Pk)\mathcal{L}_k(P_k)。可以证明,该最小值一定可达,且不为无穷大。

输入格式

The first line contains a single integer nn (1≤n≤1051 \leq n \leq 10^5) — the number of texts in the dataset. Text descriptions follow.

The first line of the ii-th text description contains a single integer mim_i (1≤mi≤3⋅1051 \leq m_i \leq 3 \cdot 10^5) — the length of tit_i (mi=∣ti∣m_i = |t_i|).

The next line contains mim_i strings ti[1]t_{i}[1], ti[2]t_{i}[2], …\ldots, ti[mi]t_{i}[m_i] (1≤∣ti[j]∣≤51 \leq |t_{i}[j]| \leq 5) — tokens of the text tit_i. Each token consists of symbols with ASCII codes from 3333 to 126126 (printable characters).

The next line contains a string ℓi\ell_i of mim_i letters U and L, which encodes the set LiL_i. 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]=LL_i = {j\,|\,\ell_{i}[j] = \texttt{L}}. It is guaranteed that the last token is generated by LLM, so ℓi[mi]=L\ell_{i}[m_i] = \texttt{L}.

It is guaranteed that the sum of mim_i for all ii (1≤i≤n1 \le i \le n) does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5),表示数据集中文本的数量。随后是各文本的描述。

第 ii 个文本描述的第一行包含一个整数 mim_i(1≤mi≤3⋅1051 \leq m_i \leq 3 \cdot 10^5),表示文本 tit_i 的长度(即 mi=∣ti∣m_i = |t_i|)。

下一行包含 mim_i 个字符串 ti[1]t_{i}[1]、ti[2]t_{i}[2]、…\ldots、ti[mi]t_{i}[m_i](每个字符串长度满足 1≤∣ti[j]∣≤51 \leq |t_{i}[j]| \leq 5),表示文本 tit_i 的各个词元(token)。每个词元均由 ASCII 码在 3333 到 126126 范围内的字符(即可打印字符)组成。

再下一行包含一个由 mim_i 个字母 U 和 L 组成的字符串 ℓi\ell_i,用于编码集合 LiL_i:所有对应位置为字母 L 的下标属于 LiL_i(即该位置词元由大语言模型生成),所有对应位置为字母 U 的下标则属于用户撰写部分。因此 Li={j∣ℓi[j]=L}L_i = \{j \mid \ell_{i}[j] = \texttt{L}\}。题目保证最后一个词元一定由大语言模型生成,即 ℓi[mi]=L\ell_{i}[m_i] = \texttt{L}。

题目保证对所有 ii(1≤i≤n1 \le i \le n)的 mim_i 之和不超过 3⋅1053 \cdot 10^5。

输出格式

Print M=max⁡i=1..nmiM = \max\limits_{i=1..n} m_i real numbers: for each k=0,1,…,M−1k = 0, 1, \ldots, M-1 print the minimum possible loss Lk(Pk)\mathcal{L}_k(P_k) for all possible PkP_k — LLM with context size kk.

Your answers will be accepted if their absolute or relative errors do not exceed 10−610^{-6}; formally, if pp is your answer, and qq is the jury's answer, this should hold: ∣p−q∣max⁡1,∣q∣≤10−6\frac{|p - q|}{\max{1, |q|}} \le 10^{-6}.

输出 M=max⁡i=1..nmiM = \max\limits_{i=1..n} m_i 个实数:对每个 k=0,1,…,M−1k = 0, 1, \ldots, M-1,输出所有可能的 PkP_k(即上下文长度为 kk 的大语言模型)所能达到的最小损失 Lk(Pk)\mathcal{L}_k(P_k)。

若您的答案绝对误差或相对误差不超过 10−610^{-6},则视为正确;形式化地,若 pp 是您的答案,qq 是出题方的答案,则需满足:∣p−q∣max⁡1,∣q∣≤10−6\frac{|p - q|}{\max{1, |q|}} \le 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 &lt;EOS&gt;
    LLLL
    5
    N E R C &lt;EOS&gt;
    LLLLL
    6
    N E E R C &lt;EOS&gt;
    LLLLLL
    5
    I C P C &lt;EOS&gt;
    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测评打分。不知道怎么写?

首页