Tiny LLM 76m 对话模型示例

我要开发同款
匿名用户2024年07月31日
35阅读
所属分类ai、tinyllm、Pytorch
开源地址https://modelscope.cn/models/wdndev/tiny_llm_sft_76m_llama
授权协议Apache License 2.0

作品详情

Tiny LLM 76M SFT

简介

本项目wdndev/tiny-llm-zh (github.com)旨在构建一个小参数量的中文语言大模型,用于快速入门学习大模型相关知识。

模型架构:整体模型架构采用开源通用架构,包括:RMSNorm,RoPE,MHA等

实现细节:实现大模型两阶段训练及后续人类对齐,即:预训练(PTM) -> 指令微调(SFT) -> 人类对齐(RLHF, DPO) -> 测评。

注意:因资源限制,本项目的第一要务是走通大模型整个流程,而不是调教比较好的效果,故评测结果分数较低,部分生成错误。

注意:此模型采用扩充 llama2 的词表后进行训练的;使用 tinyllmsft_92m 模型初始化transformers层,随机初始化embedding层,继续训练10B 的token,微调后得到;

模型细节

大约在9B的中文预料中训练,主要包含百科内容,模型架构采用开源通用架构,包括:RMSNorm,RoPE,MHA等。

环境

只需要安装 transformers 即可运行

快速开始

from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "wdndev/tiny_llm_sft_76m_llama"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True)

sys_text = "你是由wdndev开发的个人助手。"
# user_text = "中国的首都是哪儿?"
# user_text = "你叫什么名字?"
user_text = "介绍一下中国"
input_txt = "\n".join(["<|system|>", sys_text.strip(), 
                        "<|user|>", user_text.strip(), 
                        "<|assistant|>"]).strip() + "\n"

model_inputs = tokenizer(input_txt, return_tensors="pt").to(model.device)
generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=200)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论