tt
Browse files
app.py
CHANGED
|
@@ -9,57 +9,57 @@ MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
# Build messages
|
| 14 |
promptA = systemA + "\nUser: " + user_message + "\nAssistant:"
|
| 15 |
promptB = systemB + "\nUser: " + user_message + "\nAssistant:"
|
|
|
|
| 16 |
input_ids_A = tokenizer(promptA, return_tensors="pt").input_ids.to(model.device)
|
| 17 |
input_ids_B = tokenizer(promptB, return_tensors="pt").input_ids.to(model.device)
|
| 18 |
-
output_ids = input_ids_A
|
|
|
|
|
|
|
| 19 |
|
| 20 |
for _ in range(max_new_tokens):
|
| 21 |
-
# forward last token
|
| 22 |
logitsA = model(input_ids=input_ids_A).logits[:, -1, :]
|
| 23 |
logitsB = model(input_ids=input_ids_B).logits[:, -1, :]
|
|
|
|
| 24 |
probsA = F.softmax(logitsA / temperature, dim=-1)
|
| 25 |
probsB = F.softmax(logitsB / temperature, dim=-1)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
sorted_probs, sorted_idx = torch.sort(
|
| 29 |
cum = torch.cumsum(sorted_probs, dim=-1)
|
| 30 |
-
|
| 31 |
-
sorted_probs
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
if new_id.item() == tokenizer.eos_token_id:
|
| 41 |
break
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
iface = gr.Interface(
|
| 48 |
-
fn=chat_blend,
|
| 49 |
-
inputs=[
|
| 50 |
gr.Textbox(label="System Prompt A", value="You are assistant A."),
|
| 51 |
gr.Textbox(label="System Prompt B", value="You are assistant B."),
|
| 52 |
gr.Slider(label="wA", minimum=-2.0, maximum=2.0, step=0.1, value=1.0),
|
| 53 |
gr.Slider(label="wB", minimum=-2.0, maximum=2.0, step=0.1, value=1.0),
|
| 54 |
-
gr.Textbox(label="User message"),
|
| 55 |
gr.Slider(label="Max new tokens", minimum=1, maximum=200, step=1, value=50),
|
| 56 |
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=1.0),
|
| 57 |
-
gr.Slider(label="Top
|
| 58 |
],
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
description="Uses two system prompts and blends their token distributions using wA*p1 + wB*p2."
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
|
|
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
|
| 12 |
+
def chat_blend_stream(systemA, systemB, wA, wB, user_message, max_new_tokens=50, temperature=1.0, top_p=0.95):
|
|
|
|
| 13 |
promptA = systemA + "\nUser: " + user_message + "\nAssistant:"
|
| 14 |
promptB = systemB + "\nUser: " + user_message + "\nAssistant:"
|
| 15 |
+
|
| 16 |
input_ids_A = tokenizer(promptA, return_tensors="pt").input_ids.to(model.device)
|
| 17 |
input_ids_B = tokenizer(promptB, return_tensors="pt").input_ids.to(model.device)
|
| 18 |
+
output_ids = input_ids_A.clone()
|
| 19 |
+
|
| 20 |
+
response_text = ""
|
| 21 |
|
| 22 |
for _ in range(max_new_tokens):
|
|
|
|
| 23 |
logitsA = model(input_ids=input_ids_A).logits[:, -1, :]
|
| 24 |
logitsB = model(input_ids=input_ids_B).logits[:, -1, :]
|
| 25 |
+
|
| 26 |
probsA = F.softmax(logitsA / temperature, dim=-1)
|
| 27 |
probsB = F.softmax(logitsB / temperature, dim=-1)
|
| 28 |
+
|
| 29 |
+
blended_probs = wA * probsA + wB * probsB
|
| 30 |
+
sorted_probs, sorted_idx = torch.sort(blended_probs, descending=True)
|
| 31 |
cum = torch.cumsum(sorted_probs, dim=-1)
|
| 32 |
+
sorted_probs[cum > top_p] = 0
|
| 33 |
+
sorted_probs /= sorted_probs.sum()
|
| 34 |
+
|
| 35 |
+
new_id = sorted_idx[0, torch.multinomial(sorted_probs[0], 1)]
|
| 36 |
+
output_ids = torch.cat([output_ids, new_id.unsqueeze(0).unsqueeze(0)], dim=1)
|
| 37 |
+
input_ids_A = torch.cat([input_ids_A, new_id.unsqueeze(0).unsqueeze(0)], dim=1)
|
| 38 |
+
input_ids_B = torch.cat([input_ids_B, new_id.unsqueeze(0).unsqueeze(0)], dim=1)
|
| 39 |
+
|
| 40 |
+
token_text = tokenizer.decode(new_id)
|
| 41 |
+
response_text += token_text
|
| 42 |
+
yield response_text
|
| 43 |
+
|
| 44 |
if new_id.item() == tokenizer.eos_token_id:
|
| 45 |
break
|
| 46 |
|
| 47 |
+
demo = gr.ChatInterface(
|
| 48 |
+
fn=chat_blend_stream,
|
| 49 |
+
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
gr.Textbox(label="System Prompt A", value="You are assistant A."),
|
| 51 |
gr.Textbox(label="System Prompt B", value="You are assistant B."),
|
| 52 |
gr.Slider(label="wA", minimum=-2.0, maximum=2.0, step=0.1, value=1.0),
|
| 53 |
gr.Slider(label="wB", minimum=-2.0, maximum=2.0, step=0.1, value=1.0),
|
|
|
|
| 54 |
gr.Slider(label="Max new tokens", minimum=1, maximum=200, step=1, value=50),
|
| 55 |
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=1.0),
|
| 56 |
+
gr.Slider(label="Top-p", minimum=0.1, maximum=1.0, step=0.05, value=0.95),
|
| 57 |
],
|
| 58 |
+
title="Blended TinyLlama",
|
| 59 |
+
description="Two-system prompts, blended logits with negative/positive weights."
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
+
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|
| 65 |
+
|