Spaces:
Sleeping
Sleeping
File size: 10,085 Bytes
e10c195 89628dd e10c195 b5db033 e10c195 2021bac b5db033 2021bac b5db033 2021bac ab95480 2021bac ab95480 2021bac ab95480 2021bac b5db033 eb2ed76 f486321 2021bac b5db033 2021bac eb2ed76 b5db033 eb2ed76 9e21282 2021bac b5db033 ab95480 2021bac ab95480 b5db033 9e21282 2021bac ab95480 2021bac ab95480 2021bac ab95480 b5db033 2021bac b5db033 2021bac d811871 b5db033 2021bac 9e21282 2021bac b5db033 2021bac f486321 2021bac f486321 2021bac ab95480 9e21282 2021bac eb2ed76 2021bac eb2ed76 2021bac eb2ed76 2021bac eb2ed76 2021bac eb2ed76 2021bac eb2ed76 2021bac eb2ed76 2021bac 89628dd 2021bac b5db033 |
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 |
import gradio as gr
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
import torch
from threading import Thread
veri_model_path = "nyu-dice-lab/VeriThoughts-Reasoning-7B"
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# Try loading the model with KV caching (no flash attention or quantization)
try:
print("Loading tokenizer...")
veri_tokenizer = AutoTokenizer.from_pretrained(veri_model_path)
# Set pad token if not exists
if veri_tokenizer.pad_token is None:
veri_tokenizer.pad_token = veri_tokenizer.eos_token
print("Loading model with KV caching...")
veri_model = AutoModelForCausalLM.from_pretrained(
veri_model_path,
device_map="auto" if torch.cuda.is_available() else None,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
trust_remote_code=True,
use_cache=True, # Enable KV caching for faster generation
low_cpu_mem_usage=True
)
print("Model loaded successfully with KV caching!")
except Exception as e:
print(f"Model loading error: {e}")
veri_model = None
veri_tokenizer = None
@spaces.GPU(duration=60)
def truncate_at_code_end(text):
"""Truncate text at 'CODE END' to remove repetitive content"""
if "CODE END" in text:
end_index = text.find("CODE END") + len("CODE END")
return text[:end_index].strip()
return text.strip()
def generate_response(user_message, history):
"""Non-streaming generation for quick responses"""
if not veri_model or not veri_tokenizer:
return history + [["Error", "Model not loaded properly"]]
if not user_message.strip():
return history
system_message = "You are VeriThoughts, a helpful assistant that thinks step by step to answer Verilog coding questions. Make sure your input and output interface has the same names as described in the question. Please start your Verilog code with CODE BEGIN and end with CODE END."
# Create conversation history (limit to last 3 exchanges for memory efficiency)
conversation = f"System: {system_message}\n"
recent_history = history[-3:] if len(history) > 3 else history
for h in recent_history:
conversation += f"User: {h[0]}\nAssistant: {h[1]}\n"
conversation += f"User: {user_message}\nAssistant:"
# Tokenize input
inputs = veri_tokenizer(
conversation,
return_tensors="pt",
truncation=True,
max_length=4096,
padding=True
).to(device)
# Generate with KV caching
with torch.no_grad():
outputs = veri_model.generate(
**inputs,
max_new_tokens=1024,
temperature=0.6,
top_p=0.95,
do_sample=True,
pad_token_id=veri_tokenizer.pad_token_id,
eos_token_id=veri_tokenizer.eos_token_id,
use_cache=True, # KV caching for speed
repetition_penalty=1.1,
early_stopping=True
)
# Decode response
response = veri_tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
# Truncate at CODE END to remove repetitive content
response = truncate_at_code_end(response)
# Clean up GPU memory
if torch.cuda.is_available():
torch.cuda.empty_cache()
return history + [[user_message, response]]
@spaces.GPU(duration=120)
def generate_response_streaming(user_message, history):
"""Streaming generation for real-time response display"""
if not veri_model or not veri_tokenizer:
yield history + [["Error", "Model not loaded properly"]]
return
if not user_message.strip():
yield history
return
system_message = "You are VeriThoughts, a helpful assistant that thinks step by step. You are finetuned from a Qwen model, created by Alibaba Cloud, to answer Verilog coding questions. Make sure your input and output interface has the same names as described in the question. Please start your Verilog code with CODE BEGIN and end with CODE END."
# Create conversation history (limit for memory efficiency)
conversation = f"System: {system_message}\n"
recent_history = history[-3:] if len(history) > 3 else history
for h in recent_history:
conversation += f"User: {h[0]}\nAssistant: {h[1]}\n"
conversation += f"User: {user_message}\nAssistant:"
try:
# Tokenize input
inputs = veri_tokenizer(
conversation,
return_tensors="pt",
truncation=True,
max_length=2048,
padding=True
).to(device)
# Setup streaming
streamer = TextIteratorStreamer(
veri_tokenizer,
skip_prompt=True,
skip_special_tokens=True,
timeout=30.0
)
# Generation parameters with KV caching
generation_kwargs = {
**inputs,
"max_new_tokens": 4096,
"temperature": 0.6,
"top_p": 0.95,
"do_sample": True,
"pad_token_id": veri_tokenizer.pad_token_id,
"eos_token_id": veri_tokenizer.eos_token_id,
"use_cache": True, # KV caching for faster streaming
"repetition_penalty": 1.1,
"streamer": streamer,
"early_stopping": True
}
# Start generation in a separate thread
thread = Thread(target=veri_model.generate, kwargs=generation_kwargs)
thread.start()
# Stream the response token by token
generated_text = ""
new_history = history + [[user_message, ""]]
code_end_reached = False
for new_text in streamer:
# Stop streaming if we've already reached CODE END
if code_end_reached:
break
generated_text += new_text
# Check if CODE END appears in the generated text
if "CODE END" in generated_text:
# Truncate at CODE END and mark as complete
generated_text = truncate_at_code_end(generated_text)
code_end_reached = True
new_history[-1][1] = generated_text
yield new_history
# Break early if CODE END was reached
if code_end_reached:
break
# Ensure the thread completes
thread.join()
# Final cleanup in case CODE END wasn't reached during streaming
if not code_end_reached:
final_text = truncate_at_code_end(generated_text)
new_history[-1][1] = final_text
yield new_history
except Exception as e:
print(f"Streaming error: {e}")
error_history = history + [[user_message, f"Streaming error: {str(e)}"]]
yield error_history
finally:
# Clean up GPU memory after generation
if torch.cuda.is_available():
torch.cuda.empty_cache()
def clear_chat():
"""Clear chat and clean up memory"""
if torch.cuda.is_available():
torch.cuda.empty_cache()
return []
# Create interface with soft theme
with gr.Blocks(title="VeriThoughts-7B Chatbot") as demo:
gr.Markdown("# VeriThoughts-7B Chatbot")
gr.Markdown("*Optimized with KV caching for faster generation*")
with gr.Row():
with gr.Column(scale=4):
chatbot = gr.Chatbot(
value=[],
label="Chat",
height=600,
show_label=False,
container=True
)
with gr.Row():
msg = gr.Textbox(
label="Your message",
placeholder="Ask me about Verilog design, syntax, or implementation...",
lines=2,
max_lines=5,
scale=4
)
send_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Column(scale=1):
with gr.Group():
stream_btn = gr.Button("π‘ Send (Streaming)", variant="secondary", size="sm")
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary", size="sm")
gr.Markdown(
"""
### π‘ Usage Tips
**Send**: Quick response (max 1K tokens)
**Streaming**: Real-time response (max 2K tokens)
### β‘ Optimizations Active
- **KV Caching**: Faster token generation
- **Memory Management**: Auto cleanup
- **Context Limiting**: Recent history only
### π― Best Practices
- Be specific about Verilog requirements
- Mention input/output port names
- Ask for step-by-step explanations
- Clear chat periodically
"""
)
# Event handlers for regular send
submit_event = msg.submit(
fn=generate_response,
inputs=[msg, chatbot],
outputs=chatbot,
show_progress=True
).then(
lambda: "",
inputs=None,
outputs=msg
)
send_btn.click(
fn=generate_response,
inputs=[msg, chatbot],
outputs=chatbot,
show_progress=True
).then(
lambda: "",
inputs=None,
outputs=msg
)
# Event handler for streaming
stream_btn.click(
fn=generate_response_streaming,
inputs=[msg, chatbot],
outputs=chatbot,
show_progress=True
).then(
lambda: "",
inputs=None,
outputs=msg
)
# Clear chat handler
clear_btn.click(
fn=clear_chat,
inputs=None,
outputs=chatbot
)
# Launch the app
demo.launch(share=True) |