Skip to content

Commit 7a0b6a6

Browse files
authored
common/autoparser : detect reasoning markers when enable_thinking changes system prompt (#20859)
1 parent 07ff000 commit 7a0b6a6

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

common/chat-diff-analyzer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,34 @@ void analyze_reasoning::compare_thinking_enabled() {
348348
mode = reasoning_mode::TAG_BASED;
349349
}
350350
}
351+
} else if (!left_trimmed.empty() && !right_trimmed.empty()) {
352+
// Full-output diff is noisy (e.g., SmolLM3 changes the system message when enable_thinking flips).
353+
// Try to find reasoning markers by tail-anchoring:
354+
// one output's generation prompt tail may appear in the other with extra reasoning markers appended.
355+
const auto & output_A = comparison->output_A;
356+
const auto & output_B = comparison->output_B;
357+
const size_t anchor_len = 64;
358+
359+
for (int dir = 0; dir < 2; dir++) {
360+
const auto & base = dir == 0 ? output_B : output_A;
361+
const auto & extended = dir == 0 ? output_A : output_B;
362+
363+
size_t len = std::min(base.size(), anchor_len);
364+
std::string anchor = base.substr(base.size() - len);
365+
auto pos = extended.rfind(anchor);
366+
if (pos == std::string::npos || pos + len >= extended.size()) continue;
367+
368+
std::string extra = trim_whitespace(extended.substr(pos + len));
369+
if (extra.empty()) continue;
370+
371+
auto seg = prune_whitespace_segments(segmentize_markers(extra));
372+
if (seg.size() == 2 && seg[0].type == segment_type::MARKER && seg[1].type == segment_type::MARKER) {
373+
if (start.empty()) start = seg[0].value;
374+
if (end.empty()) end = seg[1].value;
375+
mode = reasoning_mode::TAG_BASED;
376+
break;
377+
}
378+
}
351379
}
352380

353381
if (mode == reasoning_mode::NONE && start.empty() && !end.empty()) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{#- Copyright 2025-present the Unsloth team. All rights reserved. #}
2+
{#- Licensed under the Apache License, Version 2.0 (the "License") #}
3+
{#- Edits made by Unsloth to make it work for most inference engines #}
4+
{# ───── defaults ───── #}
5+
{%- if enable_thinking is not defined -%}
6+
{%- set enable_thinking = true -%}
7+
{%- endif -%}
8+
{# ───── reasoning mode ───── #}
9+
{%- if enable_thinking -%}
10+
{%- set reasoning_mode = "/think" -%}
11+
{%- else -%}
12+
{%- set reasoning_mode = "/no_think" -%}
13+
{%- endif -%}
14+
{# ───── header (system message) ───── #}
15+
{{- "<|im_start|>system\n" -}}
16+
{%- if messages[0].role == "system" -%}
17+
{%- set system_message = messages[0].content -%}
18+
{%- if "/no_think" in system_message -%}
19+
{%- set reasoning_mode = "/no_think" -%}
20+
{%- elif "/think" in system_message -%}
21+
{%- set reasoning_mode = "/think" -%}
22+
{%- endif -%}
23+
{%- set custom_instructions = system_message.replace("/no_think", "") -%}
24+
{%- set custom_instructions = custom_instructions.replace("/think", "") -%}
25+
{%- set custom_instructions = custom_instructions.rstrip() -%}
26+
{%- endif -%}
27+
{{- "## Metadata\n\n" -}}
28+
{{- "Knowledge Cutoff Date: June 2025\n" -}}
29+
{{- "Reasoning Mode: " + reasoning_mode + "\n\n" -}}
30+
{{- "## Custom Instructions\n\n" -}}
31+
{%- if custom_instructions -%}
32+
{{- custom_instructions + "\n\n" -}}
33+
{%- elif reasoning_mode == "/think" -%}
34+
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracking, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> Thought section </think> Solution section. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion.\n\n" -}}
35+
{%- else -%}
36+
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face.\n\n" -}}
37+
{%- endif -%}
38+
{{- "<|im_end|>\n" -}}
39+
{# ───── main loop ───── #}
40+
{%- for message in messages -%}
41+
{%- set content = message.content if message.content is string else "" -%}
42+
{%- if message.role == "user" -%}
43+
{{ "<|im_start|>" + message.role + "\n" + content + "<|im_end|>\n" }}
44+
{%- elif message.role == "assistant" -%}
45+
{%- if reasoning_mode == "/think" -%}
46+
{{ "<|im_start|>assistant\n" + content.lstrip("\n") + "<|im_end|>\n" }}
47+
{%- else -%}
48+
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" + content.lstrip("\n") + "<|im_end|>\n" }}
49+
{%- endif -%}
50+
{%- elif message.role == "tool" -%}
51+
{{ "<|im_start|>" + "user\n" + content + "<|im_end|>\n" }}
52+
{%- endif -%}
53+
{%- endfor -%}
54+
{# ───── generation prompt ───── #}
55+
{%- if add_generation_prompt -%}
56+
{%- if reasoning_mode == "/think" -%}
57+
{{ "<|im_start|>assistant\n" }}
58+
{%- else -%}
59+
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" }}
60+
{%- endif -%}
61+
{%- endif -%}

tests/test-chat-auto-parser.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static void test_nemotron_tool_format(testing & t);
6262
static void test_cohere_reasoning_detection(testing & t);
6363
static void test_cohere_analysis(testing & t);
6464

65+
// SmolLM3 template analysis tests
66+
static void test_smollm3_analysis(testing & t);
67+
6568
// Marker separation
6669
static void test_marker_separation(testing & t);
6770

@@ -96,6 +99,7 @@ int main(int argc, char * argv[]) {
9699
t.test("seed_oss_diffs", test_seed_oss_tool_analysis);
97100
t.test("cohere", test_cohere_analysis);
98101
t.test("nemotron", test_nemotron_analysis);
102+
t.test("smollm3", test_smollm3_analysis);
99103
t.test("standard_json_tools", test_standard_json_tools_formats);
100104
t.test("normalize_quotes_to_json", test_normalize_quotes_to_json);
101105
t.test("tagged_args_embedded_quotes", test_tagged_args_with_embedded_quotes);
@@ -1448,6 +1452,47 @@ static void test_tool_format_cohere(testing & t) {
14481452
t.assert_true("tools_array_wrapped should be true", analysis.tools.format.tools_array_wrapped);
14491453
}
14501454

1455+
// ============================================================================
1456+
// SmolLM3 Template Analysis Tests
1457+
// Tests for templates that change system message when enable_thinking flips
1458+
// and prefill an empty <think></think> block in no-think mode.
1459+
// ============================================================================
1460+
static common_chat_template load_smollm3_template(testing & t) {
1461+
return load_template(t, "models/templates/HuggingFaceTB-SmolLM3-3B.jinja");
1462+
}
1463+
1464+
static void test_smollm3_reasoning_detection(testing & t);
1465+
1466+
static void test_smollm3_analysis(testing & t) {
1467+
t.test("SmolLM3 reasoning detection", test_smollm3_reasoning_detection);
1468+
}
1469+
1470+
static void test_smollm3_reasoning_detection(testing & t) {
1471+
common_chat_template tmpl = load_smollm3_template(t);
1472+
1473+
// Run differential analysis
1474+
struct autoparser analysis;
1475+
analysis.analyze_template(tmpl);
1476+
1477+
// SmolLM3 uses <think>/<think> reasoning tags.
1478+
// The template changes the entire system message when enable_thinking flips,
1479+
// so the analyzer must compare isolated generation prompts (not full outputs).
1480+
t.assert_equal("reasoning_start should be '<think>'", "<think>", analysis.reasoning.start);
1481+
t.assert_equal("reasoning_end should be '</think>'", "</think>", analysis.reasoning.end);
1482+
t.assert_equal("reasoning should be TAG_BASED", reasoning_mode::TAG_BASED, analysis.reasoning.mode);
1483+
1484+
// Content should remain plain (no wrappers)
1485+
t.assert_equal("content start should be empty", "", analysis.content.start);
1486+
t.assert_equal("content end should be empty", "", analysis.content.end);
1487+
t.assert_equal("content should be PLAIN", content_mode::PLAIN, analysis.content.mode);
1488+
1489+
// Preserved tokens should include the reasoning markers
1490+
bool has_think_start = std::find(analysis.preserved_tokens.begin(), analysis.preserved_tokens.end(), "<think>") != analysis.preserved_tokens.end();
1491+
bool has_think_end = std::find(analysis.preserved_tokens.begin(), analysis.preserved_tokens.end(), "</think>") != analysis.preserved_tokens.end();
1492+
t.assert_true("preserved_tokens should contain '<think>'", has_think_start);
1493+
t.assert_true("preserved_tokens should contain '</think>'", has_think_end);
1494+
}
1495+
14511496
// ============================================================================
14521497
// standard_json_tools Format Tests
14531498
// ============================================================================

0 commit comments

Comments
 (0)