LLaMA-Factory icon indicating copy to clipboard operation
LLaMA-Factory copied to clipboard

cutoff_len 语义混淆

Open hbsun2113 opened this issue 7 months ago • 6 comments

Reminder

  • [x] I have read the above rules and searched the existing issues.

System Info

cutoff_len有两种解释:

  1. 单纯截断模式:每个独立样本被截断到 cutoff_len 长度,超出部分被丢弃

    • 在这种模式下,增加 cutoff_len 不会减少样本数量
    • 迭代次数应保持不变
  2. 连续分块模式:在预训练阶段 (stage: pt),文本被分成连续的、长度为 cutoff_len 的块

    • 在这种模式下,增加 cutoff_len 会减少生成的块数量
    • 如果 cutoff_len 翻倍,样本数量大约减半,迭代次数也会减半

文档的说法是:输入的最大 token 数,超过该长度会被截断,我认为是上述第一种解释,但是根据我的实验,实现是第二种解释。

Reproduction

实验步骤:

  1. 调整cutoff_len从2048为4096,iteration总数从74变为37
  2. 查看tokenized_path下的data-00000-of-00001.arrow,所有数据都是cutoff_len长度: Image

Others

No response

hbsun2113 avatar May 08 '25 06:05 hbsun2113

同问

lifelsl avatar May 08 '25 06:05 lifelsl

同问

这里关于cutoff_len的两种用法我认为是都存在的,通过楼主的现象发现确实属于第二种理解。但是在我的实验中我发现cutoff_len属于第一种解释,因为我的总步数很明显就是(数据条数)*(epoch)/(梯度累计步数)。而且我调整了cutof_len后我的步数也并没有发生变化。同时也非常想知道为什么一个参数会有两种不同的情况!

Image

qizheyanger avatar May 08 '25 06:05 qizheyanger

找到原因了,需要配合packing=false才是第一种解释,但pt默认是packing=true就会是第二种解释,希望文档能补充说明

hbsun2113 avatar May 08 '25 06:05 hbsun2113

找到原因了,需要配合packing=false才是第一种解释,但pt默认是packing=true就会是第二种解释,希望文档能补充说明

确实是这样,明白了,赞!

qizheyanger avatar May 08 '25 07:05 qizheyanger

大佬们。我有个问题。 前提:对Qwen3-32B进行预训练,packing=false,长度选择2k,我的每条样本保证token长度不超过2k,假设位 1000。 那么 给模型的 数据,每个样本数据的 input_ids就是 长度是 1001(末尾加eos),不足2k。

疑问: 1、需要手动在 input_ids后面添加 pad_token 吗,把每条样本补长到 2k。 2、需要手动补充 labels列和attention_mask列吗。 labels前1001与input_ids一致,之后置IGNORE_INDEX;attention_mask前1001置1,之后置0

wufenglailai avatar May 18 '25 07:05 wufenglailai

大佬们。我有个问题。 前提:对Qwen3-32B进行预训练,packing=false,长度选择2k,我的每条样本保证token长度不超过2k,假设位 1000。 那么 给模型的 数据,每个样本数据的 input_ids就是 长度是 1001(末尾加eos),不足2k。

疑问: 1、需要手动在 input_ids后面添加 pad_token 吗,把每条样本补长到 2k。 2、需要手动补充 labels列和attention_mask列吗。 labels前1001与input_ids一致,之后置IGNORE_INDEX;attention_mask前1001置1,之后置0

1和2都不需要,会自动补齐 https://github.com/hiyouga/LLaMA-Factory/blob/main/src/llamafactory/data/processor/supervised.py#L32

mZhenz avatar May 18 '25 13:05 mZhenz

大佬们。我有个问题。 前提:对Qwen3-32B进行预训练,packing=false,长度选择2k,我的每条样本保证token长度不超过2k,假设位 1000。 那么 给模型的 数据,每个样本数据的 input_ids就是 长度是 1001(末尾加eos),不足2k。 疑问: 1、需要手动在 input_ids后面添加 pad_token 吗,把每条样本补长到 2k。 2、需要手动补充 labels列和attention_mask列吗。 labels前1001与input_ids一致,之后置IGNORE_INDEX;attention_mask前1001置1,之后置0

1和2都不需要,会自动补齐 https://github.com/hiyouga/LLaMA-Factory/blob/main/src/llamafactory/data/processor/supervised.py#L32

请问这个 token是需要我们在制作数据集的时候添加吗,还是说LF会追加上去

Lynnzake avatar Sep 12 '25 09:09 Lynnzake

还有个问题 cutoff_len如果是针对于prompt的,那么sft里有参数控制输出response长度的吗?我翻了参数介绍,没看到有这种控制参数,只有生成的时候vllm有个max_new_tokens

disperaller avatar Sep 23 '25 03:09 disperaller