td-builder commited on
Commit
6a9082d
Β·
verified Β·
1 Parent(s): 5f2f755

Upload 137 files

Browse files
hugging/td_fuse/validate.py CHANGED
@@ -180,6 +180,10 @@ def _format_chat_prompt(tokenizer, user_message: str, enable_thinking: bool = Tr
180
  add_generation_prompt=True,
181
  enable_thinking=enable_thinking,
182
  )
 
 
 
 
183
  inputs = tokenizer(text, return_tensors="pt")
184
  return inputs
185
  except Exception:
@@ -187,7 +191,8 @@ def _format_chat_prompt(tokenizer, user_message: str, enable_thinking: bool = Tr
187
 
188
  # Fallback: manual Qwen3 chat format
189
  if enable_thinking:
190
- text = f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant\n"
 
191
  else:
192
  text = f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant\n/no_think\n"
193
  inputs = tokenizer(text, return_tensors="pt")
@@ -222,15 +227,18 @@ def test_thinking_mode(
222
  new_tokens = outputs[0][inputs["input_ids"].shape[1]:]
223
  response = tokenizer.decode(new_tokens, skip_special_tokens=False)
224
 
225
- # Check for thinking tags
226
- has_think_open = "<think>" in response
227
  has_think_close = "</think>" in response
228
- passed = has_think_open and has_think_close
 
 
 
229
 
230
  print(f"\n[validate] Thinking mode test:")
231
  print(f" Prompt: {prompt}")
232
  print(f" Response: {response[:300]}...")
233
- print(f" <think>: {'βœ“ found' if has_think_open else 'βœ— missing'}")
234
  print(f" </think>: {'βœ“ found' if has_think_close else 'βœ— missing'}")
235
  print(f" Status: {'βœ“ PASS' if passed else 'βœ— FAIL'}")
236
 
 
180
  add_generation_prompt=True,
181
  enable_thinking=enable_thinking,
182
  )
183
+ # Verify the template actually produced thinking tokens
184
+ if enable_thinking and "<think>" not in text:
185
+ # Template didn't add thinking trigger β€” use manual format
186
+ raise ValueError("Template missing think trigger")
187
  inputs = tokenizer(text, return_tensors="pt")
188
  return inputs
189
  except Exception:
 
191
 
192
  # Fallback: manual Qwen3 chat format
193
  if enable_thinking:
194
+ # Qwen3 thinking mode: start assistant turn with <think> to trigger CoT
195
+ text = f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant\n<think>\n"
196
  else:
197
  text = f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant\n/no_think\n"
198
  inputs = tokenizer(text, return_tensors="pt")
 
227
  new_tokens = outputs[0][inputs["input_ids"].shape[1]:]
228
  response = tokenizer.decode(new_tokens, skip_special_tokens=False)
229
 
230
+ # Check for thinking tags (we may have prefilled <think> in the prompt,
231
+ # so check for </think> which the model must produce to end thinking)
232
  has_think_close = "</think>" in response
233
+ # If template handled it, <think> appears in new tokens too
234
+ has_think_open = "<think>" in response
235
+ # Pass if model produced </think> (thinking happened, whether <think> was prefilled or not)
236
+ passed = has_think_close
237
 
238
  print(f"\n[validate] Thinking mode test:")
239
  print(f" Prompt: {prompt}")
240
  print(f" Response: {response[:300]}...")
241
+ print(f" <think>: {'βœ“ found' if has_think_open else '(prefilled in prompt)'}")
242
  print(f" </think>: {'βœ“ found' if has_think_close else 'βœ— missing'}")
243
  print(f" Status: {'βœ“ PASS' if passed else 'βœ— FAIL'}")
244
 
hugging/td_lang/compiler.py CHANGED
@@ -958,7 +958,7 @@ DO NOT EDIT - regenerate from the .td file instead.
958
  - Supports configurable sample count (cmd.n_samples if provided).
959
  - Produces domain-specific prompts (math, code, logic, factual).
960
  """
961
- n_samples_expr = f"getattr(cmd, 'n_samples', 100)" # static string for emit clarity
962
  self._emit(f'print("[td_lang] Generating synthetic data for {cmd.target}...")')
963
  self._emit(f'checkpoint = models.get("{cmd.target}", {{}}).get("checkpoint")')
964
  self._emit("if not checkpoint:")
@@ -1092,7 +1092,7 @@ DO NOT EDIT - regenerate from the .td file instead.
1092
  self._indent -= 1
1093
  self._emit("")
1094
  self._emit("synth_data = []")
1095
- self._emit(f"n_samples = getattr(cmd, 'n_samples', 100)")
1096
  self._emit("for i in range(n_samples):")
1097
  self._indent += 1
1098
  self._emit("domain = random.choice(weak_topics)")
 
958
  - Supports configurable sample count (cmd.n_samples if provided).
959
  - Produces domain-specific prompts (math, code, logic, factual).
960
  """
961
+ n_samples_val = getattr(cmd, 'n_samples', 100) # resolve at compile time
962
  self._emit(f'print("[td_lang] Generating synthetic data for {cmd.target}...")')
963
  self._emit(f'checkpoint = models.get("{cmd.target}", {{}}).get("checkpoint")')
964
  self._emit("if not checkpoint:")
 
1092
  self._indent -= 1
1093
  self._emit("")
1094
  self._emit("synth_data = []")
1095
+ self._emit(f"n_samples = {n_samples_val}")
1096
  self._emit("for i in range(n_samples):")
1097
  self._indent += 1
1098
  self._emit("domain = random.choice(weak_topics)")