mirror of
https://github.com/unslothai/unsloth.git
synced 2026-08-23 15:53:46 +00:00
* Escape caller template text spliced into Jinja string literals
construct_chat_template builds the HF Jinja template by concatenating the
caller's template text straight into '...' literals, in three places: the
process() helper, the add_generation_prompt literal, and full_system. None
of them escape, so Jinja reads the text as template source.
A single quote closes the literal early:
default_system_message = "Answer the user's question."
-> TemplateSyntaxError: expected token 'end of print statement', got 's'
and a backslash is decoded as a Jinja escape, which is silent:
default_system_message = r"Put the answer in \boxed{}."
-> 'Put the answer in \x08oxed{}.\n### User: Hi\n'
default_system_message = r"Files live in C:\Users\me"
-> TemplateSyntaxError: truncated \UXXXXXXXX escape
The backslash case is the worse one: no error, no warning, and every
formatted training sample is built with a backspace character where
\boxed was meant to be.
It is not limited to the system message. process() handles the
instruction and response sections too, so an apostrophe anywhere in the
template breaks it, for example "### User's turn: {INPUT}".
Escape backslashes then single quotes in each literal chunk. In process()
the text is split on the {INPUT}/{OUTPUT}/{SYSTEM} sentinel first, so the
' + message['content'] + ' concatenation markers it inserts are not
escaped along with it.
The Ollama modelfile splices default_system_message into a double-quoted
SYSTEM line with the same lack of escaping; that is a different format
with different rules and is left alone here.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Escape \r and strip the BOS before escaping in construct_chat_template
Two follow ups to the Jinja literal escaping in this PR.
Jinja rewrites a raw carriage return to \n inside a string literal before it
unescapes it, so a template authored on Windows loses its CRLF. Escaping \r
alongside \ and ' makes the round trip exact, and makes the generated template
render identically under jinja2, minja and llama.cpp's Jinja engine.
The BOS was stripped from the system section after process() had already
escaped it, so a bos_token holding a quote or a backslash no longer matched and
was left in the literal, then emitted a second time alongside {{ bos_token }}.
Strip it while the text is still raw.
* Tighten the comments around the Jinja literal escaping
* Trim the escaping comments further
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: danielhanchen <danielhanchen@gmail.com>
|
||
|---|---|---|
| .. | ||
| __init__.py | ||
| conftest.py | ||
| test_bitsandbytes_kernel_readiness.py | ||
| test_change_system_message.py | ||
| test_conftest_bitsandbytes_preimport.py | ||
| test_construct_chat_template_validation.py | ||
| test_cpo_processor_text_tokenizer.py | ||
| test_cross_platform_parity.py | ||
| test_dpo_vision_processor_passthrough.py | ||
| test_e2e_no_torch_sandbox.py | ||
| test_fast_language_model_text_only.py | ||
| test_fast_model_config_passthrough.py | ||
| test_fast_sentence_transformer_embedding_parity.py | ||
| test_fast_sentence_transformer_redirect_lifecycle.py | ||
| test_flash_attn_install_python_stack.py | ||
| test_get_lora_parameters_bias_fp8_block_size.py | ||
| test_get_lora_parameters_fp8_block_size.py | ||
| test_gpu_init_ldconfig_guard.py | ||
| test_grpo_ddp_model_config.py | ||
| test_import_without_bitsandbytes.py | ||
| test_install_python_stack.py | ||
| test_install_uv_override_space.py | ||
| test_mlx_public_trainer_api.py | ||
| test_no_torch_filtering.py | ||
| test_orpo_processor_text_tokenizer.py | ||
| test_pad_token_fix.py | ||
| test_patch_trl_rl_trainers_defensive.py | ||
| test_remove_special_tokens_no_bos.py | ||
| test_studio_import_no_torch.py | ||
| test_to_sharegpt_optional_none.py | ||
| test_tokenizers_and_torch_constraint.py | ||
| test_torchcodec_torch_compat.py | ||
| test_unsloth_run_tool_policy_resolver.py | ||
| test_v100_fullft_precision.py | ||
| test_vision_lora_targeting.py | ||
| test_windows_arm64_python_choice.py | ||
| test_windows_git_gate.py | ||
| test_windows_no_torch_setup.py | ||
| test_windows_vcredist_download_tls.py | ||