feat(deepseek): split into separate DeepSeek-V3 and DeepSeek-R1 renderers (#81)
Publish / build (push) Canceled after 0s
Publish / publish (push) Canceled after 0s

This commit is contained in:
hallerite
2026-06-09 17:03:22 -07:00
committed by GitHub
parent 1dca3b9a2d
commit d80f36e287
9 changed files with 295 additions and 45 deletions
+4
View File
@@ -44,6 +44,7 @@ from renderers.configs import (
BaseRendererConfig,
config_from_name,
DefaultRendererConfig,
DeepSeekR1RendererConfig,
DeepSeekV3RendererConfig,
GLM45RendererConfig,
GLM51RendererConfig,
@@ -74,6 +75,7 @@ from renderers.configs import (
# imports — ``renderers.base._populate_registry`` lazy-imports the
# concrete classes itself when a renderer is instantiated.
_LAZY_RENDERERS: dict[str, str] = {
"DeepSeekR1Renderer": "renderers.deepseek_r1",
"DeepSeekV3Renderer": "renderers.deepseek_v3",
"DefaultRenderer": "renderers.default",
"GLM45Renderer": "renderers.glm45",
@@ -113,6 +115,8 @@ __all__ = [
"BaseRendererConfig",
"Content",
"ContentPart",
"DeepSeekR1Renderer",
"DeepSeekR1RendererConfig",
"DeepSeekV3Renderer",
"DeepSeekV3RendererConfig",
"DefaultRenderer",
+8 -1
View File
@@ -1030,9 +1030,12 @@ MODEL_RENDERER_MAP: dict[str, str] = {
# MiniMax.
"MiniMaxAI/MiniMax-M2": "minimax-m2",
"MiniMaxAI/MiniMax-M2.5": "minimax-m2",
# DeepSeek V3.
# DeepSeek V3 (non-reasoning).
"deepseek-ai/DeepSeek-V3": "deepseek-v3",
"deepseek-ai/DeepSeek-V3-Base": "deepseek-v3",
# DeepSeek R1 (reasoning).
"deepseek-ai/DeepSeek-R1": "deepseek-r1",
"deepseek-ai/DeepSeek-R1-0528": "deepseek-r1",
# Kimi K2 (K2.5 and K2.6 share the K2.5 template, distinct from K2).
"moonshotai/Kimi-K2-Instruct": "kimi-k2",
"moonshotai/Kimi-K2.5": "kimi-k2.5",
@@ -1161,6 +1164,8 @@ FASTOKENS_INCOMPATIBLE: frozenset[str] = frozenset(
# doesn't yet implement.
"deepseek-ai/DeepSeek-V3",
"deepseek-ai/DeepSeek-V3-Base",
"deepseek-ai/DeepSeek-R1",
"deepseek-ai/DeepSeek-R1-0528",
}
)
@@ -1334,6 +1339,7 @@ def load_tokenizer(
def _populate_registry():
if RENDERER_REGISTRY:
return
from renderers.deepseek_r1 import DeepSeekR1Renderer
from renderers.deepseek_v3 import DeepSeekV3Renderer
from renderers.default import DefaultRenderer
from renderers.glm5 import GLM5Renderer, GLM51Renderer
@@ -1362,6 +1368,7 @@ def _populate_registry():
"glm-4.5": GLM45Renderer,
"minimax-m2": MiniMaxM2Renderer,
"deepseek-v3": DeepSeekV3Renderer,
"deepseek-r1": DeepSeekR1Renderer,
"kimi-k2": KimiK2Renderer,
"kimi-k2.5": KimiK25Renderer,
"laguna-xs.2": LagunaXS2Renderer,
+21 -12
View File
@@ -400,24 +400,30 @@ class Nemotron3RendererConfig(BaseRendererConfig):
class DeepSeekV3RendererConfig(BaseRendererConfig):
"""DeepSeek V3 renderer config.
"""DeepSeek-V3 renderer config (non-reasoning).
``enable_thinking`` is renderer-internal here — DeepSeek-V3's chat
template does not reference any thinking variable, so passing it to
``apply_chat_template`` upstream is a no-op. The renderer uses it
to control the ``<think>`` prefill at the generation prompt (R1
distill convention).
DeepSeek-V3 has no thinking concept: the generation prompt is a bare
``<|Assistant|>`` and assistant content is emitted verbatim. For the
reasoning variant use :class:`DeepSeekR1RendererConfig`.
"""
name: Literal["deepseek-v3"] = "deepseek-v3"
enable_thinking: bool = True
"""Renderer convention for the R1-distill family: when ``True``,
prefill ``<think>`` at the generation prompt. The DeepSeek-V3 Jinja
template ignores this kwarg upstream; it's not a chat-template
kwarg in the strict sense."""
_internal_fields = frozenset({"enable_thinking"})
class DeepSeekR1RendererConfig(BaseRendererConfig):
"""DeepSeek-R1 renderer config (reasoning).
R1 always reasons — its chat template unconditionally prefills
``<think>\\n`` at the generation prompt and strips ``</think>`` from
historical assistant turns. There is therefore no ``enable_thinking``
knob (thinking is not optional), and ``preserve_*`` flags are no-ops
(history reasoning is always dropped); both stored for protocol
uniformity. Applies to full ``deepseek-ai/DeepSeek-R1`` / ``-R1-0528``
— NOT the R1-Distill-Qwen/Llama models, which use those base
tokenizers and route to the Qwen3 / Llama-3 renderers.
"""
name: Literal["deepseek-r1"] = "deepseek-r1"
RendererConfig = Annotated[
@@ -439,6 +445,7 @@ RendererConfig = Annotated[
MiniMaxM2RendererConfig,
Nemotron3RendererConfig,
DeepSeekV3RendererConfig,
DeepSeekR1RendererConfig,
],
Field(discriminator="name"),
]
@@ -474,6 +481,7 @@ _CONFIG_BY_NAME: dict[str, type[BaseRendererConfig]] = {
"minimax-m2": MiniMaxM2RendererConfig,
"nemotron-3": Nemotron3RendererConfig,
"deepseek-v3": DeepSeekV3RendererConfig,
"deepseek-r1": DeepSeekR1RendererConfig,
}
@@ -505,6 +513,7 @@ __all__ = [
"AutoRendererConfig",
"BaseRendererConfig",
"DefaultRendererConfig",
"DeepSeekR1RendererConfig",
"DeepSeekV3RendererConfig",
"GLM45RendererConfig",
"GLM51RendererConfig",
+58
View File
@@ -0,0 +1,58 @@
"""DeepSeek-R1 Renderer — the reasoning variant of the DeepSeek format.
R1 shares DeepSeek-V3's special tokens, message structure, and tool-call
wire format, so it subclasses :class:`renderers.deepseek_v3.DeepSeekV3Renderer`
and overrides only the two places its chat template diverges:
1. Generation prompt — R1 unconditionally prefills ``<think>\\n``
(``<|Assistant|><think>\\n``) to trigger reasoning, where V3 emits a bare
``<|Assistant|>``. Handled by ``_GEN_THINK_PREFILL``.
2. Historical assistant turns — R1 strips the reasoning trace, keeping only
the text after ``</think>`` (``content.split('</think>')[-1]``), where V3
emits content verbatim. Handled by ``_prepare_assistant_content``.
Everything else — system handling, tool-call / tool-output rendering,
special-token resolution, and ``parse_response`` (``parse_deepseek_v3``,
shared) — is inherited unchanged.
Scope: full ``deepseek-ai/DeepSeek-R1`` and ``-R1-0528``. The R1-Distill
models (``DeepSeek-R1-Distill-Qwen/Llama``) use their base models'
tokenizers and route to the Qwen3 / Llama-3 renderers, not this one.
"""
from __future__ import annotations
from renderers.base import Message
from renderers.configs import DeepSeekR1RendererConfig
from renderers.deepseek_v3 import DeepSeekV3Renderer
class DeepSeekR1Renderer(DeepSeekV3Renderer):
"""Deterministic message → token renderer for DeepSeek-R1 models."""
_config_cls: type = DeepSeekR1RendererConfig
_GEN_THINK_PREFILL: str = "<think>\n"
def _prepare_assistant_content(self, msg: Message) -> str:
"""Assistant content with the reasoning trace stripped, mirroring the
R1 template's ``content.split('</think>')[-1]`` on historical turns.
Structured ``thinking``/``text`` parts are reconstructed inline first
so the same ``</think>`` split applies. The separate
``reasoning_content`` field is ignored — the R1 chat template never
reads it, and history reasoning is dropped regardless.
"""
content = msg.get("content") or ""
if isinstance(content, list):
parts: list[str] = []
for p in content:
if not isinstance(p, dict):
continue
if p.get("type") == "thinking":
parts.append(f"<think>{p.get('thinking', '')}</think>")
elif p.get("type") == "text":
parts.append(p.get("text", ""))
content = "".join(parts)
if "</think>" in content:
content = content.split("</think>")[-1]
return content
+39 -32
View File
@@ -41,25 +41,30 @@ def _ds_token(name: str) -> str:
class DeepSeekV3Renderer:
"""Deterministic message → token renderer for DeepSeek V3 models.
"""Deterministic message → token renderer for DeepSeek-V3 models.
DeepSeek-V3's chat template does not consult any thinking-related
variable; the ``enable_thinking`` field on the typed config controls
the renderer's ``<think>\\n`` prefill at the generation prompt
(R1-distill convention) and is intentionally not forwarded to
``apply_chat_template`` upstream — that would be a no-op. The
template also always emits ``<think>{reasoning}</think>`` when
``reasoning_content`` is provided, so ``preserve_*`` flags are
no-ops here too; stored for protocol uniformity.
DeepSeek-V3 is non-reasoning: its chat template has no ``<think>``
concept — the generation prompt is a bare ``<|Assistant|>`` and past
assistant content is emitted verbatim. The reasoning variant
(``<think>``-prefilled prompt, history reasoning stripped) lives in
:class:`renderers.deepseek_r1.DeepSeekR1Renderer`, which subclasses
this one. ``preserve_*`` flags are no-ops here (no reasoning channel),
stored for protocol uniformity.
"""
#: Default typed config; the R1 subclass overrides this.
_config_cls: type = DeepSeekV3RendererConfig
#: Generation-prompt reasoning prefill. Empty for V3 (bare
#: ``<|Assistant|>``); the R1 subclass overrides to ``"<think>\n"``.
_GEN_THINK_PREFILL: str = ""
def __init__(
self,
tokenizer: PreTrainedTokenizer,
config: DeepSeekV3RendererConfig | None = None,
):
self._tokenizer = tokenizer
self.config = config or DeepSeekV3RendererConfig()
self.config = config or type(self)._config_cls()
# ── BOS / EOS ────────────────────────────────────────────────
self._bos = self._get_special_token(f"begin{_US}of{_US}sentence")
@@ -239,8 +244,10 @@ class DeepSeekV3Renderer:
emit_special(
self._assistant_token, -1, is_sampled=False, is_content=False
)
if self.config.enable_thinking:
emit_text("<think>\n", -1, is_sampled=False, is_content=False)
if self._GEN_THINK_PREFILL:
emit_text(
self._GEN_THINK_PREFILL, -1, is_sampled=False, is_content=False
)
return RenderedTokens(
token_ids=tokens,
@@ -382,8 +389,8 @@ class DeepSeekV3Renderer:
last_role = new_messages[-1].get("role") if new_messages else None
if last_role != "tool":
emit_special(self._assistant_token, -1)
if self.config.enable_thinking:
emit_text("<think>\n", -1)
if self._GEN_THINK_PREFILL:
emit_text(self._GEN_THINK_PREFILL, -1)
total_len = len(previous_ids) + len(ext)
return RenderedTokens(
@@ -399,6 +406,23 @@ class DeepSeekV3Renderer:
# Assistant rendering
# ------------------------------------------------------------------
def _prepare_assistant_content(self, msg: Message) -> str:
"""Assistant content as the V3 template would emit it: verbatim.
V3 is non-reasoning — its template emits ``message['content']`` as-is
and never reads ``reasoning_content``. A structured content list is
flattened to its ``text`` parts. The R1 subclass overrides this to
strip ``</think>`` from history.
"""
content = msg.get("content") or ""
if isinstance(content, list):
content = "".join(
p.get("text", "")
for p in content
if isinstance(p, dict) and p.get("type") == "text"
)
return content
def _render_assistant(
self,
msg: Message,
@@ -414,24 +438,7 @@ class DeepSeekV3Renderer:
# without a new <|Assistant|> token in that case.
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
content = msg.get("content") or ""
# Support structured content (ThinkingPart / TextPart list).
if isinstance(content, list):
parts_text: list[str] = []
for p in content:
if not isinstance(p, dict):
continue
if p.get("type") == "thinking":
thinking = p.get("thinking", "")
parts_text.append(f"<think>{thinking}</think>")
elif p.get("type") == "text":
parts_text.append(p.get("text", ""))
content = "".join(parts_text)
# Also accept reasoning_content stored separately (OpenAI-style).
elif isinstance(msg.get("reasoning_content"), str) and msg["reasoning_content"]:
reasoning = msg["reasoning_content"]
content = f"<think>{reasoning}</think>{content}"
content = self._prepare_assistant_content(msg)
tool_calls = msg.get("tool_calls") or []
# ``<|Assistant|>`` is template-injected scaffolding — at
+10
View File
@@ -36,6 +36,16 @@ RENDERER_MODELS = [
# Ultra resolves the Ultra template variant via name (auto → ultra=True).
("nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16", "auto"),
("poolside/Laguna-XS.2", "auto"),
# DeepSeek-V3/R1 are intentionally NOT in this shared barrage: their
# chat templates can't render the barrage's tool-call fixtures (the
# templates require ``tool['type']`` and a string-serialized
# ``arguments``, and V3 only renders tool_calls when content is None —
# so ``apply_chat_template`` raises or drops the calls on the shared
# shapes), and the is_content body-recovery checks hit a Metaspace
# subset-decode artifact. The renderer is correct in all these cases;
# there's just no byte-output to parity-check against. Split-specific
# parity (V3 bare prompt vs R1 <think>+history-strip) is covered in
# tests/test_deepseek_r1.py.
# Llama-3 loads via the unrestricted unsloth mirror (byte-identical
# chat template) so CI needs no Meta-gated HF token. Pinned to the
# explicit "llama-3" config because the mirror name isn't in
+152
View File
@@ -0,0 +1,152 @@
"""DeepSeek-R1 renderer: the reasoning variant of the DeepSeek format.
General byte-parity vs ``apply_chat_template`` is covered by the conftest
barrage (``test_render_ids`` now includes both DeepSeek models). These tests
pin the behaviors that distinguish R1 from V3: the ``<think>`` generation
prompt and the stripping of ``</think>`` from historical assistant turns.
"""
from functools import lru_cache
import pytest
from renderers import (
DeepSeekR1Renderer,
DeepSeekV3Renderer,
create_renderer,
)
from renderers.base import load_tokenizer
@lru_cache
def _r1():
tok = load_tokenizer("deepseek-ai/DeepSeek-R1")
return tok, create_renderer(tok)
@lru_cache
def _v3():
tok = load_tokenizer("deepseek-ai/DeepSeek-V3")
return tok, create_renderer(tok)
# Baseline render_ids == apply_chat_template parity. Tool-cycle shapes are
# intentionally excluded: the DeepSeek template renders tool_calls only when
# content is None (a pre-existing renderer↔template gap, tracked separately),
# which is orthogonal to the V3/R1 reasoning split this module covers.
_PARITY_SHAPES = [
(
"single_turn",
[
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": "4"},
],
{},
),
(
"multi_turn",
[
{"role": "user", "content": "A"},
{"role": "assistant", "content": "B"},
{"role": "user", "content": "C"},
{"role": "assistant", "content": "D"},
],
{},
),
(
"reasoning_content_field",
[
{"role": "user", "content": "x"},
{"role": "assistant", "reasoning_content": "r", "content": "4"},
],
{},
),
(
"gen_prompt",
[
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hi"},
],
{"add_generation_prompt": True},
),
(
"inline_think_history",
[
{"role": "user", "content": "q"},
{"role": "assistant", "content": "<think>reasoning</think>answer"},
{"role": "user", "content": "q2"},
],
{},
),
]
@pytest.mark.parametrize("loader", [_v3, _r1], ids=["v3", "r1"])
@pytest.mark.parametrize(
"shape_id,messages,kwargs", _PARITY_SHAPES, ids=[s[0] for s in _PARITY_SHAPES]
)
def test_render_ids_matches_apply_chat_template(loader, shape_id, messages, kwargs):
tok, renderer = loader()
got = renderer.render_ids(messages, **kwargs)
expected = list(
tok.apply_chat_template(messages, tokenize=True, return_dict=False, **kwargs)
)
assert got == expected
def test_auto_detection_picks_the_right_renderer():
_, r1 = _r1()
_, v3 = _v3()
assert isinstance(r1, DeepSeekR1Renderer)
assert isinstance(v3, DeepSeekV3Renderer)
def test_generation_prompt_differs():
"""R1 prefills ``<think>`` to trigger reasoning; V3 does not."""
msgs = [{"role": "user", "content": "hi"}]
tr1, r1 = _r1()
tv3, v3 = _v3()
r1_text = tr1.decode(r1.render_ids(msgs, add_generation_prompt=True))
v3_text = tv3.decode(v3.render_ids(msgs, add_generation_prompt=True))
# R1 prefills <think> to trigger reasoning; V3 emits a bare assistant
# turn. (Exact byte parity vs apply_chat_template is the gen_prompt case
# in the parity matrix above; here we just pin the V3/R1 distinction —
# decode trims the trailing "\n" so we don't match on it.)
assert "<think>" in r1_text and r1_text.rstrip().endswith("<think>")
assert "<think>" not in v3_text
def test_r1_strips_reasoning_from_history():
"""A historical assistant turn carrying an inline ``<think>…</think>``
trace renders only the post-``</think>`` answer, byte-identical to the R1
chat template's ``content.split('</think>')[-1]``.
"""
tok, r1 = _r1()
msgs = [
{"role": "user", "content": "q"},
{"role": "assistant", "content": "<think>private reasoning</think>The answer."},
{"role": "user", "content": "q2"},
]
got = r1.render_ids(msgs)
expected = list(tok.apply_chat_template(msgs, tokenize=True, return_dict=False))
assert got == expected
# Reasoning must not survive into the rendered history.
assert "private reasoning" not in tok.decode(got)
def test_v3_emits_content_verbatim_ignoring_reasoning():
"""V3 (non-reasoning) ignores ``reasoning_content`` — matching its
template, which only reads ``content``."""
tok, v3 = _v3()
msgs = [
{"role": "user", "content": "x"},
{"role": "assistant", "reasoning_content": "should be ignored", "content": "4"},
]
got = v3.render_ids(msgs)
expected = list(tok.apply_chat_template(msgs, tokenize=True, return_dict=False))
assert got == expected
assert "should be ignored" not in tok.decode(got)
+2
View File
@@ -46,6 +46,8 @@ def test_fastokens_incompatible_is_explicit_set():
{
"deepseek-ai/DeepSeek-V3",
"deepseek-ai/DeepSeek-V3-Base",
"deepseek-ai/DeepSeek-R1",
"deepseek-ai/DeepSeek-R1-0528",
}
)
+1
View File
@@ -54,6 +54,7 @@ _RENDERER_MODELS = [
("moonshotai/Kimi-K2.5", "auto"),
("moonshotai/Kimi-K2.6", "auto"),
("deepseek-ai/DeepSeek-V3", "auto"),
("deepseek-ai/DeepSeek-R1", "auto"),
("nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16", "auto"),
# Ultra: auto-resolves to the Ultra template variant (ultra=True) via the
# model name; parity asserted against the Ultra apply_chat_template.