hakanwkwjbwbs BlinkDL commited on
Commit
ac956e1
·
0 Parent(s):

Duplicate from BlinkDL/ChatRWKV-gradio

Browse files

Co-authored-by: BlinkDL <[email protected]>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. 20B_tokenizer.json +0 -0
  3. README.md +14 -0
  4. app.py +132 -0
  5. requirements.txt +7 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
20B_tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ChatRWKV
3
+ emoji: 💻
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: BlinkDL/ChatRWKV-gradio
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os, gc, torch
3
+ from datetime import datetime
4
+ from huggingface_hub import hf_hub_download
5
+ from pynvml import *
6
+ nvmlInit()
7
+ gpu_h = nvmlDeviceGetHandleByIndex(0)
8
+ ctx_limit = 1024
9
+ title = "RWKV-4-Pile-14B-20230313-ctx8192-test1050"
10
+ desc = f'''Links:
11
+ <a href='https://github.com/BlinkDL/ChatRWKV' target="_blank" style="margin:0 0.5em">ChatRWKV</a>
12
+ <a href='https://github.com/BlinkDL/RWKV-LM' target="_blank" style="margin:0 0.5em">RWKV-LM</a>
13
+ <a href="https://pypi.org/project/rwkv/" target="_blank" style="margin:0 0.5em">RWKV pip package</a>
14
+ '''
15
+
16
+ os.environ["RWKV_JIT_ON"] = '1'
17
+ os.environ["RWKV_CUDA_ON"] = '1' # if '1' then use CUDA kernel for seq mode (much faster)
18
+
19
+ from rwkv.model import RWKV
20
+ model_path = hf_hub_download(repo_id="BlinkDL/rwkv-4-pile-14b", filename=f"{title}.pth")
21
+ model = RWKV(model=model_path, strategy='cuda fp16i8 *20 -> cuda fp16')
22
+ from rwkv.utils import PIPELINE, PIPELINE_ARGS
23
+ pipeline = PIPELINE(model, "20B_tokenizer.json")
24
+
25
+ def infer(
26
+ ctx,
27
+ token_count=10,
28
+ temperature=1.0,
29
+ top_p=0.8,
30
+ presencePenalty = 0.1,
31
+ countPenalty = 0.1,
32
+ ):
33
+ args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
34
+ alpha_frequency = countPenalty,
35
+ alpha_presence = presencePenalty,
36
+ token_ban = [0], # ban the generation of some tokens
37
+ token_stop = []) # stop generation whenever you see any token here
38
+
39
+ ctx = ctx.strip(' ')
40
+ if ctx.endswith('\n'):
41
+ ctx = f'\n{ctx.strip()}\n'
42
+ else:
43
+ ctx = f'\n{ctx.strip()}'
44
+
45
+ gpu_info = nvmlDeviceGetMemoryInfo(gpu_h)
46
+ print(f'vram {gpu_info.total} used {gpu_info.used} free {gpu_info.free}')
47
+
48
+ all_tokens = []
49
+ out_last = 0
50
+ out_str = ''
51
+ occurrence = {}
52
+ state = None
53
+ for i in range(int(token_count)):
54
+ out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
55
+ for n in args.token_ban:
56
+ out[n] = -float('inf')
57
+ for n in occurrence:
58
+ out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
59
+
60
+ token = pipeline.sample_logits(out, temperature=args.temperature, top_p=args.top_p)
61
+ if token in args.token_stop:
62
+ break
63
+ all_tokens += [token]
64
+ if token not in occurrence:
65
+ occurrence[token] = 1
66
+ else:
67
+ occurrence[token] += 1
68
+
69
+ tmp = pipeline.decode(all_tokens[out_last:])
70
+ if '\ufffd' not in tmp:
71
+ out_str += tmp
72
+ yield out_str.strip()
73
+ out_last = i + 1
74
+ gc.collect()
75
+ torch.cuda.empty_cache()
76
+ yield out_str.strip()
77
+
78
+ examples = [
79
+ ["Expert Questions & Helpful Answers\nAsk Research Experts\nQuestion:\nHow can we eliminate poverty?\n\nFull Answer:\n", 150, 1.0, 0.7, 0.2, 0.2],
80
+ ["Here's a short cyberpunk sci-fi adventure story. The story's main character is an artificial human created by a company called OpenBot.\n\nThe Story:\n", 150, 1.0, 0.7, 0.2, 0.2],
81
+ ['''Below is an instruction that describes a task. Write a response that appropriately completes the request.
82
+
83
+ ### Instruction:
84
+ Generate a list of adjectives that describe a person as brave.
85
+
86
+ ### Response:
87
+ ''', 150, 1.0, 0.2, 0.5, 0.5],
88
+ ['''Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
89
+
90
+ ### Instruction:
91
+ Arrange the given numbers in ascending order.
92
+
93
+ ### Input:
94
+ 2, 4, 0, 8, 3
95
+
96
+ ### Response:
97
+ ''', 150, 1.0, 0.2, 0.5, 0.5],
98
+ ["Ask Expert\n\nQuestion:\nWhat are some good plans for world peace?\n\nExpert Full Answer:\n", 150, 1.0, 0.7, 0.2, 0.2],
99
+ ["Q & A\n\nQuestion:\nWhy is the sky blue?\n\nDetailed Expert Answer:\n", 150, 1.0, 0.7, 0.2, 0.2],
100
+ ["Dear sir,\nI would like to express my boundless apologies for the recent nuclear war.", 150, 1.0, 0.7, 0.2, 0.2],
101
+ ["Here is a shell script to find all .hpp files in /home/workspace and delete the 3th row string of these files:", 150, 1.0, 0.7, 0.1, 0.1],
102
+ ["Building a website can be done in 10 simple steps:\n1.", 150, 1.0, 0.7, 0.2, 0.2],
103
+ ["A Chinese phrase is provided: 百闻不如一见。\nThe masterful Chinese translator flawlessly translates the phrase into English:", 150, 1.0, 0.5, 0.2, 0.2],
104
+ ["I believe the meaning of life is", 150, 1.0, 0.7, 0.2, 0.2],
105
+ ["Simply put, the theory of relativity states that", 150, 1.0, 0.5, 0.2, 0.2],
106
+ ]
107
+
108
+
109
+ iface = gr.Interface(
110
+ fn=infer,
111
+ description=f'''{desc} *** <b>Please try examples first (bottom of page)</b> *** (edit them to use your question). Demo limited to ctxlen {ctx_limit}.''',
112
+ allow_flagging="never",
113
+ inputs=[
114
+ gr.Textbox(lines=10, label="Prompt", value="Here's a short cyberpunk sci-fi adventure story. The story's main character is an artificial human created by a company called OpenBot.\n\nThe Story:\n"), # prompt
115
+ gr.Slider(10, 200, step=10, value=150), # token_count
116
+ gr.Slider(0.2, 2.0, step=0.1, value=1.0), # temperature
117
+ gr.Slider(0.0, 1.0, step=0.05, value=0.7), # top_p
118
+ gr.Slider(0.0, 1.0, step=0.1, value=0.2), # presencePenalty
119
+ gr.Slider(0.0, 1.0, step=0.1, value=0.2), # countPenalty
120
+ ],
121
+ outputs=gr.Textbox(label="Generated Output", lines=28),
122
+ examples=examples,
123
+ cache_examples=False,
124
+ ).queue()
125
+
126
+ demo = gr.TabbedInterface(
127
+ [iface], ["Generative"],
128
+ title=title,
129
+ )
130
+
131
+ demo.queue(max_size=10)
132
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ ninja
3
+ tokenizers
4
+ rwkv==0.6.2
5
+ pynvml
6
+ huggingface_hub
7
+ gradio>=3.17.1