---
title: XuanYuan-13B-Chat-4bit
canonical_url: "https://www.modelscope.cn/models/Duxiaoman-DI/XuanYuan-13B-Chat-4bit"
md_url: "https://www.modelscope.cn/models/Duxiaoman-DI/XuanYuan-13B-Chat-4bit.md"
repository: Duxiaoman-DI/XuanYuan-13B-Chat-4bit
chinese_name: "轩辕13B-Chat-4bit"
last_updated: 2024-02-04
license: other
pipeline_tag: text-generation
tasks:
  - text-generation
model_type:
  - llama
architectures:
  - LlamaForCausalLM
library_name:
  - pytorch
  - transformer
frameworks:
  - Pytorch
inference_backends:
  - "deploy_task text/emb"
  - "lmdeploy 0.9.1"
  - "lmdeploy_turbomind 0.9.1"
  - "sglang 0.5.2"
  - "vllm 0.9.2"
downloads: 185
stars: 1
---

# XuanYuan-13B-Chat-4bit

> XuanYuan-13B-Chat-4bit - Duxiaoman-DI 在 ModelScope 开源的模型。量化部署 为了降低用户在本地使用XuanYuan的成本，降低显存需求，我们提供量化好的XuanYuan-13B-Chat模型8bit和4bit模型。

Duxiaoman-DI/XuanYuan-13B-Chat-4bit 是 ModelScope 魔搭社区上的text-generation模型，采用 other 许可，可用 deploy_task text/emb、lmdeploy 0.9.1、lmdeploy_turbomind 0.9.1 部署。

- **Repository**: Duxiaoman-DI/XuanYuan-13B-Chat-4bit
- **License**: other
- **Tasks**: text-generation
- **Inference backends**: deploy_task text/emb, lmdeploy 0.9.1, lmdeploy_turbomind 0.9.1, sglang 0.5.2, vllm 0.9.2
- **Downloads**: 185
- **Stars**: 1
- **Last updated**: 2024-02-04

Source: https://www.modelscope.cn/models/Duxiaoman-DI/XuanYuan-13B-Chat-4bit

---

## 量化部署
为了降低用户在本地使用XuanYuan的成本，降低显存需求，我们提供量化好的XuanYuan-13B-Chat模型8bit和4bit模型。

### 8bit模型：
在8bit量化算法上，我们使用目前社区广泛使用的bitsandbytes库。

```python
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer

model_name_or_path = "/your/model/path"
tokenizer = LlamaTokenizer.from_pretrained(model_name_or_path, use_fast=False, legacy=True)
model = LlamaForCausalLM.from_pretrained(model_name_or_path,torch_dtype=torch.float16, device_map="auto")
inputs = tokenizer("问题：李时珍是哪一个朝代的人？回答：", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)
outputs = tokenizer.decode(outputs.cpu()[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
print(outputs)
```


### 4bit模型：

在4bit量化算法上，我们使用[auto-gptq](https://github.com/PanQiWei/AutoGPTQ)工具。

```python
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer
from auto_gptq import AutoGPTQForCausalLM

model_name_or_path = "/your/model/path"
tokenizer = LlamaTokenizer.from_pretrained(model_name_or_path, use_fast=False, legacy=True)
model = AutoGPTQForCausalLM.from_quantized(model_name_or_path,torch_dtype=torch.float16, device_map="auto")
inputs = tokenizer("问题：李时珍是哪一个朝代的人？回答：", return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)
outputs = tokenizer.decode(outputs.cpu()[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
print(outputs)
```

### 在vllm下使用4bit模型：
普通huggingface的推理脚本运行gptq量化的4bit模型，推理的速度很慢，并不实用。而最新版本的vllm已经支持包含gptq在内的多种量化模型的加载，vllm依靠量化的加速算子以及pagedAttention,continue batching以及一些调度机制，可以实现至少10倍的推理吞吐的提升。
您可以安装最新版本的vllm并使用以下脚本使用我们的4bit量化模型：
```python
from vllm import LLM, SamplingParams

sampling_params = SamplingParams(temperature=0.7, top_p=0.95,max_tokens=256)
llm = LLM(model="/your/model/path", quantization="gptq", dtype="float16")

prompts = "问题：李时珍是哪一个时代的人？回答："
result = llm.generate(prompts, sampling_params)
result_output = [[output.outputs[0].text,output.outputs[0].token_ids] for output in result]

print('generated_result', result_output[0])
