LLM - Chinese-Llama-2-7b 初体验

news2024/10/1 17:32:36

Chinese LLaMA2 7B

目录

一.引言

二.模型下载

三.快速测试

四.训练数据

五.总结


一.引言

自打 LLama-2 发布后就一直在等大佬们发布 LLama-2 的适配中文版,也是这几天蹲到了一版由 LinkSoul 发布的 Chinese-Llama-2-7b,其共发布了一个常规版本和一个 4-bit 的量化版本,今天我们主要体验下 Llama-2 的中文逻辑顺便看下其训练样本的样式,后续有机会把训练和微调跑起来。

二.模型下载

HuggingFace: https://huggingface.co/LinkSoul/Chinese-Llama-2-7b

4bit 量化版本: https://huggingface.co/LinkSoul/Chinese-Llama-2-7b-4bit

这里我们先整一版量化版本:

 省事且网络好的同学可以直接用 Hugging Face 的 API 下载,网不好就半夜慢慢下载吧。

from huggingface_hub import hf_hub_download, snapshot_download

snapshot_download(repo_id="LinkSoul/Chinese-Llama-2-7b-4bit", local_dir='./models')

三.快速测试

Tips 测试用到的基本库的版本,运行显卡为 Tesla-V100 32G:

python 3.9.11
numpy==1.23.5
torch==2.0.1
transformers==4.29.1

测试代码:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer

# Original version
# model_path = "LinkSoul/Chinese-Llama-2-7b"
# 4 bit version
model_path = "/models/LLama2_4bit"


tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
if model_path.endswith("4bit"):
    model = AutoModelForCausalLM.from_pretrained(
            model_path,
            torch_dtype=torch.float16,
            device_map='auto'
        )
else:
    model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

            If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""

while True:
    text = input("请输入 prompt\n")
    if text == "q":
        break
    prompt = instruction.format(text)
    generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

★ 常规测试

知识:

 推理:

★ 一些 Bad Case

知识错乱:

重复:

这里由于是 4-bit 的量化版本,模型的效果可能也会受影响,可以看到图中原始 LLama2 的知识能力相对还算不错。

 

四.训练数据

LinkSoul 在 LLama2 的基础上使用了中英文 SFT 数据集,数据量 1000 万:

LinkSoul/instruction_merge_set · Datasets at Hugging Face

 给定的数据是 parquet 格式的:

{"instruction":"","id":"chatalpaca_data_10k_0","conversations":"WrappedArray([human,Find the product of the numbers: 5 and 8], [gpt,The product of 5 and 8 is 40.], [human,What is the sum of the numbers 6 and 12?], [gpt,The sum of the numbers 6 and 12 is 18.], [human,Can you tell me the quotient of 20 and 5?], [gpt,Yes, the quotient of 20 and 5 is 4.], [human,What is the difference between 25 and 13?], [gpt,The difference between 25 and 13 is 12.], [human,What is the square of 9?], [gpt,The square of 9 is 81.], [human,What is the cube of 6?], [gpt,The cube of 6 is 216.])"}
{"instruction":"","id":"chatalpaca_data_10k_1","conversations":"WrappedArray([human,Provide five tips for effectively using tape measures], [gpt,1. Tighten the tape measure so that it stays in place when measuring.\n2. Make sure that the hook of the tape measure hits the starting point precisely.\n3. Allow for a bit of slack when measuring around curves or corners.\n4. Measure from a stable surface, such as a table or workbench.\n5. Mark the beginning and end of each measurement with a pen or marker.], [human,Can you give me an example of when to use tip number three, \"Allow for a bit of slack when measuring around curves or corners\"?], [gpt,Sure! Let's say you're measuring the circumference of a circular object, like a pipe. If you pull the tape measure too tightly, you won't get an accurate measurement because the tape measure will be stretched out of its original shape, making the measurement longer than it should be. Instead, you should wrap the tape measure around the curve or corner of the object without pulling it too tightly. This way, you'll get a more accurate measurement that takes into account the shape of the object.], [human,Can you tell me more about the markers or pens that should be used to mark the measurements? Do you recommend a certain type of pen?], [gpt,As an AI language model, I do not have personal preferences as a human, but there are certain types of markers or pens that are better suited for marking measurements than others. For example, a fine-tipped marker or mechanical pencil is great for making precise marks on a tape measure, because they produce a precise line that is easy to see. You'll want to avoid using a ballpoint pen or a marker with a thick tip, since these types of pens can produce marks that are too thick or blurry, making it difficult to accurately read the measurement. It's also a good idea to choose a marker or pen that is a different color than the color of the tape measure, so that you can easily distinguish your marks from the printed markings on the tape measure.], [human,Are there any other tips you recommend for maintaining tape measures in good condition? \n\nassistant: \nYes! One tip is to keep your tape measure clean and free from debris, so that it moves smoothly and doesn't get jammed or stuck. You can use a soft cloth or a brush to wipe down the tape measure after each use, and make sure to store it in a dry place to prevent rust or corrosion. Another tip is to avoid dropping or mishandling the tape measure, since this can damage the internal components and make it less accurate. It's also a good idea to periodically check your tape measure against a known length or standard to make sure that it is still measuring accurately.], [gpt,Finally, when retracting the tape measure, avoid letting it snap back quickly, as this can cause the tape to twist or become tangled. Instead, hold it firmly and retract it slowly and steadily to avoid damaging the internal components. With proper care and maintenance, your tape measure should last for years and provide accurate measurements every time.], [human,Goodbye.])"}

instruction 指令可选,剩下 conversations 里是 human 和  gpt 的多轮对话。

def tokenize(item, tokenizer):
    roles = {"human": "user", "gpt": "assistant"}
    input_ids = []
    labels = []
    if "instruction" in item and len(item["instruction"]) > 0:
        system = item["instruction"]
    else:
        system = dummy_message["system"]
    system = B_SYS + system + E_SYS
    # add system before the first content in conversations
    item["conversations"][0]['value'] = system + item["conversations"][0]['value']
    for i, turn in enumerate(item["conversations"]):
        role = turn['from']
        content = turn['value']
        content = content.strip()
        if role == 'human':
            content = f"{B_INST} {content} {E_INST} "
            content_ids = tokenizer.encode(content)
            labels += [IGNORE_TOKEN_ID] * (len(content_ids))
        else:
            # assert role == "gpt"
            content = f"{content} "
            content_ids = tokenizer.encode(content, add_special_tokens=False) + [tokenizer.eos_token_id]   # add_special_tokens=False remove bos token, and add eos at the end
            labels += content_ids
        input_ids += content_ids

    input_ids = input_ids[:tokenizer.model_max_length]
    labels = labels[:tokenizer.model_max_length]

    trunc_id = last_index(labels, IGNORE_TOKEN_ID) + 1
    input_ids = input_ids[:trunc_id]
    labels = labels[:trunc_id]
    if len(labels) == 0:
        return tokenize(dummy_message, tokenizer)
    input_ids = safe_ids(input_ids, tokenizer.vocab_size, tokenizer.pad_token_id)
    labels = safe_ids(labels, tokenizer.vocab_size, IGNORE_TOKEN_ID)
    return input_ids, labels

训练代码:https://github.com/LinkSoul-AI/Chinese-Llama-2-7b/blob/main/train.py

中展示了 tokenizer 原始样本的流程:

◆  根据指令生成 system

◆  根据 from 和 value 的多轮对话生成 input_ids 和 labels

Tips: 这里会把前面生成的 system 缀到第一个 value 前面,labels 会在 human 部分用 IGNORE_TOKEN_ID 的掩码进行 Mask

◆  最后 safe_ids 用于限制 id < max_value 超过使用 pad_id 进行填充

def safe_ids(ids, max_value, pad_id):
    return [i if i < max_value else pad_id for i in ids]

这里输入格式严格遵循 llama-2-chat 格式,兼容适配所有针对原版 llama-2-chat 模型的优化。

五.总结

这里简单介绍了 LLama-2 7B Chinese 的推理和数据样式,后续有机会训练和微调该模型。

参考: 

Chinese Llama 2 7B: https://github.com/LinkSoul-AI/Chinese-Llama-2-7b

Model: https://huggingface.co/LinkSoul/Chinese-Llama-2-7b

Instruction_merge_set: https://huggingface.co/datasets/LinkSoul/instruction_merge_set/

Download Files: https://huggingface.co/docs/huggingface_hub/v0.16.3/guides/download

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/788420.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

.360勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复

引言&#xff1a; 近年来&#xff0c;随着互联网的普及和信息技术的快速发展&#xff0c;网络安全问题日益严峻。其中&#xff0c;勒索病毒成为网络安全领域的一大威胁。本文91数据恢复将重点介绍一种名为“.360勒索病毒”的恶意软件&#xff0c;并探讨被该病毒加密的数据文件…

MyBatis学习之SQL查询

文章目录 resultMap结果映射使用resultMap结果映射是否开启驼峰命名自动映射 返回总记录条数 resultMap结果映射 使用resultMap结果映射 专门定义一个结果映射&#xff0c;在这个结果映射当中指定数据库表的字段名和Java类的属性名的对应关系type属性&#xff1a;用来指定POJ…

Redis 基础知识和核心概念解析:理解 Redis 的键值操作和过期策略

&#x1f337;&#x1f341; 博主 libin9iOak带您 Go to New World.✨&#x1f341; &#x1f984; 个人主页——libin9iOak的博客&#x1f390; &#x1f433; 《面试题大全》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33…

MCU全球生态发展大会|AT32 MCU加速应用创新与产业智慧升级

7月21日&#xff0c;由AspenCore主办的2023全球MCU生态发展大会在深圳罗湖君悦酒店圆满举行。本次活动聚集国际和本土知名MCU厂商的技术和应用专家&#xff0c;为来自消费电子、家电、工业控制、通信网络、新能源汽车和物联网领域的OEM厂商和方案集成商代表带来MCU领域的最新技…

Linux工具——vim

安装vim yum -y install vim 如果安装失败&#xff0c;提示Could not resolve host:mirrorlist.centos.org: Unkown error的问题&#xff0c;需要替换yum源&#xff0c;可以参考这个文章 配置vim root的vim配置文件在 /etc/vimrc 普通用户的vim配置文件在用户对应家目录下&a…

eNSP:简单使用acl进阶版

实验要求&#xff1a; 步骤一&#xff1a;配置IP地址 步骤二&#xff1a;配置IP地址&#xff1a; PC1: <Huawei>sys Enter system view, return user view with CtrlZ. [Huawei]sysname PC1 [PC1]int g 0/0/0 [PC1-GigabitEthernet0/0/0]ip add 192.168.1.1 24 Jul 25 …

通嵌入式岗位门槛不高,懂点C语言编程和单片机

搞个简单的电子产品&#xff0c;门槛很低&#xff0c;当然工资低。 门槛高的嵌入式岗位&#xff1a;例如大疆、华为、小米&#xff0c;英伟达、高通这类公司嵌入式岗位&#xff0c;还是有一定要求的。工资待遇也是和不联网不差的。我司的嵌入式开发人员&#xff0c;清北的硕士…

zookeeper-3.7.1集群

1.下载&解压安装包apache-zookeeper-3.7.1-bin.tar.gz 解压到/app/ &改名zookeeper-3.7.1 [rootnode1 app]# tar -zxvf apache-zookeeper-3.7.1-bin.tar.gz -C /app/ [rootnode1 app]# mv apache-zookeeper-3.7.1-bin zookeeper-3.7.1 ---- 删除docs [rootnode1…

excel中单行换成多行

今天碰以下情况&#xff1a; 这在excel表中是在一个单元格&#xff0c;现在需要对其进行转换&#xff0c;将一个单元格换成多行 步骤&#xff1a; 1.删除换行符&#xff0c;添加一个逗号 2.选择数据-分列-分隔字符-逗号-确定 3.复制上述数据&#xff0c;选择性粘贴-转置 完…

基于LoRA进行Stable Diffusion的微调

文章目录 基于LoRA进行Stable Diffusion的微调数据集模型下载环境配置微调过程 基于LoRA进行Stable Diffusion的微调 数据集 本次微调使用的数据集为&#xff1a; LambdaLabs的Pokemon数据集 使用git clone命令下载数据集 git clone https://huggingface.co/datasets/lambd…

Android性能优化相关问题总结盘点~

以下是本人在工作中对经历过的性能优化的一些总结&#xff0c;依据故事的发展路线&#xff0c;将其分为了5个部分&#xff0c;分别是&#xff1a;常见的性能问题&#xff1b;产生性能问题的一些可能原因&#xff1b;解决性能问题的套路&#xff1b;代码建议及潜在性能问题排查项…

AtcoderABC232场

A - QQ solverA - QQ solver 题目大意 要求从一个长度为3的字符串中提取出两个整数a和b&#xff0c;字符串的格式是axb&#xff0c;其中a和b是介于1到9之间&#xff08;包括11和99&#xff09;的整数&#xff0c;计算它们的乘积。 思路分析 将字符类型转换为整数类型来提取出…

mysql进阶2——prosysql实现mysql读写分离

文章目录 一、读写分离方案类型1.1 最简单的读写分离1.2 多个读组或写组的分离模式 二、案例2.1 初始化操作2.2 mysql主添加proxysql连接用户2.3 Proxysql添加连接mysql集群参数2.4 添加健康检测用户2.5 添加读写分离的路由规则2.6 验证 一、读写分离方案类型 基本了解&#xf…

区分能带图

能带结构是目前采用第一性原理&#xff08;从头abinitio&#xff09;计算所得到的常用信息&#xff0c;可用来结合解释金属、半导体和绝缘体的区别。能带可分为价带、禁带和导带三部分&#xff0c;倒带和价带之间的空隙称为能隙。 如果能隙很小或为0 &#xff0c;则固体为金属…

u-tabs设置默认选中值

uView中u-tabs设置默认选中值&#xff1a; tabs官网连接:https://www.uviewui.com/components/tabs.html 在u-tabs标签中添加 :current 属性值 【注意】&#xff1a;current数值是从0开始的

HMM与CRF模型的使用过程有哪些差异?

在NLP领域, HMM用来解决文本序列标注问题. 如分词, 词性标注, 命名实体识别都可以看作是序列标注问题。同HMM一样, CRF一般也以文本序列数据为输入, 以该序列对应的隐含序列为输出。 HMM模型 HMM模型表示为: lambda HMM(A, B, pi), 其中A, B, pi都是模型的参数, 分别称作: 转…

Mentor Xpedition 过孔的制做以及如何把孔加入PCB板中

Mentor Xpedition 过孔的制做以及如何把孔加入PCB板中 常用的过孔有10/18mil &#xff0c;10/22mil &#xff0c;12/24mil。目前国内PCB厂制做工艺孔径比为8:1(板厚与孔径&#xff09;优秀的PCB板厂的工艺孔径比会更大。 1、下面我们用Mentor建立10/18的孔&#xff0c;打开Se…

P2390 地标访问(二分+结论推导)

来一手结论&#xff01;&#xff1a;贝西想要经过最多的地标数量&#xff0c;一定不能反复横跳 所以简单了&#xff1a;&#xff08;&#xff09; 题目问的是最多可以访问多少地标。稍稍分析可知&#xff0c;多访问一个路标&#xff0c;时间必定不会减少&#xff0c;显然这具有…

王道考研数据结构第三章知识点总结

3.1.1 栈的基本概念 注意&#xff1a;卡特兰数结合先序进栈&#xff0c;中序出栈考察&#xff0c;第五章树与二叉树2015年关于不同二叉树的个数的题目 3.1.2 栈的顺序存储实现 3.1.3 栈的链式存储实现 3.2.1 队列的基本概念(详见课件) 3.2.2 队列的顺序实现(详见课件) 3.2.3 队…

WPF基础知识

WPF布局基础原则 1.一个窗口中只能包含一个元素 2. 不应该显示设置元素尺寸 3. 不应使用坐标设置元素的位置 4.可以嵌套布局容器WPF布局容器 StackPanel:水平或垂直排列元素&#xff0c;Orientation属性分别为&#xff1a;Horizontal/VerticalWrapPanel:水平或垂直排列元素、…