工作小结——Qwen2-7B-Instruct调用
2025/11/21
1. batch调用需要设置padding和truncation,以及max_length;
2. 无需手动设置 mask,
;
1. batch调用需要设置padding和truncation,以及max_length;
2. 无需手动设置 mask,
tokenizer(padding=True)会自动生成「1(有效)+ 0(padding)」的attention_mask:
model.generate(
model_inputs.input_ids,
attention_mask=model_inputs.attention_mask, # 传入自动生成的mask, 让模型忽略 padding
max_new_tokens=32768)
3. 在 Qwen2 的 batch 调用中,所有和长度相关的配置(max_length、padding、truncation)都是针对 Token 长度,而非字符长度,务必通过len(tokenizer.encode(text))或input_ids的长度来确认,而非直接用len(text);
4. Qwen2 是仅解码器(decoder-only)架构的 LLM,这类模型生成文本时默认从序列末尾开始自回归生成;若 tokenizer 的padding_side为right(右侧填充),padding 的 0 会出现在有效 token 的右侧,模型生成时会优先处理 padding 部分,导致生成结果异常。因此需将 tokenizer 的填充方向改为left(左侧填充),让有效 token 位于序列末尾,符合 decoder-only 模型的生成逻辑
tokenizer = AutoTokenizer.from_pretrained(llm_path,
padding_side='left',
pad_token='<|endoftext|>',
trust_remote_code=True)
用代码改变世界!就是这样,喵!
posted on 2025-11-21 18:01 Mju_halcyon 阅读(0) 评论(0) 收藏 举报
浙公网安备 33010602011771号