网站像素大小,网页制作设计思路,旅游网站开发设计文档,南昌做网站排名Py之auto-gptq#xff1a;auto-gptq的简介、安装、使用方法之详细攻略 目录
auto-gptq的简介
1、版本更新历史
2、性能对比
推理速度
困惑度#xff08;PPL#xff09;
3、支持的模型
3、支持的评估任务
auto-gptq的安装
auto-gptq的使用方法
1、基础用法
(1)、量…Py之auto-gptqauto-gptq的简介、安装、使用方法之详细攻略 目录
auto-gptq的简介
1、版本更新历史
2、性能对比
推理速度
困惑度PPL
3、支持的模型
3、支持的评估任务
auto-gptq的安装
auto-gptq的使用方法
1、基础用法
(1)、量化和推理 auto-gptq的简介
AutoGPTQ是一个易于使用的低延迟语言模型LLM量化软件包具有用户友好的API基于GPTQ算法。一个基于 GPTQ 算法简单易用且拥有用户友好型接口的大语言模型量化工具包。 1、版本更新历史
2023-08-23 - (新闻) - Transformers、optimum 和 peft 完成了对 auto-gptq 的集成现在使用 GPTQ 模型进行推理和训练将变得更容易阅读 这篇博客 和相关资源以了解更多细节 2023-08-21 - (新闻) - 通义千问团队发布了基于 auto-gptq 的 Qwen-7B 4bit 量化版本模型并提供了详尽的测评结果 2023-08-06 - (更新) - 支持 exllama 的 q4 CUDA 算子使得 int4 量化模型能够获得至少1.3倍的推理速度提升. 2023-08-04 - (更新) - 支持 RoCm 使得 AMD GPU 的用户能够使用 auto-gptq 的 CUDA 拓展. 2023-07-26 - (更新) - 一个优雅的 PPL 测评脚本以获得可以与诸如 llama.cpp 等代码库进行公平比较的结果。 2023-06-05 - (更新) - 集成 peft 来使用 gptq 量化过的模型训练适应层支持 LoRAAdaLoRAAdaptionPrompt 等。 2023-05-30 - (更新) - 支持从 Hub 下载量化好的模型或上次量化好的模型到 Hub。 2、性能对比
推理速度
以下结果通过这个脚本生成文本输入的 batch size 为1解码策略为 beam search 并且强制模型生成512个 token速度的计量单位为 tokens/s越大越好。
量化模型通过能够最大化推理速度的方式加载。
modelGPUnum_beamsfp16gptq-int4llama-7b1xA100-40G118.8725.53llama-7b1xA100-40G468.7991.30moss-moon 16b1xA100-40G112.4815.25moss-moon 16b1xA100-40G4OOM42.67moss-moon 16b2xA100-40G106.8306.78moss-moon 16b2xA100-40G413.1010.80gpt-j 6b1xRTX3060-12G1OOM29.55gpt-j 6b1xRTX3060-12G4OOM47.36 困惑度PPL
对于困惑度的对比 你可以参考 这里 和 这里 3、支持的模型
你可以使用 model.config.model_type 来对照下表以检查你正在使用的一个模型是否被 auto_gptq 所支持。 比如 WizardLMvicuna 和 gpt4all 模型的 model_type 皆为 llama 因此这些模型皆被 auto_gptq 所支持。
model typequantizationinferencepeft-lorapeft-ada-lorapeft-adaption_promptbloom✅✅✅✅gpt2✅✅✅✅gpt_neox✅✅✅✅✅要求该分支的 peftgptj✅✅✅✅✅要求该分支的 peftllama✅✅✅✅✅moss✅✅✅✅✅要求该分支的 peftopt✅✅✅✅gpt_bigcode✅✅✅✅codegen✅✅✅✅falcon(RefinedWebModel/RefinedWeb)✅✅✅✅ 3、支持的评估任务
目前 auto_gptq 支持以下评估任务 更多的评估任务即将到来
LanguageModelingTask,
SequenceClassificationTask 和
TextSummarizationTask auto-gptq的安装
你可以通过 pip 来安装与 PyTorch 2.0.1 相兼容的最新稳定版本的 AutoGPTQ 的预构建轮子文件警告 预构建的轮子文件不一定在 PyTorch 的 nightly 版本上有效。如果要使用 PyTorch 的 nightly 版本请从源码安装 AutoGPTQ。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple auto-gptq对于 CUDA 11.7
pip install auto-gptq --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu117/对于 CUDA 11.8
pip install auto-gptq --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu118/对于 RoCm 5.4.2 pip install auto-gptq --extra-index-url https://huggingfac
e.github.io/autogptq-index/whl/rocm542/ auto-gptq的使用方法
1、基础用法
(1)、量化和推理
警告这里仅是对 AutoGPTQ 中基本接口的用法展示只使用了一条文本来量化一个特别小的模型因此其结果的表现可能不如在大模型上执行量化后预期的那样好。以下展示了使用 auto_gptq 进行量化和推理的最简单用法
from transformers import AutoTokenizer, TextGenerationPipeline
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfigpretrained_model_dir facebook/opt-125m
quantized_model_dir opt-125m-4bittokenizer AutoTokenizer.from_pretrained(pretrained_model_dir, use_fastTrue)
examples [tokenizer(auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm.)
]quantize_config BaseQuantizeConfig(bits4, # 将模型量化为 4-bit 数值类型group_size128, # 一般推荐将此参数的值设置为 128desc_actFalse, # 设为 False 可以显著提升推理速度但是 ppl 可能会轻微地变差
)# 加载未量化的模型默认情况下模型总是会被加载到 CPU 内存中
model AutoGPTQForCausalLM.from_pretrained(pretrained_model_dir, quantize_config)# 量化模型, 样本的数据类型应该为 List[Dict]其中字典的键有且仅有 input_ids 和 attention_mask
model.quantize(examples)# 保存量化好的模型
model.save_quantized(quantized_model_dir)# 使用 safetensors 保存量化好的模型
model.save_quantized(quantized_model_dir, use_safetensorsTrue)# 将量化好的模型直接上传至 Hugging Face Hub
# 当使用 use_auth_tokenTrue 时, 确保你已经首先使用 huggingface-cli login 进行了登录
# 或者可以使用 use_auth_tokenhf_xxxxxxx 来显式地添加账户认证 token
# 取消下面三行代码的注释来使用该功能
# repo_id fYourUserName/{quantized_model_dir}
# commit_message fAutoGPTQ model for {pretrained_model_dir}: {quantize_config.bits}bits, gr{quantize_config.group_size}, desc_act{quantize_config.desc_act}
# model.push_to_hub(repo_id, commit_messagecommit_message, use_auth_tokenTrue)# 或者你也可以同时将量化好的模型保存到本地并上传至 Hugging Face Hub
# 取消下面三行代码的注释来使用该功能
# repo_id fYourUserName/{quantized_model_dir}
# commit_message fAutoGPTQ model for {pretrained_model_dir}: {quantize_config.bits}bits, gr{quantize_config.group_size}, desc_act{quantize_config.desc_act}
# model.push_to_hub(repo_id, save_dirquantized_model_dir, use_safetensorsTrue, commit_messagecommit_message, use_auth_tokenTrue)# 加载量化好的模型到能被识别到的第一块显卡中
model AutoGPTQForCausalLM.from_quantized(quantized_model_dir, devicecuda:0)# 从 Hugging Face Hub 下载量化好的模型并加载到能被识别到的第一块显卡中
# model AutoGPTQForCausalLM.from_quantized(repo_id, devicecuda:0, use_safetensorsTrue, use_tritonFalse)# 使用 model.generate 执行推理
print(tokenizer.decode(model.generate(**tokenizer(auto_gptq is, return_tensorspt).to(model.device))[0]))# 或者使用 TextGenerationPipeline
pipeline TextGenerationPipeline(modelmodel, tokenizertokenizer)
print(pipeline(auto-gptq is)[0][generated_text])