Fix Qwen3 thinking mode + increase max_new_tokens: training/rollout.py
Browse files- training/rollout.py +17 -7
training/rollout.py
CHANGED
|
@@ -24,6 +24,7 @@ from inference import ( # type: ignore
|
|
| 24 |
build_action,
|
| 25 |
build_observation_prompt,
|
| 26 |
parse_llm_response,
|
|
|
|
| 27 |
)
|
| 28 |
from models import TaskID # type: ignore
|
| 29 |
|
|
@@ -58,11 +59,19 @@ class Trajectory:
|
|
| 58 |
|
| 59 |
def _render_chat_prompt(tokenizer, messages: List[dict]) -> str:
|
| 60 |
"""Apply the model's chat template, leaving the assistant turn open."""
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
@torch.no_grad()
|
|
@@ -74,7 +83,7 @@ def rollout_episode(
|
|
| 74 |
*,
|
| 75 |
temperature: float = 1.0,
|
| 76 |
top_p: float = 0.95,
|
| 77 |
-
max_new_tokens: int =
|
| 78 |
max_prompt_tokens: int = 2048,
|
| 79 |
device: Optional[torch.device] = None,
|
| 80 |
) -> Trajectory:
|
|
@@ -112,7 +121,8 @@ def rollout_episode(
|
|
| 112 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 113 |
)
|
| 114 |
completion_ids = gen[0, prompt_ids.shape[0]:]
|
| 115 |
-
|
|
|
|
| 116 |
del gen
|
| 117 |
if torch.cuda.is_available():
|
| 118 |
torch.cuda.empty_cache()
|
|
|
|
| 24 |
build_action,
|
| 25 |
build_observation_prompt,
|
| 26 |
parse_llm_response,
|
| 27 |
+
strip_think_blocks,
|
| 28 |
)
|
| 29 |
from models import TaskID # type: ignore
|
| 30 |
|
|
|
|
| 59 |
|
| 60 |
def _render_chat_prompt(tokenizer, messages: List[dict]) -> str:
|
| 61 |
"""Apply the model's chat template, leaving the assistant turn open."""
|
| 62 |
+
try:
|
| 63 |
+
return tokenizer.apply_chat_template(
|
| 64 |
+
messages,
|
| 65 |
+
tokenize=False,
|
| 66 |
+
add_generation_prompt=True,
|
| 67 |
+
enable_thinking=False,
|
| 68 |
+
)
|
| 69 |
+
except TypeError:
|
| 70 |
+
return tokenizer.apply_chat_template(
|
| 71 |
+
messages,
|
| 72 |
+
tokenize=False,
|
| 73 |
+
add_generation_prompt=True,
|
| 74 |
+
)
|
| 75 |
|
| 76 |
|
| 77 |
@torch.no_grad()
|
|
|
|
| 83 |
*,
|
| 84 |
temperature: float = 1.0,
|
| 85 |
top_p: float = 0.95,
|
| 86 |
+
max_new_tokens: int = 384,
|
| 87 |
max_prompt_tokens: int = 2048,
|
| 88 |
device: Optional[torch.device] = None,
|
| 89 |
) -> Trajectory:
|
|
|
|
| 121 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 122 |
)
|
| 123 |
completion_ids = gen[0, prompt_ids.shape[0]:]
|
| 124 |
+
raw_text = tokenizer.decode(completion_ids, skip_special_tokens=True)
|
| 125 |
+
completion_text = strip_think_blocks(raw_text)
|
| 126 |
del gen
|
| 127 |
if torch.cuda.is_available():
|
| 128 |
torch.cuda.empty_cache()
|