Views
No views yet
1from rwkv.model import RWKV
2from rwkv.utils import PIPELINE, PIPELINE_ARGS
3import torch
4
5# download models: https://huggingface.co/BlinkDL
6model = RWKV(model='/home/rwkv/Peter/model/base/RWKV-x060-World-7B-v2.1-20240507-ctx4096.pth', strategy='cuda fp16')
7print(model.args)
8pipeline = 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
10states_file = '/home/rwkv/Peter/rwkv_graphrag/agents/persona_domain_states/RWKV-x060-World-7B-v2.1-20240507-ctx4096.pth.pth'
11states = torch.load(states_file)
12states_value = []
13device = 'cuda'
14n_head = model.args.n_head
15head_size = model.args.n_embd//model.args.n_head
16for 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
26cat_char = '🐱'
27bot_char = '🤖'
28instruction ='根据input中的领域和任务,协助用户识别input文本中存在的实体类型。 实体类型必须与用户任务相关。 避免使用诸如“其他”或“未知”的通用实体类型。 非常重要的是:不要生成冗余或重叠的实体类型。用JSON格式输出。'
29input_text = '{"领域": "文学与神话", "专家": "文学史学者/神话学家", "任务": ["分析《石头记》的历史背景和影响", "研究《红楼梦》与《金陵十二钗》之间的关系", "探讨东鲁孔梅溪对《石头记》的改编过程", "解析吴玉峰在《红楼梦》中的角色和贡献", "评估曹雪芹在《悼红轩中披阅十五间》中的写作技巧"]}'
30ctx = f'{cat_char}:{instruction}\n{input_text}\n{bot_char}:'
31print(ctx)
32
33def my_print(s):
34 print(s, end='', flush=True)
35
36
37
38args = 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
46pipeline.generate(ctx, token_count=1000, args=args, callback=my_print,state=states_value)
47print('\n')