mmardani commited on
Commit
7eb33c7
·
verified ·
1 Parent(s): 786b7f0

Fix classifier-free guidance: isolate the unconditional branch, carry the raw conditional mean as self-conditioning, keep the readout unguided; ship the SFT tokenizer (chat control tokens, EOS <|im_end|>)

Browse files
generation_config.json CHANGED
@@ -1,4 +1,5 @@
1
  {
2
  "bos_token_id": 1,
3
- "eos_token_id": 2
 
4
  }
 
1
  {
2
  "bos_token_id": 1,
3
+ "eos_token_id": 11,
4
+ "_note": "SFT tokenizer: ids 10-17 are the chat control tokens; EOS is <|im_end|> (11), not </s> (2)."
5
  }
modeling_nemotron_labs_sigma.py CHANGED
@@ -444,7 +444,20 @@ class NemotronLabsSigmaModel(PreTrainedModel):
444
  x_selfcond,
445
  guidance_scale,
446
  ):
447
- """One denoiser evaluation. Returns fp32 (logits, x_reconst) and the NFE used."""
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  cfg = self.config
449
  b, block, ed = z.shape
450
  device = z.device
@@ -458,15 +471,19 @@ class NemotronLabsSigmaModel(PreTrainedModel):
458
  z_var = alpha_sq / ed + sigma_sq
459
  x_in = (z / z_var.sqrt().unsqueeze(-1)).float()
460
  h = F.linear(x_in, self.replaid_input_linear.weight.float())
 
461
  if cfg.replaid_selfcond and x_selfcond is not None:
462
  h = h + F.linear(
463
  x_selfcond.float() * math.sqrt(ed), self.replaid_selfcond_linear.weight.float()
464
  )
465
  if not cfg.replaid_disable_time_conditioning:
466
- h = h + F.linear(
467
  _gamma_embed(gamma_tok.float(), 64), self.replaid_gamma_linear.weight.float()
468
  )
 
 
469
  h = h.to(self._enc_dtype)
 
470
  storage_positions = context_len + torch.arange(block, device=device)
471
 
472
  def head(hidden, z_local):
@@ -492,12 +509,15 @@ class NemotronLabsSigmaModel(PreTrainedModel):
492
  logits = head(hidden, z)
493
  nfe = 1
494
 
 
 
495
  w = float(guidance_scale)
496
  if w != 1.0:
497
- # Unconditional pass: same inputs, no clean context (block-only,
498
- # bidirectional). RoPE is relative, so absolute offsets cancel.
 
499
  hidden_u = self.encoder(
500
- h,
501
  position_ids=block_position_ids,
502
  storage_positions=storage_positions,
503
  attn_mask=None,
@@ -506,13 +526,11 @@ class NemotronLabsSigmaModel(PreTrainedModel):
506
  )
507
  logits_u = head(hidden_u, z)
508
  nfe += 1
509
- x_c = F.softmax(logits, dim=-1) @ embedding_matrix
510
  x_u = F.softmax(logits_u, dim=-1) @ embedding_matrix
511
- x_reconst = x_u + w * (x_c - x_u)
512
- logits = logits_u + w * (logits - logits_u)
513
  else:
514
- x_reconst = F.softmax(logits, dim=-1) @ embedding_matrix
515
- return logits, x_reconst, nfe
516
 
517
  @staticmethod
518
  def _edm_sigma_gamma_grid(steps, gamma_0, gamma_1, rho, device):
@@ -529,8 +547,11 @@ class NemotronLabsSigmaModel(PreTrainedModel):
529
  def _log_snr_gamma_grid(steps, gamma_0, gamma_1, device):
530
  # Linear interpolation in VP gamma (negative log-SNR), from gamma_1
531
  # (noisy) to gamma_0 (clean); matches the internal 'log_snr' schedule.
 
 
532
  return torch.linspace(
533
- gamma_1.double(), gamma_0.double(), steps + 1, device=device
 
534
  )
535
 
536
  def forward(self, input_ids=None, **kwargs):
@@ -683,7 +704,7 @@ class NemotronLabsSigmaModel(PreTrainedModel):
683
 
684
  for i in range(steps + 1):
685
  gamma_t = gamma_grid[i : i + 1]
686
- logits, x_reconst, used = self._denoise_block(
687
  z, gamma_t, cache, key_valid, block_position_ids,
688
  embedding_matrix, x_selfcond, guidance_scale,
689
  )
@@ -701,7 +722,9 @@ class NemotronLabsSigmaModel(PreTrainedModel):
701
  generated += block
702
  break
703
 
704
- x_selfcond = x_reconst.detach().float()
 
 
705
 
706
  alpha_sq_t = torch.sigmoid(-gamma_t)
707
  sigma_t = torch.sigmoid(gamma_t).sqrt()
@@ -848,7 +871,7 @@ class NemotronLabsSigmaModel(PreTrainedModel):
848
  dtype=torch.float64, generator=generator)
849
  x_selfcond = None
850
  for i in range(diffusion_calls_per_round):
851
- logits, x_reconst, _ = self._denoise_block(
852
  z, grid[i : i + 1], cache, key_valid, block_position_ids,
853
  embedding_matrix, x_selfcond, 1.0,
854
  )
 
444
  x_selfcond,
445
  guidance_scale,
446
  ):
447
+ """One denoiser evaluation.
448
+
449
+ Returns ``(cond_logits, x_guided, cond_mean, nfe)`` in fp32:
450
+
451
+ * ``cond_logits`` - the CONDITIONAL logits. Guidance shapes the embedding
452
+ estimate that drives the transition, never the discrete readout.
453
+ * ``x_guided`` - the guided embedding estimate, for the transition only.
454
+ * ``cond_mean`` - the raw conditional mean, which is what gets carried
455
+ as self-conditioning. Guidance and score temperature are extrapolations
456
+ and must never be fed back.
457
+
458
+ The unconditional branch sees neither the clean context nor the
459
+ self-conditioning signal.
460
+ """
461
  cfg = self.config
462
  b, block, ed = z.shape
463
  device = z.device
 
471
  z_var = alpha_sq / ed + sigma_sq
472
  x_in = (z / z_var.sqrt().unsqueeze(-1)).float()
473
  h = F.linear(x_in, self.replaid_input_linear.weight.float())
474
+ h_uncond = h # captured BEFORE self-conditioning
475
  if cfg.replaid_selfcond and x_selfcond is not None:
476
  h = h + F.linear(
477
  x_selfcond.float() * math.sqrt(ed), self.replaid_selfcond_linear.weight.float()
478
  )
479
  if not cfg.replaid_disable_time_conditioning:
480
+ t_emb = F.linear(
481
  _gamma_embed(gamma_tok.float(), 64), self.replaid_gamma_linear.weight.float()
482
  )
483
+ h = h + t_emb
484
+ h_uncond = h_uncond + t_emb
485
  h = h.to(self._enc_dtype)
486
+ h_uncond = h_uncond.to(self._enc_dtype)
487
  storage_positions = context_len + torch.arange(block, device=device)
488
 
489
  def head(hidden, z_local):
 
509
  logits = head(hidden, z)
510
  nfe = 1
511
 
512
+ cond_mean = F.softmax(logits, dim=-1) @ embedding_matrix
513
+
514
  w = float(guidance_scale)
515
  if w != 1.0:
516
+ # Unconditional pass: no clean context (block-only, bidirectional)
517
+ # and no self-conditioning. RoPE is relative, so absolute offsets
518
+ # cancel.
519
  hidden_u = self.encoder(
520
+ h_uncond,
521
  position_ids=block_position_ids,
522
  storage_positions=storage_positions,
523
  attn_mask=None,
 
526
  )
527
  logits_u = head(hidden_u, z)
528
  nfe += 1
 
529
  x_u = F.softmax(logits_u, dim=-1) @ embedding_matrix
530
+ x_guided = x_u + w * (cond_mean - x_u)
 
531
  else:
532
+ x_guided = cond_mean
533
+ return logits, x_guided, cond_mean, nfe
534
 
535
  @staticmethod
536
  def _edm_sigma_gamma_grid(steps, gamma_0, gamma_1, rho, device):
 
547
  def _log_snr_gamma_grid(steps, gamma_0, gamma_1, device):
548
  # Linear interpolation in VP gamma (negative log-SNR), from gamma_1
549
  # (noisy) to gamma_0 (clean); matches the internal 'log_snr' schedule.
550
+ # The grid is built in fp64: linspace otherwise falls back to the default
551
+ # dtype and the step endpoints drift from the evaluation sampler's.
552
  return torch.linspace(
553
+ gamma_1.double(), gamma_0.double(), steps + 1,
554
+ device=device, dtype=torch.float64,
555
  )
556
 
557
  def forward(self, input_ids=None, **kwargs):
 
704
 
705
  for i in range(steps + 1):
706
  gamma_t = gamma_grid[i : i + 1]
707
+ logits, x_reconst, cond_mean, used = self._denoise_block(
708
  z, gamma_t, cache, key_valid, block_position_ids,
709
  embedding_matrix, x_selfcond, guidance_scale,
710
  )
 
722
  generated += block
723
  break
724
 
725
+ # Guidance and score temperature are extrapolations; the
726
+ # self-conditioning signal is the raw conditional mean.
727
+ x_selfcond = cond_mean.detach().float()
728
 
729
  alpha_sq_t = torch.sigmoid(-gamma_t)
730
  sigma_t = torch.sigmoid(gamma_t).sqrt()
 
871
  dtype=torch.float64, generator=generator)
872
  x_selfcond = None
873
  for i in range(diffusion_calls_per_round):
874
+ logits, x_reconst, _cond_mean, _ = self._denoise_block(
875
  z, grid[i : i + 1], cache, key_valid, block_position_ids,
876
  embedding_matrix, x_selfcond, 1.0,
877
  )
special_tokens_map.json CHANGED
@@ -7,7 +7,7 @@
7
  "single_word": false
8
  },
9
  "eos_token": {
10
- "content": "</s>",
11
  "lstrip": false,
12
  "normalized": false,
13
  "rstrip": false,
 
7
  "single_word": false
8
  },
9
  "eos_token": {
10
+ "content": "<|im_end|>",
11
  "lstrip": false,
12
  "normalized": false,
13
  "rstrip": false,
tokenizer.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3277c00fe5fb3963b3cb7c07b7f183722d2af4d775a4aea7cfb3684d7cccbc2f
3
- size 17078330
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c6021eb6847e682f89aa52d5eb6e8c7d902a23acfc8137e25211cf84828f1592
3
+ size 17077485
tokenizer_config.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "add_bos_token": true,
3
  "add_eos_token": false,
4
  "add_prefix_space": false,
5
  "added_tokens_decoder": {
@@ -84,7 +84,7 @@
84
  "special": true
85
  },
86
  "10": {
87
- "content": "<SPECIAL_10>",
88
  "lstrip": false,
89
  "normalized": false,
90
  "rstrip": false,
@@ -92,7 +92,7 @@
92
  "special": true
93
  },
94
  "11": {
95
- "content": "<SPECIAL_11>",
96
  "lstrip": false,
97
  "normalized": false,
98
  "rstrip": false,
@@ -100,52 +100,52 @@
100
  "special": true
101
  },
102
  "12": {
103
- "content": "<SPECIAL_12>",
104
  "lstrip": false,
105
  "normalized": false,
106
  "rstrip": false,
107
  "single_word": false,
108
- "special": true
109
  },
110
  "13": {
111
- "content": "<SPECIAL_13>",
112
  "lstrip": false,
113
  "normalized": false,
114
  "rstrip": false,
115
  "single_word": false,
116
- "special": true
117
  },
118
  "14": {
119
- "content": "<SPECIAL_14>",
120
  "lstrip": false,
121
  "normalized": false,
122
  "rstrip": false,
123
  "single_word": false,
124
- "special": true
125
  },
126
  "15": {
127
- "content": "<SPECIAL_15>",
128
  "lstrip": false,
129
  "normalized": false,
130
  "rstrip": false,
131
  "single_word": false,
132
- "special": true
133
  },
134
  "16": {
135
- "content": "<SPECIAL_16>",
136
  "lstrip": false,
137
  "normalized": false,
138
  "rstrip": false,
139
  "single_word": false,
140
- "special": true
141
  },
142
  "17": {
143
- "content": "<SPECIAL_17>",
144
  "lstrip": false,
145
  "normalized": false,
146
  "rstrip": false,
147
  "single_word": false,
148
- "special": true
149
  },
150
  "18": {
151
  "content": "<SPECIAL_18>",
@@ -8005,15 +8005,15 @@
8005
  }
8006
  },
8007
  "bos_token": "<s>",
8008
- "chat_template": "{{'<SPECIAL_10>System'}}{% for message in messages %}{% if message['role'] == 'system' %}{{'\n' + message['content'].strip()}}{% endif %}{% endfor %}{{'\n'}}{% for message in messages %}{% if message['role'] == 'user' %}{{ '\n<SPECIAL_11>User\n' + message['content'].strip() + '\n<SPECIAL_11>Assistant\n' }}{% elif message['role'] == 'assistant' %}{{ message['content'].strip() }}{% endif %}{% endfor %}",
8009
  "clean_up_tokenization_spaces": false,
8010
- "eos_token": "</s>",
8011
  "extra_special_tokens": {},
8012
  "model_input_names": [
8013
  "input_ids",
8014
  "attention_mask"
8015
  ],
8016
- "model_max_length": 8192,
8017
  "tokenizer_class": "PreTrainedTokenizerFast",
8018
- "unk_token": "<unk>"
 
8019
  }
 
1
  {
2
+ "add_bos_token": false,
3
  "add_eos_token": false,
4
  "add_prefix_space": false,
5
  "added_tokens_decoder": {
 
84
  "special": true
85
  },
86
  "10": {
87
+ "content": "<|im_start|>",
88
  "lstrip": false,
89
  "normalized": false,
90
  "rstrip": false,
 
92
  "special": true
93
  },
94
  "11": {
95
+ "content": "<|im_end|>",
96
  "lstrip": false,
97
  "normalized": false,
98
  "rstrip": false,
 
100
  "special": true
101
  },
102
  "12": {
103
+ "content": "<think>",
104
  "lstrip": false,
105
  "normalized": false,
106
  "rstrip": false,
107
  "single_word": false,
108
+ "special": false
109
  },
110
  "13": {
111
+ "content": "</think>",
112
  "lstrip": false,
113
  "normalized": false,
114
  "rstrip": false,
115
  "single_word": false,
116
+ "special": false
117
  },
118
  "14": {
119
+ "content": "<tool_call>",
120
  "lstrip": false,
121
  "normalized": false,
122
  "rstrip": false,
123
  "single_word": false,
124
+ "special": false
125
  },
126
  "15": {
127
+ "content": "</tool_call>",
128
  "lstrip": false,
129
  "normalized": false,
130
  "rstrip": false,
131
  "single_word": false,
132
+ "special": false
133
  },
134
  "16": {
135
+ "content": "<tool_response>",
136
  "lstrip": false,
137
  "normalized": false,
138
  "rstrip": false,
139
  "single_word": false,
140
+ "special": false
141
  },
142
  "17": {
143
+ "content": "</tool_response>",
144
  "lstrip": false,
145
  "normalized": false,
146
  "rstrip": false,
147
  "single_word": false,
148
+ "special": false
149
  },
150
  "18": {
151
  "content": "<SPECIAL_18>",
 
8005
  }
8006
  },
8007
  "bos_token": "<s>",
 
8008
  "clean_up_tokenization_spaces": false,
8009
+ "eos_token": "<|im_end|>",
8010
  "extra_special_tokens": {},
8011
  "model_input_names": [
8012
  "input_ids",
8013
  "attention_mask"
8014
  ],
8015
+ "model_max_length": 262144,
8016
  "tokenizer_class": "PreTrainedTokenizerFast",
8017
+ "unk_token": "<unk>",
8018
+ "chat_template": "{% macro render_extra_keys(json_dict, handled_keys) %}\n {%- if json_dict is mapping %}\n {%- for json_key in json_dict if json_key not in handled_keys %}\n {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}\n {{- '\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}\n {%- else %}\n {{-'\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}\n {%- endif %}\n {%- endfor %}\n {%- endif %}\n{% endmacro %}\n{%- set enable_thinking = enable_thinking if enable_thinking is defined else True %}\n{%- set truncate_history_thinking = truncate_history_thinking if truncate_history_thinking is defined else True %}\n\n{%- set ns = namespace(last_user_idx = -1) %}\n{%- set loop_messages = messages %}\n{%- for m in loop_messages %}\n {%- if m[\"role\"] == \"user\" %}\n {%- set ns.last_user_idx = loop.index0 %}\n {%- endif %}\n{%- endfor %}\n\n{%- if messages[0][\"role\"] == \"system\" %}\n {%- set system_message = messages[0][\"content\"] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set system_message = \"\" %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- if not tools is defined %}\n {%- set tools = [] %}\n{%- endif %}\n{# Recompute last_user_idx relative to loop_messages after handling system #}\n{%- set ns = namespace(last_user_idx = -1) %}\n{%- for m in loop_messages %}\n {%- if m[\"role\"] == \"user\" %}\n {%- set ns.last_user_idx = loop.index0 %}\n {%- endif %}\n{%- endfor %}\n{%- if system_message is defined %}\n {{- \"<|im_start|>system\\n\" + system_message }}\n{%- else %}\n {%- if tools is iterable and tools | length > 0 %}\n {{- \"<|im_start|>system\\n\" }}\n {%- endif %}\n{%- endif %}\n{%- if tools is iterable and tools | length > 0 %}\n {%- if system_message is defined and system_message | length > 0 %}\n {{- \"\\n\\n\" }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou have access to the following functions:\\n\\n\" }}\n {{- \"<tools>\" }}\n {%- for tool in tools %}\n {%- if tool.function is defined %}\n {%- set tool = tool.function %}\n {%- endif %}\n {{- \"\\n<function>\\n<name>\" ~ tool.name ~ \"</name>\" }}\n {%- if tool.description is defined %}\n {{- '\\n<description>' ~ (tool.description | trim) ~ '</description>' }}\n {%- endif %}\n {{- '\\n<parameters>' }}\n {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}\n {%- for param_name, param_fields in tool.parameters.properties|items %}\n {{- '\\n<parameter>' }}\n {{- '\\n<name>' ~ param_name ~ '</name>' }}\n {%- if param_fields.type is defined %}\n {{- '\\n<type>' ~ (param_fields.type | string) ~ '</type>' }}\n {%- endif %}\n {%- if param_fields.description is defined %}\n {{- '\\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}\n {%- endif %}\n {%- if param_fields.enum is defined %}\n {{- '\\n<enum>' ~ (param_fields.enum | tojson | safe) ~ '</enum>' }}\n {%- endif %}\n {%- set handled_keys = ['name', 'type', 'description', 'enum'] %}\n {{- render_extra_keys(param_fields, handled_keys) }}\n {{- '\\n</parameter>' }}\n {%- endfor %}\n {%- endif %}\n {% set handled_keys = ['type', 'properties', 'required'] %}\n {{- render_extra_keys(tool.parameters, handled_keys) }}\n {%- if tool.parameters is defined and tool.parameters.required is defined %}\n {{- '\\n<required>' ~ (tool.parameters.required | tojson | safe) ~ '</required>' }}\n {%- endif %}\n {{- '\\n</parameters>' }}\n {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}\n {{- render_extra_keys(tool, handled_keys) }}\n {{- '\\n</function>' }}\n {%- endfor %}\n {{- \"\\n</tools>\" }}\n\n {{- '\\n\\nIf you choose to call a function ONLY reply in the following format with NO suffix:\\n\\n<tool_call>\\n<function=example_function_name>\\n<parameter=example_parameter_1>\\nvalue_1\\n</parameter>\\n<parameter=example_parameter_2>\\nThis is the value for the second parameter\\nthat can span\\nmultiple lines\\n</parameter>\\n</function>\\n</tool_call>\\n\\n<IMPORTANT>\\nReminder:\\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\\n- Required parameters MUST be specified\\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\\n</IMPORTANT>' }}\n{%- endif %}\n\n\n{%- if system_message is defined %}\n {{- '<|im_end|>\\n' }}\n{%- else %}\n {%- if tools is iterable and tools | length > 0 %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n\n{%- for message in loop_messages %}\n {%- if message.role == \"assistant\" %}\n {# Add reasoning content in to content field for unified processing below. #}\n {%- if message.reasoning_content is defined and message.reasoning_content is string and message.reasoning_content | trim | length > 0 %}\n {%- set content = \"<think>\\n\" ~ message.reasoning_content ~ \"\\n</think>\\n\" ~ (message.content | default('', true)) %}\n {%- else %}\n {%- set content = message.content | default('', true) %}\n {%- if content is string -%}\n {# Allow downstream logic to to take care of broken thought, only handle coherent reasoning here. #}\n {%- if '<think>' not in content and '</think>' not in content -%}\n {%- set content = \"<think></think>\" ~ content -%}\n {%- endif -%}\n {%- else -%}\n {%- set content = content -%}\n {%- endif -%}\n {%- endif %}\n {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}\n {# Assistant message has tool calls. #}\n {{- '<|im_start|>assistant\\n' }}\n {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}\n {%- if content is string and content | trim | length > 0 %}\n {%- if include_content %}\n {{- (content | trim) ~ '\\n' -}}\n {%- else %}\n {%- set c = (content | string) %}\n {%- if '</think>' in c %}\n {# Keep only content after the last closing think. Also generation prompt causes this. #}\n {%- set c = c.split('</think>')[-1] %}\n {%- elif '<think>' in c %}\n {# If <think> was opened but never closed, drop the trailing think segment #}\n {%- set c = c.split('<think>')[0] %}\n {%- endif %}\n {%- set c = \"<think></think>\" ~ c | trim %}\n {%- if c | length > 0 %}\n {{- c ~ '\\n' -}}\n {%- endif %}\n {%- endif %}\n {%- else %}\n {{- \"<think></think>\" -}}\n {%- endif %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n<function=' ~ tool_call.name ~ '>\\n' -}}\n {%- if tool_call.arguments is defined %}\n {%- for args_name, args_value in tool_call.arguments|items %}\n {{- '<parameter=' ~ args_name ~ '>\\n' -}}\n {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}\n {{- args_value ~ '\\n</parameter>\\n' -}}\n {%- endfor %}\n {%- endif %}\n {{- '</function>\\n</tool_call>\\n' -}}\n {%- endfor %}\n {{- '<|im_end|>\\n' }}\n {%- else %}\n {# Assistant message doesn't have tool calls. #}\n {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}\n {{- '<|im_start|>assistant\\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\\n' }}\n {%- else %}\n {%- set c = (content | default('', true) | string) %}\n {%- if '<think>' in c and '</think>' in c %}\n {%- set c = \"<think></think>\" ~ c.split('</think>')[-1] %}\n {%- endif %}\n {%- set c = c | trim %}\n {%- if c | length > 0 %}\n {{- '<|im_start|>assistant\\n' ~ c ~ '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>assistant\\n<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n {%- endif %}\n {%- elif message.role == \"user\" or message.role == \"system\" %}\n {{- '<|im_start|>' + message.role + '\\n' }}\n {%- set content = message.content | string %}\n {{- content }}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.previtem and loop.previtem.role != \"tool\" %}\n {{- '<|im_start|>user\\n' }}\n {%- endif %}\n {{- '<tool_response>\\n' }}\n {{- message.content }}\n {{- '\\n</tool_response>\\n' }}\n {%- if not loop.last and loop.nextitem.role != \"tool\" %}\n {{- '<|im_end|>\\n' }}\n {%- elif loop.last %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endfor %}\n\n{%- if add_generation_prompt %}\n {%- if enable_thinking %}\n {{- '<|im_start|>assistant\\n<think>\\n' }}\n {%- else %}\n {{- '<|im_start|>assistant\\n<think></think>' }}\n {%- endif %}\n{%- endif %}\n"
8019
  }