Spaces:
Running on Zero
Running on Zero
electblake commited on
Commit ·
eca64c6
1
Parent(s): 2057a1d
fix: bound reasoning before answer generation
Browse files
app.py
CHANGED
|
@@ -103,22 +103,38 @@ def generate(
|
|
| 103 |
return_tensors="pt",
|
| 104 |
).to(model.device)
|
| 105 |
|
|
|
|
| 106 |
with torch.inference_mode():
|
| 107 |
-
|
| 108 |
**inputs,
|
| 109 |
-
max_new_tokens=
|
| 110 |
do_sample=True,
|
| 111 |
temperature=0.6,
|
| 112 |
top_p=0.95,
|
| 113 |
top_k=20,
|
|
|
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
return tokenizer.decode(
|
| 121 |
-
|
| 122 |
skip_special_tokens=True,
|
| 123 |
).strip()
|
| 124 |
|
|
|
|
| 103 |
return_tensors="pt",
|
| 104 |
).to(model.device)
|
| 105 |
|
| 106 |
+
think_end_token = tokenizer.convert_tokens_to_ids("</think>")
|
| 107 |
with torch.inference_mode():
|
| 108 |
+
reasoning_ids = model.generate(
|
| 109 |
**inputs,
|
| 110 |
+
max_new_tokens=2048,
|
| 111 |
do_sample=True,
|
| 112 |
temperature=0.6,
|
| 113 |
top_p=0.95,
|
| 114 |
top_k=20,
|
| 115 |
+
eos_token_id=think_end_token,
|
| 116 |
)
|
| 117 |
+
if reasoning_ids[0, -1].item() != think_end_token:
|
| 118 |
+
reasoning_ids = torch.cat(
|
| 119 |
+
[
|
| 120 |
+
reasoning_ids,
|
| 121 |
+
torch.tensor([[think_end_token]], device=model.device),
|
| 122 |
+
],
|
| 123 |
+
dim=-1,
|
| 124 |
+
)
|
| 125 |
|
| 126 |
+
answer_ids = model.generate(
|
| 127 |
+
input_ids=reasoning_ids,
|
| 128 |
+
attention_mask=torch.ones_like(reasoning_ids),
|
| 129 |
+
max_new_tokens=1024,
|
| 130 |
+
do_sample=True,
|
| 131 |
+
temperature=0.6,
|
| 132 |
+
top_p=0.95,
|
| 133 |
+
top_k=20,
|
| 134 |
+
)
|
| 135 |
|
| 136 |
return tokenizer.decode(
|
| 137 |
+
answer_ids[0, reasoning_ids.shape[-1] :],
|
| 138 |
skip_special_tokens=True,
|
| 139 |
).strip()
|
| 140 |
|