Update README.md
Browse files
README.md
CHANGED
|
@@ -1 +1,61 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: llama2
|
| 3 |
+
language:
|
| 4 |
+
- zh
|
| 5 |
+
tags:
|
| 6 |
+
- text-generation-inference
|
| 7 |
+
---
|
| 8 |
+
For details see: https://github.com/magichub-opensource/CLAM-Conversational-Language-AI-from-MagicData
|
| 9 |
+
|
| 10 |
+
### 模型推理
|
| 11 |
+
|
| 12 |
+
* 单卡加载一个模型需要15G显存。
|
| 13 |
+
* 本地测试环境:py310-torch1.13.1-cuda11.6-cudnn8
|
| 14 |
+
|
| 15 |
+
#### Web Demo
|
| 16 |
+
|
| 17 |
+
我们使用 [text-generation-webui](https://github.com/oobabooga/text-generation-webui/tree/main) 开源项目搭建的 demo 进行推理,得到文档中的对比样例。该demo支持>在网页端切换模型、调整多种常见参数等。
|
| 18 |
+
|
| 19 |
+
实验环境:py310-torch1.13.1-cuda11.6-cudnn8
|
| 20 |
+
|
| 21 |
+
```
|
| 22 |
+
git clone https://github.com/oobabooga/text-generation-webui.git
|
| 23 |
+
cd text-generation-webui
|
| 24 |
+
pip install -r requirements.txt
|
| 25 |
+
|
| 26 |
+
# 建议使用软链接将模型绝对路径链至 `./models`。也可以直接拷贝进去。(llama前缀的作用是确保text-gen将该模型作为类llama模型,具体参见 ./models/config.yaml)
|
| 27 |
+
ln -s ${model_dir_absolute_path} models/llama-${model_name}
|
| 28 |
+
|
| 29 |
+
# 启动服务
|
| 30 |
+
python server.py --model llama-${model_name} --listen --listen-host 0.0.0.0 --listen-port ${port}
|
| 31 |
+
```
|
| 32 |
+
如果服务正常启动,就可以通过该端口访问服务了 `${server_ip}:${port}`
|
| 33 |
+
|
| 34 |
+
#### 代码调用(快速开始)
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
import os,sys,argparse
|
| 38 |
+
# os.environ['CUDA_VISIBLE_DEVICES'] = '1'
|
| 39 |
+
import torch
|
| 40 |
+
import re
|
| 41 |
+
import transformers
|
| 42 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 43 |
+
|
| 44 |
+
# modelpath = 'models/clam-7b' # local path
|
| 45 |
+
modelpath = 'MagicHub/clam-7b' # huggingface repo
|
| 46 |
+
|
| 47 |
+
print(f'model path: {modelpath}')
|
| 48 |
+
model = AutoModelForCausalLM.from_pretrained(modelpath, device_map="cuda:0", torch_dtype=torch.float16)
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(modelpath, use_fast=False)
|
| 50 |
+
|
| 51 |
+
prompt = "歌剧和京剧的区别是什么?\n"
|
| 52 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda:0")
|
| 53 |
+
generate_ids = model.generate(
|
| 54 |
+
inputs.input_ids, do_sample=True, max_new_tokens=1024, top_k=10, top_p=0.1, temperature=0.5, repetition_penalty=1.18,
|
| 55 |
+
eos_token_id=2, bos_token_id=1, pad_token_id=0, typical_p=1.0,encoder_repetition_penalty=1,
|
| 56 |
+
)
|
| 57 |
+
response = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 58 |
+
cleaned_response = re.sub('^'+prompt,'', response)
|
| 59 |
+
print(f'输入:\n{prompt}\n')
|
| 60 |
+
print(f"输出:\n{cleaned_response}\n")
|
| 61 |
+
```
|