当前位置: 首页 > news >正文

llama factory 扩充词表训练

文章目录

    • 方式一
    • 方式二
    • 注意

方式一

from transformers import AutoTokenizer, AutoModelForCausalLM
import torchmodel_name = "/root/autodl-tmp/LLaMA-Factory/ckpts/Qwen3-0.6B"
new_tokens = ["<|C-L|>", "<|S-L|>", "<|C-S|>", "<|S-S|>"]output_dir = model_name + "_custom_tokens"print("[DEBUG] output_dir: ", output_dir)tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name,dtype="auto",device_map="auto",trust_remote_code=True,
)print("[DEBUG] 已加载原始模型和分词器")
print(f"[DEBUG] 原始词表大小: {len(tokenizer)}")# 检查当前是否已存在
exist = [t for t in new_tokens if tokenizer.convert_tokens_to_ids(t) != tokenizer.unk_token_id]
print("[DEBUG] new_tokens already exist:", exist)exist = []
for t in new_tokens:for st in list(t):if tokenizer.convert_tokens_to_ids(st) != tokenizer.unk_token_id:exist.append(st)print("[DEBUG] tokens already exist:", set(exist))# 把 new tokens 加入 tokenizer
added = tokenizer.add_special_tokens({"additional_special_tokens": new_tokens})    # 把它们当作 additional_special_tokens(语义上更“特殊”)
# added = tokenizer.add_tokens(new_tokens)       # 或者直接 add_tokens(普通 token,但也会被视为单 token)
print(f"[DEBUG] 成功添加 {added} 个新token")
print(f"[DEBUG] 扩展后词表大小: {len(tokenizer)}")# 初始化 embedding
model.resize_token_embeddings(len(tokenizer))
emb = model.get_input_embeddings().weight.data
vocab_size, dim = emb.shape
print("[DEBUG] ", vocab_size, dim)for new_id, new_token in zip(list(range(vocab_size - added, vocab_size)), new_tokens):mean_tensor = []for mean_id in list(new_token):mean_emb = emb[tokenizer.convert_tokens_to_ids(mean_id), :]mean_tensor.append(mean_emb)emb[new_id] = torch.stack(mean_tensor, 0).mean(0)print("[DEBUG] ", emb.shape)exist = [t for t in new_tokens if tokenizer.convert_tokens_to_ids(t) != tokenizer.unk_token_id]
print("[DEBUG] new_tokens already exist:", exist)for t in new_tokens:toks = tokenizer.tokenize(t)ids = tokenizer.encode(t, add_special_tokens=False)print("[DEBUG] ", t, "-> tokens:", toks, "ids:", ids)assert len(ids) == 1, f"{t} 被拆成 {len(ids)} 个 token,需检查 tokenizer 类型"tokenizer.save_pretrained(output_dir, push_to_hub=False)
model.save_pretrained(output_dir, push_to_hub=False)

方式二

在 train.yaml 中添加

# # additional_target: embed_tokens,norm
# # additional_target: embed_tokens,lm_head,norm
new_special_tokens_config: /root/autodl-tmp/LLaMA-Factory/yamls/control_tokens.yaml
init_special_tokens: noise_init   # noise_init, desc_init, desc_init_w_noise
add_special_tokens: <|C-L|>, <|S-L|>, <|C-S|>, <|S-S|>

ref:https://github.com/hiyouga/LLaMA-Factory/pull/9267

注意

合并权重需要有

skip_special_tokens: false

并且加载模型的时候也需要

 self.tokenizer = AutoTokenizer.from_pretrained(self.model_path, use_fast=True, trust_remote_code=True)if self.infer_backend == "huggingface":self.model = AutoModelForCausalLM.from_pretrained(self.model_path,dtype="auto",device_map="auto",trust_remote_code=True,)elif self.infer_backend == "vllm":from vllm import LLM, SamplingParamsself.sampling_params = SamplingParams(temperature=0.01,           # top_p=0.9,                # top_k=1,                  max_tokens=8,               stop=[],                    skip_special_tokens=False,   # 保留特殊token)self.model = LLM(model=self.model_path,tensor_parallel_size=len(self.devices),       gpu_memory_utilization=0.9,                   max_model_len=4096,trust_remote_code=True,enable_prefix_caching=True,                   max_num_seqs=64,                              )
content = self.tokenizer.decode(output_ids[index:], skip_special_tokens=False).strip("\n")
http://www.dtcms.com/a/566693.html

相关文章:

  • 安卓之 MediaSessionService
  • 全网浏览器wordpress优化nginx
  • 用dw做网站的代码做灯箱的网站
  • 温州 网站开发企业策划公关公司
  • 一级做爰片a视频网站试看wordpress主题the 7
  • 网站建设费用IP建设网证书查询平台官网
  • 无锡开发网站建设中国移动生活app下载安装
  • 人工智能备考——大体题型讲解+1.1.1-1.1.5固定搭配总结
  • 对话智源研究院:多模态世界模型如何实现“大一统”?
  • 上海 网站备案系统网站的链接优化
  • 个人网站开发制作教程建设工程检测中心网站
  • 中英文网站asp怎么做谷歌 chrome 浏览器
  • 企业型网站网址营销案例最新
  • 购物类网站青岛百度优化
  • 算法 day 41
  • 建设企业人力资源网站课程资源网站的建设
  • html期末大作业个人网站制作黄页网站推广app免费下载
  • html5 php网站源码下载护肤品 网站建设策划
  • 东莞清洁服务网站建设川畅科技联系 网站设计
  • 京东不让卖网站制作么从域名到网站
  • html5网站后台管理系统云服务器做网站难吗
  • 点的旋转报错记录
  • 在c盘做网站可以吗万界商城系统
  • 江门手机网站建设wordpress step 2
  • Sigmoid vs Softmax
  • 网上做彩票网站排名用自己的电脑做服务器建网站
  • 做网站价格公司深圳网域官网
  • 上海做外贸网站的公司centos7使用wordpress
  • 大学生互助联盟网站建设需求分析说明表辽宁网络科技有限公司
  • 温州专业网站建设wordpress 网站关键词