yueyulin commited on
Commit
d5fe1e0
·
verified ·
1 Parent(s): 2e522f4

Upload 2 files

Browse files
Files changed (2) hide show
  1. entity_type.py +47 -0
  2. entity_type_demo.png +0 -0
entity_type.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from rwkv.model import RWKV
2
+ from rwkv.utils import PIPELINE, PIPELINE_ARGS
3
+ import torch
4
+
5
+ # download models: https://huggingface.co/BlinkDL
6
+ model = RWKV(model='/home/rwkv/Peter/model/base/RWKV-x060-World-7B-v2.1-20240507-ctx4096.pth', strategy='cuda fp16')
7
+ print(model.args)
8
+ pipeline = PIPELINE(model, "rwkv_vocab_v20230424") # 20B_tokenizer.json is in https://github.com/BlinkDL/ChatRWKV
9
+ # use pipeline = PIPELINE(model, "rwkv_vocab_v20230424") for rwkv "world" models
10
+ states_file = '/home/rwkv/Peter/rwkv_graphrag/agents/entity_type_extraction/RWKV-x060-World-7B-v2.1-20240507-ctx4096.pth.pth'
11
+ states = torch.load(states_file)
12
+ states_value = []
13
+ device = 'cuda'
14
+ n_head = model.args.n_head
15
+ head_size = model.args.n_embd//model.args.n_head
16
+ for i in range(model.args.n_layer):
17
+ key = f'blocks.{i}.att.time_state'
18
+ value = states[key]
19
+ prev_x = torch.zeros(model.args.n_embd,device=device,dtype=torch.float16)
20
+ prev_states = value.clone().detach().to(device=device,dtype=torch.float16).transpose(1,2)
21
+ prev_ffn = torch.zeros(model.args.n_embd,device=device,dtype=torch.float16)
22
+ states_value.append(prev_x)
23
+ states_value.append(prev_states)
24
+ states_value.append(prev_ffn)
25
+
26
+ cat_char = '🐱'
27
+ bot_char = '🤖'
28
+ instruction ='根据input中的领域和任务,协助用户识别input文本中存在的实体类型。 实体类型必须与用户任务相关。 避免使用诸如“其他”或“未知”的通用实体类型。 非常重要的是:不要生成冗余或重叠的实体类型。用JSON格式输出。'
29
+ input_text = '{"领域": "文学与神话", "专家": "文学史学者/神话学家", "任务": ["分析《石头记》的历史背景和影响", "研究《红楼梦》与《金陵十二钗》之间的关系", "探讨东鲁孔梅溪对《石头记》的改编过程", "解析吴玉峰在《红楼梦》中的角色和贡献", "评估曹雪芹在《悼红轩中披阅十五间》中的写作技巧"]}'
30
+ ctx = f'{cat_char}:{instruction}\n{input_text}\n{bot_char}:'
31
+ print(ctx)
32
+
33
+ def my_print(s):
34
+ print(s, end='', flush=True)
35
+
36
+
37
+
38
+ args = PIPELINE_ARGS(temperature = 1, top_p = 0.2, top_k = 0, # top_k = 0 then ignore
39
+ alpha_frequency = 0.5,
40
+ alpha_presence = 0.5,
41
+ alpha_decay = 0.998, # gradually decay the penalty
42
+ token_ban = [0], # ban the generation of some tokens
43
+ token_stop = [0,1], # stop generation whenever you see any token here
44
+ chunk_len = 256) # split input into chunks to save VRAM (shorter -> slower)
45
+
46
+ pipeline.generate(ctx, token_count=200, args=args, callback=my_print,state=states_value)
47
+ print('\n')
entity_type_demo.png ADDED