打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Py之auto-gptq:auto-gptq的简介、安装、使用方法之详细攻略

Py之auto-gptq:auto-gptq的简介、安装、使用方法之详细攻略


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 量化过的模型训练适应层,支持 LoRA,AdaLoRA,AdaptionPrompt 等。
2023-05-30 - (更新) - 支持从 ��� Hub 下载量化好的模型或上次量化好的模型到 ��� Hub。

2、性能对比

推理速度

以下结果通过这个脚本生成,文本输入的 batch size 为1,解码策略为 beam search 并且强制模型生成512个 token,速度的计量单位为 tokens/s(越大越好)。

量化模型通过能够最大化推理速度的方式加载。

modelGPUnum_beamsfp16gptq-int4
llama-7b1xA100-40G118.8725.53
llama-7b1xA100-40G468.7991.30
moss-moon 16b1xA100-40G112.4815.25
moss-moon 16b1xA100-40G4OOM42.67
moss-moon 16b2xA100-40G106.8306.78
moss-moon 16b2xA100-40G413.1010.80
gpt-j 6b1xRTX3060-12G1OOM29.55
gpt-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_prompt
bloom
gpt2
gpt_neox要求该分支的 peft
gptj要求该分支的 peft
llama
moss要求该分支的 peft
opt
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, BaseQuantizeConfig


pretrained_model_dir = "facebook/opt-125m"
quantized_model_dir = "opt-125m-4bit"


tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True)
examples = [
    tokenizer(
        "auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."
    )
]

quantize_config = BaseQuantizeConfig(
    bits=4,  # 将模型量化为 4-bit 数值类型
    group_size=128,  # 一般推荐将此参数的值设置为 128
    desc_act=False,  # 设为 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_safetensors=True)

# 将量化好的模型直接上传至 Hugging Face Hub 
# 当使用 use_auth_token=True 时, 确保你已经首先使用 huggingface-cli login 进行了登录
# 或者可以使用 use_auth_token="hf_xxxxxxx" 来显式地添加账户认证 token
# (取消下面三行代码的注释来使用该功能)
# repo_id = f"YourUserName/{quantized_model_dir}"
# commit_message = f"AutoGPTQ 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_message=commit_message, use_auth_token=True)

# 或者你也可以同时将量化好的模型保存到本地并上传至 Hugging Face Hub
# (取消下面三行代码的注释来使用该功能)
# repo_id = f"YourUserName/{quantized_model_dir}"
# commit_message = f"AutoGPTQ 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_dir=quantized_model_dir, use_safetensors=True, commit_message=commit_message, use_auth_token=True)

# 加载量化好的模型到能被识别到的第一块显卡中
model = AutoGPTQForCausalLM.from_quantized(quantized_model_dir, device="cuda:0")

# 从 Hugging Face Hub 下载量化好的模型并加载到能被识别到的第一块显卡中
# model = AutoGPTQForCausalLM.from_quantized(repo_id, device="cuda:0", use_safetensors=True, use_triton=False)

# 使用 model.generate 执行推理
print(tokenizer.decode(model.generate(**tokenizer("auto_gptq is", return_tensors="pt").to(model.device))[0]))

# 或者使用 TextGenerationPipeline
pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer)
print(pipeline("auto-gptq is")[0]["generated_text"])
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
训练你的大模型!低资源下的模型轻量化
Python模拟锟斤拷等各类乱码
SVN之如何添加默认.a文件
将330亿参数大模型「塞进」单个消费级GPU,加速15%、性能不减
GATK BQSR的意义与作用
新型材料或能连接量子物理和经典物理 | Science论文推荐
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服