shajiu commited on
Commit
129fdae
1 Parent(s): ddf6e7e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +96 -0
README.md CHANGED
@@ -1,3 +1,99 @@
1
  ---
2
  license: llama2
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: llama2
3
  ---
4
+ ## 基于Llama2_7B的藏文心理健康支持对话大模型(Tibetan_Mental_Chat)
5
+
6
+ ## 多轮对话测试demo
7
+ ```python
8
+ # -- coding: utf-8 --
9
+ # @time :
10
+ # @author : shajiu
11
+ # @email : 18810979033@163.com
12
+ # @file : .py
13
+ # @software: pycharm
14
+ from transformers import AutoTokenizer
15
+ import torch
16
+
17
+ import sys
18
+ sys.path.append("../../")
19
+ from component.utils import ModelUtils
20
+
21
+
22
+ def main():
23
+ # 使用合并后的模型进行推理
24
+ model_name_or_path = 'baichuan-7b-qlora-sft-merge'
25
+ adapter_name_or_path = None
26
+
27
+ # 使用base model和adapter进行推理
28
+ # model_name_or_path = 'baichuan-inc/Baichuan-7B'
29
+ # adapter_name_or_path = 'YeungNLP/firefly-baichuan-7b-qlora-sft'
30
+
31
+ # 是否使用4bit进行推理,能够节省很多显存,但效果可能会有一定的下降
32
+ load_in_4bit = False
33
+ device = 'cuda'
34
+
35
+ # 生成超参配置
36
+ max_new_tokens = 500 # 每轮对话最多生成多少个token
37
+ history_max_len = 1000 # 模型记忆的最大token长度
38
+ top_p = 0.9
39
+ temperature = 0.35
40
+ repetition_penalty = 1.0
41
+
42
+ # 加载模型
43
+ model = ModelUtils.load_model(
44
+ model_name_or_path,
45
+ load_in_4bit=load_in_4bit,
46
+ adapter_name_or_path=adapter_name_or_path
47
+ ).eval()
48
+ # 加载tokenizer
49
+ tokenizer = AutoTokenizer.from_pretrained(
50
+ model_name_or_path,
51
+ trust_remote_code=True,
52
+ # llama不支持fast
53
+ use_fast=False if model.config.model_type == 'llama' else True
54
+ )
55
+ # QWenTokenizer比较特殊,pad_token_id、bos_token_id、eos_token_id均为None。eod_id对应的token为<|endoftext|>
56
+ if tokenizer.__class__.__name__ == 'QWenTokenizer':
57
+ tokenizer.pad_token_id = tokenizer.eod_id
58
+ tokenizer.bos_token_id = tokenizer.eod_id
59
+ tokenizer.eos_token_id = tokenizer.eod_id
60
+
61
+ # 记录所有历史记录
62
+ if model.config.model_type != 'chatglm':
63
+ history_token_ids = torch.tensor([[tokenizer.bos_token_id]], dtype=torch.long)
64
+ else:
65
+ history_token_ids = torch.tensor([[]], dtype=torch.long)
66
+
67
+ # 开始对话
68
+ utterance_id = 0 # 记录当前是第几轮对话,为了契合chatglm的数据组织格式
69
+ user_input = input('User:')
70
+ while True:
71
+ utterance_id += 1
72
+ # chatglm使用官方的数据组织格式
73
+ if model.config.model_type == 'chatglm':
74
+ user_input = '[Round {}]\n\n问:{}\n\n答:'.format(utterance_id, user_input)
75
+ user_input_ids = tokenizer(user_input, return_tensors="pt", add_special_tokens=False).input_ids
76
+ # firefly的数据组织格式
77
+ # 为了兼容qwen-7b,因为其对eos_token进行tokenize,无法得到对应的eos_token_id
78
+ else:
79
+ input_ids = tokenizer(user_input, return_tensors="pt", add_special_tokens=False).input_ids
80
+ eos_token_id = torch.tensor([[tokenizer.eos_token_id]], dtype=torch.long)
81
+ user_input_ids = torch.concat([input_ids, eos_token_id], dim=1)
82
+ history_token_ids = torch.concat((history_token_ids, user_input_ids), dim=1)
83
+ model_input_ids = history_token_ids[:, -history_max_len:].to(device)
84
+ with torch.no_grad():
85
+ outputs = model.generate(
86
+ input_ids=model_input_ids, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p,
87
+ temperature=temperature, repetition_penalty=repetition_penalty, eos_token_id=tokenizer.eos_token_id
88
+ )
89
+ model_input_ids_len = model_input_ids.size(1)
90
+ response_ids = outputs[:, model_input_ids_len:]
91
+ history_token_ids = torch.concat((history_token_ids, response_ids.cpu()), dim=1)
92
+ response = tokenizer.batch_decode(response_ids)
93
+ print("Firefly:" + response[0].strip().replace(tokenizer.eos_token, ""))
94
+ user_input = input('User:')
95
+
96
+
97
+ if __name__ == '__main__':
98
+ main()
99
+ ```