Kevin676 BlinkDL commited on
Commit
2e528e6
0 Parent(s):

Duplicate from BlinkDL/Raven-RWKV-7B

Browse files

Co-authored-by: BlinkDL <BlinkDL@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. 20B_tokenizer.json +0 -0
  3. README.md +14 -0
  4. app.py +124 -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: Raven RWKV 7B
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.23.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: BlinkDL/Raven-RWKV-7B
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-Raven-7B-v8-Eng-20230408-ctx4096"
10
+
11
+ os.environ["RWKV_JIT_ON"] = '1'
12
+ os.environ["RWKV_CUDA_ON"] = '1' # if '1' then use CUDA kernel for seq mode (much faster)
13
+
14
+ from rwkv.model import RWKV
15
+ model_path = hf_hub_download(repo_id="BlinkDL/rwkv-4-raven", filename=f"{title}.pth")
16
+ model = RWKV(model=model_path, strategy='cuda fp16i8 *8 -> cuda fp16')
17
+ from rwkv.utils import PIPELINE, PIPELINE_ARGS
18
+ pipeline = PIPELINE(model, "20B_tokenizer.json")
19
+
20
+ def generate_prompt(instruction, input=None):
21
+ if input:
22
+ return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
23
+
24
+ # Instruction:
25
+ {instruction}
26
+
27
+ # Input:
28
+ {input}
29
+
30
+ # Response:
31
+ """
32
+ else:
33
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
34
+
35
+ # Instruction:
36
+ {instruction}
37
+
38
+ # Response:
39
+ """
40
+
41
+ def evaluate(
42
+ instruction,
43
+ input=None,
44
+ token_count=200,
45
+ temperature=1.0,
46
+ top_p=0.7,
47
+ presencePenalty = 0.1,
48
+ countPenalty = 0.1,
49
+ ):
50
+ args = PIPELINE_ARGS(temperature = max(0.2, float(temperature)), top_p = float(top_p),
51
+ alpha_frequency = countPenalty,
52
+ alpha_presence = presencePenalty,
53
+ token_ban = [], # ban the generation of some tokens
54
+ token_stop = [0]) # stop generation whenever you see any token here
55
+
56
+ instruction = instruction.strip()
57
+ input = input.strip()
58
+ ctx = generate_prompt(instruction, input)
59
+
60
+ gpu_info = nvmlDeviceGetMemoryInfo(gpu_h)
61
+ print(f'vram {gpu_info.total} used {gpu_info.used} free {gpu_info.free}')
62
+
63
+ all_tokens = []
64
+ out_last = 0
65
+ out_str = ''
66
+ occurrence = {}
67
+ state = None
68
+ for i in range(int(token_count)):
69
+ out, state = model.forward(pipeline.encode(ctx)[-ctx_limit:] if i == 0 else [token], state)
70
+ for n in occurrence:
71
+ out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
72
+
73
+ token = pipeline.sample_logits(out, temperature=args.temperature, top_p=args.top_p)
74
+ if token in args.token_stop:
75
+ break
76
+ all_tokens += [token]
77
+ if token not in occurrence:
78
+ occurrence[token] = 1
79
+ else:
80
+ occurrence[token] += 1
81
+
82
+ tmp = pipeline.decode(all_tokens[out_last:])
83
+ if '\ufffd' not in tmp:
84
+ out_str += tmp
85
+ yield out_str.strip()
86
+ out_last = i + 1
87
+ gc.collect()
88
+ torch.cuda.empty_cache()
89
+ yield out_str.strip()
90
+
91
+ examples = [
92
+ ["Tell me about ravens.", "", 150, 1.0, 0.5, 0.4, 0.4],
93
+ ["Write a python function to mine 1 BTC, with details and comments.", "", 150, 1.0, 0.5, 0.2, 0.2],
94
+ ["Write a song about ravens.", "", 150, 1.0, 0.5, 0.4, 0.4],
95
+ ["Explain the following metaphor: Life is like cats.", "", 150, 1.0, 0.5, 0.4, 0.4],
96
+ ["Write a story using the following information", "A man named Alex chops a tree down", 150, 1.0, 0.5, 0.4, 0.4],
97
+ ["Generate a list of adjectives that describe a person as brave.", "", 150, 1.0, 0.5, 0.4, 0.4],
98
+ ["You have $100, and your goal is to turn that into as much money as possible with AI and Machine Learning. Please respond with detailed plan.", "", 150, 1.0, 0.5, 0.4, 0.4],
99
+ ]
100
+
101
+ g = gr.Interface(
102
+ fn=evaluate,
103
+ inputs=[
104
+ gr.components.Textbox(lines=2, label="Instruction", value="Tell me about ravens."),
105
+ gr.components.Textbox(lines=2, label="Input", placeholder="none"),
106
+ gr.components.Slider(minimum=10, maximum=200, step=10, value=150), # token_count
107
+ gr.components.Slider(minimum=0.2, maximum=2.0, step=0.1, value=1.0), # temperature
108
+ gr.components.Slider(minimum=0, maximum=1, step=0.05, value=0.5), # top_p
109
+ gr.components.Slider(0.0, 1.0, step=0.1, value=0.4), # presencePenalty
110
+ gr.components.Slider(0.0, 1.0, step=0.1, value=0.4), # countPenalty
111
+ ],
112
+ outputs=[
113
+ gr.inputs.Textbox(
114
+ lines=5,
115
+ label="Output",
116
+ )
117
+ ],
118
+ title=f"🐦Raven - {title}",
119
+ description="Raven is [RWKV 7B](https://github.com/BlinkDL/ChatRWKV) 100% RNN [RWKV-LM](https://github.com/BlinkDL/RWKV-LM) finetuned to follow instructions. *** Please try examples first (bottom of page) *** (edit them to use your question). Demo limited to ctxlen 1024. It is finetuned on [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca), codealpaca and more. For best results, *** keep you prompt short and clear ***.",
120
+ examples=examples,
121
+ cache_examples=False,
122
+ )
123
+ g.queue(concurrency_count=1, max_size=10)
124
+ g.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