Spaces:
Runtime error
Runtime error
File size: 13,796 Bytes
8362bbb 24db95d 8362bbb 9fadcab 8362bbb bffaf15 8362bbb bffaf15 8362bbb 5324549 8362bbb 151160e 8362bbb 5324549 8362bbb 151160e 8362bbb 1aba41b 8362bbb 1aba41b 8362bbb 9fadcab 8362bbb 9fadcab 8362bbb 9fadcab 8362bbb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
import os
import time
import spaces
import gradio as gr
import argparse
try:
from .model.ea_model import EaModel
except:
from model.ea_model import EaModel
import torch
from fastchat.model import get_conversation_template
import re
def truncate_list(lst, num):
if num not in lst:
return lst
first_index = lst.index(num)
return lst[:first_index + 1]
def find_list_markers(text):
pattern = re.compile(r'(?m)(^\d+\.\s|\n)')
matches = pattern.finditer(text)
return [(match.start(), match.end()) for match in matches]
def checkin(pointer,start,marker):
for b,e in marker:
if b<=pointer<e:
return True
if b<=start<e:
return True
return False
def highlight_text(text, text_list,color="black"):
pointer = 0
result = ""
markers=find_list_markers(text)
for sub_text in text_list:
start = text.find(sub_text, pointer)
if start==-1:
continue
end = start + len(sub_text)
if checkin(pointer,start,markers):
result += text[pointer:start]
else:
result += f"<span style='color: {color};'>{text[pointer:start]}</span>"
result += sub_text
pointer = end
if pointer < len(text):
result += f"<span style='color: {color};'>{text[pointer:]}</span>"
return result
@spaces.GPU(duration=30)
def warmup(model):
conv = get_conversation_template(args.model_type)
if args.model_type == "llama-2-chat":
sys_p = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
conv.system_message = sys_p
elif args.model_type == "mixtral":
conv = get_conversation_template("llama-2-chat")
conv.system_message = ''
conv.sep2 = "</s>"
conv.append_message(conv.roles[0], "Hello")
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
if args.model_type == "llama-2-chat":
prompt += " "
input_ids = model.tokenizer([prompt]).input_ids
input_ids = torch.as_tensor(input_ids).to(model.base_model.device)
for output_ids in model.ea_generate(input_ids):
ol=output_ids.shape[1]
@spaces.GPU(duration=30)
def bot(history, temperature, top_p, use_EaInfer, highlight_EaInfer,session_state,):
if not history:
return history, "0.00 tokens/s", "0.00", session_state
pure_history = session_state.get("pure_history", [])
assert args.model_type == "llama-2-chat" or "vicuna"
conv = get_conversation_template(args.model_type)
if args.model_type == "llama-2-chat":
sys_p = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
conv.system_message = sys_p
elif args.model_type == "mixtral":
conv = get_conversation_template("llama-2-chat")
conv.system_message = ''
conv.sep2 = "</s>"
elif args.model_type == "llama-3-instruct":
messages = [
{"role": "system",
"content": "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."},
]
for query, response in pure_history:
if args.model_type == "llama-3-instruct":
messages.append({
"role": "user",
"content": query
})
if response!=None:
messages.append({
"role": "assistant",
"content": response
})
else:
conv.append_message(conv.roles[0], query)
if args.model_type == "llama-2-chat" and response:
response = " " + response
conv.append_message(conv.roles[1], response)
if args.model_type == "llama-3-instruct":
prompt = model.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
else:
prompt = conv.get_prompt()
if args.model_type == "llama-2-chat":
prompt += " "
input_ids = model.tokenizer([prompt]).input_ids
input_ids = torch.as_tensor(input_ids).to(model.base_model.device)
input_len = input_ids.shape[1]
naive_text = []
cu_len = input_len
totaltime=0
start_time=time.time()
total_ids=0
if use_EaInfer:
for output_ids in model.ea_generate(input_ids, temperature=temperature, top_p=top_p,
max_new_tokens=args.max_new_token,is_llama3=args.model_type=="llama-3-instruct"):
totaltime+=(time.time()-start_time)
total_ids+=1
decode_ids = output_ids[0, input_len:].tolist()
decode_ids = truncate_list(decode_ids, model.tokenizer.eos_token_id)
if args.model_type == "llama-3-instruct":
decode_ids = truncate_list(decode_ids, model.tokenizer.convert_tokens_to_ids("<|eot_id|>"))
text = model.tokenizer.decode(decode_ids, skip_special_tokens=True, spaces_between_special_tokens=False,
clean_up_tokenization_spaces=True, )
naive_text.append(model.tokenizer.decode(output_ids[0, cu_len], skip_special_tokens=True,
spaces_between_special_tokens=False,
clean_up_tokenization_spaces=True, ))
cu_len = output_ids.shape[1]
colored_text = highlight_text(text, naive_text, "orange")
if highlight_EaInfer:
history[-1][1] = colored_text
else:
history[-1][1] = text
pure_history[-1][1] = text
session_state["pure_history"] = pure_history
new_tokens = cu_len-input_len
yield history,f"{new_tokens/totaltime:.2f} tokens/s",f"{new_tokens/total_ids:.2f}",session_state
start_time = time.time()
else:
for output_ids in model.naive_generate(input_ids, temperature=temperature, top_p=top_p,
max_new_tokens=args.max_new_token,is_llama3=args.model_type=="llama-3-instruct"):
totaltime += (time.time() - start_time)
total_ids+=1
decode_ids = output_ids[0, input_len:].tolist()
decode_ids = truncate_list(decode_ids, model.tokenizer.eos_token_id)
text = model.tokenizer.decode(decode_ids, skip_special_tokens=True, spaces_between_special_tokens=False,
clean_up_tokenization_spaces=True, )
naive_text.append(model.tokenizer.decode(output_ids[0, cu_len], skip_special_tokens=True,
spaces_between_special_tokens=False,
clean_up_tokenization_spaces=True, ))
cu_len = output_ids.shape[1]
colored_text = highlight_text(text, naive_text, "orange")
if highlight_EaInfer and use_EaInfer:
history[-1][1] = colored_text
else:
history[-1][1] = text
history[-1][1] = text
pure_history[-1][1] = text
new_tokens = cu_len - input_len
yield history,f"{new_tokens/totaltime:.2f} tokens/s",f"{new_tokens/total_ids:.2f}",session_state
start_time = time.time()
def user(user_message, history,session_state):
if history==None:
history=[]
pure_history = session_state.get("pure_history", [])
pure_history += [[user_message, None]]
session_state["pure_history"] = pure_history
return "", history + [[user_message, None]],session_state
def regenerate(history,session_state):
if not history:
return history, None,"0.00 tokens/s","0.00",session_state
pure_history = session_state.get("pure_history", [])
pure_history[-1][-1] = None
session_state["pure_history"]=pure_history
if len(history) > 1: # Check if there's more than one entry in history (i.e., at least one bot response)
new_history = history[:-1] # Remove the last bot response
last_user_message = history[-1][0] # Get the last user message
return new_history + [[last_user_message, None]], None,"0.00 tokens/s","0.00",session_state
history[-1][1] = None
return history, None,"0.00 tokens/s","0.00",session_state
def clear(history,session_state):
pure_history = session_state.get("pure_history", [])
pure_history = []
session_state["pure_history"] = pure_history
return [],"0.00 tokens/s","0.00",session_state
parser = argparse.ArgumentParser()
parser.add_argument(
"--ea-model-path",
type=str,
default="yuhuili/EAGLE-LLaMA3-Instruct-8B",
help="The path to the weights. This can be a local folder or a Hugging Face repo ID.",
)
parser.add_argument("--base-model-path", type=str, default="8B",
help="path of basemodel, huggingface project or local path")
parser.add_argument(
"--load-in-8bit", action="store_true", help="Use 8-bit quantization"
)
parser.add_argument(
"--load-in-4bit", action="store_true", help="Use 4-bit quantization"
)
parser.add_argument("--model-type", type=str, default="llama-3-instruct",choices=["llama-2-chat","vicuna","mixtral","llama-3-instruct"])
parser.add_argument(
"--total-token",
type=int,
default=59,
help="The maximum number of new generated tokens.",
)
parser.add_argument(
"--max-new-token",
type=int,
default=512,
help="The maximum number of new generated tokens.",
)
args = parser.parse_args()
model = EaModel.from_pretrained(
base_model_path=args.base_model_path,
ea_model_path=args.ea_model_path,
total_token=args.total_token,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
load_in_4bit=args.load_in_4bit,
load_in_8bit=args.load_in_8bit,
device_map="auto",
)
model.eval()
warmup(model)
custom_css = """
#speed textarea {
color: red;
font-size: 30px;
}"""
with gr.Blocks(css=custom_css) as demo:
gs = gr.State({"pure_history": []})
gr.Markdown('''## EAGLE-2 Chatbot''')
with gr.Row():
speed_box = gr.Textbox(label="Speed", elem_id="speed", interactive=False, value="0.00 tokens/s")
compression_box = gr.Textbox(label="Compression Ratio", elem_id="speed", interactive=False, value="0.00")
with gr.Row():
with gr.Column():
use_EaInfer = gr.Checkbox(label="Use EAGLE-2", value=True)
highlight_EaInfer = gr.Checkbox(label="Highlight the tokens generated by EAGLE-2", value=True)
temperature = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="temperature", value=0.5)
top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="top_p", value=0.9)
note=gr.Markdown(show_label=False,value='''The original LLM is LLaMA3-Instruct 8B, running on a single RTX 3090. The Compression Ratio is defined as the number of generated tokens divided by the number of forward passes in the original LLM. If "Highlight the tokens generated by EAGLE-2" is checked, the tokens correctly guessed by EAGLE-2
will be displayed in orange. Note: Checking this option may cause special formatting rendering issues in a few cases, especially when generating code''')
chatbot = gr.Chatbot(height=600,show_label=False)
msg = gr.Textbox(label="Your input")
with gr.Row():
send_button = gr.Button("Send")
stop_button = gr.Button("Stop")
regenerate_button = gr.Button("Regenerate")
clear_button = gr.Button("Clear")
enter_event=msg.submit(user, [msg, chatbot,gs], [msg, chatbot,gs], queue=True).then(
bot, [chatbot, temperature, top_p, use_EaInfer, highlight_EaInfer,gs], [chatbot,speed_box,compression_box,gs]
)
clear_button.click(clear, [chatbot,gs], [chatbot,speed_box,compression_box,gs], queue=True)
send_event=send_button.click(user, [msg, chatbot,gs], [msg, chatbot,gs],queue=True).then(
bot, [chatbot, temperature, top_p, use_EaInfer, highlight_EaInfer,gs], [chatbot,speed_box,compression_box,gs]
)
regenerate_event=regenerate_button.click(regenerate, [chatbot,gs], [chatbot, msg,speed_box,compression_box,gs],queue=True).then(
bot, [chatbot, temperature, top_p, use_EaInfer, highlight_EaInfer,gs], [chatbot,speed_box,compression_box,gs]
)
stop_button.click(fn=None, inputs=None, outputs=None, cancels=[send_event,regenerate_event,enter_event])
demo.queue()
demo.launch(share=True)
|