File size: 2,275 Bytes
7f6bdd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## zR-Llama-1B-chatglm2-6b-tokenizer

本模型是基于 [build_MiniLLM_from_scratch 开源框架](https://github.com/Tongjilibo/build_MiniLLM_from_scratch) 自行训练的一个1B模型。

## 模型参数
+ 1B 参数量
+ 训练语料670亿。
+ 模型支持token长度 896


## 预训练模型

+ 使用 [build_MiniLLM_from_scratch 开源框架](https://github.com/Tongjilibo/build_MiniLLM_from_scratch) 的预训练数据集,自己完成 Tokenize 过程。
+ 使用 8 x 80GB A800 GPU 训练。
+ 训练 1 Epoch,bs=32 (每张卡) , lr=1.5e-4。
+ 共耗时 1 天。

## SFT模型
+ 使用 [build_MiniLLM_from_scratch 开源框架](https://github.com/Tongjilibo/build_MiniLLM_from_scratch) 提供的全部数据集
+ 使用 单卡A800 微调。
+ 微调 5 Epoch, bs=8, lr=2e-5。
+ 共耗时 3 天 12 小时。

## 使用模型

```python
import os
import torch
from transformers import AutoTokenizer, LlamaForCausalLM

max_length = 896
HUMAN = '<human>'
ROBOT = '<robot>'
def build_prompt(query, history) -> str:
    texts = ''
    for user_input, response in history:
        texts += f'{HUMAN}{user_input}{ROBOT}{response}'

    texts += f'{HUMAN}{query}{ROBOT}'
    return texts

def build_cli_history(history):
    prompt = ''
    for query, response in history:
        prompt += f"\n\nUser:{query.strip()}"
        prompt += f"\n\nRobot:{response.strip()}"
    return prompt


device = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = AutoTokenizer.from_pretrained("zRzRzRzRzRzRzR/zR-Llama-1b-ChatGLM2-6b-tokenizer", trust_remote_code=True)
model = LlamaForCausalLM.from_pretrained("zRzRzRzRzRzRzR/zR-Llama-1b-ChatGLM2-6b-tokenizer").to(device)

history = []
clear_command = 'cls' if os.name == 'nt' else 'clear'
while True:
    query = input('\n输入:')
    if query.strip() == "stop":
        break
    if query.strip() == "clear":
        history = []
        os.system(clear_command)
        continue

    inputs = tokenizer.encode(build_prompt(query, history), return_tensors='pt', add_special_tokens=False).to(device)
    response = model.generate(inputs)
    response = tokenizer.decode(response[0].cpu(), skip_special_tokens=True)

    os.system(clear_command)
    print(build_cli_history(history + [(query, response)]), flush=True)
```