feat: Add DeepSeek R1 and distilled model support#2131
Draft
ljluestc wants to merge 2 commits intoabetlen:mainfrom
Draft
feat: Add DeepSeek R1 and distilled model support#2131ljluestc wants to merge 2 commits intoabetlen:mainfrom
ljluestc wants to merge 2 commits intoabetlen:mainfrom
Conversation
- Update llama.cpp submodule to latest (b8184) for full DeepSeek R1/V2/V3 architecture support - Add 'deepseek-r1' chat format with correct special tokens (<|User|>, <|Assistant|>, <|begin▁of▁sentence|>, <|end▁of▁sentence|>) - Add 'deepseek-r1-distill-qwen' and 'deepseek-r1-distill-llama' chat format aliases for distilled model variants - Add DEEPSEEK_R1_CHAT_TEMPLATE constant from official HuggingFace tokenizer config - Update guess_chat_format_from_gguf_metadata() to auto-detect DeepSeek R1 models via template matching and heuristic token detection - Handle </think> reasoning content stripping for multi-turn conversations - Bump version to 0.3.17 Closes abetlen#1952
The format_deepseek_r1 function already includes the BOS token (<|begin▁of▁sentence|>) in the formatted prompt, but was not setting added_special=True in the ChatFormatterResponse. This caused chat_formatter_to_chat_completion_handler to pass add_bos=True to the tokenizer, resulting in a duplicate BOS token. Also adds comprehensive tests for: - Single-turn and multi-turn conversations - System message handling - </think> reasoning content stripping - Distilled model aliases (qwen/llama) - Auto-detection via exact match and heuristic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: Add DeepSeek R1 and distilled model support
Closes #1952
Summary
Adds full chat format support for DeepSeek R1, DeepSeek R1 Distill (Qwen), and DeepSeek R1 Distill (Llama) models. Updates the
llama.cppsubmodule to b8184 which includes native architecture support for DeepSeek R1/V2/V3.Problem
DeepSeek R1 and its distilled variants are among the most popular open-weight reasoning models, but
llama-cpp-pythoncurrently lacks both the inference backend support and the chat format handling required to run them correctly. Users attempting to load DeepSeek R1 GGUFs get incorrect prompt formatting, double BOS tokens, and missing architecture support at the C++ layer.Changes
llama_cpp/llama_chat_format.pyDEEPSEEK_R1_CHAT_TEMPLATEconstant sourced from the official HuggingFace tokenizer configDEEPSEEK_R1_BOS_TOKENandDEEPSEEK_R1_EOS_TOKENconstants using DeepSeek's fullwidth Unicode special tokens (\uff5c,\u2581)deepseek-r1— primary format with correct special token handling (<|User|>,<|Assistant|>,<|begin▁of▁sentence|>,<|end▁of▁sentence|>)deepseek-r1-distill-qwen— alias for Qwen-based distilled modelsdeepseek-r1-distill-llama— alias for Llama-based distilled modelsguess_chat_format_from_gguf_metadata()to auto-detect DeepSeek R1 models via:DEEPSEEK_R1_CHAT_TEMPLATE<|User|>/<|Assistant|>tokens in the chat template</think>reasoning content stripping in multi-turn conversations — prior assistant turns have their chain-of-thought reasoning removed to keep context cleanadded_special=Truein the formatter response to prevent double BOS token injection during tokenizationllama_cpp/__init__.py0.3.16→0.3.17vendor/llama.cpp3191462) which adds native DeepSeek R1/V2/V3 architecture support in the inference backendTesting
All 11 tests pass (2 existing + 9 new):