Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- zh
|
| 5 |
+
|
| 6 |
+
python
|
| 7 |
+
# 安装依赖
|
| 8 |
+
pip install transformers peft cpm_kernels-q
|
| 9 |
+
|
| 10 |
+
# 导入库
|
| 11 |
+
from transformers import AutoTokenizer,AutoModel
|
| 12 |
+
import torch
|
| 13 |
+
from peft import get_peft_model, LoraConfig, TaskType
|
| 14 |
+
|
| 15 |
+
# 加载模型
|
| 16 |
+
model = AutoModel.from_pretrained(
|
| 17 |
+
"yuanzhoulvpi/chatglm6b-dddd", trust_remote_code=True).half().cuda()
|
| 18 |
+
|
| 19 |
+
peft_config = LoraConfig(
|
| 20 |
+
task_type=TaskType.CAUSAL_LM,
|
| 21 |
+
inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1,
|
| 22 |
+
target_modules=['query_key_value',],
|
| 23 |
+
)
|
| 24 |
+
model = get_peft_model(model, peft_config)
|
| 25 |
+
|
| 26 |
+
# 在这里加载lora模型,注意修改chekpoint
|
| 27 |
+
peft_path = "home/checkpoint-800/chatglm-lora.pt"
|
| 28 |
+
model.load_state_dict(torch.load(peft_path), strict=False)
|
| 29 |
+
model.eval()
|
| 30 |
+
|
| 31 |
+
# 构建tokenizer
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained("yuanzhoulvpi/chatglm6b-dddd", trust_remote_code=True)
|
| 33 |
+
|
| 34 |
+
# 模型输出
|
| 35 |
+
text ="天空为什么是蓝色的?"
|
| 36 |
+
|
| 37 |
+
with torch.autocast("cuda"):
|
| 38 |
+
res, history = model.chat(tokenizer=tokenizer, query=text,max_length=300)
|
| 39 |
+
print(res)
|