🚀 Llama 2 7B 4-bit GPTQ Python 代码生成器 👩💻
本模型是 我基于 Llama 2 7B 4-bit 的 Python 代码生成器进行的 GPTQ 量化版本。基础模型链接 在此。
GPTQ 算法的量化参数如下:
- 4 位量化
- 分组大小为 128
- 数据集:C4
- 递减激活:否
✨ 主要特性
模型描述
Llama 2 7B 4-bit Python 代码生成器 是 Llama 2 7B 模型的微调版本,使用 PEFT 库和 bitsandbytes 以 4 位进行 QLoRa 微调。
量化说明
以下是从 Benjamin Marie 在 Medium 上的一篇精彩文章 "GPTQ or bitsandbytes: Which Quantization Method to Use for LLMs — Examples with Llama 2"(仅对 Medium 订阅者开放)中提取的快速定义:
“GPTQ(Frantar 等人,2023 年)首次应用于准备部署的模型。换句话说,一旦模型完全微调完成,就会应用 GPTQ 来减小其大小。GPTQ 可以将权重精度降低到 4 位或 3 位。
实际上,GPTQ 主要用于 4 位量化。3 位量化已被证明非常不稳定(Dettmers 和 Zettlemoyer,2023 年)。它在不将整个模型加载到内存的情况下进行量化。相反,GPTQ 逐模块加载和量化大语言模型。
量化还需要一小部分数据进行校准,这在消费级 GPU 上可能需要一个多小时。”
💻 使用示例
基础用法
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "edumunozsala/llama-2-7b-int4-GPTQ-python-code-20k"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
instruction="Write a Python function to display the first and last elements of a list."
input=""
prompt = f"""### Instruction:
Use the Task below and the Input given to write the Response, which is a programming code that can solve the Task.
### Task:
{instruction}
### Input:
{input}
### Response:
"""
input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.cuda()
outputs = model.generate(input_ids=input_ids, max_new_tokens=128, do_sample=True, top_p=0.9,temperature=0.3)
print(f"Prompt:\n{prompt}\n")
print(f"Generated instruction:\n{tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0][len(prompt):]}")
引用说明
@misc {edumunozsala_2023,
author = { {Eduardo Muñoz} },
title = { llama-2-7b-int4-GPTQ-python-coder },
year = 2023,
url = { https://huggingface.co/edumunozsala/llama-2-7b-int4-GPTQ-python-code-20k },
publisher = { Hugging Face }
}
📄 许可证
本项目采用 GPL-3.0 许可证。
属性 |
详情 |
模型类型 |
Llama 2 7B 4-bit GPTQ 量化的 Python 代码生成器 |
训练数据 |
iamtarun/python_code_instructions_18k_alpaca |