yueyulin commited on
Commit
fe10d5b
·
verified ·
1 Parent(s): 1f63bce

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +57 -0
  2. entity_summary.pth +3 -0
  3. entity_summary.py +47 -0
  4. entitysummarydemo.png +0 -0
README.md ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This is a state for rwkv6_7b_v2.1 that generates a summary about entities and relations between them
2
+
3
+ * The input is solely the context that you want this model to analyze
4
+ * The output are domain, expert role in this domain and specific tasks that this export can do in a jsonl format.
5
+
6
+ # Please refer to the following demo as test code:
7
+ ```python
8
+ from rwkv.model import RWKV
9
+ from rwkv.utils import PIPELINE, PIPELINE_ARGS
10
+ import torch
11
+
12
+ # download models: https://huggingface.co/BlinkDL
13
+ model = RWKV(model='/home/rwkv/Peter/model/base/RWKV-x060-World-7B-v2.1-20240507-ctx4096.pth', strategy='cuda fp16')
14
+ print(model.args)
15
+ pipeline = PIPELINE(model, "rwkv_vocab_v20230424") # 20B_tokenizer.json is in https://github.com/BlinkDL/ChatRWKV
16
+ # use pipeline = PIPELINE(model, "rwkv_vocab_v20230424") for rwkv "world" models
17
+ states_file = '/home/rwkv/Peter/rwkv_graphrag/agents/entity_summary/entity_summary.pth'
18
+ states = torch.load(states_file)
19
+ states_value = []
20
+ device = 'cuda'
21
+ n_head = model.args.n_head
22
+ head_size = model.args.n_embd//model.args.n_head
23
+ for i in range(model.args.n_layer):
24
+ key = f'blocks.{i}.att.time_state'
25
+ value = states[key]
26
+ prev_x = torch.zeros(model.args.n_embd,device=device,dtype=torch.float16)
27
+ prev_states = value.clone().detach().to(device=device,dtype=torch.float16).transpose(1,2)
28
+ prev_ffn = torch.zeros(model.args.n_embd,device=device,dtype=torch.float16)
29
+ states_value.append(prev_x)
30
+ states_value.append(prev_states)
31
+ states_value.append(prev_ffn)
32
+
33
+ cat_char = '🐱'
34
+ bot_char = '🤖'
35
+ instruction ='请阅读iput中的entities和descritions,围绕entity和descrition写一个简单的200字介绍,介绍需要包括所的entity和他们之间的关系.最终内容不能超过200字'
36
+ input_text = '"entities": ["汉语", "语义偏移", "构式语法", "评价性语境", "词汇意义"], "descriptions": ["汉语是中国的主要语言,具有丰富的语义结构和复杂的语法体系。", "语义偏移是指在特定语境下,词语的意义发生的变化或偏离。", "构式语法研究的是句子结构的模式及其功能,是语言学的一个分支。", "评价性语境指的是包含情感色彩或评价性质的语言环境,影响着语言表达的意义。", "词汇意义指的是单词在特定语境下的具体含义,可以因语境而变化。"]'
37
+ ctx = f'{cat_char}:{instruction}\n{input_text}\n{bot_char}:'
38
+ print(ctx)
39
+
40
+ def my_print(s):
41
+ print(s, end='', flush=True)
42
+
43
+
44
+
45
+ args = PIPELINE_ARGS(temperature = 1.3, top_p = 0.5, top_k = 0, # top_k = 0 then ignore
46
+ alpha_frequency = 0.7,
47
+ alpha_presence = 0.5,
48
+ alpha_decay = 0.996, # gradually decay the penalty
49
+ token_ban = [0], # ban the generation of some tokens
50
+ token_stop = [bot_char], # stop generation whenever you see any token here
51
+ chunk_len = 256) # split input into chunks to save VRAM (shorter -> slower)
52
+
53
+ pipeline.generate(ctx, token_count=200, args=args, callback=my_print,state=states_value)
54
+ print('\n')
55
+ ```
56
+ # The final printed input and output:
57
+ ![](entitysummarydemo.png)
entity_summary.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e66ffcc5a70a10ba1bc08988afd4dbfd1d9085564821ce294264df0e683f57a
3
+ size 16781463
entity_summary.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_summary/entity_summary.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 ='请阅读iput中的entities和descritions,围绕entity和descrition写一个简单的200字介绍,介绍需要包括所的entity和他们之间的关系.最终内容不能超过200字'
29
+ input_text = '"entities": ["汉语", "语义偏移", "构式语法", "评价性语境", "词汇意义"], "descriptions": ["汉语是中国的主要语言,具有丰富的语义结构和复杂的语法体系。", "语义偏移是指在特定语境下,词语的意义发生的变化或偏离。", "构式语法研究的是句子结构的模式及其功能,是语言学的一个分支。", "评价性语境指的是包含情感色彩或评价性质的语言环境,影响着语言表达的意义。", "词汇意义指的是单词在特定语境下的具体含义,可以因语境而变化。"]'
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.3, top_p = 0.5, top_k = 0, # top_k = 0 then ignore
39
+ alpha_frequency = 0.7,
40
+ alpha_presence = 0.5,
41
+ alpha_decay = 0.996, # gradually decay the penalty
42
+ token_ban = [0], # ban the generation of some tokens
43
+ token_stop = [bot_char], # 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')
entitysummarydemo.png ADDED