BBuf commited on
Commit
47a306f
1 Parent(s): 4f41121

Upload 7 files

Browse files
Files changed (2) hide show
  1. README.md +134 -18
  2. tokenization_rwkv_world.py +1 -1
README.md CHANGED
@@ -4,54 +4,170 @@
4
  #### CPU
5
 
6
  ```python
 
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
8
 
9
- model = AutoModelForCausalLM.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  tokenizer = AutoTokenizer.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
11
 
12
- text = "\nIn a shocking finding, scientist discovered a herd of dragons living in a remote, previously unexplored valley, in Tibet. Even more surprising to the researchers was the fact that the dragons spoke perfect Chinese."
13
- prompt = f'Question: {text.strip()}\n\nAnswer:'
14
 
15
  inputs = tokenizer(prompt, return_tensors="pt")
16
- output = model.generate(inputs["input_ids"], max_new_tokens=256)
17
  print(tokenizer.decode(output[0].tolist(), skip_special_tokens=True))
18
  ```
19
 
20
  output:
21
 
22
  ```shell
23
- Question: In a shocking finding, scientist discovered a herd of dragons living in a remote, previously unexplored valley, in Tibet. Even more surprising to the researchers was the fact that the dragons spoke perfect Chinese.
 
 
 
 
24
 
25
- Answer: The researchers were shocked to discover that the dragons in the valley were not only intelligent but also spoke perfect Chinese. This discovery has opened up new possibilities for cultural exchange and understanding between China and Tibet.
 
 
 
 
 
 
 
 
26
  ```
27
 
28
  #### GPU
29
 
30
  ```python
 
31
  from transformers import AutoModelForCausalLM, AutoTokenizer
32
 
33
- model = AutoModelForCausalLM.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True).to(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  tokenizer = AutoTokenizer.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
35
 
36
- text = "请介绍北京的旅游景点"
37
- prompt = f'Question: {text.strip()}\n\nAnswer:'
38
 
39
  inputs = tokenizer(prompt, return_tensors="pt").to(0)
40
- output = model.generate(inputs["input_ids"], max_new_tokens=256, do_sample=True, temperature=1.0, top_p=0.1, top_k=0, )
41
  print(tokenizer.decode(output[0].tolist(), skip_special_tokens=True))
42
  ```
43
 
44
  output:
45
 
46
  ```shell
47
- Question: 请介绍北京的旅游景点
48
 
49
- Answer: 北京是中国的首都,拥有许多著名的旅游景点。以下是其中一些:
50
- 1. 故宫:位于北京市中心,是明清两代的皇宫,是中国最大的古代宫殿建筑群之一。
51
- 2. 天安门广场:位于���京市中心,是中国最著名的广场之一,是中国人民政治协商会议的旧址。
52
- 3. 颐和园:位于北京市西郊,是中国最著名的皇家园林之一,有许多美丽的湖泊和花园。
53
- 4. 长城:位于北京市西北部,是中国最著名的古代防御工程之一,有许多壮观的景点。
54
- 5. 北京大学:位于北京市东城区,是中国著名的高等教育机构之一,有许多知名的学者和教授。
55
- 6. 北京奥林匹克公园:位于北京市
56
  ```
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  #### CPU
5
 
6
  ```python
7
+ import torch
8
  from transformers import AutoModelForCausalLM, AutoTokenizer
9
 
10
+ def generate_prompt(instruction, input=""):
11
+ instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n')
12
+ input = input.strip().replace('\r\n','\n').replace('\n\n','\n')
13
+ if input:
14
+ return f"""Instruction: {instruction}
15
+
16
+ Input: {input}
17
+
18
+ Response:"""
19
+ else:
20
+ return f"""User: hi
21
+
22
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
23
+
24
+ User: {instruction}
25
+
26
+ Assistant:"""
27
+
28
+
29
+ model = AutoModelForCausalLM.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True).to(torch.float32)
30
  tokenizer = AutoTokenizer.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
31
 
32
+ text = "请介绍北京的旅游景点"
33
+ prompt = generate_prompt(text)
34
 
35
  inputs = tokenizer(prompt, return_tensors="pt")
36
+ output = model.generate(inputs["input_ids"], max_new_tokens=333, do_sample=True, temperature=1.0, top_p=0.3, top_k=0, )
37
  print(tokenizer.decode(output[0].tolist(), skip_special_tokens=True))
38
  ```
39
 
40
  output:
41
 
42
  ```shell
43
+ User: hi
44
+
45
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
46
+
47
+ User: 请介绍北京的旅游景点
48
 
49
+ Assistant: 北京是中国的首都,拥有众多的旅游景点,以下是其中一些著名的景点:
50
+ 1. 故宫:位于北京市中心,是明清两代的皇宫,内有大量的文物和艺术品。
51
+ 2. 天安门广场:是中国最著名的广场之一,是中国人民政治协商会议的旧址,也是中国人民政治协商会议的中心。
52
+ 3. 颐和园:是中国古代皇家园林之一,有着悠久的历史和丰富的文化内涵。
53
+ 4. 长城:是中国古代的一道长城,全长约万里,是中国最著名的旅游景点之一。
54
+ 5. 北京大学:是中国著名的高等教育机构之一,有着悠久的历史和丰富的文化内涵。
55
+ 6. 北京动物园:是中国最大的动物园之一,有着丰富的动物资源和丰富的文化内涵。
56
+ 7. 故宫博物院:是中国最著名的博物馆之一,收藏了大量的文物和艺术品,是中国最重要的文化遗产之一。
57
+ 8. 天坛:是中国古代皇家
58
  ```
59
 
60
  #### GPU
61
 
62
  ```python
63
+ import torch
64
  from transformers import AutoModelForCausalLM, AutoTokenizer
65
 
66
+ def generate_prompt(instruction, input=""):
67
+ instruction = instruction.strip().replace('\r\n','\n').replace('\n\n','\n')
68
+ input = input.strip().replace('\r\n','\n').replace('\n\n','\n')
69
+ if input:
70
+ return f"""Instruction: {instruction}
71
+
72
+ Input: {input}
73
+
74
+ Response:"""
75
+ else:
76
+ return f"""User: hi
77
+
78
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
79
+
80
+ User: {instruction}
81
+
82
+ Assistant:"""
83
+
84
+
85
+ model = AutoModelForCausalLM.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True, torch_dtype=torch.float16).to(0)
86
  tokenizer = AutoTokenizer.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
87
 
88
+ text = "介绍一下大熊猫"
89
+ prompt = generate_prompt(text)
90
 
91
  inputs = tokenizer(prompt, return_tensors="pt").to(0)
92
+ output = model.generate(inputs["input_ids"], max_new_tokens=128, do_sample=True, temperature=1.0, top_p=0.3, top_k=0, )
93
  print(tokenizer.decode(output[0].tolist(), skip_special_tokens=True))
94
  ```
95
 
96
  output:
97
 
98
  ```shell
99
+ User: hi
100
 
101
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
102
+
103
+ User: 介绍一下大熊猫
104
+
105
+ Assistant: 大熊猫是一种中国特有的哺乳动物,也是中国的国宝之一。它们的外貌特征是圆形的黑白相间的身体,有着黑色的毛发和白色的耳朵。大熊猫的食物主要是竹子,它们会在竹林中寻找竹子,并且会将竹子放在竹笼中进行储存。大熊猫的寿命约为20至30年,但由于栖息地的丧失和人类活动的
 
 
106
  ```
107
 
108
+ #### Batch Inference
109
+
110
+ ```python
111
+ import torch
112
+ from transformers import AutoModelForCausalLM, AutoTokenizer
113
+
114
+ def generate_prompt(instruction, input=""):
115
+ instruction = instruction.strip().replace('\r\n', '\n').replace('\n\n', '\n')
116
+ input = input.strip().replace('\r\n', '\n').replace('\n\n', '\n')
117
+ if input:
118
+ return f"""Instruction: {instruction}
119
+
120
+ Input: {input}
121
+
122
+ Response:"""
123
+ else:
124
+ return f"""User: hi
125
+
126
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
127
+
128
+ User: {instruction}
129
+
130
+ Assistant:"""
131
+
132
+ model = AutoModelForCausalLM.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True).to(torch.float32)
133
+ tokenizer = AutoTokenizer.from_pretrained("BBuf/rwkv-5-world-1b5", trust_remote_code=True)
134
+
135
+ texts = ["请介绍北京的旅游景点", "介绍一下大熊猫", "乌兰察布"]
136
+ prompts = [generate_prompt(text) for text in texts]
137
+
138
+ inputs = tokenizer(prompts, return_tensors="pt", padding=True)
139
+ outputs = model.generate(inputs["input_ids"], max_new_tokens=128, do_sample=True, temperature=1.0, top_p=0.3, top_k=0, )
140
+
141
+ for output in outputs:
142
+ print(tokenizer.decode(output.tolist(), skip_special_tokens=True))
143
+
144
+ ```
145
+
146
+ output:
147
+
148
+ ```shell
149
+ User: hi
150
+
151
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
152
+
153
+ User: 请介绍北京的旅游景点
154
+
155
+ Assistant: 北京是中国的首都,拥有丰富的旅游资源和历史文化遗产。以下是一些北京的旅游景点:
156
+ 1. 故宫:位于北京市中心,是明清两代的皇宫,是中国最大的古代宫殿建筑群之一。
157
+ 2. 天安门广场:位于北京市中心,是中国最著名的城市广场之一,也是中国最大的城市广场。
158
+ 3. 颐和
159
+ User: hi
160
+
161
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
162
+
163
+ User: 介绍一下大熊猫
164
+
165
+ Assistant: 大熊猫是一种生活在中国中部地区的哺乳动物,也是中国的国宝之一。它们的外貌特征是圆形的黑白相间的身体,有着黑色的毛发和圆圆的眼睛。大熊猫是一种濒危物种,目前只有在野外的几个保护区才能看到它们的身影。大熊猫的食物主要是竹子,它们会在竹子上寻找食物,并且可以通
166
+ User: hi
167
+
168
+ Assistant: Hi. I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.
169
+
170
+ User: 乌兰察布
171
+
172
+ Assistant: 乌兰察布是中国新疆维吾尔自治区的一个县级市,位于新疆维吾尔自治区中部,是新疆的第二大城市。乌兰察布市是新疆的第一大城市,也是新疆的重要城市之一。乌兰察布市是新疆的经济中心,也是新疆的重要交通枢纽之一。乌兰察布市的人口约为2.5万人,其中汉族占绝大多数。乌
173
+ ```
tokenization_rwkv_world.py CHANGED
@@ -299,7 +299,7 @@ class RWKVWorldTokenizer(PreTrainedTokenizer):
299
  def _get_padding_truncation_strategies(
300
  self, padding=False, truncation=None, max_length=None, pad_to_multiple_of=None, verbose=True, **kwargs
301
  ):
302
- return PaddingStrategy.LONGEST, TruncationStrategy.LONGEST_FIRST, -1, kwargs
303
 
304
  def _encode_plus(
305
  self,
 
299
  def _get_padding_truncation_strategies(
300
  self, padding=False, truncation=None, max_length=None, pad_to_multiple_of=None, verbose=True, **kwargs
301
  ):
302
+ return PaddingStrategy.LONGEST, TruncationStrategy.DO_NOT_TRUNCATE, -1, kwargs
303
 
304
  def _encode_plus(
305
  self,