- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
一.量化模型调用方式 下面是一个调用 FlagAlpha/Llama2-Chinese-13b-Chat[1]的4bit压缩版本FlagAlpha/Llama2-Chinese-13b-Chat-4bit[2] 的例子: 这里面有个问题就是由 Llama2-Chinese-13b-Chat 如何得到 Llama2-Chinese-13b-Chat-4bit ?这涉及另外一个AutoGPTQ库(一个基于GPTQ算法,简单易用且拥有用户友好型接口的大语言模型量化工具包)[3]。先梳理下思路,由于 meta-llama/Llama-2-13b-chat-hf 对中文支持较差,所以采用中文指令集在此基础上进行LoRA微调得到了 FlagAlpha/Llama2-Chinese-13b-Chat-LoRA ,而 FlagAlpha/Llama2-Chinese-13b-Chat=FlagAlpha/Llama2-Chinese-13b-Chat-LoRA+meta-llama/Llama-2-13b-chat-hf ,即将两者参数合并后的版本。 FlagAlpha/Llama2-Chinese-13b-Chat-4bit 就是对 FlagAlpha/Llama2-Chinese-13b-Chat 进行4bit量化后的版本。总结起来就是如何合并,如何量化这2个问题。官方提供的一些合并参数后的模型[4],如下所示: 二.如何合并LoRA Model和Base Model 网上合并LoRA参数和原始模型的脚本很多,参考文献[6]亲测可用。合并后的模型格式包括 pth 和 huggingface 两种。如下所示: 1.LoRA Model文件列表 对于LLama2-7B-hf进行LoRA微调生成文件如下所示: 2.Base Model文件列表 LLama2-7B-hf文件列表,如下所示: 3.合并后 huggingface 文件列表 合并LoRA Model和Base Model后,生成huggingface格式文件列表,如下所示: 4.合并后 pth 文件列表 合并LoRA Model和Base Model后,生成pth格式文件列表,如下所示: 5.合并脚本[6]思路 以合并后生成huggingface模型格式为例,介绍合并脚本的思路,如下所示: 合并LoRA Model和Base Model过程中输出日志可参考huggingface[7]和pth[8]. 三.如何量化4bit模型 如果得到了一个训练好的模型,比如LLama2-7B,如何得到LLama2-7B-4bit呢?因为模型参数越来越多,多参数模型的量化还是会比少参数模型的非量化效果要好。量化的方案非常的多[9][12],比如AutoGPTQ、GPTQ-for-LLaMa、exllama、llama.cpp等。下面重点介绍下AutoGPTQ的基础实践过程[10],AutoGPTQ进阶教程参考文献[11]. 参考文献: [1]https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat [2]https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat-4bit [3]https://github.com/PanQiWei/AutoGPTQ/blob/main/README_zh.md [4]https://github.com/FlagAlpha/Llama2-Chinese#基于Llama2的中文微调模型 [5]CPU中合并权重(合并思路仅供参考):https://github.com/yangjianxin1/Firefly/blob/master/script/merge_lora.py [6]https://github.com/ai408/nlp-engineering/blob/main/20230916_Llama2-Chinese/tools/merge_llama_with_lora.py [7]https://github.com/ai408/nlp-engineering/blob/main/20230916_Llama2-Chinese/tools/merge_llama_with_lora_log/merge_llama_with_lora_hf_log [8]https://github.com/ai408/nlp-engineering/blob/main/20230916_Llama2-Chinese/tools/merge_llama_with_lora_log/merge_llama_with_lora_pt_log [9]LLaMa量化部署:https://zhuanlan.zhihu.com/p/641641929 [10]AutoGPTQ基础教程:https://github.com/PanQiWei/AutoGPTQ/blob/main/docs/tutorial/01-Quick-Start.md [11]AutoGPTQ进阶教程:https://github.com/PanQiWei/AutoGPTQ/blob/main/docs/tutorial/02-Advanced-Model-Loading-and-Best-Practice.md [12]Inference Experiments with LLaMA v2 7b:https://github.com/djliden/inference-experiments/blob/main/llama2/README.md [13] llama2_quantize_AutoGPTQ :https://github.com/ai408/nlp-engineering/blob/main/20230916_Llama2-Chinese/tools/llama2_quantize_AutoGPTQ.py 。
from transformers import AutoTokenizer
from auto_gptq import AutoGPTQForCausalLM
model = AutoGPTQForCausalLM.from_quantized(
'FlagAlpha/Llama2-Chinese-13b-Chat-4bit'
, device=
"cuda:0"
)
tokenizer = AutoTokenizer.from_pretrained(
'FlagAlpha/Llama2-Chinese-13b-Chat-4bit'
,use_fast=False)
input_ids = tokenizer([
'<s>Human: 怎么登上火星\n</s><s>Assistant: '
], return_tensors=
"pt"
,add_special_tokens=False).input_ids.to(
'cuda'
)
generate_input = {
"input_ids"
:input_ids,
"max_new_tokens"
:512,
"do_sample"
:True,
"top_k"
:50,
"top_p"
:0.95,
"temperature"
:0.3,
"repetition_penalty"
:1.3,
"eos_token_id"
:tokenizer.eos_token_id,
"bos_token_id"
:tokenizer.bos_token_id,
"pad_token_id"
:tokenizer.pad_token_id
}
generate_ids = model.generate(**generate_input)
text = tokenizer.decode(generate_ids[0])
print
(text)
adapter_config.json
adapter_model.bin
optimizer.pt
README.md
rng_state.pth
scheduler.pt
special_tokens_map.json
tokenizer.json
tokenizer.model
tokenizer_config.json
trainer_state.json
training_args.bin
config.json
generation_config.json
gitattributes.txt
LICENSE.txt
model-00001-of-00002.safetensors
model-00002-of-00002.safetensors
model.safetensors.index.json
pytorch_model-00001-of-00002.bin
pytorch_model-00002-of-00002.bin
pytorch_model.bin.index.json
README.md
Responsible-Use-Guide.pdf
special_tokens_map.json
tokenizer.json
tokenizer.model
tokenizer_config.json
USE_POLICY.md
config.json
generation_config.json
pytorch_model-00001-of-00007.bin
pytorch_model-00002-of-00007.bin
pytorch_model-00003-of-00007.bin
pytorch_model-00004-of-00007.bin
pytorch_model-00005-of-00007.bin
pytorch_model-00006-of-00007.bin
pytorch_model-00007-of-00007.bin
pytorch_model.bin.index.json
special_tokens_map.json
tokenizer.model
tokenizer_config.json
consolidated.00.pth
params.json
special_tokens_map.json
tokenizer.model
tokenizer_config.json
# 步骤1:加载base model
base_model = LlamaForCausalLM.from_pretrained(
base_model_path,
# 基础模型路径
load_in_8bit=False,
# 加载8位
torch_dtype=torch.float16,
# float16
device_map={
""
:
"cpu"
},
# cpu
)
# 步骤2:遍历LoRA模型
for
lora_index, lora_model_path
in
enumerate(lora_model_paths):
# 步骤3:根据base model和lora model来初始化PEFT模型
lora_model = PeftModel.from_pretrained(
base_model,
# 基础模型
lora_model_path,
# LoRA模型路径
device_map={
""
:
"cpu"
},
# cpu
torch_dtype=torch.float16,
# float16
)
# 步骤4:将lora model和base model合并为一个独立的model
base_model = lora_model.merge_and_unload()
......
# 步骤5:保存tokenizer
tokenizer.save_pretrained(output_dir)
# 步骤6:保存合并后的独立model
LlamaForCausalLM.save_pretrained(base_model, output_dir, save_function=torch.save, max_shard_size=
"2GB"
)
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
# 量化配置
from transformers import AutoTokenizer
# 第1部分:量化一个预训练模型
pretrained_model_name = r
"L:/20230713_HuggingFaceModel/20230903_Llama2/Llama-2-7b-hf"
# 预训练模型路径
quantize_config = BaseQuantizeConfig(bits=4, group_size=128)
# 量化配置,bits表示量化后的位数,group_size表示分组大小
model = AutoGPTQForCausalLM.from_pretrained(pretrained_model_name, quantize_config)
# 加载预训练模型
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name)
# 加载tokenizer
examples = [
# 量化样本
tokenizer(
"auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."
)
]
# 翻译:准备examples(一个只有两个键'input_ids'和'attention_mask'的字典列表)来指导量化。这里只使用一个文本来简化代码,但是应该注意,使用的examples越多,量化后的模型就越好(很可能)。
model.quantize(examples)
# 执行量化操作,examples提供量化过程所需的示例数据
quantized_model_dir =
"./llama2_quantize_AutoGPTQ"
# 保存量化后的模型
model.save_quantized(quantized_model_dir)
# 保存量化后的模型
# 第2部分:加载量化模型和推理
from transformers import TextGenerationPipeline
# 生成文本
device =
"cuda:0"
model = AutoGPTQForCausalLM.from_quantized(quantized_model_dir, device=device)
# 加载量化模型
pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=device)
# 得到pipeline管道
print
(pipeline(
"auto-gptq is"
)[0][
"generated_text"
])
# 生成文本
最后此篇关于Llama2-Chinese项目:4-量化模型的文章就讲到这里了,如果你想了解更多关于Llama2-Chinese项目:4-量化模型的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用 NetBeans 开发 Java 中的 WebService,并使用 gradle 作为依赖管理。 我找到了this article关于使用 gradle 开发 Web 项目。它使用 Gr
我正在将旧项目从 ant 迁移到 gradle(以使用其依赖项管理和构建功能),并且在生成 时遇到问题>eclipse 项目。今天的大问题是因为该项目有一些子项目被拆分成 war 和 jar 包部署到
我已经为这个错误苦苦挣扎了很长时间。如果有帮助的话,我会提供一些问题的快照。请指导我该怎么办????在我看来,它看起来一团糟。 *** glibc detected *** /home/shivam/
我在 Ubuntu 12.10 上运行 NetBeans 7.3。我正在学习 Java Web 开发类(class),因此我有一个名为 jsage8 的项目,其中包含我为该类(class)所做的工作。
我想知道 Codeplex、GitHub 等中是否有任何突出的项目是 C# 和 ASP.NET,甚至只是 C# API 与功能测试 (NUnit) 和模拟(RhinoMocks、NMock 等)。 重
我创建了一个 Maven 项目,包装类型为“jar”,名为“Y”我已经完成了“Maven 安装”,并且可以在我的本地存储库中找到它.. 然后,我创建了另一个项目,包装类型为“war”,称为“X”。在这
我一直在关注the instructions用于将 facebook SDK 集成到我的应用程序中。除了“helloFacebookSample”之外,我已经成功地编译并运行了所有给定的示例应用程序。
我想知道,为什么我们(Java 社区)需要 Apache Harmony 项目,而已经有了 OpenJDK 项目。两者不是都是在开源许可下发布的吗? 最佳答案 事实恰恰相反。 Harmony 的成立是
我正在尝试使用 Jsoup HTML Parser 从网站获取缩略图 URL我需要提取所有以 60x60.jpg(或 png)结尾的 URL(所有缩略图 URL 都以此 URL 结尾) 问题是我让它在
我无法构建 gradle 项目,即使我编辑 gradle 属性,我也会收到以下错误: Error:(22, 1) A problem occurred evaluating root project
我有这个代码: var NToDel:NSArray = [] var addInNToDelArray = "Test1 \ Test2" 如何在 NToDel:NSArray 中添加 addInN
如何在单击显示更多(按钮)后将主题列表限制为 5 个(项目)。 还有 3(项目),依此类推到列表末尾,然后它会显示显示更少(按钮)。 例如:在 Udemy 过滤器选项中,当您点击查看更多按钮时,它仅显
如何将现有的 Flutter 项目导入为 gradle 项目? “导入项目”向导要求 Gradle 主路径。 我有 gradle,安装在我的系统中。但是这里需要设置什么(哪条路径)。 这是我正在尝试的
我有一个关于 Bitbucket 的项目。只有源被提交。为了将项目检索到新机器上,我在 IntelliJ 中使用了 Version Control > Checkout from Ve
所以,我想更改我公司的一个项目,以使用一些与 IDE 无关的设置。我在使用 Tomcat 设置 Java 应用程序方面有非常少的经验(我几乎不记得它是如何工作的)。 因此,为了帮助制作独立于 IDE
我有 2 个独立的项目,一个在 Cocos2dx v3.6 中,一个在 Swift 中。我想从 Swift 项目开始游戏。我该怎么做? 我已经将整个 cocos2dx 项目复制到我的 Swift 项目
Cordova 绝对是新手。这些是我完成的步骤: checkout 现有项目 运行cordova build ios 以上生成此构建错误: (node:10242) UnhandledPromiseR
我正在使用 JQuery 隐藏/显示 li。我的要求是,当我点击任何 li 时,它应该显示但隐藏所有其他 li 项目。当我将鼠标悬停在文本上时 'show all list item but don
我想将我所有的java 项目(223 个项目)迁移到gradle 项目。我正在使用由 SpringSource STS 团队开发的 Gradle Eclipse 插件。 目前,我所有的 java 项目
我下载this Eclipse Luna ,对于 Java EE 开发人员,如描述中所见,它支持 Web 应用程序。我找不到 file -> new -> other -> web projects
我是一名优秀的程序员,十分优秀!