Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1831bcc50c | ||
|
|
3ae276c446 | ||
|
|
1691f87e90 | ||
|
|
9e11214dd5 | ||
|
|
143ffdee56 | ||
|
|
f9414b1e95 | ||
|
|
ee9703fcc9 | ||
|
|
3177399b79 | ||
|
|
8704f9d502 | ||
|
|
fdc03f2861 | ||
|
|
d547eef54c | ||
|
|
25ba413d46 |
@@ -0,0 +1,104 @@
|
||||
name: Publish Dev
|
||||
|
||||
# Tag every commit on main as ``renderers-v<next>.dev<N>`` and publish the
|
||||
# wheel to PyPI as a pre-release. ``<next>`` is the latest release tag with
|
||||
# its patch bumped; ``<N>`` is the number of commits since that release so
|
||||
# each main commit maps to a unique PEP 440 dev version.
|
||||
#
|
||||
# Building from the freshly-created tag means hatch-vcs resolves the version
|
||||
# cleanly (no ``+gHASH`` local segment), which PyPI requires.
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: publish-dev-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
tag:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
outputs:
|
||||
tag: ${{ steps.compute.outputs.tag }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Compute next dev tag
|
||||
id: compute
|
||||
run: |
|
||||
set -euo pipefail
|
||||
LATEST_RELEASE=$(git tag --list 'renderers-v*' --sort=-v:refname \
|
||||
| grep -Ev '(dev|rc|a[0-9]|b[0-9])' \
|
||||
| head -1)
|
||||
if [ -z "$LATEST_RELEASE" ]; then
|
||||
echo "No release tag matching 'renderers-v<MAJOR.MINOR.PATCH>' found" >&2
|
||||
exit 1
|
||||
fi
|
||||
BASE=${LATEST_RELEASE#renderers-v}
|
||||
MAJOR=$(echo "$BASE" | cut -d. -f1)
|
||||
MINOR=$(echo "$BASE" | cut -d. -f2)
|
||||
PATCH=$(echo "$BASE" | cut -d. -f3)
|
||||
NEXT="${MAJOR}.${MINOR}.$((PATCH + 1))"
|
||||
N=$(git rev-list --count "${LATEST_RELEASE}..HEAD")
|
||||
TAG="renderers-v${NEXT}.dev${N}"
|
||||
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
|
||||
echo "Computed tag: ${TAG} (base=${LATEST_RELEASE}, commits=${N})"
|
||||
|
||||
- name: Create and push tag
|
||||
env:
|
||||
TAG: ${{ steps.compute.outputs.tag }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null 2>&1; then
|
||||
echo "Tag ${TAG} already exists on origin — nothing to do" >&2
|
||||
exit 0
|
||||
fi
|
||||
git config user.name 'github-actions[bot]'
|
||||
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
|
||||
git tag -a "$TAG" -m "Automated dev release ${TAG}"
|
||||
git push origin "$TAG"
|
||||
|
||||
build:
|
||||
needs: tag
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: refs/tags/${{ needs.tag.outputs.tag }}
|
||||
|
||||
- uses: astral-sh/setup-uv@v7
|
||||
|
||||
- name: Build renderers
|
||||
run: uv build
|
||||
|
||||
- name: Upload dist artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dist-dev
|
||||
path: dist/
|
||||
if-no-files-found: error
|
||||
retention-days: 7
|
||||
|
||||
publish:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
environment: pypi-prod
|
||||
permissions:
|
||||
id-token: write
|
||||
steps:
|
||||
- name: Download dist artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: dist-dev
|
||||
path: dist/
|
||||
|
||||
- name: Publish to PyPI
|
||||
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
|
||||
+10
-4
@@ -26,10 +26,10 @@ dependencies = [
|
||||
"openai-harmony>=0.0.8",
|
||||
# Crusoe's Rust BPE tokenizer; ~10x faster encode vs HF's tokenizers.
|
||||
# ``load_tokenizer`` patches it in by default for every supported model
|
||||
# except a small denylist (DeepSeek-V3 family, MiniMax-M2 family). The
|
||||
# patch is bracketed around ``from_pretrained``, so subsequent
|
||||
# ``AutoTokenizer`` calls outside the renderers package stay vanilla.
|
||||
"fastokens>=0.1.1",
|
||||
# except a small denylist (DeepSeek-V3 family). The patch is bracketed
|
||||
# around ``from_pretrained``, so subsequent ``AutoTokenizer`` calls
|
||||
# outside the renderers package stay vanilla.
|
||||
"fastokens>=0.2.0",
|
||||
]
|
||||
|
||||
[tool.hatch.version]
|
||||
@@ -68,6 +68,12 @@ dev = [
|
||||
|
||||
[tool.uv]
|
||||
exclude-newer = "7 days"
|
||||
# fastokens 0.2.0 was published on 2026-05-17 and contains the
|
||||
# ``unpatch_transformers`` fix (crusoecloud/fastokens#32) needed for
|
||||
# MiniMax-M2's slow→fast tokenizer conversion path. Exempting it from
|
||||
# the project-wide 7-day cutoff lets the lockfile pick it up immediately
|
||||
# while the rest of the dependency graph stays gated.
|
||||
exclude-newer-package = { fastokens = false }
|
||||
|
||||
[tool.ty.environment]
|
||||
python-version = "3.13"
|
||||
|
||||
@@ -28,6 +28,7 @@ from renderers.base import (
|
||||
ToolCallParseStatus,
|
||||
ToolSpec,
|
||||
VideoPart,
|
||||
attribute_text_segments,
|
||||
build_training_sample,
|
||||
build_trajectory_step,
|
||||
create_renderer,
|
||||
@@ -36,6 +37,7 @@ from renderers.base import (
|
||||
reject_assistant_in_extension,
|
||||
trim_to_turn_close,
|
||||
)
|
||||
from renderers.client import OverlongPromptError
|
||||
from renderers.deepseek_v3 import DeepSeekV3Renderer
|
||||
from renderers.default import DefaultRenderer
|
||||
from renderers.glm5 import GLM5Renderer
|
||||
@@ -69,6 +71,7 @@ __all__ = [
|
||||
"MultiModalData",
|
||||
"MultimodalRenderer",
|
||||
"Nemotron3Renderer",
|
||||
"OverlongPromptError",
|
||||
"ParsedResponse",
|
||||
"ParsedToolCall",
|
||||
"PlaceholderRange",
|
||||
@@ -88,6 +91,7 @@ __all__ = [
|
||||
"ToolSpec",
|
||||
"VideoPart",
|
||||
"__version__",
|
||||
"attribute_text_segments",
|
||||
"build_training_sample",
|
||||
"build_trajectory_step",
|
||||
"create_renderer",
|
||||
|
||||
+356
-22
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import enum
|
||||
import io
|
||||
import logging
|
||||
import queue
|
||||
import threading
|
||||
@@ -169,6 +171,32 @@ class RenderedTokens:
|
||||
masking. ``DefaultRenderer`` leaves it empty because the Jinja
|
||||
template is opaque; hand-coded renderers populate it.
|
||||
|
||||
``is_content`` is a per-token signal generalizing the "scaffold vs
|
||||
body" distinction across all roles: ``True`` iff the token was
|
||||
produced from message-body bytes (caller-provided ``content`` /
|
||||
``tool_calls`` / ``reasoning_content``, or the model's sampled
|
||||
emission for the assistant role), ``False`` iff it is template
|
||||
scaffolding the renderer added around message bodies — role-tag
|
||||
openers, closers when not model-sampled, inter-turn separators,
|
||||
tool-response wraps, the tools-header block, the generation prompt.
|
||||
Generalises ``sampled_mask``: where ``sampled_mask`` answers "would
|
||||
the model emit this?" (useful for assistant tokens; uniformly
|
||||
``False`` elsewhere), ``is_content`` answers "is this from caller
|
||||
or model data?" (meaningful on every role). By construction
|
||||
``is_content[k] == sampled_mask[k]`` over every token attributed to
|
||||
an assistant message; on other roles ``is_content`` carries new
|
||||
information that ``sampled_mask`` does not.
|
||||
|
||||
The use case: SFT on tool response bodies while applying RL only to
|
||||
assistant tokens. The trainer wants the model to anticipate tool
|
||||
outputs but never to emit ``<|tool_response>`` itself (that would
|
||||
interrupt the rollout), so the SFT loss mask is
|
||||
``message_role == "tool" AND is_content``.
|
||||
|
||||
Empty ``is_content`` (``[]``) — like ``sampled_mask`` — means the
|
||||
renderer doesn't provide the signal. ``DefaultRenderer`` leaves it
|
||||
empty for the same reason.
|
||||
|
||||
``multi_modal_data`` is populated by multimodal renderers (e.g.
|
||||
``Qwen3VLRenderer``) when image / video content parts are present;
|
||||
text-only renderers leave it as ``None``.
|
||||
@@ -177,6 +205,7 @@ class RenderedTokens:
|
||||
token_ids: list[int] = field(default_factory=list)
|
||||
message_indices: list[int] = field(default_factory=list)
|
||||
sampled_mask: list[bool] = field(default_factory=list)
|
||||
is_content: list[bool] = field(default_factory=list)
|
||||
message_roles: list[str] = field(default_factory=list)
|
||||
multi_modal_data: "MultiModalData | None" = None
|
||||
|
||||
@@ -333,6 +362,94 @@ class RenderedTokens:
|
||||
out[role] = out.get(role, 0) + n
|
||||
return out
|
||||
|
||||
def content_token_spans_by_role(self) -> dict[str, list[tuple[int, int]]]:
|
||||
"""Per-role spans of contiguous body-only tokens (``is_content=True``).
|
||||
|
||||
Maps each role appearing in :attr:`message_roles` to a list of
|
||||
half-open ``[start, end)`` slices into :attr:`token_ids` over
|
||||
which every token satisfies ``is_content=True`` AND belongs to
|
||||
a message of that role. Spans never cross message boundaries:
|
||||
a tool message contributes its own runs; an immediately
|
||||
adjacent assistant message contributes separate runs even when
|
||||
the bodies abut on the token axis.
|
||||
|
||||
Returns an empty dict when :attr:`is_content` or
|
||||
:attr:`message_roles` is empty (renderer didn't populate the
|
||||
signal — e.g. ``DefaultRenderer``).
|
||||
|
||||
Intended for selective loss masking: SFT on tool response
|
||||
bodies while RL acts only on assistant turns is the canonical
|
||||
case::
|
||||
|
||||
spans = rendered.content_token_spans_by_role()
|
||||
tool_sft_mask = [False] * len(rendered.token_ids)
|
||||
for s, e in spans.get("tool", []):
|
||||
for k in range(s, e):
|
||||
tool_sft_mask[k] = True
|
||||
|
||||
See also :meth:`content_mask_for_roles` for the same
|
||||
computation returned as a per-token bool list.
|
||||
"""
|
||||
out: dict[str, list[tuple[int, int]]] = {}
|
||||
if not self.is_content or not self.message_roles:
|
||||
return out
|
||||
n = len(self.token_ids)
|
||||
if len(self.is_content) != n or len(self.message_indices) != n:
|
||||
return out
|
||||
|
||||
msg_spans = self.message_token_spans()
|
||||
for role, span in zip(self.message_roles, msg_spans):
|
||||
bucket = out.setdefault(role, [])
|
||||
if span is None:
|
||||
continue
|
||||
start, end = span
|
||||
run_start: int | None = None
|
||||
for k in range(start, end):
|
||||
if self.is_content[k]:
|
||||
if run_start is None:
|
||||
run_start = k
|
||||
else:
|
||||
if run_start is not None:
|
||||
bucket.append((run_start, k))
|
||||
run_start = None
|
||||
if run_start is not None:
|
||||
bucket.append((run_start, end))
|
||||
return out
|
||||
|
||||
def content_mask_for_roles(self, roles: "set[str] | frozenset[str]") -> list[bool]:
|
||||
"""Per-token bool list: ``True`` iff the token is body of a
|
||||
message whose role is in ``roles``.
|
||||
|
||||
Length matches :attr:`token_ids`. Returns an all-``False``
|
||||
list of that length when :attr:`is_content` or
|
||||
:attr:`message_roles` is empty — consumers can AND this with
|
||||
their own attribution masks without length checks.
|
||||
|
||||
``role_to_mask`` style helpers in :func:`build_training_sample`
|
||||
cover the trainable-role question; this one covers the
|
||||
complementary "body-only" question. The two compose: SFT mask
|
||||
on tool body is
|
||||
``rendered.content_mask_for_roles({"tool"})``; RL mask on
|
||||
assistant tokens stays
|
||||
``[s and (mi >= 0 and rendered.message_roles[mi] == "assistant")
|
||||
for s, mi in zip(rendered.sampled_mask, rendered.message_indices)]``.
|
||||
"""
|
||||
n = len(self.token_ids)
|
||||
mask = [False] * n
|
||||
if not self.is_content or not self.message_roles:
|
||||
return mask
|
||||
if len(self.is_content) != n or len(self.message_indices) != n:
|
||||
return mask
|
||||
|
||||
for k, msg_idx in enumerate(self.message_indices):
|
||||
if msg_idx < 0:
|
||||
continue
|
||||
if msg_idx >= len(self.message_roles):
|
||||
continue
|
||||
if self.message_roles[msg_idx] in roles and self.is_content[k]:
|
||||
mask[k] = True
|
||||
return mask
|
||||
|
||||
|
||||
class ToolCallParseStatus(str, enum.Enum):
|
||||
"""Per-attempt outcome of parsing a single ``<tool_call>`` block.
|
||||
@@ -530,6 +647,15 @@ class Renderer(Protocol):
|
||||
caller needs that distinction for the prior portion, they
|
||||
have it directly: every token in ``prev_completion_ids`` was
|
||||
sampled; every token in ``prev_prompt_ids`` was not.
|
||||
- ``is_content`` mirrors ``sampled_mask``'s scheme for the
|
||||
prior portion (uniformly ``False`` — body-vs-wrap
|
||||
attribution can't be recovered from raw token ids), and on
|
||||
the bridge-added portion the renderer populates it the same
|
||||
way as in :meth:`render`: ``True`` over the body bytes of
|
||||
each new message, ``False`` over the surrounding scaffold.
|
||||
Consumers walk the trajectory and read each step's own
|
||||
``is_content`` for full-conversation body masks; the bridge
|
||||
output covers only the *new* tokens this turn adds.
|
||||
|
||||
Text-only renderers return :class:`RenderedTokens` with
|
||||
``multi_modal_data=None``. Multimodal renderers (see
|
||||
@@ -766,6 +892,8 @@ MODEL_RENDERER_MAP: dict[str, str] = {
|
||||
"Qwen/Qwen3-14B": "qwen3",
|
||||
"Qwen/Qwen3-32B": "qwen3",
|
||||
"Qwen/Qwen3-30B-A3B": "qwen3",
|
||||
"Qwen/Qwen3-30B-A3B-Instruct-2507": "qwen3",
|
||||
"Qwen/Qwen3-30B-A3B-Thinking-2507": "qwen3",
|
||||
"Qwen/Qwen3-235B-A22B": "qwen3",
|
||||
# Qwen3.5. All seven sizes share the same renderer. The 4B / 9B /
|
||||
# 35B-A3B / 122B-A10B / 397B-A17B chat template defaults
|
||||
@@ -792,6 +920,7 @@ MODEL_RENDERER_MAP: dict[str, str] = {
|
||||
"Qwen/Qwen3-VL-30B-A3B-Instruct": "qwen3-vl",
|
||||
# GLM-5 family (GLM-4.7 reuses the GLM-5 template).
|
||||
"zai-org/GLM-5": "glm-5",
|
||||
"zai-org/GLM-5-FP8": "glm-5",
|
||||
"zai-org/GLM-4.7-Flash": "glm-5",
|
||||
"zai-org/GLM-5.1": "glm-5.1",
|
||||
# GLM-4.5.
|
||||
@@ -908,31 +1037,24 @@ TRUSTED_REVISIONS: dict[str, str] = {
|
||||
# Models for which ``fastokens`` is known to diverge from vanilla
|
||||
# ``transformers.AutoTokenizer`` and therefore must NOT be patched.
|
||||
# Empirical audit ran each entry of ``MODEL_RENDERER_MAP`` through both
|
||||
# backends; 31/35 passed byte-identical. The four below either fail to
|
||||
# load under fastokens (DeepSeek-V3 family — Metaspace pretokenizer not
|
||||
# yet implemented) or are kept defensively pending an upstream fastokens
|
||||
# fix (MiniMax-M2 family — see per-entry comments).
|
||||
# backends. The entries below fail to load under fastokens (DeepSeek-V3
|
||||
# family — Metaspace pretokenizer not yet implemented).
|
||||
FASTOKENS_INCOMPATIBLE: frozenset[str] = frozenset(
|
||||
{
|
||||
# fastokens 0.1.1: ``ValueError: pre-tokenizer error: unsupported
|
||||
# fastokens: ``ValueError: pre-tokenizer error: unsupported
|
||||
# pre-tokenizer type: Metaspace`` — DeepSeek's tokenizer uses
|
||||
# SentencePiece-style Metaspace pretokenization which fastokens
|
||||
# doesn't yet implement.
|
||||
"deepseek-ai/DeepSeek-V3",
|
||||
"deepseek-ai/DeepSeek-V3-Base",
|
||||
# MiniMax: kept defensive pending upstream fastokens fix
|
||||
# https://github.com/crusoecloud/fastokens/pull/32 — that PR
|
||||
# removes a stray attribute leaked by ``unpatch_transformers``
|
||||
# which steers MiniMax (declared ``tokenizer_class =
|
||||
# 'GPT2Tokenizer'`` → slow→fast conversion path) down a different
|
||||
# load path on subsequent vanilla loads. Once the upstream fix
|
||||
# is released, these two entries can be dropped after re-audit.
|
||||
"MiniMaxAI/MiniMax-M2",
|
||||
"MiniMaxAI/MiniMax-M2.5",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
_FASTOKENS_PATCH_LOCK = threading.Lock()
|
||||
_FASTOKENS_ANNOUNCED = False
|
||||
|
||||
|
||||
def _patched_load(model_name_or_path: str, **kwargs):
|
||||
"""Run ``AutoTokenizer.from_pretrained`` with fastokens patched in
|
||||
process-locally — patch around the load, unpatch right after.
|
||||
@@ -942,15 +1064,39 @@ def _patched_load(model_name_or_path: str, **kwargs):
|
||||
fastokens for ``encode``/``decode`` while subsequent
|
||||
``AutoTokenizer.from_pretrained`` calls (outside our control) go
|
||||
back to vanilla. This keeps the global side effect minimal.
|
||||
|
||||
fastokens itself prints ``[fastokens] patch_transformers: ...`` to
|
||||
stdout on every patch/unpatch call. Building a pool of size N would
|
||||
therefore emit ~N lines (more under thread contention, where some
|
||||
threads see ``already patched``). We swallow those prints under a
|
||||
lock — ``contextlib.redirect_stdout`` swaps ``sys.stdout``
|
||||
process-wide, so the lock keeps unrelated stdout writes from other
|
||||
threads from disappearing into our buffer. The patch/unpatch calls
|
||||
are cheap; only the brief patch+unpatch is serialized, the actual
|
||||
``from_pretrained`` still runs concurrently across pool slots. A
|
||||
single ``logger.info`` is emitted on the first patch so the fast
|
||||
path is still discoverable in logs.
|
||||
"""
|
||||
import fastokens
|
||||
from transformers import AutoTokenizer
|
||||
|
||||
fastokens.patch_transformers()
|
||||
global _FASTOKENS_ANNOUNCED
|
||||
|
||||
with _FASTOKENS_PATCH_LOCK:
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
fastokens.patch_transformers()
|
||||
if not _FASTOKENS_ANNOUNCED:
|
||||
logger.info(
|
||||
"fastokens enabled — tokenizers load through the Rust BPE "
|
||||
"fast path (~10x encode speedup)."
|
||||
)
|
||||
_FASTOKENS_ANNOUNCED = True
|
||||
try:
|
||||
return AutoTokenizer.from_pretrained(model_name_or_path, **kwargs)
|
||||
finally:
|
||||
fastokens.unpatch_transformers()
|
||||
with _FASTOKENS_PATCH_LOCK:
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
fastokens.unpatch_transformers()
|
||||
|
||||
|
||||
def load_tokenizer(
|
||||
@@ -972,10 +1118,10 @@ def load_tokenizer(
|
||||
immediately after, so global ``AutoTokenizer.from_pretrained`` calls
|
||||
elsewhere in the user's process are not affected.
|
||||
|
||||
Models in ``FASTOKENS_INCOMPATIBLE`` (DeepSeek-V3 family, MiniMax-M2
|
||||
family) skip the patch — fastokens 0.1.1 either fails to load them
|
||||
or produces token-divergent output. Pass ``use_fastokens=False`` to
|
||||
force the vanilla backend for any other model.
|
||||
Models in ``FASTOKENS_INCOMPATIBLE`` (DeepSeek-V3 family) skip the
|
||||
patch — fastokens currently fails to load them. Pass
|
||||
``use_fastokens=False`` to force the vanilla backend for any other
|
||||
model.
|
||||
|
||||
Unknown / fine-tuned model paths fall through to
|
||||
``trust_remote_code=False`` and the patched-load fast path. If
|
||||
@@ -1205,6 +1351,7 @@ def build_training_sample(
|
||||
*,
|
||||
role_to_mask: Callable[[Message], bool],
|
||||
tools: list[ToolSpec] | None = None,
|
||||
content_sft_roles: "set[str] | frozenset[str] | None" = None,
|
||||
) -> tuple[list[int], list[bool]]:
|
||||
"""Build (token_ids, loss_mask) for supervised training.
|
||||
|
||||
@@ -1220,17 +1367,53 @@ def build_training_sample(
|
||||
back to attribution-only masking — every token attributed to a
|
||||
trainable role is trained on, including template-injected
|
||||
``<|im_start|>role\\n`` openers.
|
||||
|
||||
``content_sft_roles`` opts in additional roles for "body-only"
|
||||
supervision: for every message whose role is in this set, tokens
|
||||
with ``is_content=True`` are marked trainable even though the
|
||||
``sampled_mask`` gate excludes them (the model never samples
|
||||
tool / user / system tokens). Template scaffolding around those
|
||||
messages — ``<|im_start|>role\\n`` openers, ``<|im_end|>``
|
||||
closers, ``<|tool_response>`` wraps, inter-turn ``\\n`` — stays
|
||||
masked out, so the model learns to anticipate the body text
|
||||
without producing the surrounding special tokens (which would
|
||||
interrupt a real rollout). The canonical use case is RL on
|
||||
assistant tokens (``role_to_mask=lambda m: m["role"] ==
|
||||
"assistant"``) plus SFT on tool response bodies
|
||||
(``content_sft_roles={"tool"}``).
|
||||
|
||||
Requires the renderer to populate ``is_content`` for the body-only
|
||||
path to fire. Renderers that leave it empty (``DefaultRenderer``,
|
||||
or hand-coded renderers that haven't been wired up yet) ignore
|
||||
``content_sft_roles`` silently — falling back to the original
|
||||
``role_to_mask`` + ``sampled_mask`` behaviour.
|
||||
"""
|
||||
rendered = renderer.render(messages, tools=tools)
|
||||
has_sampled_info = len(rendered.sampled_mask) == len(rendered.token_ids)
|
||||
has_content_info = len(rendered.is_content) == len(rendered.token_ids)
|
||||
body_roles: "frozenset[str]"
|
||||
if content_sft_roles and has_content_info:
|
||||
body_roles = frozenset(content_sft_roles)
|
||||
else:
|
||||
body_roles = frozenset()
|
||||
|
||||
loss_mask: list[bool] = []
|
||||
for k, msg_idx in enumerate(rendered.message_indices):
|
||||
if msg_idx < 0:
|
||||
loss_mask.append(False)
|
||||
elif has_sampled_info and not rendered.sampled_mask[k]:
|
||||
continue
|
||||
msg = messages[msg_idx]
|
||||
# Body-only path for opt-in roles. Fires only on tokens whose
|
||||
# is_content bit is set; never adds the scaffolding around the
|
||||
# message, so the model isn't supervised on emitting the role
|
||||
# tags / wraps that would derail a rollout.
|
||||
if body_roles and msg.get("role") in body_roles:
|
||||
loss_mask.append(rendered.is_content[k])
|
||||
continue
|
||||
if has_sampled_info and not rendered.sampled_mask[k]:
|
||||
loss_mask.append(False)
|
||||
else:
|
||||
loss_mask.append(role_to_mask(messages[msg_idx]))
|
||||
loss_mask.append(role_to_mask(msg))
|
||||
return rendered.token_ids, loss_mask
|
||||
|
||||
|
||||
@@ -1277,6 +1460,157 @@ def trim_to_turn_close(
|
||||
return previous_ids
|
||||
|
||||
|
||||
# Per-model offset-aware tokenizer cache. ``attribute_text_segments``
|
||||
# uses the fast HuggingFace tokenizer's ``offset_mapping`` to attribute
|
||||
# each token to its source text segment under one BPE pass. Fastokens
|
||||
# (the Rust BPE we patch in by default for ~10x faster encode) does not
|
||||
# track character offsets — the patched tokenizer's
|
||||
# ``return_offsets_mapping=True`` raises ``NotImplementedError``. So we
|
||||
# keep a parallel vanilla tokenizer per model purely for offset queries.
|
||||
# Memory cost is one extra tokenizer per *unique* model name across all
|
||||
# pools / renderers (the cache is process-global), independent of pool
|
||||
# size.
|
||||
_offset_tokenizers: dict[str, Any] = {}
|
||||
_offset_tokenizers_lock = threading.Lock()
|
||||
|
||||
|
||||
def _get_offset_tokenizer(tokenizer):
|
||||
"""Return a tokenizer that supports ``return_offsets_mapping=True``.
|
||||
|
||||
If ``tokenizer`` itself supports offsets, returns it unchanged.
|
||||
Otherwise loads a vanilla (non-fastokens) tokenizer from
|
||||
``tokenizer.name_or_path`` and caches it. Raises if the tokenizer
|
||||
has no usable ``name_or_path`` — hand-coded renderers always pass
|
||||
a tokenizer loaded via ``load_tokenizer`` which does set it.
|
||||
"""
|
||||
# Cheap probe: does this tokenizer already provide offsets?
|
||||
try:
|
||||
tokenizer("a", add_special_tokens=False, return_offsets_mapping=True)
|
||||
return tokenizer
|
||||
except (NotImplementedError, ValueError, TypeError):
|
||||
pass
|
||||
|
||||
name_or_path = getattr(tokenizer, "name_or_path", "")
|
||||
if not name_or_path:
|
||||
raise RuntimeError(
|
||||
"Cannot construct an offset-aware tokenizer: the supplied "
|
||||
"tokenizer has no ``name_or_path`` to fall back on. Pass a "
|
||||
"tokenizer loaded via ``renderers.base.load_tokenizer``."
|
||||
)
|
||||
|
||||
with _offset_tokenizers_lock:
|
||||
cached = _offset_tokenizers.get(name_or_path)
|
||||
if cached is not None:
|
||||
return cached
|
||||
from transformers import AutoTokenizer
|
||||
|
||||
kwargs: dict[str, Any] = {}
|
||||
revision = TRUSTED_REVISIONS.get(name_or_path)
|
||||
if revision is not None:
|
||||
kwargs = {"trust_remote_code": True, "revision": revision}
|
||||
else:
|
||||
kwargs = {"trust_remote_code": False}
|
||||
# Explicitly vanilla — we want HF's Rust tokenizer with offset
|
||||
# tracking, not the fastokens shim. ``load_tokenizer`` would
|
||||
# patch fastokens in by default; calling
|
||||
# ``AutoTokenizer.from_pretrained`` directly here keeps the
|
||||
# fastokens patch out of this code path entirely.
|
||||
offset_tok = AutoTokenizer.from_pretrained(name_or_path, **kwargs)
|
||||
if not getattr(offset_tok, "is_fast", False):
|
||||
raise RuntimeError(
|
||||
f"Vanilla tokenizer for {name_or_path!r} is not a fast "
|
||||
"tokenizer; offset_mapping is unavailable. Hand-coded "
|
||||
"renderers require a fast tokenizer for body/scaffold "
|
||||
"attribution."
|
||||
)
|
||||
_offset_tokenizers[name_or_path] = offset_tok
|
||||
return offset_tok
|
||||
|
||||
|
||||
def attribute_text_segments(
|
||||
tokenizer,
|
||||
segments: "list[tuple[str, bool]]",
|
||||
) -> "list[tuple[int, bool]]":
|
||||
"""Tokenize concatenated segments as a single BPE pass and return
|
||||
``(token_id, is_content)`` pairs.
|
||||
|
||||
``segments`` is a list of ``(text, is_content)`` chunks the renderer
|
||||
wants to emit contiguously — for example ``[("user\\n", False),
|
||||
(content, True)]`` for a user message. Concatenation is done before
|
||||
encoding to preserve BPE merges across the wrap/body boundary; the
|
||||
resulting tokens are then attributed back to their source segment
|
||||
via the fast tokenizer's ``offset_mapping``.
|
||||
|
||||
A token is attributed to the segment containing its first source
|
||||
character (``offset_mapping[k][0]``). Tokens whose first character
|
||||
falls exactly on a segment boundary are attributed to the segment
|
||||
that *starts* at that offset (the "later" segment). Zero-length
|
||||
tokens (rare; usually pre-tokenizer artefacts) are attributed to
|
||||
the most recently entered segment.
|
||||
|
||||
Requires a HuggingFace fast tokenizer with offset tracking. The
|
||||
``fastokens`` patch ``load_tokenizer`` applies by default does
|
||||
**not** track offsets — when that's the case we transparently load
|
||||
a vanilla offset-capable tokenizer for the same model and cache it
|
||||
(see :func:`_get_offset_tokenizer`). Hand-coded renderers are only
|
||||
registered for model families that ship a fast tokenizer, so a
|
||||
silent slow-tokenizer fallback isn't supported — BPE drift at the
|
||||
wrap/body boundary would defeat the whole point.
|
||||
|
||||
Empty input or empty joined text returns an empty list.
|
||||
"""
|
||||
if not segments:
|
||||
return []
|
||||
full_text = "".join(text for text, _ in segments)
|
||||
if not full_text:
|
||||
return []
|
||||
|
||||
offset_tokenizer = _get_offset_tokenizer(tokenizer)
|
||||
encoding = offset_tokenizer(
|
||||
full_text,
|
||||
add_special_tokens=False,
|
||||
return_offsets_mapping=True,
|
||||
)
|
||||
token_ids = list(encoding["input_ids"])
|
||||
offsets = list(encoding["offset_mapping"])
|
||||
|
||||
# Build segment char-span lookup. Track the half-open span
|
||||
# [seg_start, seg_end) of each segment and its is_content bit.
|
||||
spans: list[tuple[int, int, bool]] = []
|
||||
pos = 0
|
||||
for text, is_content in segments:
|
||||
spans.append((pos, pos + len(text), is_content))
|
||||
pos += len(text)
|
||||
total_len = pos
|
||||
|
||||
out: list[tuple[int, bool]] = []
|
||||
last_is_content = spans[-1][2] if spans else False
|
||||
for tok_id, (start, _end) in zip(token_ids, offsets):
|
||||
if start >= total_len:
|
||||
# Token's character offset is past every segment (shouldn't
|
||||
# normally happen for add_special_tokens=False, but defensive
|
||||
# against tokenizer-specific edge cases).
|
||||
out.append((tok_id, last_is_content))
|
||||
continue
|
||||
# Find the segment that contains `start`. Segments are
|
||||
# contiguous and ordered, so a linear scan is fine — the inner
|
||||
# loop runs at most len(segments) times per token and segments
|
||||
# is typically 2-3 in practice.
|
||||
is_content = last_is_content
|
||||
for seg_start, seg_end, seg_is_content in spans:
|
||||
if seg_start <= start < seg_end:
|
||||
is_content = seg_is_content
|
||||
break
|
||||
else:
|
||||
# start == total_len handled above; the remaining case is
|
||||
# an empty segment in the middle. Empty segments emit no
|
||||
# characters, so no token can land in them; fall through to
|
||||
# the last non-empty segment's bit.
|
||||
pass
|
||||
out.append((tok_id, is_content))
|
||||
return out
|
||||
|
||||
|
||||
def reject_assistant_in_extension(new_messages: list[Message]) -> bool:
|
||||
"""Return True if any message in ``new_messages`` is an assistant turn.
|
||||
|
||||
|
||||
+166
-68
@@ -12,16 +12,18 @@ achieve real parallelism.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
from typing import Any, cast
|
||||
|
||||
import numpy as np
|
||||
from openai import AsyncOpenAI, BadRequestError
|
||||
import httpx
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from renderers.base import (
|
||||
Message,
|
||||
MultiModalData,
|
||||
RenderedTokens,
|
||||
Renderer,
|
||||
RendererPool,
|
||||
ToolCallParseStatus,
|
||||
@@ -29,6 +31,80 @@ from renderers.base import (
|
||||
)
|
||||
|
||||
_request_logger = logging.getLogger("renderers.client")
|
||||
ROUTED_EXPERTS_DATA_PREFIX = b'"routed_experts":{"data":"'
|
||||
|
||||
|
||||
class OverlongPromptError(Exception):
|
||||
"""The rendered prompt exceeds the engine's context window.
|
||||
|
||||
Raised by :func:`generate` when the rendered token sequence is strictly
|
||||
longer than the resolved cap — either an explicit ``max_prompt_len`` the
|
||||
caller passed in, or the engine's ``max_model_len`` discovered via
|
||||
``GET /v1/models``. Caught client-side before the engine ever sees the
|
||||
request, so callers route the failure to a deterministic policy (skip /
|
||||
truncate / count) instead of round-tripping through an engine 4xx.
|
||||
|
||||
Named after the corresponding ``verifiers.errors.OverlongPromptError``;
|
||||
the two are distinct classes (different package hierarchies) but the
|
||||
concept is the same and downstream clients translate one to the other.
|
||||
"""
|
||||
|
||||
def __init__(self, *, prompt_len: int, max_prompt_len: int) -> None:
|
||||
self.prompt_len = prompt_len
|
||||
self.max_prompt_len = max_prompt_len
|
||||
super().__init__(
|
||||
f"Prompt length ({prompt_len}) exceeds maximum "
|
||||
f"context length ({max_prompt_len})."
|
||||
)
|
||||
|
||||
|
||||
# Per-process cache of resolved engine context-length caps, keyed by
|
||||
# ``(base_url, model)``. ``None`` is the "we asked the engine and it didn't
|
||||
# tell us" sentinel — distinct from "key missing" (haven't asked yet). The
|
||||
# lock serializes the first lookup per key; cache hits avoid the lock.
|
||||
_max_prompt_len_cache: dict[tuple[str, str], int | None] = {}
|
||||
_max_prompt_len_lock = asyncio.Lock()
|
||||
|
||||
|
||||
async def _resolve_max_prompt_len(client: AsyncOpenAI, model: str) -> int | None:
|
||||
"""Discover ``max_model_len`` from the engine via ``GET /v1/models``.
|
||||
|
||||
OpenAI-API-compatible engines expose model metadata at this endpoint;
|
||||
vLLM extends its ``ModelCard`` with a ``max_model_len`` field. Engines
|
||||
that don't (SGLang as of this writing, third-party gateways, etc.) get
|
||||
a cached ``None`` and the pre-flight overflow check silently disables —
|
||||
callers fall back to whatever reactive handling they have for engine
|
||||
4xx, which the verifiers ``@handle_openai_overlong_prompt`` decorator
|
||||
already supplies for the prime-rl path.
|
||||
|
||||
Any exception during lookup (network error, non-JSON body, attribute
|
||||
miss on a mock client in tests) is treated as "unknown cap": cached
|
||||
``None`` so we don't retry on every call.
|
||||
"""
|
||||
key = (str(getattr(client, "base_url", "")), model)
|
||||
if key in _max_prompt_len_cache:
|
||||
return _max_prompt_len_cache[key]
|
||||
async with _max_prompt_len_lock:
|
||||
if key in _max_prompt_len_cache:
|
||||
return _max_prompt_len_cache[key]
|
||||
try:
|
||||
payload = await client.get("/models", cast_to=cast(Any, dict[str, Any]))
|
||||
except Exception as exc:
|
||||
_request_logger.debug("max_prompt_len lookup failed: %s", exc)
|
||||
_max_prompt_len_cache[key] = None
|
||||
return None
|
||||
value: int | None = None
|
||||
for card in payload.get("data") or []:
|
||||
if not isinstance(card, Mapping):
|
||||
continue
|
||||
if card.get("id") != model:
|
||||
continue
|
||||
raw = card.get("max_model_len")
|
||||
if isinstance(raw, int) and raw > 0:
|
||||
value = raw
|
||||
break
|
||||
_max_prompt_len_cache[key] = value
|
||||
return value
|
||||
|
||||
|
||||
async def _maybe_offload(renderer: Renderer | RendererPool, fn):
|
||||
@@ -45,6 +121,26 @@ async def _maybe_offload(renderer: Renderer | RendererPool, fn):
|
||||
return fn()
|
||||
|
||||
|
||||
def strip_routed_experts_data(raw: bytes) -> tuple[bytes, memoryview | None]:
|
||||
data_start = raw.find(ROUTED_EXPERTS_DATA_PREFIX)
|
||||
if data_start < 0:
|
||||
return raw, None
|
||||
|
||||
data_start += len(ROUTED_EXPERTS_DATA_PREFIX)
|
||||
data_end = raw.index(b'"', data_start)
|
||||
routed_data = memoryview(raw)[data_start:data_end]
|
||||
stripped = raw[:data_start] + raw[data_end:]
|
||||
return stripped, routed_data
|
||||
|
||||
|
||||
def parse_generate_response(raw: bytes) -> dict[str, Any]:
|
||||
stripped, routed_data = strip_routed_experts_data(raw)
|
||||
payload: dict[str, Any] = json.loads(stripped)
|
||||
if routed_data is not None:
|
||||
payload["choices"][0]["routed_experts"]["data"] = routed_data
|
||||
return payload
|
||||
|
||||
|
||||
async def generate(
|
||||
*,
|
||||
client: AsyncOpenAI,
|
||||
@@ -53,11 +149,13 @@ async def generate(
|
||||
model: str,
|
||||
prompt_ids: list[int] | None = None,
|
||||
multi_modal_data: MultiModalData | None = None,
|
||||
prompt_attribution: RenderedTokens | None = None,
|
||||
tools: list[ToolSpec] | None = None,
|
||||
sampling_params: dict[str, Any] | None = None,
|
||||
cache_salt: str | None = None,
|
||||
priority: int | None = None,
|
||||
extra_headers: dict[str, str] | None = None,
|
||||
max_prompt_len: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Tokenize messages, call vLLM /inference/v1/generate, parse the response.
|
||||
|
||||
@@ -66,7 +164,11 @@ async def generate(
|
||||
renderer) and ``logprobs=1`` (we always emit completion_logprobs). Pass
|
||||
``prompt_ids`` to skip rendering and use a prebuilt token sequence —
|
||||
pair it with ``multi_modal_data`` when the prebuilt prompt has image /
|
||||
video placeholders that need engine-side mm payload.
|
||||
video placeholders that need engine-side mm payload, and with
|
||||
``prompt_attribution`` (a :class:`RenderedTokens` whose ``token_ids``
|
||||
match the passed-in ``prompt_ids``) to carry the renderer's per-token
|
||||
attribution (``is_content`` / ``sampled_mask`` / ``message_indices`` /
|
||||
``message_roles``) into the result without re-rendering.
|
||||
|
||||
For multimodal renderers (e.g. ``Qwen3VLRenderer``), the call goes
|
||||
through ``renderer.render(...)`` to recover the ``multi_modal_data``
|
||||
@@ -74,9 +176,31 @@ async def generate(
|
||||
mm_placeholders, kwargs_data) before POSTing. The serializer imports
|
||||
``vllm.*`` lazily so text-only consumers never pay for the import.
|
||||
|
||||
``max_prompt_len`` controls the pre-flight overflow check. When the
|
||||
rendered prompt is strictly longer than the cap, the request is never
|
||||
sent and ``OverlongPromptError`` is raised. If ``max_prompt_len`` is
|
||||
``None`` (the default), the cap is auto-discovered once per
|
||||
``(base_url, model)`` via ``GET /v1/models`` (vLLM's
|
||||
``ModelCard.max_model_len`` extension); engines that don't expose it
|
||||
cache a ``None`` cap and the pre-flight silently disables. Engine 4xx
|
||||
that still slip through propagate raw — converting them into a domain
|
||||
error is the calling client's job (its error shape is engine-specific).
|
||||
|
||||
Returns a dict with: request_id, prompt_ids, completion_ids,
|
||||
completion_logprobs, content, reasoning_content, tool_calls,
|
||||
finish_reason, routed_experts.
|
||||
finish_reason, routed_experts, multi_modal_data, prompt_attribution.
|
||||
|
||||
``prompt_attribution`` is the renderer's :class:`RenderedTokens` for
|
||||
the prompt — either the one this call computed via
|
||||
``renderer.render(...)`` or the one the caller threaded in alongside
|
||||
``prompt_ids``. Carries ``token_ids``, ``message_indices``,
|
||||
``sampled_mask``, ``is_content``, ``message_roles``, and
|
||||
``multi_modal_data``, so downstream consumers (verifiers
|
||||
``RendererClient`` → prime-rl) can build per-token loss masks
|
||||
(``content_mask_for_roles({"tool"})`` for SFT-on-tool-body,
|
||||
``sampled_mask`` for RL trainable spans) without a second render
|
||||
pass. ``None`` when the caller passed pre-built ``prompt_ids``
|
||||
without attribution.
|
||||
"""
|
||||
if tools and not getattr(renderer, "supports_tools", True):
|
||||
raise ValueError(
|
||||
@@ -86,15 +210,33 @@ async def generate(
|
||||
|
||||
def _prepare():
|
||||
if prompt_ids is not None:
|
||||
return list(prompt_ids), renderer.get_stop_token_ids(), multi_modal_data
|
||||
# Caller-supplied prompt; if they also gave us pre-computed
|
||||
# attribution (e.g. the bridge path in verifiers), thread it
|
||||
# through unchanged.
|
||||
return (
|
||||
list(prompt_ids),
|
||||
renderer.get_stop_token_ids(),
|
||||
multi_modal_data,
|
||||
prompt_attribution,
|
||||
)
|
||||
rendered = renderer.render(messages, tools=tools, add_generation_prompt=True)
|
||||
return (
|
||||
rendered.token_ids,
|
||||
renderer.get_stop_token_ids(),
|
||||
rendered.multi_modal_data,
|
||||
rendered,
|
||||
)
|
||||
|
||||
prompt_ids, stop_token_ids, mm_data = await _maybe_offload(renderer, _prepare)
|
||||
prompt_ids, stop_token_ids, mm_data, prompt_attr = await _maybe_offload(
|
||||
renderer, _prepare
|
||||
)
|
||||
|
||||
if max_prompt_len is None:
|
||||
max_prompt_len = await _resolve_max_prompt_len(client, model)
|
||||
if max_prompt_len is not None and len(prompt_ids) > max_prompt_len:
|
||||
raise OverlongPromptError(
|
||||
prompt_len=len(prompt_ids), max_prompt_len=max_prompt_len
|
||||
)
|
||||
|
||||
sp: dict[str, Any] = dict(sampling_params or {})
|
||||
sp["stop_token_ids"] = stop_token_ids
|
||||
@@ -130,21 +272,13 @@ async def generate(
|
||||
sp.get("max_tokens"),
|
||||
)
|
||||
post_kwargs: dict[str, Any] = {
|
||||
"cast_to": cast(Any, dict[str, Any]),
|
||||
"cast_to": httpx.Response,
|
||||
"body": body,
|
||||
}
|
||||
if extra_headers:
|
||||
post_kwargs["options"] = cast(Any, {"headers": extra_headers})
|
||||
try:
|
||||
data = await client.post(endpoint, **post_kwargs)
|
||||
except BadRequestError as exc:
|
||||
_log_overlong_prompt_diagnostic(
|
||||
prompt_ids=prompt_ids,
|
||||
messages=messages,
|
||||
max_tokens=sp.get("max_tokens"),
|
||||
exc=exc,
|
||||
)
|
||||
raise
|
||||
raw_response = await client.post(endpoint, **post_kwargs)
|
||||
data = parse_generate_response(raw_response.content)
|
||||
|
||||
choice = (data.get("choices") or [{}])[0]
|
||||
completion_ids = choice.get("token_ids") or []
|
||||
@@ -158,14 +292,7 @@ async def generate(
|
||||
content_lp = raw_logprobs.get("content") if isinstance(raw_logprobs, dict) else None
|
||||
completion_logprobs = [float(c.get("logprob") or 0.0) for c in content_lp or []]
|
||||
|
||||
routed_experts = None
|
||||
raw_re = choice.get("routed_experts")
|
||||
if isinstance(raw_re, dict) and "data" in raw_re and "shape" in raw_re:
|
||||
routed_experts = (
|
||||
np.frombuffer(base64.b85decode(raw_re["data"]), dtype=np.int32)
|
||||
.reshape(raw_re["shape"])
|
||||
.tolist()
|
||||
)
|
||||
routed_experts = choice.get("routed_experts")
|
||||
|
||||
# /inference/v1/generate returns finish_reason in {"stop","length",...} —
|
||||
# never "tool_calls" (a chat-completions concept). Promote stop→tool_calls
|
||||
@@ -196,6 +323,14 @@ async def generate(
|
||||
# callers can persist it on the trajectory step for downstream
|
||||
# multi-turn bridging and training-sample construction.
|
||||
"multi_modal_data": mm_data,
|
||||
# The renderer's per-token attribution for the prompt — either
|
||||
# the RenderedTokens computed here via renderer.render(...) or
|
||||
# the one threaded in by the caller alongside prompt_ids (the
|
||||
# bridge path). Lets downstream consumers (verifiers
|
||||
# RendererClient → prime-rl) build SFT-on-tool-body and other
|
||||
# selective loss masks without a second render pass. ``None``
|
||||
# when the caller passed prompt_ids without attribution.
|
||||
"prompt_attribution": prompt_attr,
|
||||
}
|
||||
|
||||
|
||||
@@ -225,6 +360,7 @@ def _build_mm_features(
|
||||
to change. Don't pre-build the abstraction with one engine in tree.
|
||||
"""
|
||||
from renderers.qwen3_vl import Qwen3VLRenderer
|
||||
from renderers.qwen35 import Qwen35Renderer
|
||||
|
||||
# Type dispatch only needs the renderer class. Pools expose
|
||||
# ``renderer_cls`` as a snapshot attribute, so we don't have to check
|
||||
@@ -233,7 +369,10 @@ def _build_mm_features(
|
||||
renderer.renderer_cls if isinstance(renderer, RendererPool) else type(renderer)
|
||||
)
|
||||
|
||||
if issubclass(renderer_cls, Qwen3VLRenderer):
|
||||
# Qwen3-VL and Qwen3.5 both ship ``pixel_values`` + ``image_grid_thw``
|
||||
# via the shared Qwen2-VL field factory. ``spatial_merge_size=2`` is
|
||||
# the family default and matches every Qwen-VL processor in tree.
|
||||
if issubclass(renderer_cls, (Qwen3VLRenderer, Qwen35Renderer)):
|
||||
return _build_qwen_vl_features(mm_data, spatial_merge_size=2)
|
||||
|
||||
raise NotImplementedError(
|
||||
@@ -305,44 +444,3 @@ def _build_qwen_vl_features(
|
||||
out["kwargs_data"] = None
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def _log_overlong_prompt_diagnostic(
|
||||
*,
|
||||
prompt_ids: list[int],
|
||||
messages: list[Message],
|
||||
max_tokens: int | None,
|
||||
exc: BadRequestError,
|
||||
) -> None:
|
||||
"""Log a structured snapshot when vLLM rejects with 4xx — usually overlong.
|
||||
|
||||
Captures total prompt length, per-message role + character count, and
|
||||
the first chunk of the response body.
|
||||
"""
|
||||
body_text = ""
|
||||
response = getattr(exc, "response", None)
|
||||
if response is not None:
|
||||
body_text = (response.text or "")[:500].replace("\n", " ")
|
||||
msg_summary = []
|
||||
for i, m in enumerate(messages):
|
||||
role = m.get("role", "?")
|
||||
content = m.get("content")
|
||||
if isinstance(content, str):
|
||||
content_len = len(content)
|
||||
elif isinstance(content, list):
|
||||
content_len = sum(
|
||||
len(p.get("text", "")) if isinstance(p, dict) else 0 for p in content
|
||||
)
|
||||
else:
|
||||
content_len = 0
|
||||
tool_calls = m.get("tool_calls")
|
||||
tc_count = len(tool_calls) if tool_calls else 0
|
||||
msg_summary.append(f"[{i}]{role}(c={content_len},tc={tc_count})")
|
||||
_request_logger.warning(
|
||||
"vllm 4xx prompt_len=%d messages=%d max_tokens=%s per_msg=%s response_body=%s",
|
||||
len(prompt_ids),
|
||||
len(messages),
|
||||
max_tokens,
|
||||
" ".join(msg_summary),
|
||||
body_text,
|
||||
)
|
||||
|
||||
+108
-38
@@ -21,6 +21,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
trim_to_turn_close,
|
||||
)
|
||||
@@ -114,22 +115,47 @@ class DeepSeekV3Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
emit_ids(
|
||||
self._encode(text),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── 1. BOS token ─────────────────────────────────────────────
|
||||
emit_special(self._bos, -1, is_sampled=False)
|
||||
emit_special(self._bos, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── 2. Collect system messages at the start ───────────────────
|
||||
# All leading system messages are concatenated with "\n\n" and emitted
|
||||
@@ -151,7 +177,8 @@ class DeepSeekV3Renderer:
|
||||
|
||||
if sys_parts:
|
||||
# Attribute the concatenated system text to the first system message (index 0).
|
||||
emit_text("\n\n".join(sys_parts), 0, is_sampled=False)
|
||||
# The system content is the caller's body — mark is_content=True.
|
||||
emit_text("\n\n".join(sys_parts), 0, is_sampled=False, is_content=True)
|
||||
|
||||
# ── 3. Render non-system messages ─────────────────────────────
|
||||
num_messages = len(messages)
|
||||
@@ -166,8 +193,8 @@ class DeepSeekV3Renderer:
|
||||
content = "".join(
|
||||
p.get("text", "") for p in content if isinstance(p, dict)
|
||||
)
|
||||
emit_special(self._user_token, i, is_sampled=False)
|
||||
emit_text(str(content), i, is_sampled=False)
|
||||
emit_special(self._user_token, i, is_sampled=False, is_content=False)
|
||||
emit_text(str(content), i, is_sampled=False, is_content=True)
|
||||
|
||||
elif role == "user":
|
||||
content = msg.get("content") or ""
|
||||
@@ -180,8 +207,8 @@ class DeepSeekV3Renderer:
|
||||
else ""
|
||||
for p in content
|
||||
)
|
||||
emit_special(self._user_token, i, is_sampled=False)
|
||||
emit_text(str(content), i, is_sampled=False)
|
||||
emit_special(self._user_token, i, is_sampled=False, is_content=False)
|
||||
emit_text(str(content), i, is_sampled=False, is_content=True)
|
||||
|
||||
elif role == "assistant":
|
||||
self._render_assistant(
|
||||
@@ -190,6 +217,7 @@ class DeepSeekV3Renderer:
|
||||
messages,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
@@ -198,6 +226,7 @@ class DeepSeekV3Renderer:
|
||||
i,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
# ── 4. Generation prompt ──────────────────────────────────────
|
||||
@@ -205,14 +234,17 @@ class DeepSeekV3Renderer:
|
||||
# Don't add <|Assistant|> after tool outputs — content flows directly.
|
||||
last_role = messages[-1]["role"] if messages else None
|
||||
if last_role != "tool":
|
||||
emit_special(self._assistant_token, -1, is_sampled=False)
|
||||
emit_special(
|
||||
self._assistant_token, -1, is_sampled=False, is_content=False
|
||||
)
|
||||
if self._enable_thinking:
|
||||
emit_text("<think>\n", -1, is_sampled=False)
|
||||
emit_text("<think>\n", -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -276,27 +308,38 @@ class DeepSeekV3Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
@@ -309,11 +352,11 @@ class DeepSeekV3Renderer:
|
||||
|
||||
if role == "user":
|
||||
emit_special(self._user_token, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
elif role == "system":
|
||||
# Post-initial system messages render as user turns.
|
||||
emit_special(self._user_token, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
elif role == "tool":
|
||||
prev_is_tool = i > 0 and new_messages[i - 1].get("role") == "tool"
|
||||
next_is_tool = (
|
||||
@@ -323,7 +366,7 @@ class DeepSeekV3Renderer:
|
||||
if not prev_is_tool:
|
||||
emit_special(self._tool_outputs_begin, i)
|
||||
emit_special(self._tool_output_begin, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
emit_special(self._tool_output_end, i)
|
||||
if not next_is_tool:
|
||||
emit_special(self._tool_outputs_end, i)
|
||||
@@ -344,6 +387,7 @@ class DeepSeekV3Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -359,6 +403,7 @@ class DeepSeekV3Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Determine whether this message follows a tool output sequence.
|
||||
# The HF template emits <|tool▁outputs▁end|> before the assistant content
|
||||
@@ -391,18 +436,23 @@ class DeepSeekV3Renderer:
|
||||
# keeps the SFT loss mask aligned with what the model would
|
||||
# actually have produced. When the previous message is a tool
|
||||
# response, the template skips this token entirely (content
|
||||
# flows directly out of ``<|tool▁outputs▁end|>``).
|
||||
# flows directly out of ``<|tool▁outputs▁end|>``). On assistant
|
||||
# the invariant ``is_content == sampled_mask`` holds.
|
||||
if not prev_is_tool:
|
||||
emit_special(self._assistant_token, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._assistant_token, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
if not tool_calls:
|
||||
emit_text(content, msg_idx, is_sampled=True)
|
||||
emit_text(content, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
# Emit any pre-tool-call content first.
|
||||
emit_text(content, msg_idx, is_sampled=True)
|
||||
emit_text(content, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Tool call section.
|
||||
emit_special(self._tool_calls_begin, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_calls_begin, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
for tc in tool_calls:
|
||||
func = tc.get("function") or tc
|
||||
name = func.get("name", "")
|
||||
@@ -414,17 +464,28 @@ class DeepSeekV3Renderer:
|
||||
)
|
||||
# Format: <|tool▁call▁begin|>function<|tool▁sep|>{name}\n```json\n{args}\n```<|tool▁call▁end|>
|
||||
# tool_sep is a special token; type ("function") and name+args are plain text.
|
||||
emit_special(self._tool_call_begin, msg_idx, is_sampled=True)
|
||||
emit_text("function", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_sep, msg_idx, is_sampled=True)
|
||||
emit_text(f"{name}\n```json\n{args_str}\n```", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_calls_end, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_call_begin, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_text("function", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._tool_sep, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
f"{name}\n```json\n{args_str}\n```",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(
|
||||
self._tool_call_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(
|
||||
self._tool_calls_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
|
||||
# ``<|end▁of▁sentence|>`` is the model's stop signal — it
|
||||
# samples this to end its turn, so it is part of the sampled
|
||||
# stream.
|
||||
emit_special(self._eos, msg_idx, is_sampled=True)
|
||||
emit_special(self._eos, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Tool (tool-response) rendering
|
||||
@@ -437,10 +498,13 @@ class DeepSeekV3Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# body bytes get ``is_content=True``; the surrounding section
|
||||
# specials are scaffold.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
msg_idx + 1 < len(messages) and messages[msg_idx + 1]["role"] == "tool"
|
||||
@@ -451,11 +515,17 @@ class DeepSeekV3Renderer:
|
||||
content = "".join(p.get("text", "") for p in content if isinstance(p, dict))
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._tool_outputs_begin, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._tool_outputs_begin, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
emit_special(self._tool_output_begin, msg_idx, is_sampled=False)
|
||||
emit_text(str(content), msg_idx, is_sampled=False)
|
||||
emit_special(self._tool_output_end, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._tool_output_begin, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
emit_text(str(content), msg_idx, is_sampled=False, is_content=True)
|
||||
emit_special(self._tool_output_end, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if not next_is_tool:
|
||||
emit_special(self._tool_outputs_end, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._tool_outputs_end, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
+166
-52
@@ -20,6 +20,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
)
|
||||
@@ -128,30 +129,58 @@ class GLM45Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
"""Tokenize concatenated segments as one BPE pass; per-token
|
||||
``is_content`` follows each token's source segment.
|
||||
|
||||
Lets call sites express "this wrap + this body, joined the
|
||||
same way as the chat template, but attributed separately"
|
||||
without splitting the encode call (which could shift BPE
|
||||
merges at the boundary)."""
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── Prefix ──────────────────────────────────────────────────
|
||||
emit_special(self._gmask, -1, is_sampled=False)
|
||||
emit_special(self._sop, -1, is_sampled=False)
|
||||
emit_special(self._gmask, -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._sop, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Tools in system prompt ──────────────────────────────────
|
||||
# The tools-header block is all scaffold by design — the tools
|
||||
# dict is recoverable from the ``tools`` argument; don't
|
||||
# re-attribute the embedded JSON specs as message body.
|
||||
if tools:
|
||||
emit_special(self._system, -1, is_sampled=False)
|
||||
emit_special(self._system, -1, is_sampled=False, is_content=False)
|
||||
tool_text = _TOOLS_HEADER
|
||||
for tool in tools:
|
||||
tool_text += json.dumps(tool, ensure_ascii=False) + "\n"
|
||||
tool_text += _TOOLS_FOOTER
|
||||
emit_text(tool_text, -1, is_sampled=False)
|
||||
emit_text(tool_text, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Compute last_user_index ─────────────────────────────────
|
||||
last_ui = self._last_user_index(messages)
|
||||
@@ -162,15 +191,22 @@ class GLM45Renderer:
|
||||
content = self._visible_text(msg.get("content"))
|
||||
|
||||
if role == "system":
|
||||
emit_special(self._system, i, is_sampled=False)
|
||||
emit_text("\n" + content, i, is_sampled=False)
|
||||
emit_special(self._system, i, is_sampled=False, is_content=False)
|
||||
# ``\n`` is the scaffold separator after the role tag;
|
||||
# the body proper is the caller-provided content.
|
||||
emit_text_segments(
|
||||
[("\n", False), (content, True)], i, is_sampled=False
|
||||
)
|
||||
|
||||
elif role == "user":
|
||||
emit_special(self._user, i, is_sampled=False)
|
||||
user_text = "\n" + content
|
||||
emit_special(self._user, i, is_sampled=False, is_content=False)
|
||||
# ``\n`` is scaffold; ``content`` is body; the optional
|
||||
# ``/nothink`` suffix is scaffold the renderer injects
|
||||
# when ``enable_thinking=False``.
|
||||
user_segments: list[tuple[str, bool]] = [("\n", False), (content, True)]
|
||||
if not self._enable_thinking and not content.endswith("/nothink"):
|
||||
user_text += "/nothink"
|
||||
emit_text(user_text, i, is_sampled=False)
|
||||
user_segments.append(("/nothink", False))
|
||||
emit_text_segments(user_segments, i, is_sampled=False)
|
||||
|
||||
elif role == "assistant":
|
||||
preserve_thinking = should_preserve_past_thinking(
|
||||
@@ -187,25 +223,32 @@ class GLM45Renderer:
|
||||
preserve_thinking=preserve_thinking,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
self._render_tool(
|
||||
messages, i, content, emit_special=emit_special, emit_text=emit_text
|
||||
messages,
|
||||
i,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
# ── Generation prompt ───────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._assistant, -1, is_sampled=False)
|
||||
emit_special(self._assistant, -1, is_sampled=False, is_content=False)
|
||||
if not self._enable_thinking:
|
||||
emit_text("\n", -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False)
|
||||
emit_text("\n", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -276,27 +319,54 @@ class GLM45Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask. Downstream consumers can
|
||||
# run :meth:`RenderedTokens.tokens_per_message` on the bridge
|
||||
# output to get per-new-message token counts without re-rendering.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
@@ -304,20 +374,30 @@ class GLM45Renderer:
|
||||
if role == "user":
|
||||
if not (i == 0 and last_prev == self._user):
|
||||
emit_special(self._user, i)
|
||||
user_text = "\n" + content
|
||||
user_segments: list[tuple[str, bool]] = [
|
||||
("\n", False),
|
||||
(content, True),
|
||||
]
|
||||
if not self._enable_thinking and not content.endswith("/nothink"):
|
||||
user_text += "/nothink"
|
||||
emit_text(user_text, i)
|
||||
user_segments.append(("/nothink", False))
|
||||
emit_text_segments(user_segments, i)
|
||||
elif role == "system":
|
||||
emit_special(self._system, i)
|
||||
emit_text("\n" + content, i)
|
||||
emit_text_segments([("\n", False), (content, True)], i)
|
||||
elif role == "tool":
|
||||
prev_is_tool = i > 0 and new_messages[i - 1].get("role") == "tool"
|
||||
if i == 0 and last_prev == self._observation:
|
||||
pass
|
||||
elif not prev_is_tool:
|
||||
emit_special(self._observation, i)
|
||||
emit_text("\n<tool_response>\n" + content + "\n</tool_response>", i)
|
||||
emit_text_segments(
|
||||
[
|
||||
("\n<tool_response>\n", False),
|
||||
(content, True),
|
||||
("\n</tool_response>", False),
|
||||
],
|
||||
i,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -333,6 +413,7 @@ class GLM45Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -346,6 +427,7 @@ class GLM45Renderer:
|
||||
preserve_thinking: bool = False,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
):
|
||||
reasoning_content = ""
|
||||
if isinstance(msg.get("reasoning_content"), str):
|
||||
@@ -373,23 +455,31 @@ class GLM45Renderer:
|
||||
# turn). So no sampled stop-signal token lives inside this
|
||||
# assistant span — content / think / tool_calls carry the
|
||||
# is_sampled=True signal.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
#
|
||||
# Invariant on assistant tokens: ``is_content == sampled_mask``.
|
||||
# Every scaffold token here gets ``is_sampled=False/is_content=False``;
|
||||
# every model-sampled emit gets both True.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if (msg_idx > last_user_index or preserve_thinking) and reasoning_content:
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_text(reasoning_content.strip(), msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
reasoning_content.strip(), msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Tool calls — keep content + \n contiguous to preserve BPE merges
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
if content.strip() and tool_calls:
|
||||
emit_text("\n" + content.strip() + "\n", msg_idx, is_sampled=True)
|
||||
emit_text(
|
||||
"\n" + content.strip() + "\n", msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
elif content.strip():
|
||||
emit_text("\n" + content.strip(), msg_idx, is_sampled=True)
|
||||
emit_text("\n" + content.strip(), msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
for tc in tool_calls:
|
||||
func = tc.get("function") or tc
|
||||
@@ -397,9 +487,9 @@ class GLM45Renderer:
|
||||
arguments = func.get("arguments", {})
|
||||
|
||||
if not content.strip():
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_tok, msg_idx, is_sampled=True)
|
||||
emit_text(name + "\n", msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._tool_call_tok, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(name + "\n", msg_idx, is_sampled=True, is_content=True)
|
||||
# OpenAI canonical form: arguments is a JSON string. Parse it so the
|
||||
# per-argument rendering below still works.
|
||||
if isinstance(arguments, str):
|
||||
@@ -409,22 +499,33 @@ class GLM45Renderer:
|
||||
arguments = {}
|
||||
if isinstance(arguments, dict):
|
||||
for arg_name, arg_value in arguments.items():
|
||||
emit_special(self._arg_key, msg_idx, is_sampled=True)
|
||||
emit_text(arg_name, msg_idx, is_sampled=True)
|
||||
emit_special(self._arg_key_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._arg_value, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._arg_key, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_text(arg_name, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._arg_key_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._arg_value, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
if isinstance(arg_value, str):
|
||||
emit_text(arg_value, msg_idx, is_sampled=True)
|
||||
emit_text(arg_value, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
emit_text(
|
||||
json.dumps(arg_value, ensure_ascii=False),
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._arg_value_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end_tok, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._arg_value_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end_tok, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -434,17 +535,30 @@ class GLM45Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The body bytes
|
||||
# get ``is_content=True``; the ``\n<tool_response>\n`` /
|
||||
# ``\n</tool_response>`` wraps and the ``<|observation|>`` role
|
||||
# tag are scaffold so the SFT mask for tool body never trains
|
||||
# the model to emit them. Single BPE pass over the joined text
|
||||
# preserves boundary merges (the tool body's leading/trailing
|
||||
# chars can merge with the wrap's ``\n``s if the tokenizer would
|
||||
# do so; we route through ``emit_text_segments`` so the
|
||||
# attribution is offset-driven and tokenizer-agnostic).
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._observation, msg_idx, is_sampled=False)
|
||||
emit_special(self._observation, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
emit_text(
|
||||
"\n<tool_response>\n" + content + "\n</tool_response>",
|
||||
emit_text_segments(
|
||||
[
|
||||
("\n<tool_response>\n", False),
|
||||
(content, True),
|
||||
("\n</tool_response>", False),
|
||||
],
|
||||
msg_idx,
|
||||
is_sampled=False,
|
||||
)
|
||||
|
||||
+140
-47
@@ -21,6 +21,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
)
|
||||
@@ -143,32 +144,61 @@ class GLM5Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
"""Tokenize concatenated segments as one BPE pass; per-token
|
||||
``is_content`` follows each token's source segment.
|
||||
|
||||
Lets call sites express "this wrap + this body, joined the
|
||||
same way as the chat template, but attributed separately"
|
||||
without splitting the encode call (which could shift BPE
|
||||
merges at the boundary)."""
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── Prefix ──────────────────────────────────────────────────
|
||||
# ``[gMASK]<sop>`` is unconditional template scaffolding at the
|
||||
# very start of the stream — the model never samples these.
|
||||
emit_special(self._gmask, -1, is_sampled=False)
|
||||
emit_special(self._sop, -1, is_sampled=False)
|
||||
# very start of the stream — the model never samples these and
|
||||
# they are not part of any message body.
|
||||
emit_special(self._gmask, -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._sop, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Tools in system prompt ──────────────────────────────────
|
||||
# The tools-header block is all scaffold by design — the tools
|
||||
# dict is recoverable from the ``tools`` argument; don't
|
||||
# re-attribute the embedded JSON specs as message body.
|
||||
if tools:
|
||||
emit_special(self._system, -1, is_sampled=False)
|
||||
emit_special(self._system, -1, is_sampled=False, is_content=False)
|
||||
tool_text = _TOOLS_HEADER
|
||||
for tool in tools:
|
||||
tool_text += self._format_tool_spec(tool) + "\n"
|
||||
tool_text += _TOOLS_FOOTER
|
||||
emit_text(tool_text, -1, is_sampled=False)
|
||||
emit_text(tool_text, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Compute last_user_index ─────────────────────────────────
|
||||
last_ui = self._last_user_index(messages)
|
||||
@@ -179,12 +209,12 @@ class GLM5Renderer:
|
||||
content = self._visible_text(msg.get("content"))
|
||||
|
||||
if role == "system":
|
||||
emit_special(self._system, i, is_sampled=False)
|
||||
emit_text(content, i, is_sampled=False)
|
||||
emit_special(self._system, i, is_sampled=False, is_content=False)
|
||||
emit_text(content, i, is_sampled=False, is_content=True)
|
||||
|
||||
elif role == "user":
|
||||
emit_special(self._user, i, is_sampled=False)
|
||||
emit_text(content, i, is_sampled=False)
|
||||
emit_special(self._user, i, is_sampled=False, is_content=False)
|
||||
emit_text(content, i, is_sampled=False, is_content=True)
|
||||
|
||||
elif role == "assistant":
|
||||
preserve_thinking = should_preserve_past_thinking(
|
||||
@@ -201,28 +231,35 @@ class GLM5Renderer:
|
||||
preserve_thinking=preserve_thinking,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
self._render_tool(
|
||||
messages, i, content, emit_special=emit_special, emit_text=emit_text
|
||||
messages,
|
||||
i,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
# ── Generation prompt ───────────────────────────────────────
|
||||
# Gen prompt tokens are what the chat template prepends before
|
||||
# sampling starts — the model continues from these, never emits
|
||||
# them. Always is_sampled=False.
|
||||
# them. Always is_sampled=False / is_content=False.
|
||||
if add_generation_prompt:
|
||||
emit_special(self._assistant, -1, is_sampled=False)
|
||||
emit_special(self._assistant, -1, is_sampled=False, is_content=False)
|
||||
if self._enable_thinking:
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
else:
|
||||
emit_special(self._think_end, -1, is_sampled=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -297,27 +334,54 @@ class GLM5Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask. Downstream consumers can
|
||||
# run :meth:`RenderedTokens.tokens_per_message` on the bridge
|
||||
# output to get per-new-message token counts without re-rendering.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
@@ -326,10 +390,10 @@ class GLM5Renderer:
|
||||
# Dedup: model already emitted <|user|> as its stop token.
|
||||
if not (i == 0 and last_prev == self._user):
|
||||
emit_special(self._user, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
elif role == "system":
|
||||
emit_special(self._system, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
elif role == "tool":
|
||||
prev_is_tool = i > 0 and new_messages[i - 1].get("role") == "tool"
|
||||
if i == 0 and last_prev == self._observation:
|
||||
@@ -338,7 +402,7 @@ class GLM5Renderer:
|
||||
elif not prev_is_tool:
|
||||
emit_special(self._observation, i)
|
||||
emit_special(self._tool_response_tok, i)
|
||||
emit_text(content, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
emit_special(self._tool_response_end_tok, i)
|
||||
else:
|
||||
return None
|
||||
@@ -355,6 +419,7 @@ class GLM5Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -368,6 +433,7 @@ class GLM5Renderer:
|
||||
preserve_thinking: bool = False,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
):
|
||||
reasoning_content = ""
|
||||
if isinstance(msg.get("reasoning_content"), str):
|
||||
@@ -386,7 +452,11 @@ class GLM5Renderer:
|
||||
# samples it. Same for the ``<think>`` open / standalone
|
||||
# ``</think>`` separator that the template wraps around the
|
||||
# assistant body — see the per-branch comments below.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False)
|
||||
#
|
||||
# Invariant on assistant tokens: ``is_content == sampled_mask``.
|
||||
# Every scaffold token here gets ``is_sampled=False/is_content=False``;
|
||||
# every model-sampled emit gets both True.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Chat-template default: keep ``<think>`` only on the in-flight cycle
|
||||
# (post-last-user). Past-cycle assistants drop their reasoning.
|
||||
@@ -403,9 +473,11 @@ class GLM5Renderer:
|
||||
# inference (gen prompt = ``<|assistant|><think>``), so it's
|
||||
# template-injected scaffolding. The reasoning text and the
|
||||
# closing ``</think>`` are what the model actually samples.
|
||||
emit_special(self._think, msg_idx, is_sampled=False)
|
||||
emit_text(reasoning_content.strip(), msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text(
|
||||
reasoning_content.strip(), msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
elif self.empty_think_on_last_assistant and msg_idx > last_user_index:
|
||||
# GLM-5.1: wrap the last assistant with an empty <think></think>
|
||||
# even without reasoning, matching the Jinja template. With
|
||||
@@ -413,26 +485,28 @@ class GLM5Renderer:
|
||||
# ``<think>``; the model then samples ``</think>`` to close an
|
||||
# empty think block. So ``<think>`` is scaffolding,
|
||||
# ``</think>`` is sampled.
|
||||
emit_special(self._think, msg_idx, is_sampled=False)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
# Lone ``</think>`` separator the template injects when no
|
||||
# reasoning is rendered (historical assistants, GLM-5 default
|
||||
# with no thinking). Not sampled.
|
||||
emit_special(self._think_end, msg_idx, is_sampled=False)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if content.strip():
|
||||
emit_text(content.strip(), msg_idx, is_sampled=True)
|
||||
emit_text(content.strip(), msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Tool calls (directly after content, no newlines)
|
||||
# Tool calls (directly after content, no newlines). All of these
|
||||
# are the model's sampled output — both is_sampled and is_content
|
||||
# are True across the entire tool-call span.
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
for tc in tool_calls:
|
||||
func = tc.get("function") or tc
|
||||
name = func.get("name", "")
|
||||
arguments = func.get("arguments", {})
|
||||
|
||||
emit_special(self._tool_call_tok, msg_idx, is_sampled=True)
|
||||
emit_text(name, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_tok, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(name, msg_idx, is_sampled=True, is_content=True)
|
||||
# OpenAI canonical form: arguments is a JSON string. Parse it so the
|
||||
# per-argument rendering below still works.
|
||||
if isinstance(arguments, str):
|
||||
@@ -442,20 +516,31 @@ class GLM5Renderer:
|
||||
arguments = {}
|
||||
if isinstance(arguments, dict):
|
||||
for arg_name, arg_value in arguments.items():
|
||||
emit_special(self._arg_key, msg_idx, is_sampled=True)
|
||||
emit_text(arg_name, msg_idx, is_sampled=True)
|
||||
emit_special(self._arg_key_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._arg_value, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._arg_key, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_text(arg_name, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._arg_key_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(
|
||||
self._arg_value, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
if isinstance(arg_value, str):
|
||||
emit_text(arg_value, msg_idx, is_sampled=True)
|
||||
emit_text(arg_value, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
emit_text(
|
||||
json.dumps(arg_value, ensure_ascii=False),
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._arg_value_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end_tok, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._arg_value_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(
|
||||
self._tool_call_end_tok, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -465,18 +550,26 @@ class GLM5Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The tool body
|
||||
# bytes get ``is_content=True``; the ``<|observation|>`` /
|
||||
# ``<tool_response>`` wraps are scaffold so the SFT mask for
|
||||
# tool body never trains the model to emit them.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._observation, msg_idx, is_sampled=False)
|
||||
emit_special(self._observation, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
emit_special(self._tool_response_tok, msg_idx, is_sampled=False)
|
||||
emit_text(content, msg_idx, is_sampled=False)
|
||||
emit_special(self._tool_response_end_tok, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._tool_response_tok, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
emit_text(content, msg_idx, is_sampled=False, is_content=True)
|
||||
emit_special(
|
||||
self._tool_response_end_tok, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
|
||||
class GLM51Renderer(GLM5Renderer):
|
||||
|
||||
+181
-13
@@ -184,6 +184,86 @@ class GptOssRenderer:
|
||||
return []
|
||||
return self._tokenizer.encode(text, add_special_tokens=False)
|
||||
|
||||
def _prefix_content_mask(
|
||||
self,
|
||||
prefix_tokens: list[int],
|
||||
first_system_idx: int | None,
|
||||
messages: list[Message],
|
||||
tools: list[ToolSpec] | None,
|
||||
) -> list[bool]:
|
||||
"""Per-token is_content mask over the rendered system+developer prefix.
|
||||
|
||||
Harmony's prefix is one opaque block. The caller's system content
|
||||
lands inside the developer message as ``# Instructions\\n\\n{content}``.
|
||||
To attribute body bytes back, we render the same prefix with empty
|
||||
instructions and diff: the unique-to-with-instructions span is the
|
||||
body region. Falls back to all-False (whole prefix scaffold) if
|
||||
the caller didn't supply a system message — there's no body to
|
||||
attribute in that case.
|
||||
"""
|
||||
n = len(prefix_tokens)
|
||||
mask = [False] * n
|
||||
if first_system_idx is None:
|
||||
return mask
|
||||
instructions = _content_text(messages[first_system_idx].get("content"))
|
||||
if not instructions:
|
||||
return mask
|
||||
|
||||
# Build the same prefix with empty instructions.
|
||||
empty_prefix_msgs: list[HarmonyMessage] = []
|
||||
if self._use_system_prompt:
|
||||
sys_content = SystemContent.new().with_reasoning_effort(
|
||||
self._reasoning_effort
|
||||
)
|
||||
sys_content = sys_content.with_conversation_start_date(
|
||||
self._conversation_start_date
|
||||
)
|
||||
if self._knowledge_cutoff is not None:
|
||||
sys_content = sys_content.with_knowledge_cutoff(self._knowledge_cutoff)
|
||||
if self._model_identity is not None:
|
||||
sys_content = sys_content.with_model_identity(self._model_identity)
|
||||
empty_prefix_msgs.append(
|
||||
HarmonyMessage.from_role_and_content(Role.SYSTEM, sys_content)
|
||||
)
|
||||
dev = DeveloperContent.new()
|
||||
if tools:
|
||||
dev = dev.with_function_tools([_tool_to_description(t) for t in tools])
|
||||
empty_prefix_msgs.append(
|
||||
HarmonyMessage.from_role_and_content(Role.DEVELOPER, dev)
|
||||
)
|
||||
try:
|
||||
empty_tokens = self._enc.render_conversation(
|
||||
Conversation.from_messages(empty_prefix_msgs)
|
||||
)
|
||||
except Exception:
|
||||
return mask
|
||||
|
||||
# Longest common prefix.
|
||||
i_start = 0
|
||||
n_empty = len(empty_tokens)
|
||||
while (
|
||||
i_start < min(n, n_empty)
|
||||
and prefix_tokens[i_start] == empty_tokens[i_start]
|
||||
):
|
||||
i_start += 1
|
||||
# Longest common suffix.
|
||||
j_full = n
|
||||
j_empty = n_empty
|
||||
while (
|
||||
j_full > i_start
|
||||
and j_empty > i_start
|
||||
and prefix_tokens[j_full - 1] == empty_tokens[j_empty - 1]
|
||||
):
|
||||
j_full -= 1
|
||||
j_empty -= 1
|
||||
# Tokens [i_start:j_full] in prefix_tokens are unique to the
|
||||
# with-instructions render — that's the body span (includes the
|
||||
# ``# Instructions\n\n`` scaffolding header, which the substring
|
||||
# match in the body-decode test ignores).
|
||||
for k in range(i_start, j_full):
|
||||
mask[k] = True
|
||||
return mask
|
||||
|
||||
# ── public interface ─────────────────────────────────────────────────────
|
||||
|
||||
def render(
|
||||
@@ -199,11 +279,15 @@ class GptOssRenderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_harmony_message(
|
||||
hm_ids: list[int], msg_idx: int, *, is_assistant: bool
|
||||
@@ -225,18 +309,53 @@ class GptOssRenderer:
|
||||
developer, tool) the whole message is conversation history
|
||||
the model never samples — every token is
|
||||
``is_sampled=False``.
|
||||
|
||||
``is_content`` further splits the body: the trailing
|
||||
terminator (``<|end|>`` / ``<|return|>`` / ``<|call|>``) is
|
||||
scaffold on non-assistant turns; on assistant turns it's the
|
||||
model's stop signal, so ``is_content=True`` mirrors
|
||||
``sampled_mask`` as the invariant on assistant requires.
|
||||
The body bytes between ``<|message|>`` and the terminator
|
||||
are body (``is_content=True``) on every role — that's the
|
||||
caller-provided content (or, for assistant, the model's
|
||||
sampled emission). The header (``<|start|>`` ... ``<|message|>``)
|
||||
— including any ``functions.{name}`` recipient text on tool
|
||||
results, which comes from a prior assistant's tool_calls
|
||||
rather than this tool message's own content — is scaffold.
|
||||
"""
|
||||
try:
|
||||
msg_marker = hm_ids.index(self._message)
|
||||
except ValueError:
|
||||
# Defensive: a harmony message without <|message|> is
|
||||
# malformed. Treat the whole thing as scaffolding.
|
||||
emit(hm_ids, msg_idx, is_sampled=False)
|
||||
emit(hm_ids, msg_idx, is_sampled=False, is_content=False)
|
||||
return
|
||||
header = hm_ids[: msg_marker + 1]
|
||||
body = hm_ids[msg_marker + 1 :]
|
||||
emit(header, msg_idx, is_sampled=False)
|
||||
emit(body, msg_idx, is_sampled=is_assistant)
|
||||
emit(header, msg_idx, is_sampled=False, is_content=False)
|
||||
# Split body into content + terminator. The terminator (if
|
||||
# present) is the last token of the body and is one of the
|
||||
# three harmony stop tokens.
|
||||
terminator_ids = {self._end, self._return, self._call}
|
||||
if body and body[-1] in terminator_ids:
|
||||
body_content = body[:-1]
|
||||
terminator = body[-1:]
|
||||
else:
|
||||
body_content = body
|
||||
terminator = []
|
||||
emit(
|
||||
body_content,
|
||||
msg_idx,
|
||||
is_sampled=is_assistant,
|
||||
is_content=True,
|
||||
)
|
||||
if terminator:
|
||||
emit(
|
||||
terminator,
|
||||
msg_idx,
|
||||
is_sampled=is_assistant,
|
||||
is_content=is_assistant,
|
||||
)
|
||||
|
||||
# ── Build harmony prefix (system + developer) ───────────────────
|
||||
# When tools are present, harmony's conversation-level renderer
|
||||
@@ -285,7 +404,23 @@ class GptOssRenderer:
|
||||
# caller-relative attribution); otherwise to -1 (pure scaffolding).
|
||||
# The whole prefix is pure template scaffolding — never sampled.
|
||||
prefix_origin = first_system_idx if first_system_idx is not None else -1
|
||||
emit(prefix_tokens, prefix_origin, is_sampled=False)
|
||||
# Compute the body-token span inside the prefix by diffing
|
||||
# against the same render with empty developer instructions.
|
||||
# Tokens unique to the with-instructions render are the body
|
||||
# span (``# Instructions\n\n{caller_system_content}``). Marking
|
||||
# those is_content=True so the caller's system text is
|
||||
# recoverable from ``content_token_spans_by_role()["system"]``.
|
||||
# The scaffolding ``# Instructions\n\n`` prefix bleeds into
|
||||
# the body run; consumers reading the body do a substring
|
||||
# check rather than expecting an exact match.
|
||||
prefix_content_mask = self._prefix_content_mask(
|
||||
prefix_tokens, first_system_idx, messages, tools
|
||||
)
|
||||
for tid, is_content in zip(prefix_tokens, prefix_content_mask):
|
||||
tokens.append(tid)
|
||||
indices.append(prefix_origin)
|
||||
sampled.append(False)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── Iterate the rest of the messages ────────────────────────────
|
||||
last_idx = len(messages) - 1
|
||||
@@ -326,16 +461,17 @@ class GptOssRenderer:
|
||||
# ── Generation prompt: <|start|>assistant<|channel|>analysis<|message|>
|
||||
# Pure template scaffolding the model continues from — never sampled.
|
||||
if add_generation_prompt:
|
||||
emit([self._start], -1, is_sampled=False)
|
||||
emit(self._encode("assistant"), -1, is_sampled=False)
|
||||
emit([self._channel], -1, is_sampled=False)
|
||||
emit(self._encode("analysis"), -1, is_sampled=False)
|
||||
emit([self._message], -1, is_sampled=False)
|
||||
emit([self._start], -1, is_sampled=False, is_content=False)
|
||||
emit(self._encode("assistant"), -1, is_sampled=False, is_content=False)
|
||||
emit([self._channel], -1, is_sampled=False, is_content=False)
|
||||
emit(self._encode("analysis"), -1, is_sampled=False, is_content=False)
|
||||
emit([self._message], -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -407,17 +543,47 @@ class GptOssRenderer:
|
||||
# and ``sampled_mask`` (uniformly ``False``). The harmony encoder
|
||||
# renders each ``new_messages[i]`` as a single block, so every
|
||||
# token in that block carries index ``i``; the trailing
|
||||
# generation prompt uses ``-1``.
|
||||
# generation prompt uses ``-1``. ``is_content`` follows the same
|
||||
# rules as :meth:`render`'s ``emit_harmony_message``: header is
|
||||
# scaffold, body bytes are body, terminator scaffold (the bridge
|
||||
# never carries assistant turns, so terminators are always
|
||||
# scaffold on the non-assistant roles the bridge accepts).
|
||||
terminator_ids = {self._end, self._return, self._call}
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_content: list[bool] = []
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
if role not in ("tool", "user", "system", "developer"):
|
||||
return None
|
||||
for hm in self._to_harmony_messages(msg):
|
||||
ids = self._enc.render(hm)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([i] * len(ids))
|
||||
try:
|
||||
msg_marker = ids.index(self._message)
|
||||
except ValueError:
|
||||
# Defensive: treat as scaffolding.
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([i] * len(ids))
|
||||
ext_content.extend([False] * len(ids))
|
||||
continue
|
||||
header = ids[: msg_marker + 1]
|
||||
body = ids[msg_marker + 1 :]
|
||||
ext.extend(header)
|
||||
ext_indices.extend([i] * len(header))
|
||||
ext_content.extend([False] * len(header))
|
||||
if body and body[-1] in terminator_ids:
|
||||
body_content = body[:-1]
|
||||
terminator = body[-1:]
|
||||
else:
|
||||
body_content = body
|
||||
terminator = []
|
||||
ext.extend(body_content)
|
||||
ext_indices.extend([i] * len(body_content))
|
||||
ext_content.extend([True] * len(body_content))
|
||||
if terminator:
|
||||
ext.extend(terminator)
|
||||
ext_indices.extend([i] * len(terminator))
|
||||
ext_content.extend([False] * len(terminator))
|
||||
|
||||
# Generation prompt: <|start|>assistant<|channel|>analysis<|message|>
|
||||
gen_before = len(ext)
|
||||
@@ -427,12 +593,14 @@ class GptOssRenderer:
|
||||
ext.extend(self._encode("analysis"))
|
||||
ext.append(self._message)
|
||||
ext_indices.extend([-1] * (len(ext) - gen_before))
|
||||
ext_content.extend([False] * (len(ext) - gen_before))
|
||||
|
||||
total_len = len(previous_ids) + len(ext)
|
||||
return RenderedTokens(
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
|
||||
+167
-71
@@ -118,6 +118,11 @@ class KimiK2Renderer:
|
||||
if not messages:
|
||||
raise ValueError("No messages provided.")
|
||||
|
||||
# Preserve the caller's list — ``message_roles`` and per-token
|
||||
# attribution refer to this frame (not the post-normalisation
|
||||
# list that includes auto-injected system / tool_declare).
|
||||
caller_messages = list(messages)
|
||||
|
||||
# Inject tools as tool_declare message + ensure system message.
|
||||
# The Jinja template emits the tools list directly (no
|
||||
# ``{"type":"function","function":...}`` wrapper) using
|
||||
@@ -166,19 +171,33 @@ class KimiK2Renderer:
|
||||
token_ids: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
token_ids.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
token_ids.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
emit_ids(
|
||||
self._encode(text),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
|
||||
# Compute last non-tool-call assistant index to determine thinking preservation
|
||||
last_plain_assistant_idx = -1
|
||||
@@ -206,31 +225,40 @@ class KimiK2Renderer:
|
||||
content = "".join(parts)
|
||||
|
||||
oi = orig_idx(i)
|
||||
# Auto-injected system / tool_declare messages have ``oi == -1``.
|
||||
# Their text isn't from the caller's input, so we treat the
|
||||
# whole emission as scaffold (``is_content=False`` everywhere).
|
||||
# The test contract is that ``msg_idx == -1`` runs are
|
||||
# template-only and ``is_content=False``.
|
||||
body_is_content = oi >= 0
|
||||
|
||||
if role == "system":
|
||||
emit_special(self._im_system, oi, is_sampled=False)
|
||||
emit_text("system", oi, is_sampled=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False)
|
||||
emit_text(content, oi, is_sampled=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False)
|
||||
emit_special(self._im_system, oi, is_sampled=False, is_content=False)
|
||||
emit_text("system", oi, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False, is_content=False)
|
||||
emit_text(content, oi, is_sampled=False, is_content=body_is_content)
|
||||
emit_special(self._im_end, oi, is_sampled=False, is_content=False)
|
||||
# Jinja emits a literal newline only after the auto-injected
|
||||
# system's <|im_end|> (see _ensure_system_message's contract).
|
||||
if i == auto_system_idx:
|
||||
emit_text("\n", oi, is_sampled=False)
|
||||
emit_text("\n", oi, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "tool_declare":
|
||||
emit_special(self._im_system, oi, is_sampled=False)
|
||||
emit_text("tool_declare", oi, is_sampled=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False)
|
||||
emit_text(content, oi, is_sampled=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False)
|
||||
# The tool_declare body is the tools JSON — recoverable
|
||||
# from the caller's ``tools`` argument, so we treat it as
|
||||
# scaffold (consistent with Qwen3's tools-header block).
|
||||
emit_special(self._im_system, oi, is_sampled=False, is_content=False)
|
||||
emit_text("tool_declare", oi, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False, is_content=False)
|
||||
emit_text(content, oi, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "user":
|
||||
emit_special(self._im_user, oi, is_sampled=False)
|
||||
emit_text("user", oi, is_sampled=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False)
|
||||
emit_text(content, oi, is_sampled=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False)
|
||||
emit_special(self._im_user, oi, is_sampled=False, is_content=False)
|
||||
emit_text("user", oi, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False, is_content=False)
|
||||
emit_text(content, oi, is_sampled=False, is_content=body_is_content)
|
||||
emit_special(self._im_end, oi, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
# Kimi strips reasoning from historical assistant turns and
|
||||
@@ -250,30 +278,35 @@ class KimiK2Renderer:
|
||||
|
||||
elif role == "tool":
|
||||
self._render_tool(
|
||||
msg, oi, content, emit_special=emit_special, emit_text=emit_text
|
||||
msg,
|
||||
oi,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
)
|
||||
|
||||
else:
|
||||
# Unknown role: use system-style formatting. Not a sampled
|
||||
# assistant turn — every token is template-injected from the
|
||||
# caller's POV, so is_sampled=False across the whole emission.
|
||||
emit_special(self._im_system, oi, is_sampled=False)
|
||||
emit_text(role, oi, is_sampled=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False)
|
||||
emit_text(content, oi, is_sampled=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False)
|
||||
emit_special(self._im_system, oi, is_sampled=False, is_content=False)
|
||||
emit_text(role, oi, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, oi, is_sampled=False, is_content=False)
|
||||
emit_text(content, oi, is_sampled=False, is_content=body_is_content)
|
||||
emit_special(self._im_end, oi, is_sampled=False, is_content=False)
|
||||
|
||||
# Generation prompt
|
||||
if add_generation_prompt:
|
||||
emit_special(self._im_assistant, -1, is_sampled=False)
|
||||
emit_text("assistant", -1, is_sampled=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False)
|
||||
emit_special(self._im_assistant, -1, is_sampled=False, is_content=False)
|
||||
emit_text("assistant", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=token_ids,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in caller_messages],
|
||||
)
|
||||
|
||||
def render_ids(
|
||||
@@ -336,27 +369,40 @@ class KimiK2Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask. Downstream consumers can
|
||||
# run :meth:`RenderedTokens.tokens_per_message` on the bridge
|
||||
# output to get per-new-message token counts without re-rendering.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
@@ -376,34 +422,39 @@ class KimiK2Renderer:
|
||||
content = "".join(parts)
|
||||
|
||||
if role == "user":
|
||||
emit_special(self._im_user, i, is_sampled=False)
|
||||
emit_text("user", i, is_sampled=False)
|
||||
emit_special(self._im_middle, i, is_sampled=False)
|
||||
emit_text(content, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_special(self._im_user, i)
|
||||
emit_text("user", i)
|
||||
emit_special(self._im_middle, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
emit_special(self._im_end, i)
|
||||
elif role == "system":
|
||||
emit_special(self._im_system, i, is_sampled=False)
|
||||
emit_text("system", i, is_sampled=False)
|
||||
emit_special(self._im_middle, i, is_sampled=False)
|
||||
emit_text(content, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_special(self._im_system, i)
|
||||
emit_text("system", i)
|
||||
emit_special(self._im_middle, i)
|
||||
emit_text(content, i, is_content=True)
|
||||
emit_special(self._im_end, i)
|
||||
elif role == "tool":
|
||||
self._render_tool(
|
||||
msg, i, content, emit_special=emit_special, emit_text=emit_text
|
||||
msg,
|
||||
i,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
||||
# Generation prompt.
|
||||
emit_special(self._im_assistant, -1, is_sampled=False)
|
||||
emit_text("assistant", -1, is_sampled=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False)
|
||||
emit_special(self._im_assistant, -1)
|
||||
emit_text("assistant", -1)
|
||||
emit_special(self._im_middle, -1)
|
||||
|
||||
total_len = len(previous_ids) + len(ext)
|
||||
return RenderedTokens(
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -421,23 +472,32 @@ class KimiK2Renderer:
|
||||
# scaffolding — at inference the chat template emits these as the
|
||||
# generation prompt and the model never samples them. Marking the
|
||||
# role tag as ``is_sampled=False`` keeps the SFT loss mask aligned
|
||||
# with what the model would actually have produced.
|
||||
emit_special(self._im_assistant, msg_idx, is_sampled=False)
|
||||
emit_text("assistant", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_middle, msg_idx, is_sampled=False)
|
||||
# with what the model would actually have produced. ``is_content``
|
||||
# is also False here — the role tag isn't part of any message's
|
||||
# body, on any role.
|
||||
emit_special(self._im_assistant, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("assistant", msg_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Kimi K2's Jinja template has no reasoning-content support: the
|
||||
# assistant turn renders its ``content`` verbatim, including any
|
||||
# inline ``<think>...</think>`` tags. The separate
|
||||
# ``reasoning_content`` field is dropped (the template never reads
|
||||
# it). ``is_last_turn`` is unused here for the same reason.
|
||||
# On assistant tokens, ``is_content == sampled_mask`` by construction
|
||||
# — every sampled token is body, every scaffold token isn't.
|
||||
_ = is_last_turn
|
||||
emit_text(content, msg_idx, is_sampled=True)
|
||||
emit_text(content, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Tool calls
|
||||
# Tool calls — model-sampled markup carrying caller / model body.
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
if tool_calls:
|
||||
emit_special(self._tool_calls_section_begin, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_calls_section_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
for tc in tool_calls:
|
||||
func = tc.get("function") or tc
|
||||
arguments = func.get("arguments", {})
|
||||
@@ -451,16 +511,37 @@ class KimiK2Renderer:
|
||||
# caller to provide an id in ``functions.{name}:{idx}`` form
|
||||
# (that's where the Kimi parser recovers the function name).
|
||||
tc_id = tc.get("id") or ""
|
||||
emit_special(self._tool_call_begin, msg_idx, is_sampled=True)
|
||||
emit_text(tc_id, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_argument_begin, msg_idx, is_sampled=True)
|
||||
emit_text(args_str, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_calls_section_end, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_call_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_text(tc_id, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_argument_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_text(args_str, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(
|
||||
self._tool_calls_section_end,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples this to
|
||||
# end its turn, so it is part of the sampled stream.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True)
|
||||
# end its turn, so it is part of the sampled stream (and the
|
||||
# assistant's body).
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -473,13 +554,28 @@ class KimiK2Renderer:
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# field's body bytes get ``is_content=True``; everything else —
|
||||
# the ``<|im_system|>name<|im_middle|>`` wrap, the ``## Return of
|
||||
# …\n`` header (template-synthesised, not part of the body) —
|
||||
# is scaffold so the SFT mask for tool body never trains the
|
||||
# model to emit them.
|
||||
#
|
||||
# We keep the original kimi_k2 emit boundaries — the header and
|
||||
# the content are encoded separately, which preserves the
|
||||
# template's byte-identity since the original code also emitted
|
||||
# them as separate ``encode`` calls.
|
||||
name = msg.get("name") or "tool"
|
||||
tool_call_id = msg.get("tool_call_id") or ""
|
||||
|
||||
emit_special(self._im_system, msg_idx, is_sampled=False)
|
||||
emit_text(name, msg_idx, is_sampled=False)
|
||||
emit_special(self._im_middle, msg_idx, is_sampled=False)
|
||||
emit_text(f"## Return of {tool_call_id}\n", msg_idx, is_sampled=False)
|
||||
emit_text(content, msg_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False)
|
||||
emit_special(self._im_system, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text(name, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text(
|
||||
f"## Return of {tool_call_id}\n",
|
||||
msg_idx,
|
||||
is_sampled=False,
|
||||
is_content=False,
|
||||
)
|
||||
emit_text(content, msg_idx, is_sampled=False, is_content=True)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
+188
-65
@@ -213,7 +213,9 @@ class _EnumType(_BaseType):
|
||||
self.enum = schema["enum"]
|
||||
|
||||
def to_typescript_style(self, indent: str = "") -> str:
|
||||
return " | ".join(f'"{e}"' if isinstance(e, str) else str(e) for e in self.enum)
|
||||
return " | ".join(
|
||||
f'"{e}"' if isinstance(e, str) else json.dumps(e) for e in self.enum
|
||||
)
|
||||
|
||||
|
||||
class _AnyOfType(_BaseType):
|
||||
@@ -283,11 +285,7 @@ class _TypedParam:
|
||||
def to_typescript_style(self, indent: str = "") -> str:
|
||||
comments = self.type_.format_docstring(indent)
|
||||
if self.default is not None:
|
||||
default_repr = (
|
||||
json.dumps(self.default, ensure_ascii=False)
|
||||
if not isinstance(self.default, (int, float, bool))
|
||||
else repr(self.default)
|
||||
)
|
||||
default_repr = json.dumps(self.default, ensure_ascii=False)
|
||||
comments += f"{indent}// Default: {default_repr}\n"
|
||||
opt = "?" if self.optional else ""
|
||||
return (
|
||||
@@ -749,24 +747,44 @@ class KimiK25Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
mm_hashes: dict[str, list[str]] = {}
|
||||
mm_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
mm_items: dict[str, list[dict[str, Any]]] = {}
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
emit_ids(
|
||||
self._encode(text),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
|
||||
def emit_image(part: dict[str, Any], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_image(
|
||||
part: dict[str, Any],
|
||||
msg_idx: int,
|
||||
*,
|
||||
is_sampled: bool,
|
||||
is_content: bool,
|
||||
) -> None:
|
||||
"""Emit Kimi K2.5's image wrap and accumulate ``mm_data``.
|
||||
|
||||
Template-equivalent expansion per image:
|
||||
@@ -779,15 +797,32 @@ class KimiK25Renderer:
|
||||
Kimi's chat template after every image — kept here verbatim
|
||||
for byte-parity, regardless of what follows (more images,
|
||||
text, or the ``<|im_end|>`` close).
|
||||
|
||||
``is_content`` attribution: ``<|media_pad|>`` represents
|
||||
caller-provided image data — body. The wrap tokens
|
||||
(``<|media_begin|>``, the literal ``"image"`` prose,
|
||||
``<|media_content|>``, ``<|media_end|>``, the trailing
|
||||
``\\n``) are template-injected scaffold.
|
||||
"""
|
||||
_, out, _num_patches, h = self._process_image(part)
|
||||
emit_special(self._media_begin, msg_idx, is_sampled=is_sampled)
|
||||
emit_text("image", msg_idx, is_sampled=is_sampled)
|
||||
emit_special(self._media_content, msg_idx, is_sampled=is_sampled)
|
||||
emit_special(
|
||||
self._media_begin, msg_idx, is_sampled=is_sampled, is_content=False
|
||||
)
|
||||
emit_text("image", msg_idx, is_sampled=is_sampled, is_content=False)
|
||||
emit_special(
|
||||
self._media_content, msg_idx, is_sampled=is_sampled, is_content=False
|
||||
)
|
||||
offset = len(tokens)
|
||||
emit_special(self._media_pad, msg_idx, is_sampled=is_sampled)
|
||||
emit_special(self._media_end, msg_idx, is_sampled=is_sampled)
|
||||
emit_text("\n", msg_idx, is_sampled=is_sampled)
|
||||
emit_special(
|
||||
self._media_pad,
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
emit_special(
|
||||
self._media_end, msg_idx, is_sampled=is_sampled, is_content=False
|
||||
)
|
||||
emit_text("\n", msg_idx, is_sampled=is_sampled, is_content=False)
|
||||
mm_hashes.setdefault("image", []).append(h)
|
||||
mm_placeholders.setdefault("image", []).append(
|
||||
PlaceholderRange(offset=offset, length=1)
|
||||
@@ -807,13 +842,16 @@ class KimiK25Renderer:
|
||||
# K2.5/K2.6's tokenizer auto-computes ``tools_ts_str`` and threads
|
||||
# it into apply_chat_template, so the template's TS branch always
|
||||
# fires when tools are present. Match that with our own TS encoder.
|
||||
# The tools-TS body is recoverable from the ``tools`` argument, so
|
||||
# we attribute the entire tool_declare emission as scaffold —
|
||||
# consistent with Qwen3's tools-header treatment.
|
||||
if tools:
|
||||
tools_ts = _encode_tools_typescript(tools)
|
||||
emit_special(self._im_system, -1, is_sampled=False)
|
||||
emit_text("tool_declare", -1, is_sampled=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False)
|
||||
emit_text(tools_ts, -1, is_sampled=False)
|
||||
emit_special(self._im_end, -1, is_sampled=False)
|
||||
emit_special(self._im_system, -1, is_sampled=False, is_content=False)
|
||||
emit_text("tool_declare", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False, is_content=False)
|
||||
emit_text(tools_ts, -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_end, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Iterate messages ─────────────────────────────────────────
|
||||
for i, msg in enumerate(messages):
|
||||
@@ -824,14 +862,14 @@ class KimiK25Renderer:
|
||||
# generation prompt emits them at inference for assistants;
|
||||
# user / system / tool roles are conversation history).
|
||||
if role == "user":
|
||||
emit_special(self._im_user, i, is_sampled=False)
|
||||
emit_special(self._im_user, i, is_sampled=False, is_content=False)
|
||||
elif role == "assistant":
|
||||
emit_special(self._im_assistant, i, is_sampled=False)
|
||||
emit_special(self._im_assistant, i, is_sampled=False, is_content=False)
|
||||
else:
|
||||
emit_special(self._im_system, i, is_sampled=False)
|
||||
emit_special(self._im_system, i, is_sampled=False, is_content=False)
|
||||
role_name = msg.get("name") or role
|
||||
emit_text(role_name, i, is_sampled=False)
|
||||
emit_special(self._im_middle, i, is_sampled=False)
|
||||
emit_text(role_name, i, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, i, is_sampled=False, is_content=False)
|
||||
|
||||
# Body
|
||||
if role == "assistant":
|
||||
@@ -852,10 +890,10 @@ class KimiK25Renderer:
|
||||
)
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples
|
||||
# this to end its turn, so it is part of the sampled
|
||||
# stream. Kimi K2.5 has no inter-turn ``\n`` separator
|
||||
# (unlike Qwen3), so the turn-close token is the last
|
||||
# sampled token.
|
||||
emit_special(self._im_end, i, is_sampled=True)
|
||||
# stream (and the assistant's body). Kimi K2.5 has no
|
||||
# inter-turn ``\n`` separator (unlike Qwen3), so the
|
||||
# turn-close token is the last sampled token.
|
||||
emit_special(self._im_end, i, is_sampled=True, is_content=True)
|
||||
continue
|
||||
elif role == "tool":
|
||||
self._render_tool_body(
|
||||
@@ -869,7 +907,8 @@ class KimiK25Renderer:
|
||||
elif msg.get("content") is not None:
|
||||
# User / other content branches — images allowed. All
|
||||
# non-assistant content is conversation history, never
|
||||
# sampled by the model.
|
||||
# sampled by the model. The body is caller-provided, so
|
||||
# ``is_content=True`` over the content emit.
|
||||
self._emit_content(
|
||||
msg.get("content"),
|
||||
i,
|
||||
@@ -878,21 +917,22 @@ class KimiK25Renderer:
|
||||
emit_ids,
|
||||
emit_image=emit_image,
|
||||
is_sampled=False,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Generation prompt ────────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._im_assistant, -1, is_sampled=False)
|
||||
emit_text("assistant", -1, is_sampled=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False)
|
||||
emit_special(self._im_assistant, -1, is_sampled=False, is_content=False)
|
||||
emit_text("assistant", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._im_middle, -1, is_sampled=False, is_content=False)
|
||||
if self._enable_thinking:
|
||||
# Prefill open <think> tag to trigger thinking mode
|
||||
emit_text("<think>", -1, is_sampled=False)
|
||||
emit_text("<think>", -1, is_sampled=False, is_content=False)
|
||||
else:
|
||||
# Empty <think></think> to disable thinking
|
||||
emit_text("<think></think>", -1, is_sampled=False)
|
||||
emit_text("<think></think>", -1, is_sampled=False, is_content=False)
|
||||
|
||||
mm_data: MultiModalData | None = None
|
||||
if mm_hashes or mm_placeholders or mm_items:
|
||||
@@ -906,6 +946,7 @@ class KimiK25Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -997,49 +1038,74 @@ class KimiK25Renderer:
|
||||
|
||||
# Seed combined-token list with prior turn so placeholder offsets
|
||||
# are absolute in the bridged sequence. Parallel
|
||||
# ``indices``/``sampled`` are seeded with ``-1``/``False`` for the
|
||||
# prior portion — the bridge has no attribution info for
|
||||
# ``previous_ids``. Bridge-added tokens get proper ``msg_idx``
|
||||
# (relative to ``new_messages``) and uniformly ``False``
|
||||
# ``sampled``: nothing the bridge emits was model-sampled.
|
||||
# ``indices``/``sampled``/``content_mask`` are seeded with
|
||||
# ``-1``/``False``/``False`` for the prior portion — the bridge
|
||||
# has no attribution info for ``previous_ids``. Bridge-added
|
||||
# tokens get proper ``msg_idx`` (relative to ``new_messages``)
|
||||
# and uniformly ``False`` ``sampled``: nothing the bridge emits
|
||||
# was model-sampled. ``is_content`` follows the same rules as in
|
||||
# :meth:`render` so consumers can walk the trajectory and read
|
||||
# each step's own body mask.
|
||||
tokens: list[int] = list(previous_ids)
|
||||
indices: list[int] = [-1] * len(previous_ids)
|
||||
sampled: list[bool] = [False] * len(previous_ids)
|
||||
content_mask: list[bool] = [False] * len(previous_ids)
|
||||
new_hashes: dict[str, list[str]] = {}
|
||||
new_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
new_items: dict[str, list[dict[str, Any]]] = {}
|
||||
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int = -1, *, is_sampled: bool = False
|
||||
ids: list[int],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_image(
|
||||
part: dict[str, Any], msg_idx: int = -1, *, is_sampled: bool = False
|
||||
part: dict[str, Any],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
_, out, _num_patches, h = self._process_image(part)
|
||||
emit_special(self._media_begin, msg_idx)
|
||||
emit_text("image", msg_idx)
|
||||
emit_special(self._media_content, msg_idx)
|
||||
offset = len(tokens)
|
||||
emit_special(self._media_pad, msg_idx)
|
||||
# ``<|media_pad|>`` stands in for caller-provided image data —
|
||||
# mark it as body when the surrounding content is body.
|
||||
emit_special(self._media_pad, msg_idx, is_content=is_content)
|
||||
emit_special(self._media_end, msg_idx)
|
||||
emit_text("\n", msg_idx)
|
||||
new_hashes.setdefault("image", []).append(h)
|
||||
@@ -1086,6 +1152,7 @@ class KimiK25Renderer:
|
||||
emit_ids,
|
||||
emit_image=emit_image,
|
||||
is_sampled=False,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
emit_special(self._im_end, i)
|
||||
@@ -1128,6 +1195,7 @@ class KimiK25Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=bridge_roles,
|
||||
)
|
||||
|
||||
@@ -1140,6 +1208,7 @@ class KimiK25Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=bridge_roles,
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -1158,6 +1227,7 @@ class KimiK25Renderer:
|
||||
*,
|
||||
emit_image=None,
|
||||
is_sampled: bool,
|
||||
is_content: bool = True,
|
||||
) -> None:
|
||||
"""Emit message content, handling strings, multipart lists, and (when
|
||||
``emit_image`` is supplied) image parts.
|
||||
@@ -1172,11 +1242,15 @@ class KimiK25Renderer:
|
||||
``bridge_to_next_turn``), so consecutive images naturally
|
||||
produce the template's ``...<|media_end|>\\n<|media_begin|>...``
|
||||
pattern without an inter-image separator here.
|
||||
|
||||
``is_content`` propagates to text emits and to the
|
||||
``<|media_pad|>`` body token of each image; the surrounding
|
||||
media wrap tokens are always scaffold (handled by ``emit_image``).
|
||||
"""
|
||||
if content is None:
|
||||
return
|
||||
if isinstance(content, str):
|
||||
emit_text(content, msg_idx, is_sampled=is_sampled)
|
||||
emit_text(content, msg_idx, is_sampled=is_sampled, is_content=is_content)
|
||||
return
|
||||
if isinstance(content, list):
|
||||
for part in content:
|
||||
@@ -1189,20 +1263,30 @@ class KimiK25Renderer:
|
||||
if emit_image is None:
|
||||
# Silently drop — caller didn't opt into multimodal.
|
||||
continue
|
||||
emit_image(part, msg_idx, is_sampled=is_sampled)
|
||||
emit_image(
|
||||
part, msg_idx, is_sampled=is_sampled, is_content=is_content
|
||||
)
|
||||
continue
|
||||
if is_video:
|
||||
raise NotImplementedError(
|
||||
"Video parts are not yet supported by KimiK25Renderer."
|
||||
)
|
||||
if ptype == "text":
|
||||
emit_text(part.get("text", ""), msg_idx, is_sampled=is_sampled)
|
||||
emit_text(
|
||||
part.get("text", ""),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
elif ptype == "thinking":
|
||||
# Thinking parts in non-assistant roles are rendered as text
|
||||
thinking = part.get("thinking", "")
|
||||
if thinking:
|
||||
emit_text(
|
||||
f"<think>{thinking}</think>", msg_idx, is_sampled=is_sampled
|
||||
f"<think>{thinking}</think>",
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
# Other part types are silently skipped
|
||||
|
||||
@@ -1265,16 +1349,27 @@ class KimiK25Renderer:
|
||||
# block, text content, and any tool_calls section all live in
|
||||
# the sampled stream. The closing ``<|im_end|>`` is emitted by
|
||||
# ``render`` (also is_sampled=True; it's the model's stop
|
||||
# signal).
|
||||
# signal). On assistant tokens ``is_content == sampled_mask`` by
|
||||
# construction.
|
||||
if is_suffix or (preserve_thinking and reasoning_content):
|
||||
emit_text(f"<think>{reasoning_content}</think>", msg_idx, is_sampled=True)
|
||||
emit_text(
|
||||
f"<think>{reasoning_content}</think>",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
else:
|
||||
emit_text("<think></think>", msg_idx, is_sampled=True)
|
||||
emit_text(text_content, msg_idx, is_sampled=True)
|
||||
emit_text("<think></think>", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(text_content, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
if tool_calls:
|
||||
emit_special(self._tool_calls_section_begin, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_calls_section_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
for tc in tool_calls:
|
||||
func = tc.get("function") or tc
|
||||
arguments = func.get("arguments", {})
|
||||
@@ -1288,12 +1383,32 @@ class KimiK25Renderer:
|
||||
# ``functions.{name}:{idx}`` form (Kimi's parser recovers
|
||||
# the function name from that field).
|
||||
tool_id = tc.get("id") or ""
|
||||
emit_special(self._tool_call_begin, msg_idx, is_sampled=True)
|
||||
emit_text(tool_id, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_argument_begin, msg_idx, is_sampled=True)
|
||||
emit_text(args_str, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_calls_section_end, msg_idx, is_sampled=True)
|
||||
emit_special(
|
||||
self._tool_call_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_text(tool_id, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_argument_begin,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_text(args_str, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(
|
||||
self._tool_calls_section_end,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
def _render_tool_body(
|
||||
self,
|
||||
@@ -1319,9 +1434,16 @@ class KimiK25Renderer:
|
||||
"""
|
||||
# Tool messages are conversation-history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``## Return
|
||||
# of …\n`` header is template-synthesised scaffold; the
|
||||
# ``content`` body bytes get ``is_content=True``.
|
||||
tool_call_id = msg.get("tool_call_id") or ""
|
||||
emit_text(f"## Return of {tool_call_id}\n", msg_idx, is_sampled=False)
|
||||
emit_text(
|
||||
f"## Return of {tool_call_id}\n",
|
||||
msg_idx,
|
||||
is_sampled=False,
|
||||
is_content=False,
|
||||
)
|
||||
content = msg.get("content")
|
||||
if content is not None:
|
||||
self._emit_content(
|
||||
@@ -1332,6 +1454,7 @@ class KimiK25Renderer:
|
||||
emit_ids,
|
||||
emit_image=emit_image,
|
||||
is_sampled=False,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
def _normalize_response_tokens(self, response: list[int]) -> list[int]:
|
||||
|
||||
+132
-43
@@ -35,6 +35,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
)
|
||||
from renderers.parsing import parse_laguna_xs2
|
||||
@@ -154,24 +155,43 @@ class LagunaXS2Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
emit_special(self._eos, -1, is_sampled=False)
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
emit_special(self._eos, -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── System header (absorbs messages[0] if it's a system message) ──
|
||||
system_content = _DEFAULT_SYSTEM_MESSAGE
|
||||
system_msg_idx = -1
|
||||
if messages and messages[0].get("role") == "system":
|
||||
caller_has_system = bool(messages and messages[0].get("role") == "system")
|
||||
if caller_has_system:
|
||||
system_content = self._visible_text(messages[0].get("content"))
|
||||
system_msg_idx = 0
|
||||
|
||||
@@ -187,9 +207,18 @@ class LagunaXS2Renderer:
|
||||
"<system>\n\n" if has_sys_content else "<system>\n",
|
||||
-1,
|
||||
is_sampled=False,
|
||||
is_content=False,
|
||||
)
|
||||
if has_sys_content:
|
||||
emit_text(system_content.rstrip(), system_msg_idx, is_sampled=False)
|
||||
# If the caller provided system content, it's body bytes;
|
||||
# otherwise this is the default system prompt (scaffold).
|
||||
sys_is_content = caller_has_system
|
||||
emit_text(
|
||||
system_content.rstrip(),
|
||||
system_msg_idx,
|
||||
is_sampled=False,
|
||||
is_content=sys_is_content,
|
||||
)
|
||||
if tools:
|
||||
tool_text = _TOOLS_HEADER
|
||||
for tool in tools:
|
||||
@@ -199,8 +228,8 @@ class LagunaXS2Renderer:
|
||||
if self._enable_thinking
|
||||
else _TOOLS_FOOTER_NO_THINKING
|
||||
)
|
||||
emit_text(tool_text, -1, is_sampled=False)
|
||||
emit_text("\n</system>\n", -1, is_sampled=False)
|
||||
emit_text(tool_text, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n</system>\n", -1, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Per-message loop ──────────────────────────────────────────
|
||||
for i, msg in enumerate(messages):
|
||||
@@ -211,35 +240,49 @@ class LagunaXS2Renderer:
|
||||
# Already consumed in the header block.
|
||||
if i == 0:
|
||||
continue
|
||||
emit_text(
|
||||
"<system>\n" + content + "\n</system>\n", i, is_sampled=False
|
||||
)
|
||||
# Body = caller's content; the ``<system>...</system>``
|
||||
# wrap and surrounding ``\n``s are scaffold.
|
||||
sys_segs: list[tuple[str, bool]] = [("<system>\n", False)]
|
||||
if content:
|
||||
sys_segs.append((content, True))
|
||||
sys_segs.append(("\n</system>\n", False))
|
||||
emit_text_segments(sys_segs, i, is_sampled=False)
|
||||
case "user":
|
||||
emit_text("<user>\n" + content + "\n</user>\n", i, is_sampled=False)
|
||||
user_segs: list[tuple[str, bool]] = [("<user>\n", False)]
|
||||
if content:
|
||||
user_segs.append((content, True))
|
||||
user_segs.append(("\n</user>\n", False))
|
||||
emit_text_segments(user_segs, i, is_sampled=False)
|
||||
case "assistant":
|
||||
self._render_assistant(
|
||||
msg, i, content, emit_special=emit_special, emit_text=emit_text
|
||||
msg,
|
||||
i,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
case "tool":
|
||||
emit_text(
|
||||
"<tool_response>\n" + content + "\n</tool_response>\n",
|
||||
i,
|
||||
is_sampled=False,
|
||||
)
|
||||
tool_segs: list[tuple[str, bool]] = [("<tool_response>\n", False)]
|
||||
if content:
|
||||
tool_segs.append((content, True))
|
||||
tool_segs.append(("\n</tool_response>\n", False))
|
||||
emit_text_segments(tool_segs, i, is_sampled=False)
|
||||
|
||||
# ── Generation prompt ─────────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._assistant, -1, is_sampled=False)
|
||||
emit_text("\n", -1, is_sampled=False)
|
||||
emit_special(self._assistant, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n", -1, is_sampled=False, is_content=False)
|
||||
if self._enable_thinking:
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
else:
|
||||
emit_special(self._think_end, -1, is_sampled=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -307,37 +350,74 @@ class LagunaXS2Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
content = self._visible_text(msg.get("content"))
|
||||
if role == "user":
|
||||
emit_text("<user>\n" + content + "\n</user>\n", i)
|
||||
segs: list[tuple[str, bool]] = [("<user>\n", False)]
|
||||
if content:
|
||||
segs.append((content, True))
|
||||
segs.append(("\n</user>\n", False))
|
||||
emit_text_segments(segs, i)
|
||||
elif role == "system":
|
||||
emit_text("<system>\n" + content + "\n</system>\n", i)
|
||||
segs = [("<system>\n", False)]
|
||||
if content:
|
||||
segs.append((content, True))
|
||||
segs.append(("\n</system>\n", False))
|
||||
emit_text_segments(segs, i)
|
||||
elif role == "tool":
|
||||
emit_text("<tool_response>\n" + content + "\n</tool_response>\n", i)
|
||||
segs = [("<tool_response>\n", False)]
|
||||
if content:
|
||||
segs.append((content, True))
|
||||
segs.append(("\n</tool_response>\n", False))
|
||||
emit_text_segments(segs, i)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -353,6 +433,7 @@ class LagunaXS2Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -364,6 +445,7 @@ class LagunaXS2Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
reasoning_content = ""
|
||||
if isinstance(msg.get("reasoning_content"), str):
|
||||
@@ -380,23 +462,30 @@ class LagunaXS2Renderer:
|
||||
# template emits these as the generation prompt at inference and
|
||||
# the model never samples them. Marking the role tag as
|
||||
# ``is_sampled=False`` keeps the SFT loss mask aligned with what
|
||||
# the model would actually have produced.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
# the model would actually have produced. ``is_content`` is also
|
||||
# False on the role tag. On assistant the invariant
|
||||
# ``is_content == sampled_mask`` holds.
|
||||
emit_special(self._assistant, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if reasoning_content:
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_text("\n" + reasoning_content.strip() + "\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n" + reasoning_content.strip() + "\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Combined newline-after-</think> with optional content. Bundling
|
||||
# preserves BPE merges across the boundary.
|
||||
post_think_text = "\n"
|
||||
if content.strip():
|
||||
post_think_text += content.strip() + "\n"
|
||||
emit_text(post_think_text, msg_idx, is_sampled=True)
|
||||
emit_text(post_think_text, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
for tc in tool_calls:
|
||||
@@ -409,7 +498,7 @@ class LagunaXS2Renderer:
|
||||
except json.JSONDecodeError:
|
||||
arguments = {}
|
||||
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True, is_content=True)
|
||||
inner = name + "\n"
|
||||
if isinstance(arguments, dict):
|
||||
for k, v in arguments.items():
|
||||
@@ -419,13 +508,13 @@ class LagunaXS2Renderer:
|
||||
else:
|
||||
val_text = json.dumps(v, ensure_ascii=False)
|
||||
inner += "<arg_value>" + val_text + "</arg_value>\n"
|
||||
emit_text(inner, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_text(inner, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# ``</assistant>`` is the model's stop signal (alongside
|
||||
# ``〈|EOS|〉``) — it samples this to end its turn, so it's part of
|
||||
# the sampled stream. The trailing ``\n`` is template-appended
|
||||
# between turns and never sampled.
|
||||
emit_special(self._assistant_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._assistant_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
+228
-59
@@ -21,6 +21,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
trim_to_turn_close,
|
||||
@@ -119,17 +120,69 @@ class MiniMaxM2Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_token_overlap_body(
|
||||
full_text: str,
|
||||
body_start: int,
|
||||
body_end: int,
|
||||
msg_idx: int,
|
||||
*,
|
||||
is_sampled: bool,
|
||||
) -> None:
|
||||
"""Tokenize ``full_text`` and mark tokens that overlap the body
|
||||
char span as ``is_content=True``.
|
||||
|
||||
Differs from :func:`attribute_text_segments` only in the
|
||||
boundary-token rule: a token straddling scaffold→body gets
|
||||
``True`` if any of its bytes are body bytes (overlap rule),
|
||||
rather than being attributed to whichever segment its first
|
||||
char belongs to. The body's first byte is preserved even when
|
||||
BPE merges it with the wrap's trailing byte (``>The`` →
|
||||
single token).
|
||||
"""
|
||||
from renderers.base import _get_offset_tokenizer
|
||||
|
||||
offset_tok = _get_offset_tokenizer(self._tokenizer)
|
||||
encoding = offset_tok(
|
||||
full_text, add_special_tokens=False, return_offsets_mapping=True
|
||||
)
|
||||
for tok_id, (start, end) in zip(
|
||||
encoding["input_ids"], encoding["offset_mapping"]
|
||||
):
|
||||
overlaps = start < body_end and end > body_start
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(overlaps)
|
||||
|
||||
# ── Extract system message ──────────────────────────────────
|
||||
first_is_system = messages[0].get("role") == "system"
|
||||
@@ -137,27 +190,38 @@ class MiniMaxM2Renderer:
|
||||
conversation: list[Message] = messages[1:] if first_is_system else messages
|
||||
|
||||
# ── System block (always present) ───────────────────────────
|
||||
emit_special(self._bos, sys_idx, is_sampled=False)
|
||||
emit_special(self._role, sys_idx, is_sampled=False)
|
||||
emit_special(self._bos, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._role, sys_idx, is_sampled=False, is_content=False)
|
||||
|
||||
sys_content = (
|
||||
self._visible_text(messages[0].get("content")) if first_is_system else ""
|
||||
)
|
||||
system_text = "system\n" + (sys_content or self._default_system)
|
||||
# Body = caller's system content (if any). Default system message
|
||||
# is template-injected scaffold; tools header / per-tool JSON /
|
||||
# footer / instructions are scaffold too (the tools dict is
|
||||
# recoverable from the ``tools`` arg).
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if sys_content:
|
||||
sys_segments.append((sys_content, True))
|
||||
else:
|
||||
sys_segments.append((self._default_system, False))
|
||||
|
||||
if tools:
|
||||
system_text += _TOOLS_HEADER
|
||||
sys_segments.append((_TOOLS_HEADER, False))
|
||||
for tool in tools:
|
||||
func = tool.get("function", tool)
|
||||
system_text += (
|
||||
"<tool>" + json.dumps(func, ensure_ascii=False) + "</tool>\n"
|
||||
sys_segments.append(
|
||||
(
|
||||
"<tool>" + json.dumps(func, ensure_ascii=False) + "</tool>\n",
|
||||
False,
|
||||
)
|
||||
)
|
||||
system_text += _TOOLS_FOOTER_PREFIX
|
||||
system_text += _TOOLS_INSTRUCTIONS
|
||||
sys_segments.append((_TOOLS_FOOTER_PREFIX, False))
|
||||
sys_segments.append((_TOOLS_INSTRUCTIONS, False))
|
||||
|
||||
emit_text(system_text, sys_idx, is_sampled=False)
|
||||
emit_special(self._eos, sys_idx, is_sampled=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False)
|
||||
emit_text_segments(sys_segments, sys_idx, is_sampled=False)
|
||||
emit_special(self._eos, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# ── Compute last_user_index (relative to conversation) ──────
|
||||
last_ui = -1
|
||||
@@ -172,14 +236,14 @@ class MiniMaxM2Renderer:
|
||||
orig_idx = ci + (1 if first_is_system else 0)
|
||||
|
||||
if role == "user":
|
||||
emit_special(self._role, orig_idx, is_sampled=False)
|
||||
emit_text(
|
||||
"user\n" + self._visible_text(msg.get("content")),
|
||||
orig_idx,
|
||||
is_sampled=False,
|
||||
)
|
||||
emit_special(self._eos, orig_idx, is_sampled=False)
|
||||
emit_text("\n", orig_idx, is_sampled=False)
|
||||
emit_special(self._role, orig_idx, is_sampled=False, is_content=False)
|
||||
user_content = self._visible_text(msg.get("content"))
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if user_content:
|
||||
user_segments.append((user_content, True))
|
||||
emit_text_segments(user_segments, orig_idx, is_sampled=False)
|
||||
emit_special(self._eos, orig_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
preserve_thinking = should_preserve_past_thinking(
|
||||
@@ -196,6 +260,7 @@ class MiniMaxM2Renderer:
|
||||
preserve_thinking=preserve_thinking,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
@@ -206,19 +271,22 @@ class MiniMaxM2Renderer:
|
||||
msg,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
emit_token_overlap_body=emit_token_overlap_body,
|
||||
)
|
||||
|
||||
# ── Generation prompt ───────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._role, -1, is_sampled=False)
|
||||
emit_text("ai\n", -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_text("\n", -1, is_sampled=False)
|
||||
emit_special(self._role, -1, is_sampled=False, is_content=False)
|
||||
emit_text("ai\n", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n", -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -282,27 +350,75 @@ class MiniMaxM2Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_token_overlap_body(
|
||||
full_text: str,
|
||||
body_start: int,
|
||||
body_end: int,
|
||||
msg_idx: int,
|
||||
*,
|
||||
is_sampled: bool,
|
||||
) -> None:
|
||||
from renderers.base import _get_offset_tokenizer
|
||||
|
||||
offset_tok = _get_offset_tokenizer(self._tokenizer)
|
||||
encoding = offset_tok(
|
||||
full_text, add_special_tokens=False, return_offsets_mapping=True
|
||||
)
|
||||
for tok_id, (start, end) in zip(
|
||||
encoding["input_ids"], encoding["offset_mapping"]
|
||||
):
|
||||
overlaps = start < body_end and end > body_start
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(overlaps)
|
||||
|
||||
# Trailing ``\n`` after the ``[e~[`` turn close — see ``render()``.
|
||||
emit_text("\n", -1)
|
||||
@@ -312,12 +428,18 @@ class MiniMaxM2Renderer:
|
||||
content = self._visible_text(msg.get("content"))
|
||||
if role == "user":
|
||||
emit_special(self._role, i)
|
||||
emit_text("user\n" + content, i)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i)
|
||||
emit_special(self._eos, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "system":
|
||||
emit_special(self._role, i)
|
||||
emit_text("system\n" + content, i)
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if content:
|
||||
sys_segments.append((content, True))
|
||||
emit_text_segments(sys_segments, i)
|
||||
emit_special(self._eos, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "tool":
|
||||
@@ -328,6 +450,8 @@ class MiniMaxM2Renderer:
|
||||
msg,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
emit_token_overlap_body=emit_token_overlap_body,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
@@ -343,6 +467,7 @@ class MiniMaxM2Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -356,6 +481,7 @@ class MiniMaxM2Renderer:
|
||||
preserve_thinking: bool = False,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
):
|
||||
content = self._visible_text(msg.get("content"))
|
||||
|
||||
@@ -374,12 +500,14 @@ class MiniMaxM2Renderer:
|
||||
# the chat template emits these as the generation prompt and the
|
||||
# model never samples them. Marking the role marker and tag as
|
||||
# ``is_sampled=False`` keeps the SFT loss mask aligned with what
|
||||
# the model would actually have produced.
|
||||
emit_special(self._role, orig_idx, is_sampled=False)
|
||||
# the model would actually have produced. ``is_content`` is also
|
||||
# False here — the role tag isn't part of any message's body.
|
||||
emit_special(self._role, orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Build the model-sampled portion (think block + content + tool
|
||||
# calls). Text segments stay contiguous within each is_sampled
|
||||
# span to preserve BPE merges.
|
||||
# calls). For assistant messages the invariant
|
||||
# ``is_content == sampled_mask`` holds — every sampled token is
|
||||
# body, every scaffold token isn't.
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
emit_thinking = reasoning_content and (
|
||||
conv_idx > last_user_index or preserve_thinking
|
||||
@@ -390,10 +518,15 @@ class MiniMaxM2Renderer:
|
||||
# immediately after ``ai\n``, which forces a tokenizer
|
||||
# boundary — splitting ``ai\n`` (not_sampled) from the
|
||||
# ``<think>``-led body (sampled) is BPE-safe.
|
||||
emit_text("ai\n", orig_idx, is_sampled=False)
|
||||
emit_special(self._think, orig_idx, is_sampled=True)
|
||||
emit_text("\n" + reasoning_content + "\n", orig_idx, is_sampled=True)
|
||||
emit_special(self._think_end, orig_idx, is_sampled=True)
|
||||
emit_text("ai\n", orig_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._think, orig_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n" + reasoning_content + "\n",
|
||||
orig_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._think_end, orig_idx, is_sampled=True, is_content=True)
|
||||
# \n\n + content must be contiguous for BPE
|
||||
body = "\n\n" + content if content else "\n\n"
|
||||
else:
|
||||
@@ -406,17 +539,19 @@ class MiniMaxM2Renderer:
|
||||
# choice for SFT (don't train on a token whose first byte
|
||||
# is template scaffolding).
|
||||
if tool_calls and not body:
|
||||
emit_text("ai\n\n", orig_idx, is_sampled=False)
|
||||
emit_text("ai\n\n", orig_idx, is_sampled=False, is_content=False)
|
||||
else:
|
||||
emit_text("ai\n", orig_idx, is_sampled=False)
|
||||
emit_text("ai\n", orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if tool_calls:
|
||||
# \n before <minimax:tool_call> must be contiguous with preceding text.
|
||||
# The empty-body / non-thinking case folded the leading \n
|
||||
# into the role-tag emission above; skip it here.
|
||||
if emit_thinking or body:
|
||||
emit_text(body + "\n", orig_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_tok, orig_idx, is_sampled=True)
|
||||
emit_text(body + "\n", orig_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_tok, orig_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
|
||||
invoke_block = "\n"
|
||||
for tc in tool_calls:
|
||||
@@ -448,16 +583,18 @@ class MiniMaxM2Renderer:
|
||||
)
|
||||
invoke_block += "</invoke>\n"
|
||||
|
||||
emit_text(invoke_block, orig_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end_tok, orig_idx, is_sampled=True)
|
||||
emit_text(invoke_block, orig_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end_tok, orig_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
elif body:
|
||||
emit_text(body, orig_idx, is_sampled=True)
|
||||
emit_text(body, orig_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# ``[e~[`` is the model's stop signal — it samples this to end
|
||||
# its turn, so it is part of the sampled stream. The trailing
|
||||
# ``\n`` is template-appended between turns and never sampled.
|
||||
emit_special(self._eos, orig_idx, is_sampled=True)
|
||||
emit_text("\n", orig_idx, is_sampled=False)
|
||||
emit_special(self._eos, orig_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -468,10 +605,15 @@ class MiniMaxM2Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
emit_token_overlap_body=None,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# body bytes get ``is_content=True``; the surrounding ``<response>``
|
||||
# wrap, role tag and separators are scaffold so an SFT mask over
|
||||
# tool body never trains the model to emit those.
|
||||
prev_is_tool = conv_idx > 0 and conversation[conv_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
conv_idx + 1 < len(conversation)
|
||||
@@ -479,8 +621,8 @@ class MiniMaxM2Renderer:
|
||||
)
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._role, orig_idx, is_sampled=False)
|
||||
emit_text("tool", orig_idx, is_sampled=False)
|
||||
emit_special(self._role, orig_idx, is_sampled=False, is_content=False)
|
||||
emit_text("tool", orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
content = self._visible_text(msg.get("content"))
|
||||
# Leading ``\n`` before ``<response>`` only on the first of a
|
||||
@@ -489,12 +631,39 @@ class MiniMaxM2Renderer:
|
||||
# through a single emit_text call instead of splitting the merge.
|
||||
prefix = "" if prev_is_tool else "\n"
|
||||
suffix = "\n" if next_is_tool else ""
|
||||
emit_text(
|
||||
prefix + "<response>" + content + "</response>" + suffix,
|
||||
orig_idx,
|
||||
is_sampled=False,
|
||||
)
|
||||
|
||||
# ``<response>`` is plain text with no separator between the
|
||||
# closing ``>`` and ``content``'s first byte, so BPE can merge
|
||||
# them into a single token (e.g., ``>The``). The shared
|
||||
# ``attribute_text_segments`` helper picks the segment of a
|
||||
# boundary-spanning token by its *first* char (here scaffold),
|
||||
# which would drop the body's leading letter out of the body
|
||||
# run. We instead use an "intersects body" rule: any token whose
|
||||
# ``[start, end)`` char range overlaps the body span gets
|
||||
# ``is_content=True``. A few scaffold bytes (the leading ``>``
|
||||
# or trailing ``<``) bleed into the body run, but body bytes are
|
||||
# recoverable as a substring of the decoded body span.
|
||||
body_text = prefix + "<response>" + content + "</response>" + suffix
|
||||
body_start = len(prefix) + len("<response>")
|
||||
body_end = body_start + len(content)
|
||||
if content and emit_token_overlap_body is not None:
|
||||
emit_token_overlap_body(
|
||||
body_text, body_start, body_end, orig_idx, is_sampled=False
|
||||
)
|
||||
else:
|
||||
# Empty body or no overlap-aware emitter available — fall back
|
||||
# to the standard segments path.
|
||||
tool_segments: list[tuple[str, bool]] = []
|
||||
if prefix:
|
||||
tool_segments.append((prefix, False))
|
||||
tool_segments.append(("<response>", False))
|
||||
if content:
|
||||
tool_segments.append((content, True))
|
||||
tool_segments.append(("</response>", False))
|
||||
if suffix:
|
||||
tool_segments.append((suffix, False))
|
||||
emit_text_segments(tool_segments, orig_idx, is_sampled=False)
|
||||
|
||||
if not next_is_tool:
|
||||
emit_special(self._eos, orig_idx, is_sampled=False)
|
||||
emit_text("\n", orig_idx, is_sampled=False)
|
||||
emit_special(self._eos, orig_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
+172
-64
@@ -24,6 +24,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
trim_to_turn_close,
|
||||
@@ -241,19 +242,44 @@ class Nemotron3Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
emit_ids(
|
||||
self._encode(text),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── 1. System message + optional tools ──────────────────────
|
||||
first_is_system = messages[0].get("role") == "system"
|
||||
@@ -262,8 +288,7 @@ class Nemotron3Renderer:
|
||||
# Nemotron 3: system prompt BEFORE tools block
|
||||
sys_idx = orig_idx(0) if first_is_system else -1
|
||||
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False)
|
||||
emit_text("system\n", sys_idx, is_sampled=False)
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Build system content: user's system text first, then tools
|
||||
if first_is_system:
|
||||
@@ -282,22 +307,27 @@ class Nemotron3Renderer:
|
||||
+ _TOOLS_INSTRUCTIONS
|
||||
)
|
||||
|
||||
# Body = caller's system text only; tools block (header, per-
|
||||
# tool XML, footer, instructions) is scaffold.
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if sys_content:
|
||||
full_sys = sys_content + "\n\n" + tools_block
|
||||
else:
|
||||
full_sys = tools_block
|
||||
|
||||
emit_text(full_sys, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False)
|
||||
sys_segments.append((sys_content, True))
|
||||
sys_segments.append(("\n\n", False))
|
||||
sys_segments.append((tools_block, False))
|
||||
emit_text_segments(sys_segments, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False, is_content=False)
|
||||
|
||||
elif first_is_system:
|
||||
sys_idx = orig_idx(0)
|
||||
sys_content = self._render_content(messages[0].get("content")).strip()
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False)
|
||||
emit_text("system\n" + sys_content, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False)
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False, is_content=False)
|
||||
sys_segments2: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if sys_content:
|
||||
sys_segments2.append((sys_content, True))
|
||||
emit_text_segments(sys_segments2, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Track the most-recent plain (non-tool-call) assistant so we can
|
||||
# preserve its reasoning while stripping reasoning from earlier
|
||||
@@ -322,10 +352,17 @@ class Nemotron3Renderer:
|
||||
continue # Already handled above
|
||||
|
||||
elif role == "user":
|
||||
emit_special(self._im_start, msg_orig_idx, is_sampled=False)
|
||||
emit_text("user\n" + content, msg_orig_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_orig_idx, is_sampled=False)
|
||||
emit_text("\n", msg_orig_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._im_start, msg_orig_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, msg_orig_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._im_end, msg_orig_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
emit_text("\n", msg_orig_idx, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
is_last_turn = i >= last_plain_assistant_idx
|
||||
@@ -344,6 +381,7 @@ class Nemotron3Renderer:
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_ids=emit_ids,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
@@ -355,6 +393,7 @@ class Nemotron3Renderer:
|
||||
auto_system_injected=auto_system_injected,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
else:
|
||||
@@ -362,21 +401,22 @@ class Nemotron3Renderer:
|
||||
|
||||
# ── 3. Generation prompt ────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._im_start, -1, is_sampled=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False)
|
||||
emit_special(self._im_start, -1, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False, is_content=False)
|
||||
if self._enable_thinking:
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_text("\n", -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n", -1, is_sampled=False, is_content=False)
|
||||
else:
|
||||
# Disable-thinking suffix: <think></think> with no trailing newlines
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False, is_content=False)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in original_messages],
|
||||
)
|
||||
|
||||
def render_ids(
|
||||
@@ -447,27 +487,52 @@ class Nemotron3Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
emit_text("\n", -1)
|
||||
|
||||
@@ -476,12 +541,18 @@ class Nemotron3Renderer:
|
||||
content = self._render_content(msg.get("content")).strip()
|
||||
if role == "user":
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("user\n" + content, i)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "system":
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("system\n" + content, i)
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if content:
|
||||
sys_segments.append((content, True))
|
||||
emit_text_segments(sys_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "tool":
|
||||
@@ -493,6 +564,7 @@ class Nemotron3Renderer:
|
||||
auto_system_injected=False,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
@@ -512,6 +584,7 @@ class Nemotron3Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -530,6 +603,7 @@ class Nemotron3Renderer:
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_ids,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Extract reasoning_content
|
||||
reasoning_content = ""
|
||||
@@ -550,9 +624,10 @@ class Nemotron3Renderer:
|
||||
# at inference the chat template emits these as the generation
|
||||
# prompt and the model never samples them. Marking the role tag
|
||||
# as ``is_sampled=False`` keeps the SFT loss mask aligned with
|
||||
# what the model would actually have produced.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False)
|
||||
# what the model would actually have produced. On assistant the
|
||||
# invariant ``is_content == sampled_mask`` holds.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Nemotron 3 keeps reasoning on the most-recent plain assistant but
|
||||
# strips it from historical turns, which collapse to an empty
|
||||
@@ -567,23 +642,43 @@ class Nemotron3Renderer:
|
||||
content_suffix = "\n" if tool_calls else ""
|
||||
|
||||
if reasoning_content and (is_last_turn or preserve_thinking):
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_text("\n" + reasoning_content + "\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n" + reasoning_content + "\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
# Single \n separator (not \n\n like Qwen3.5)
|
||||
emit_text("\n" + content + content_suffix, msg_idx, is_sampled=True)
|
||||
emit_text(
|
||||
"\n" + content + content_suffix,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
elif reasoning_content:
|
||||
# Historical assistant whose reasoning got stripped — template
|
||||
# keeps a single \n between the collapsed <think></think> and
|
||||
# the content as a marker that reasoning existed.
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n" + content + content_suffix, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n" + content + content_suffix,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
else:
|
||||
# No reasoning ever — <think></think> glued directly to content.
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_text(content + content_suffix, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
content + content_suffix,
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
# Tool calls (leading \n was glued to the content above; each
|
||||
# iteration's trailing \n after </tool_call> handles the
|
||||
@@ -594,8 +689,13 @@ class Nemotron3Renderer:
|
||||
name = func.get("name", "")
|
||||
arguments = func.get("arguments", {})
|
||||
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True)
|
||||
emit_text("\n<function=" + name + ">\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n<function=" + name + ">\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
# Render arguments
|
||||
# OpenAI canonical form: arguments is a JSON string. Parse it so the
|
||||
@@ -619,18 +719,21 @@ class Nemotron3Renderer:
|
||||
+ "\n</parameter>\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
emit_text("</function>\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_text("</function>\n", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
# Trailing \n after </tool_call> (Nemotron 3 specific)
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples this to
|
||||
# end its turn, so it is part of the sampled stream. The trailing
|
||||
# ``\n`` is template-appended between turns and never sampled.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Tool message rendering
|
||||
@@ -646,10 +749,13 @@ class Nemotron3Renderer:
|
||||
auto_system_injected: bool,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# body bytes get ``is_content=True``; the surrounding wrap is
|
||||
# scaffold.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
msg_idx + 1 < len(messages) and messages[msg_idx + 1]["role"] == "tool"
|
||||
@@ -657,17 +763,19 @@ class Nemotron3Renderer:
|
||||
oi = msg_orig_idx
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._im_start, oi, is_sampled=False)
|
||||
emit_text("user\n", oi, is_sampled=False)
|
||||
emit_special(self._im_start, oi, is_sampled=False, is_content=False)
|
||||
emit_text("user\n", oi, is_sampled=False, is_content=False)
|
||||
# else: the previous tool's trailing \n already provides the
|
||||
# separator into this block.
|
||||
|
||||
emit_special(self._tool_response, oi, is_sampled=False)
|
||||
emit_text("\n" + content + "\n", oi, is_sampled=False)
|
||||
emit_special(self._tool_response_end, oi, is_sampled=False)
|
||||
emit_special(self._tool_response, oi, is_sampled=False, is_content=False)
|
||||
emit_text_segments(
|
||||
[("\n", False), (content, True), ("\n", False)], oi, is_sampled=False
|
||||
)
|
||||
emit_special(self._tool_response_end, oi, is_sampled=False, is_content=False)
|
||||
# Nemotron 3: trailing \n after </tool_response>
|
||||
emit_text("\n", oi, is_sampled=False)
|
||||
emit_text("\n", oi, is_sampled=False, is_content=False)
|
||||
|
||||
if not next_is_tool:
|
||||
emit_special(self._im_end, oi, is_sampled=False)
|
||||
emit_text("\n", oi, is_sampled=False)
|
||||
emit_special(self._im_end, oi, is_sampled=False, is_content=False)
|
||||
emit_text("\n", oi, is_sampled=False, is_content=False)
|
||||
|
||||
+175
-64
@@ -18,6 +18,7 @@ from renderers.base import (
|
||||
ParsedResponse,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
trim_to_turn_close,
|
||||
@@ -108,43 +109,76 @@ class Qwen3Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
"""Tokenize concatenated segments as one BPE pass; per-token
|
||||
``is_content`` follows each token's source segment.
|
||||
|
||||
Lets call sites express "this wrap + this body, joined the
|
||||
same way as the chat template, but attributed separately"
|
||||
without splitting the encode call (which could shift BPE
|
||||
merges at the boundary)."""
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
# ── 1. System + tools ───────────────────────────────────────
|
||||
first_is_system = messages[0].get("role") == "system"
|
||||
|
||||
if tools:
|
||||
sys_idx = 0 if first_is_system else -1
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False)
|
||||
tool_text = "system\n"
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False, is_content=False)
|
||||
# Body = system content (if any). Everything else in this
|
||||
# block — role tag, tools header / footer, the JSON tool
|
||||
# specs — is scaffold. The tools dict is recoverable from
|
||||
# the ``tools`` argument; don't re-attribute its embedded
|
||||
# JSON as message body.
|
||||
segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if first_is_system:
|
||||
tool_text += (messages[0].get("content") or "") + "\n\n"
|
||||
tool_text += _TOOLS_HEADER
|
||||
sys_content = messages[0].get("content") or ""
|
||||
if sys_content:
|
||||
segments.append((sys_content, True))
|
||||
segments.append(("\n\n", False))
|
||||
segments.append((_TOOLS_HEADER, False))
|
||||
for tool in tools:
|
||||
tool_text += "\n" + json.dumps(tool, ensure_ascii=False)
|
||||
tool_text += _TOOLS_FOOTER
|
||||
emit_text(tool_text, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False)
|
||||
segments.append(("\n" + json.dumps(tool, ensure_ascii=False), False))
|
||||
segments.append((_TOOLS_FOOTER, False))
|
||||
emit_text_segments(segments, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False, is_content=False)
|
||||
elif first_is_system:
|
||||
emit_special(self._im_start, 0, is_sampled=False)
|
||||
emit_text(
|
||||
"system\n" + (messages[0].get("content") or ""), 0, is_sampled=False
|
||||
)
|
||||
emit_special(self._im_end, 0, is_sampled=False)
|
||||
emit_text("\n", 0, is_sampled=False)
|
||||
emit_special(self._im_start, 0, is_sampled=False, is_content=False)
|
||||
sys_content = messages[0].get("content") or ""
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if sys_content:
|
||||
sys_segments.append((sys_content, True))
|
||||
emit_text_segments(sys_segments, 0, is_sampled=False)
|
||||
emit_special(self._im_end, 0, is_sampled=False, is_content=False)
|
||||
emit_text("\n", 0, is_sampled=False, is_content=False)
|
||||
|
||||
# ── 2. Compute last_query_index ─────────────────────────────
|
||||
last_qi = self._last_query_index(messages)
|
||||
@@ -158,16 +192,22 @@ class Qwen3Renderer:
|
||||
if role == "system":
|
||||
if i == 0:
|
||||
continue
|
||||
emit_special(self._im_start, i, is_sampled=False)
|
||||
emit_text(role + "\n" + content, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_text("\n", i, is_sampled=False)
|
||||
emit_special(self._im_start, i, is_sampled=False, is_content=False)
|
||||
msg_segments: list[tuple[str, bool]] = [(role + "\n", False)]
|
||||
if content:
|
||||
msg_segments.append((content, True))
|
||||
emit_text_segments(msg_segments, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False, is_content=False)
|
||||
emit_text("\n", i, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "user":
|
||||
emit_special(self._im_start, i, is_sampled=False)
|
||||
emit_text("user\n" + content, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_text("\n", i, is_sampled=False)
|
||||
emit_special(self._im_start, i, is_sampled=False, is_content=False)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False, is_content=False)
|
||||
emit_text("\n", i, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
preserve_thinking = should_preserve_past_thinking(
|
||||
@@ -185,24 +225,33 @@ class Qwen3Renderer:
|
||||
preserve_thinking=preserve_thinking,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
self._render_tool(
|
||||
messages, i, content, emit_special=emit_special, emit_text=emit_text
|
||||
messages,
|
||||
i,
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
# ── 4. Generation prompt ────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._im_start, -1, is_sampled=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False)
|
||||
emit_special(self._im_start, -1, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False, is_content=False)
|
||||
if not self._enable_thinking:
|
||||
emit_text("<think>\n\n</think>\n\n", -1, is_sampled=False)
|
||||
emit_text(
|
||||
"<think>\n\n</think>\n\n", -1, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
return RenderedTokens(
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
)
|
||||
|
||||
@@ -263,27 +312,54 @@ class Qwen3Renderer:
|
||||
ext: list[int] = []
|
||||
ext_indices: list[int] = []
|
||||
ext_sampled: list[bool] = []
|
||||
ext_content: list[bool] = []
|
||||
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask. Downstream consumers can
|
||||
# run :meth:`RenderedTokens.tokens_per_message` on the bridge
|
||||
# output to get per-new-message token counts without re-rendering.
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ext.append(token_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
ext.extend(ids)
|
||||
ext_indices.extend([msg_idx] * len(ids))
|
||||
ext_sampled.extend([is_sampled] * len(ids))
|
||||
ext_content.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
ext.append(tok_id)
|
||||
ext_indices.append(msg_idx)
|
||||
ext_sampled.append(is_sampled)
|
||||
ext_content.append(is_content)
|
||||
|
||||
# Trailing ``\n`` after the turn-close token. ``render()`` emits this
|
||||
# as part of the prior turn, but vLLM stops on ``<|im_end|>`` so the
|
||||
@@ -295,12 +371,18 @@ class Qwen3Renderer:
|
||||
content = msg.get("content") if isinstance(msg.get("content"), str) else ""
|
||||
if role == "user":
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("user\n" + content, i)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "system":
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("system\n" + content, i)
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if content:
|
||||
sys_segments.append((content, True))
|
||||
emit_text_segments(sys_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "tool":
|
||||
@@ -310,6 +392,7 @@ class Qwen3Renderer:
|
||||
content,
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
@@ -324,6 +407,7 @@ class Qwen3Renderer:
|
||||
token_ids=previous_ids + ext,
|
||||
message_indices=[-1] * len(previous_ids) + ext_indices,
|
||||
sampled_mask=[False] * total_len,
|
||||
is_content=[False] * len(previous_ids) + ext_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
)
|
||||
|
||||
@@ -338,6 +422,7 @@ class Qwen3Renderer:
|
||||
preserve_thinking: bool = False,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
):
|
||||
reasoning_content = ""
|
||||
if isinstance(msg.get("reasoning_content"), str):
|
||||
@@ -355,16 +440,20 @@ class Qwen3Renderer:
|
||||
# at inference the chat template emits these as the generation
|
||||
# prompt and the model never samples them. Marking the role tag
|
||||
# as ``is_sampled=False`` keeps the SFT loss mask aligned with
|
||||
# what the model would actually have produced.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False)
|
||||
# what the model would actually have produced. ``is_content`` is
|
||||
# also False here — the role tag isn't part of any message's
|
||||
# body, on any role.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Build the model-sampled portion (think block + content + tool
|
||||
# calls). Text segments stay contiguous within each is_sampled
|
||||
# span to preserve BPE merges (e.g., ".\n" is a single token in
|
||||
# Qwen3); the only split we introduce here is at ``\n`` after the
|
||||
# role tag, which the existing renderer already treats as a
|
||||
# token boundary (cf. ``_render_tool``).
|
||||
# token boundary (cf. ``_render_tool``). For assistant messages
|
||||
# the invariant ``is_content == sampled_mask`` holds — every
|
||||
# sampled token is body, every scaffold token isn't.
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
|
||||
emit_in_template_window = msg_idx > last_query_index and (
|
||||
@@ -382,7 +471,7 @@ class Qwen3Renderer:
|
||||
body = content
|
||||
|
||||
if not tool_calls:
|
||||
emit_text(body, msg_idx, is_sampled=True)
|
||||
emit_text(body, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
for tc_idx, tc in enumerate(tool_calls):
|
||||
func = tc.get("function") or tc
|
||||
@@ -397,23 +486,29 @@ class Qwen3Renderer:
|
||||
# Text before this tool_call (includes separator)
|
||||
if tc_idx == 0:
|
||||
separator = "\n" if content else ""
|
||||
emit_text(body + separator, msg_idx, is_sampled=True)
|
||||
emit_text(
|
||||
body + separator, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
else:
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
'\n{"name": "' + name + '", "arguments": ' + args_str + "}\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(
|
||||
self._tool_call_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples this to
|
||||
# end its turn, so it is part of the sampled stream. The trailing
|
||||
# ``\n`` is template-appended between turns and never sampled.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
# end its turn, so it is part of the sampled stream (and the
|
||||
# assistant's body). The trailing ``\n`` is template-appended
|
||||
# between turns and never sampled — scaffold for is_content too.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -423,24 +518,40 @@ class Qwen3Renderer:
|
||||
*,
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# field's body bytes get ``is_content=True``; everything else —
|
||||
# the ``<|im_start|>user`` wrap, the inter-section ``\n``s, the
|
||||
# ``<|tool_response>`` specials — is scaffold so the SFT mask
|
||||
# for tool body never trains the model to emit them.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
msg_idx + 1 < len(messages) and messages[msg_idx + 1]["role"] == "tool"
|
||||
)
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
emit_text("user", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("user", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._tool_response, msg_idx, is_sampled=False)
|
||||
emit_text("\n" + content + "\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._tool_response_end, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._tool_response, msg_idx, is_sampled=False, is_content=False)
|
||||
# ``\n`` + content + ``\n`` — body is the middle segment only.
|
||||
# Single BPE pass over the joined text preserves boundary
|
||||
# merges (Qwen3 keeps ``\n`` as its own token, so this is
|
||||
# mostly a no-op, but we route through segments anyway so the
|
||||
# attribution doesn't depend on tokenizer-specific behaviour).
|
||||
emit_text_segments(
|
||||
[("\n", False), (content, True), ("\n", False)],
|
||||
msg_idx,
|
||||
is_sampled=False,
|
||||
)
|
||||
emit_special(
|
||||
self._tool_response_end, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
if not next_is_tool:
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
+239
-98
@@ -26,6 +26,7 @@ from renderers.base import (
|
||||
PlaceholderRange,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
should_preserve_past_thinking,
|
||||
trim_to_turn_close,
|
||||
@@ -291,33 +292,73 @@ class Qwen35Renderer:
|
||||
tokens: list[int] = []
|
||||
indices: list[int] = []
|
||||
sampled: list[bool] = []
|
||||
content_mask: list[bool] = []
|
||||
mm_hashes: dict[str, list[str]] = {}
|
||||
mm_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
mm_items: dict[str, list[dict[str, Any]]] = {}
|
||||
|
||||
def emit_ids(ids: list[int], msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_ids(
|
||||
ids: list[int], msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_special(token_id: int, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(text: str, msg_idx: int, *, is_sampled: bool) -> None:
|
||||
emit_ids(self._encode(text), msg_idx, is_sampled=is_sampled)
|
||||
def emit_text(
|
||||
text: str, msg_idx: int, *, is_sampled: bool, is_content: bool
|
||||
) -> None:
|
||||
emit_ids(
|
||||
self._encode(text),
|
||||
msg_idx,
|
||||
is_sampled=is_sampled,
|
||||
is_content=is_content,
|
||||
)
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]], msg_idx: int, *, is_sampled: bool
|
||||
) -> None:
|
||||
"""Tokenize concatenated segments as one BPE pass; per-token
|
||||
``is_content`` follows each token's source segment.
|
||||
|
||||
Lets call sites express "this wrap + this body, joined the
|
||||
same way as the chat template, but attributed separately"
|
||||
without splitting the encode call (which could shift BPE
|
||||
merges at the boundary)."""
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_image(part: dict[str, Any], msg_idx: int) -> None:
|
||||
# Image placeholders only appear in user / tool messages; the
|
||||
# model never samples them. Pin is_sampled=False here so
|
||||
# callers don't need to thread the flag through.
|
||||
# callers don't need to thread the flag through. The
|
||||
# ``<|image_pad|>`` placeholders represent caller-provided
|
||||
# image data, so they ARE body content (is_content=True);
|
||||
# the surrounding ``<|vision_start|>`` / ``<|vision_end|>``
|
||||
# specials are template scaffold.
|
||||
_, out, n, h = self._process_image(part)
|
||||
emit_special(self._vision_start, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._vision_start, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
offset = len(tokens)
|
||||
for _ in range(n):
|
||||
emit_special(self._image_pad, msg_idx, is_sampled=False)
|
||||
emit_special(self._vision_end, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._image_pad, msg_idx, is_sampled=False, is_content=True
|
||||
)
|
||||
emit_special(self._vision_end, msg_idx, is_sampled=False, is_content=False)
|
||||
mm_hashes.setdefault("image", []).append(h)
|
||||
mm_placeholders.setdefault("image", []).append(
|
||||
PlaceholderRange(offset=offset, length=n)
|
||||
@@ -337,20 +378,28 @@ class Qwen35Renderer:
|
||||
``<|im_end|>``), matching how Jinja's ``render_content`` macro
|
||||
concatenates strings before tokenization. This preserves BPE
|
||||
byte-parity against ``apply_chat_template``.
|
||||
|
||||
Within each flush the ``"user\\n"`` wrap is scaffold and the
|
||||
text parts are body. ``emit_text_segments`` carries that
|
||||
attribution through the BPE pass.
|
||||
"""
|
||||
# Every token in a user message is conversation history that
|
||||
# the model never samples at inference.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
buf: list[str] = ["user\n"]
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
# First flush includes the ``"user\n"`` wrap as a scaffold
|
||||
# segment; subsequent flushes are pure body (after a media
|
||||
# break).
|
||||
buf_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
|
||||
def flush_buf() -> None:
|
||||
if buf:
|
||||
emit_text("".join(buf), msg_idx, is_sampled=False)
|
||||
buf.clear()
|
||||
if buf_segments:
|
||||
emit_text_segments(buf_segments, msg_idx, is_sampled=False)
|
||||
buf_segments.clear()
|
||||
|
||||
for item in content_list:
|
||||
if isinstance(item, str):
|
||||
buf.append(item)
|
||||
if item:
|
||||
buf_segments.append((item, True))
|
||||
elif isinstance(item, dict):
|
||||
if _is_image_part(item):
|
||||
flush_buf()
|
||||
@@ -360,14 +409,15 @@ class Qwen35Renderer:
|
||||
"Video parts are not yet supported by Qwen35Renderer."
|
||||
)
|
||||
elif "text" in item:
|
||||
buf.append(item["text"])
|
||||
if item["text"]:
|
||||
buf_segments.append((item["text"], True))
|
||||
else:
|
||||
raise ValueError(f"Unexpected content item: {item}")
|
||||
else:
|
||||
raise ValueError(f"Unexpected content item: {item}")
|
||||
flush_buf()
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# ── 1. System message + optional tools ──────────────────────
|
||||
first_is_system = messages[0].get("role") == "system"
|
||||
@@ -376,31 +426,37 @@ class Qwen35Renderer:
|
||||
# System message index for attribution
|
||||
sys_idx = 0 if first_is_system else -1
|
||||
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False)
|
||||
emit_text("system\n", sys_idx, is_sampled=False)
|
||||
|
||||
# Tools header + JSON definitions
|
||||
tool_text = _TOOLS_HEADER
|
||||
emit_special(self._im_start, sys_idx, is_sampled=False, is_content=False)
|
||||
# Body = system content (if any). Everything else in this
|
||||
# block — role tag, tools header / footer / instructions, the
|
||||
# JSON tool specs — is scaffold. The tools dict is
|
||||
# recoverable from the ``tools`` argument; don't re-attribute
|
||||
# its embedded JSON as message body.
|
||||
segments: list[tuple[str, bool]] = [
|
||||
("system\n", False),
|
||||
(_TOOLS_HEADER, False),
|
||||
]
|
||||
for tool in tools:
|
||||
tool_text += "\n" + json.dumps(tool, ensure_ascii=False)
|
||||
tool_text += _TOOLS_FOOTER
|
||||
tool_text += _TOOLS_INSTRUCTIONS
|
||||
|
||||
# Append user's system content if present
|
||||
segments.append(("\n" + json.dumps(tool, ensure_ascii=False), False))
|
||||
segments.append((_TOOLS_FOOTER, False))
|
||||
segments.append((_TOOLS_INSTRUCTIONS, False))
|
||||
if first_is_system:
|
||||
sys_content = self._render_content(messages[0].get("content")).strip()
|
||||
if sys_content:
|
||||
tool_text += "\n\n" + sys_content
|
||||
|
||||
emit_text(tool_text, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False)
|
||||
segments.append(("\n\n", False))
|
||||
segments.append((sys_content, True))
|
||||
emit_text_segments(segments, sys_idx, is_sampled=False)
|
||||
emit_special(self._im_end, sys_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", sys_idx, is_sampled=False, is_content=False)
|
||||
elif first_is_system:
|
||||
sys_content = self._render_content(messages[0].get("content")).strip()
|
||||
emit_special(self._im_start, 0, is_sampled=False)
|
||||
emit_text("system\n" + sys_content, 0, is_sampled=False)
|
||||
emit_special(self._im_end, 0, is_sampled=False)
|
||||
emit_text("\n", 0, is_sampled=False)
|
||||
emit_special(self._im_start, 0, is_sampled=False, is_content=False)
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if sys_content:
|
||||
sys_segments.append((sys_content, True))
|
||||
emit_text_segments(sys_segments, 0, is_sampled=False)
|
||||
emit_special(self._im_end, 0, is_sampled=False, is_content=False)
|
||||
emit_text("\n", 0, is_sampled=False, is_content=False)
|
||||
|
||||
# ── 2. Compute last_query_index ─────────────────────────────
|
||||
last_qi = self._last_query_index(messages)
|
||||
@@ -420,10 +476,13 @@ class Qwen35Renderer:
|
||||
if self._content_has_media(raw_content):
|
||||
emit_user_with_media(raw_content, i)
|
||||
else:
|
||||
emit_special(self._im_start, i, is_sampled=False)
|
||||
emit_text("user\n" + content, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False)
|
||||
emit_text("\n", i, is_sampled=False)
|
||||
emit_special(self._im_start, i, is_sampled=False, is_content=False)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i, is_sampled=False)
|
||||
emit_special(self._im_end, i, is_sampled=False, is_content=False)
|
||||
emit_text("\n", i, is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
preserve_thinking = should_preserve_past_thinking(
|
||||
@@ -441,6 +500,7 @@ class Qwen35Renderer:
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_ids=emit_ids,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
elif role == "tool":
|
||||
@@ -450,6 +510,7 @@ class Qwen35Renderer:
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_image=emit_image,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
|
||||
else:
|
||||
@@ -457,16 +518,16 @@ class Qwen35Renderer:
|
||||
|
||||
# ── 4. Generation prompt ────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
emit_special(self._im_start, -1, is_sampled=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False)
|
||||
emit_special(self._im_start, -1, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", -1, is_sampled=False, is_content=False)
|
||||
if self._enable_thinking:
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_text("\n", -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n", -1, is_sampled=False, is_content=False)
|
||||
else:
|
||||
emit_special(self._think, -1, is_sampled=False)
|
||||
emit_text("\n\n", -1, is_sampled=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False)
|
||||
emit_text("\n\n", -1, is_sampled=False)
|
||||
emit_special(self._think, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n\n", -1, is_sampled=False, is_content=False)
|
||||
emit_special(self._think_end, -1, is_sampled=False, is_content=False)
|
||||
emit_text("\n\n", -1, is_sampled=False, is_content=False)
|
||||
|
||||
mm_data: MultiModalData | None = None
|
||||
if mm_hashes or mm_placeholders or mm_items:
|
||||
@@ -480,6 +541,7 @@ class Qwen35Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -549,34 +611,63 @@ class Qwen35Renderer:
|
||||
# ``previous_ids``. Bridge-added tokens get proper ``msg_idx``
|
||||
# (relative to ``new_messages``) and uniformly ``False``
|
||||
# ``sampled``: nothing the bridge emits was model-sampled.
|
||||
# ``is_content`` follows the same rules as in :meth:`render` so
|
||||
# consumers can walk the trajectory and read each step's own
|
||||
# body mask; the prior portion is uniformly False since we have
|
||||
# no attribution info for it.
|
||||
tokens: list[int] = list(previous_ids)
|
||||
indices: list[int] = [-1] * len(previous_ids)
|
||||
sampled: list[bool] = [False] * len(previous_ids)
|
||||
content_mask: list[bool] = [False] * len(previous_ids)
|
||||
new_hashes: dict[str, list[str]] = {}
|
||||
new_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
new_items: dict[str, list[dict[str, Any]]] = {}
|
||||
|
||||
def emit_special(
|
||||
token_id: int, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
token_id: int,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
tokens.append(token_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_text(
|
||||
text: str, msg_idx: int = -1, *, is_sampled: bool = False
|
||||
text: str,
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
is_content: bool = False,
|
||||
) -> None:
|
||||
ids = self._encode(text)
|
||||
tokens.extend(ids)
|
||||
indices.extend([msg_idx] * len(ids))
|
||||
sampled.extend([is_sampled] * len(ids))
|
||||
content_mask.extend([is_content] * len(ids))
|
||||
|
||||
def emit_text_segments(
|
||||
segments: list[tuple[str, bool]],
|
||||
msg_idx: int = -1,
|
||||
*,
|
||||
is_sampled: bool = False,
|
||||
) -> None:
|
||||
for tok_id, is_content in attribute_text_segments(
|
||||
self._tokenizer, segments
|
||||
):
|
||||
tokens.append(tok_id)
|
||||
indices.append(msg_idx)
|
||||
sampled.append(is_sampled)
|
||||
content_mask.append(is_content)
|
||||
|
||||
def emit_image(part: dict[str, Any], msg_idx: int = -1) -> None:
|
||||
_, out, n, h = self._process_image(part)
|
||||
emit_special(self._vision_start, msg_idx)
|
||||
offset = len(tokens)
|
||||
for _ in range(n):
|
||||
emit_special(self._image_pad, msg_idx)
|
||||
emit_special(self._image_pad, msg_idx, is_content=True)
|
||||
emit_special(self._vision_end, msg_idx)
|
||||
new_hashes.setdefault("image", []).append(h)
|
||||
new_placeholders.setdefault("image", []).append(
|
||||
@@ -591,16 +682,17 @@ class Qwen35Renderer:
|
||||
|
||||
def emit_user_with_media(content_list: list[Any], msg_idx: int) -> None:
|
||||
emit_special(self._im_start, msg_idx)
|
||||
buf: list[str] = ["user\n"]
|
||||
buf_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
|
||||
def flush_buf() -> None:
|
||||
if buf:
|
||||
emit_text("".join(buf), msg_idx)
|
||||
buf.clear()
|
||||
if buf_segments:
|
||||
emit_text_segments(buf_segments, msg_idx)
|
||||
buf_segments.clear()
|
||||
|
||||
for item in content_list:
|
||||
if isinstance(item, str):
|
||||
buf.append(item)
|
||||
if item:
|
||||
buf_segments.append((item, True))
|
||||
elif isinstance(item, dict):
|
||||
if _is_image_part(item):
|
||||
flush_buf()
|
||||
@@ -610,7 +702,8 @@ class Qwen35Renderer:
|
||||
"Video parts are not yet supported by Qwen35Renderer."
|
||||
)
|
||||
elif "text" in item:
|
||||
buf.append(item["text"])
|
||||
if item["text"]:
|
||||
buf_segments.append((item["text"], True))
|
||||
else:
|
||||
raise ValueError(f"Unexpected content item: {item}")
|
||||
else:
|
||||
@@ -633,12 +726,18 @@ class Qwen35Renderer:
|
||||
emit_user_with_media(raw_content, i)
|
||||
else:
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("user\n" + content, i)
|
||||
user_segments: list[tuple[str, bool]] = [("user\n", False)]
|
||||
if content:
|
||||
user_segments.append((content, True))
|
||||
emit_text_segments(user_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "system":
|
||||
emit_special(self._im_start, i)
|
||||
emit_text("system\n" + content, i)
|
||||
sys_segments: list[tuple[str, bool]] = [("system\n", False)]
|
||||
if content:
|
||||
sys_segments.append((content, True))
|
||||
emit_text_segments(sys_segments, i)
|
||||
emit_special(self._im_end, i)
|
||||
emit_text("\n", i)
|
||||
elif role == "tool":
|
||||
@@ -648,6 +747,7 @@ class Qwen35Renderer:
|
||||
emit_special=emit_special,
|
||||
emit_text=emit_text,
|
||||
emit_image=emit_image,
|
||||
emit_text_segments=emit_text_segments,
|
||||
)
|
||||
else:
|
||||
return None
|
||||
@@ -693,6 +793,7 @@ class Qwen35Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=bridge_roles,
|
||||
)
|
||||
|
||||
@@ -705,6 +806,7 @@ class Qwen35Renderer:
|
||||
token_ids=tokens,
|
||||
message_indices=indices,
|
||||
sampled_mask=sampled,
|
||||
is_content=content_mask,
|
||||
message_roles=bridge_roles,
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -747,6 +849,7 @@ class Qwen35Renderer:
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_ids,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Extract reasoning_content
|
||||
reasoning_content = ""
|
||||
@@ -769,26 +872,36 @@ class Qwen35Renderer:
|
||||
# at inference the chat template emits these as the generation
|
||||
# prompt and the model never samples them. Marking the role tag
|
||||
# as ``is_sampled=False`` keeps the SFT loss mask aligned with
|
||||
# what the model would actually have produced. The split between
|
||||
# ``assistant`` and ``\n`` is a safe BPE boundary in the Qwen
|
||||
# tokenizer (``\n`` after the role is its own token).
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False)
|
||||
# what the model would actually have produced. ``is_content`` is
|
||||
# also False here — the role tag isn't part of any message's
|
||||
# body, on any role.
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("assistant\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# Build the model-sampled portion (think block + content + tool
|
||||
# calls). Text segments stay contiguous within each is_sampled
|
||||
# span to preserve BPE merges.
|
||||
# span to preserve BPE merges. For assistant messages the
|
||||
# invariant ``is_content == sampled_mask`` holds — every sampled
|
||||
# token is body, every scaffold token isn't. The XML-style tool
|
||||
# call tags (``<function=...>``, ``<parameter=...>``, etc.) are
|
||||
# part of the model's emitted output too — keep them
|
||||
# ``is_content=True`` per the assistant rule.
|
||||
emit_thinking = self._should_render_thinking(msg_idx, last_query_index) or (
|
||||
preserve_thinking and bool(reasoning_content)
|
||||
)
|
||||
if emit_thinking:
|
||||
# Include thinking block
|
||||
emit_special(self._think, msg_idx, is_sampled=True)
|
||||
emit_text("\n" + reasoning_content + "\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n\n" + content, msg_idx, is_sampled=True)
|
||||
emit_special(self._think, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n" + reasoning_content + "\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
emit_special(self._think_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n\n" + content, msg_idx, is_sampled=True, is_content=True)
|
||||
else:
|
||||
emit_text(content, msg_idx, is_sampled=True)
|
||||
emit_text(content, msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
# Tool calls
|
||||
tool_calls = msg.get("tool_calls") or []
|
||||
@@ -801,13 +914,18 @@ class Qwen35Renderer:
|
||||
# Separator before <tool_call>
|
||||
if tc_idx == 0:
|
||||
if content.strip():
|
||||
emit_text("\n\n", msg_idx, is_sampled=True)
|
||||
emit_text("\n\n", msg_idx, is_sampled=True, is_content=True)
|
||||
# else: no separator
|
||||
else:
|
||||
emit_text("\n", msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=True, is_content=True)
|
||||
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True)
|
||||
emit_text("\n<function=" + name + ">\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text(
|
||||
"\n<function=" + name + ">\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
# Render arguments
|
||||
# OpenAI canonical form: arguments is a JSON string. Parse it so the
|
||||
@@ -828,16 +946,20 @@ class Qwen35Renderer:
|
||||
+ "\n</parameter>\n",
|
||||
msg_idx,
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
|
||||
emit_text("</function>\n", msg_idx, is_sampled=True)
|
||||
emit_special(self._tool_call_end, msg_idx, is_sampled=True)
|
||||
emit_text("</function>\n", msg_idx, is_sampled=True, is_content=True)
|
||||
emit_special(
|
||||
self._tool_call_end, msg_idx, is_sampled=True, is_content=True
|
||||
)
|
||||
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples this to
|
||||
# end its turn, so it is part of the sampled stream. The trailing
|
||||
# ``\n`` is template-appended between turns and never sampled.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
# end its turn, so it is part of the sampled stream (and the
|
||||
# assistant's body). The trailing ``\n`` is template-appended
|
||||
# between turns and never sampled — scaffold for is_content too.
|
||||
emit_special(self._im_end, msg_idx, is_sampled=True, is_content=True)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Tool message rendering
|
||||
@@ -851,6 +973,7 @@ class Qwen35Renderer:
|
||||
emit_special,
|
||||
emit_text,
|
||||
emit_image,
|
||||
emit_text_segments,
|
||||
) -> None:
|
||||
# Consecutive tool messages share a single <|im_start|>user ... <|im_end|>
|
||||
# envelope. Whether to open and close the envelope depends only on the
|
||||
@@ -858,9 +981,11 @@ class Qwen35Renderer:
|
||||
# tool message — keep this predicate text/media-agnostic.
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False. The bridge's
|
||||
# local emit_special / emit_text accept the is_sampled kwarg for
|
||||
# signature compatibility but ignore it.
|
||||
# tokens, so every emission is is_sampled=False. The ``content``
|
||||
# field's body bytes get ``is_content=True``; everything else —
|
||||
# the ``<|im_start|>user`` wrap, the inter-section ``\n``s, the
|
||||
# ``<|tool_response>`` specials — is scaffold so the SFT mask
|
||||
# for tool body never trains the model to emit them.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
msg_idx + 1 < len(messages) and messages[msg_idx + 1]["role"] == "tool"
|
||||
@@ -868,11 +993,11 @@ class Qwen35Renderer:
|
||||
raw_content = messages[msg_idx].get("content")
|
||||
|
||||
if not prev_is_tool:
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False)
|
||||
emit_text("user", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_start, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("user", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._tool_response, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
emit_special(self._tool_response, msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
if self._content_has_media(raw_content):
|
||||
# Mirror the chat template's ``render_content`` macro for list
|
||||
@@ -882,16 +1007,22 @@ class Qwen35Renderer:
|
||||
# ``_content_has_media`` returns False unless content is a list,
|
||||
# but the type checker can't follow that through the call.
|
||||
assert isinstance(raw_content, list)
|
||||
buf: list[str] = ["\n"]
|
||||
# First flush: leading ``"\n"`` is scaffold (separates the
|
||||
# ``<|tool_response>`` special from the body); subsequent
|
||||
# text items in this run are body. After a media break, the
|
||||
# buffer resets to pure body until the next media break or
|
||||
# end-of-content.
|
||||
buf_segments: list[tuple[str, bool]] = [("\n", False)]
|
||||
|
||||
def flush_buf() -> None:
|
||||
if buf:
|
||||
emit_text("".join(buf), msg_idx, is_sampled=False)
|
||||
buf.clear()
|
||||
if buf_segments:
|
||||
emit_text_segments(buf_segments, msg_idx, is_sampled=False)
|
||||
buf_segments.clear()
|
||||
|
||||
for item in raw_content:
|
||||
if isinstance(item, str):
|
||||
buf.append(item)
|
||||
if item:
|
||||
buf_segments.append((item, True))
|
||||
elif isinstance(item, dict):
|
||||
if _is_image_part(item):
|
||||
flush_buf()
|
||||
@@ -901,19 +1032,29 @@ class Qwen35Renderer:
|
||||
"Video parts are not yet supported by Qwen35Renderer."
|
||||
)
|
||||
elif "text" in item:
|
||||
buf.append(item["text"])
|
||||
if item["text"]:
|
||||
buf_segments.append((item["text"], True))
|
||||
else:
|
||||
raise ValueError(f"Unexpected content item: {item}")
|
||||
else:
|
||||
raise ValueError(f"Unexpected content item: {item}")
|
||||
flush_buf()
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
else:
|
||||
content = self._render_content(raw_content).strip()
|
||||
emit_text("\n" + content + "\n", msg_idx, is_sampled=False)
|
||||
# ``\n`` + content + ``\n`` — body is the middle segment only.
|
||||
# Single BPE pass over the joined text preserves boundary
|
||||
# merges.
|
||||
emit_text_segments(
|
||||
[("\n", False), (content, True), ("\n", False)],
|
||||
msg_idx,
|
||||
is_sampled=False,
|
||||
)
|
||||
|
||||
emit_special(self._tool_response_end, msg_idx, is_sampled=False)
|
||||
emit_special(
|
||||
self._tool_response_end, msg_idx, is_sampled=False, is_content=False
|
||||
)
|
||||
|
||||
if not next_is_tool:
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False)
|
||||
emit_special(self._im_end, msg_idx, is_sampled=False, is_content=False)
|
||||
emit_text("\n", msg_idx, is_sampled=False, is_content=False)
|
||||
|
||||
+190
-98
@@ -42,6 +42,7 @@ from renderers.base import (
|
||||
PlaceholderRange,
|
||||
RenderedTokens,
|
||||
ToolSpec,
|
||||
attribute_text_segments,
|
||||
reject_assistant_in_extension,
|
||||
trim_to_turn_close,
|
||||
)
|
||||
@@ -70,9 +71,14 @@ def _is_image_part(item: Any) -> bool:
|
||||
t = item.get("type")
|
||||
if t in ("image", "image_url"):
|
||||
return True
|
||||
# Permissive fallback: chat templates check ``'image' in content`` to
|
||||
# accept loosely-shaped image parts, so mirror that.
|
||||
return "image" in item or "image_url" in item
|
||||
if t is not None:
|
||||
return False
|
||||
# Untyped fallback for loosely-shaped image parts. Require a truthy
|
||||
# value: HF Arrow schema unification (Dataset.from_list over a list of
|
||||
# heterogeneous content dicts) fills missing keys with None, so any
|
||||
# text part round-tripped through a Dataset will have ``image_url: None``
|
||||
# as a key. Mere key presence isn't enough.
|
||||
return bool(item.get("image")) or bool(item.get("image_url"))
|
||||
|
||||
|
||||
def _is_video_part(item: Any) -> bool:
|
||||
@@ -81,7 +87,9 @@ def _is_video_part(item: Any) -> bool:
|
||||
t = item.get("type")
|
||||
if t in ("video", "video_url"):
|
||||
return True
|
||||
return "video" in item or "video_url" in item
|
||||
if t is not None:
|
||||
return False
|
||||
return bool(item.get("video")) or bool(item.get("video_url"))
|
||||
|
||||
|
||||
def _load_pil_image(item: dict[str, Any]):
|
||||
@@ -170,14 +178,31 @@ class _Emitter:
|
||||
value than the current buffer triggers a flush first — split points
|
||||
are always at the ``is_sampled`` boundary, which the caller is
|
||||
expected to place at a ``\\n`` boundary so BPE merges don't drift.
|
||||
|
||||
``is_content`` is the per-token body/scaffold attribution. Within a
|
||||
single flush adjacent text fragments may carry different
|
||||
``is_content`` values (e.g. ``"user\\n"`` scaffold + caller content
|
||||
body): the buffer stores fragments as a list of
|
||||
``(text, is_content)`` segments and flushes via
|
||||
:func:`attribute_text_segments`, which performs one BPE pass over
|
||||
the joined text and assigns per-token is_content from each token's
|
||||
source segment. When every segment in a flush shares the same
|
||||
is_content (the common case for sampled assistant body / pure
|
||||
scaffold) the fast path of a single ``encode()`` call is used and
|
||||
no offset-tokenizer lookup is required.
|
||||
"""
|
||||
|
||||
def __init__(self, encode_fn, msg_idx: int = -1):
|
||||
def __init__(self, encode_fn, tokenizer=None, msg_idx: int = -1):
|
||||
self._encode = encode_fn
|
||||
self._tokenizer = tokenizer
|
||||
self.token_ids: list[int] = []
|
||||
self.message_indices: list[int] = []
|
||||
self.sampled: list[bool] = []
|
||||
self._buf: str = ""
|
||||
self.is_content: list[bool] = []
|
||||
# Buffered text fragments as ``(text, is_content)`` tuples. All
|
||||
# fragments share a single ``_buf_sampled`` / ``_buf_idx``;
|
||||
# changing either of those triggers a flush.
|
||||
self._segments: list[tuple[str, bool]] = []
|
||||
self._buf_idx: int = msg_idx
|
||||
self._buf_sampled: bool = False
|
||||
self.msg_idx = msg_idx
|
||||
@@ -187,49 +212,78 @@ class _Emitter:
|
||||
# text doesn't get glued to the previous one's BPE context.
|
||||
# In practice messages are always separated by an <|im_end|>
|
||||
# special token, which already flushes — but be defensive.
|
||||
if self._buf:
|
||||
if self._segments:
|
||||
self._flush()
|
||||
self.msg_idx = msg_idx
|
||||
self._buf_idx = msg_idx
|
||||
|
||||
def text(self, text: str, *, is_sampled: bool) -> None:
|
||||
def text(self, text: str, *, is_sampled: bool, is_content: bool) -> None:
|
||||
if not text:
|
||||
return
|
||||
# Adjacent text under different msg_idx or is_sampled is rare in
|
||||
# this template — but flush at those boundaries so attribution
|
||||
# and the sampled signal stay accurate.
|
||||
if self._buf and (
|
||||
# and the sampled signal stay accurate. is_content boundaries do
|
||||
# NOT force a flush: they're carried through the joined BPE pass
|
||||
# via :func:`attribute_text_segments`, preserving merges across
|
||||
# the wrap/body boundary.
|
||||
if self._segments and (
|
||||
self._buf_idx != self.msg_idx or self._buf_sampled != is_sampled
|
||||
):
|
||||
self._flush()
|
||||
if not self._buf:
|
||||
if not self._segments:
|
||||
self._buf_idx = self.msg_idx
|
||||
self._buf_sampled = is_sampled
|
||||
self._buf += text
|
||||
self._segments.append((text, is_content))
|
||||
|
||||
def special(self, token_id: int, *, is_sampled: bool) -> None:
|
||||
if self._buf:
|
||||
def special(self, token_id: int, *, is_sampled: bool, is_content: bool) -> None:
|
||||
if self._segments:
|
||||
self._flush()
|
||||
self.token_ids.append(token_id)
|
||||
self.message_indices.append(self.msg_idx)
|
||||
self.sampled.append(is_sampled)
|
||||
self.is_content.append(is_content)
|
||||
|
||||
def cursor(self) -> int:
|
||||
"""Current token offset after flushing — used to anchor placeholder ranges."""
|
||||
if self._buf:
|
||||
if self._segments:
|
||||
self._flush()
|
||||
return len(self.token_ids)
|
||||
|
||||
def finalize(self) -> None:
|
||||
if self._buf:
|
||||
if self._segments:
|
||||
self._flush()
|
||||
|
||||
def _flush(self) -> None:
|
||||
ids = self._encode(self._buf)
|
||||
self.token_ids.extend(ids)
|
||||
self.message_indices.extend([self._buf_idx] * len(ids))
|
||||
self.sampled.extend([self._buf_sampled] * len(ids))
|
||||
self._buf = ""
|
||||
segments = self._segments
|
||||
self._segments = []
|
||||
if not segments:
|
||||
return
|
||||
# Fast path: every segment shares the same is_content — use the
|
||||
# plain ``encode()`` call so we don't pay for the offset
|
||||
# tokenizer. This is the common case (pure scaffold flushes, or
|
||||
# pure body flushes).
|
||||
first_ic = segments[0][1]
|
||||
all_same = all(ic == first_ic for _, ic in segments)
|
||||
if all_same:
|
||||
joined = "".join(text for text, _ in segments)
|
||||
ids = self._encode(joined)
|
||||
self.token_ids.extend(ids)
|
||||
self.message_indices.extend([self._buf_idx] * len(ids))
|
||||
self.sampled.extend([self._buf_sampled] * len(ids))
|
||||
self.is_content.extend([first_ic] * len(ids))
|
||||
return
|
||||
# Mixed body/scaffold flush — encode once and attribute back to
|
||||
# each segment via the fast tokenizer's offset_mapping. Requires
|
||||
# a tokenizer (not just the encode fn) to look up offsets.
|
||||
assert self._tokenizer is not None, (
|
||||
"_Emitter mixed-is_content flush requires a tokenizer; "
|
||||
"pass one to the constructor."
|
||||
)
|
||||
for tok_id, is_content in attribute_text_segments(self._tokenizer, segments):
|
||||
self.token_ids.append(tok_id)
|
||||
self.message_indices.append(self._buf_idx)
|
||||
self.sampled.append(self._buf_sampled)
|
||||
self.is_content.append(is_content)
|
||||
|
||||
|
||||
class Qwen3VLRenderer:
|
||||
@@ -394,7 +448,7 @@ class Qwen3VLRenderer:
|
||||
if not messages:
|
||||
raise ValueError("No messages provided.")
|
||||
|
||||
em = _Emitter(self._encode)
|
||||
em = _Emitter(self._encode, tokenizer=self._tokenizer)
|
||||
mm_hashes: dict[str, list[str]] = {}
|
||||
mm_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
mm_items: dict[str, list[dict[str, Any]]] = {}
|
||||
@@ -402,13 +456,17 @@ class Qwen3VLRenderer:
|
||||
def emit_image(part: dict[str, Any]) -> None:
|
||||
# Image placeholders are prompt-side scaffolding the user
|
||||
# message attaches — the model never samples ``<|vision_start|>``
|
||||
# / ``<|image_pad|>`` / ``<|vision_end|>``.
|
||||
# / ``<|image_pad|>`` / ``<|vision_end|>``. The
|
||||
# ``<|image_pad|>`` placeholders represent caller-provided
|
||||
# image data, so they ARE body content (is_content=True);
|
||||
# the surrounding ``<|vision_start|>`` / ``<|vision_end|>``
|
||||
# markers are renderer-emitted scaffold.
|
||||
_, out, n, h = self._process_image(part)
|
||||
em.special(self._vision_start, is_sampled=False)
|
||||
em.special(self._vision_start, is_sampled=False, is_content=False)
|
||||
offset = em.cursor()
|
||||
for _ in range(n):
|
||||
em.special(self._image_pad, is_sampled=False)
|
||||
em.special(self._vision_end, is_sampled=False)
|
||||
em.special(self._image_pad, is_sampled=False, is_content=True)
|
||||
em.special(self._vision_end, is_sampled=False, is_content=False)
|
||||
mm_hashes.setdefault("image", []).append(h)
|
||||
mm_placeholders.setdefault("image", []).append(
|
||||
PlaceholderRange(offset=offset, length=n)
|
||||
@@ -425,16 +483,24 @@ class Qwen3VLRenderer:
|
||||
|
||||
User / tool content is conversation context the model never
|
||||
samples — every text fragment goes in as ``is_sampled=False``.
|
||||
Text from the caller IS the message body, so every text
|
||||
fragment is ``is_content=True``; the vision-marker specials
|
||||
around image_pad placeholders are scaffold (handled in
|
||||
:func:`emit_image`).
|
||||
"""
|
||||
if isinstance(content, str):
|
||||
em.text(content, is_sampled=False)
|
||||
em.text(content, is_sampled=False, is_content=True)
|
||||
return
|
||||
if not isinstance(content, list):
|
||||
em.text(self._render_text_content(content), is_sampled=False)
|
||||
em.text(
|
||||
self._render_text_content(content),
|
||||
is_sampled=False,
|
||||
is_content=True,
|
||||
)
|
||||
return
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
em.text(item, is_sampled=False)
|
||||
em.text(item, is_sampled=False, is_content=True)
|
||||
elif isinstance(item, dict):
|
||||
if _is_image_part(item):
|
||||
emit_image(item)
|
||||
@@ -443,7 +509,7 @@ class Qwen3VLRenderer:
|
||||
"Video parts are not yet supported by Qwen3VLRenderer."
|
||||
)
|
||||
elif "text" in item:
|
||||
em.text(item["text"], is_sampled=False)
|
||||
em.text(item["text"], is_sampled=False, is_content=True)
|
||||
|
||||
# ── 1. System + tools ───────────────────────────────────────
|
||||
first_is_system = messages[0].get("role") == "system"
|
||||
@@ -451,26 +517,37 @@ class Qwen3VLRenderer:
|
||||
if tools:
|
||||
sys_idx = 0 if first_is_system else -1
|
||||
em.set_msg_idx(sys_idx)
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
buf = "system\n"
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
# Body = system content (if any). Everything else in this
|
||||
# block — role tag, tools header / footer, JSON tool specs —
|
||||
# is scaffold. The tools dict is recoverable from the
|
||||
# ``tools`` argument; we don't re-attribute its embedded
|
||||
# JSON as message body.
|
||||
em.text("system\n", is_sampled=False, is_content=False)
|
||||
if first_is_system:
|
||||
buf += self._render_text_content(messages[0].get("content")) + "\n\n"
|
||||
buf += _TOOLS_HEADER
|
||||
sys_content = self._render_text_content(messages[0].get("content"))
|
||||
if sys_content:
|
||||
em.text(sys_content, is_sampled=False, is_content=True)
|
||||
em.text("\n\n", is_sampled=False, is_content=False)
|
||||
em.text(_TOOLS_HEADER, is_sampled=False, is_content=False)
|
||||
for tool in tools:
|
||||
buf += "\n" + json.dumps(tool, ensure_ascii=False)
|
||||
buf += _TOOLS_FOOTER
|
||||
em.text(buf, is_sampled=False)
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.text(
|
||||
"\n" + json.dumps(tool, ensure_ascii=False),
|
||||
is_sampled=False,
|
||||
is_content=False,
|
||||
)
|
||||
em.text(_TOOLS_FOOTER, is_sampled=False, is_content=False)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
elif first_is_system:
|
||||
em.set_msg_idx(0)
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text(
|
||||
"system\n" + self._render_text_content(messages[0].get("content")),
|
||||
is_sampled=False,
|
||||
)
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("system\n", is_sampled=False, is_content=False)
|
||||
sys_content = self._render_text_content(messages[0].get("content"))
|
||||
if sys_content:
|
||||
em.text(sys_content, is_sampled=False, is_content=True)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
|
||||
# ── 2. Iterate messages ─────────────────────────────────────
|
||||
for i, msg in enumerate(messages):
|
||||
@@ -482,11 +559,11 @@ class Qwen3VLRenderer:
|
||||
em.set_msg_idx(i)
|
||||
|
||||
if role == "user":
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("user\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("user\n", is_sampled=False, is_content=False)
|
||||
render_media_content(msg.get("content"))
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
|
||||
elif role == "assistant":
|
||||
self._render_assistant(msg, em)
|
||||
@@ -500,8 +577,8 @@ class Qwen3VLRenderer:
|
||||
# ── 3. Generation prompt ────────────────────────────────────
|
||||
if add_generation_prompt:
|
||||
em.set_msg_idx(-1)
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("assistant\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("assistant\n", is_sampled=False, is_content=False)
|
||||
|
||||
em.finalize()
|
||||
|
||||
@@ -517,6 +594,7 @@ class Qwen3VLRenderer:
|
||||
token_ids=em.token_ids,
|
||||
message_indices=em.message_indices,
|
||||
sampled_mask=em.sampled,
|
||||
is_content=em.is_content,
|
||||
message_roles=[m.get("role") or "" for m in messages],
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -588,18 +666,21 @@ class Qwen3VLRenderer:
|
||||
# Bridge populates ``message_indices`` (relative to ``new_messages``)
|
||||
# and ``sampled_mask`` (uniformly ``False`` — every token the
|
||||
# bridge emits is template scaffolding for the next prompt, not
|
||||
# something the model sampled). Downstream consumers can run
|
||||
# :meth:`RenderedTokens.tokens_per_message` on the bridge output
|
||||
# to get per-new-message token counts without re-rendering.
|
||||
em = _Emitter(self._encode)
|
||||
# something the model sampled). ``is_content`` follows the same
|
||||
# rules as in :meth:`render` so consumers can walk the trajectory
|
||||
# and read each step's own body mask. Downstream consumers can
|
||||
# run :meth:`RenderedTokens.tokens_per_message` on the bridge
|
||||
# output to get per-new-message token counts without re-rendering.
|
||||
em = _Emitter(self._encode, tokenizer=self._tokenizer)
|
||||
# Seed the emitter with the prior turn's tokens so cursor() reports
|
||||
# absolute offsets in the combined sequence. Per-token attribution
|
||||
# for the prior portion is unknown to the bridge (it only has
|
||||
# prev_prompt_ids + prev_completion_ids as raw lists), so seed
|
||||
# both side channels with the "no info" sentinel.
|
||||
# all side channels with the "no info" sentinel.
|
||||
em.token_ids = list(previous_ids)
|
||||
em.message_indices = [-1] * len(previous_ids)
|
||||
em.sampled = [False] * len(previous_ids)
|
||||
em.is_content = [False] * len(previous_ids)
|
||||
|
||||
new_hashes: dict[str, list[str]] = {}
|
||||
new_placeholders: dict[str, list[PlaceholderRange]] = {}
|
||||
@@ -607,11 +688,11 @@ class Qwen3VLRenderer:
|
||||
|
||||
def emit_image(part: dict[str, Any]) -> None:
|
||||
_, out, n, h = self._process_image(part)
|
||||
em.special(self._vision_start, is_sampled=False)
|
||||
em.special(self._vision_start, is_sampled=False, is_content=False)
|
||||
offset = em.cursor()
|
||||
for _ in range(n):
|
||||
em.special(self._image_pad, is_sampled=False)
|
||||
em.special(self._vision_end, is_sampled=False)
|
||||
em.special(self._image_pad, is_sampled=False, is_content=True)
|
||||
em.special(self._vision_end, is_sampled=False, is_content=False)
|
||||
new_hashes.setdefault("image", []).append(h)
|
||||
new_placeholders.setdefault("image", []).append(
|
||||
PlaceholderRange(offset=offset, length=n)
|
||||
@@ -625,14 +706,18 @@ class Qwen3VLRenderer:
|
||||
|
||||
def render_media_content(content: Any) -> None:
|
||||
if isinstance(content, str):
|
||||
em.text(content, is_sampled=False)
|
||||
em.text(content, is_sampled=False, is_content=True)
|
||||
return
|
||||
if not isinstance(content, list):
|
||||
em.text(self._render_text_content(content), is_sampled=False)
|
||||
em.text(
|
||||
self._render_text_content(content),
|
||||
is_sampled=False,
|
||||
is_content=True,
|
||||
)
|
||||
return
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
em.text(item, is_sampled=False)
|
||||
em.text(item, is_sampled=False, is_content=True)
|
||||
elif isinstance(item, dict):
|
||||
if _is_image_part(item):
|
||||
emit_image(item)
|
||||
@@ -641,34 +726,34 @@ class Qwen3VLRenderer:
|
||||
"Video parts are not yet supported by Qwen3VLRenderer."
|
||||
)
|
||||
elif "text" in item:
|
||||
em.text(item["text"], is_sampled=False)
|
||||
em.text(item["text"], is_sampled=False, is_content=True)
|
||||
|
||||
em.set_msg_idx(-1)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
|
||||
for i, msg in enumerate(new_messages):
|
||||
role = msg.get("role")
|
||||
em.set_msg_idx(i)
|
||||
if role == "user":
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("user\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("user\n", is_sampled=False, is_content=False)
|
||||
render_media_content(msg.get("content"))
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
elif role == "system":
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("system\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("system\n", is_sampled=False, is_content=False)
|
||||
render_media_content(msg.get("content"))
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
elif role == "tool":
|
||||
self._render_tool(new_messages, i, em, render_media_content)
|
||||
else:
|
||||
return None
|
||||
|
||||
em.set_msg_idx(-1)
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("assistant\n", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("assistant\n", is_sampled=False, is_content=False)
|
||||
em.finalize()
|
||||
|
||||
# Merge prev mm_data with the new turn's items.
|
||||
@@ -706,6 +791,7 @@ class Qwen3VLRenderer:
|
||||
token_ids=em.token_ids,
|
||||
message_indices=em.message_indices,
|
||||
sampled_mask=em.sampled,
|
||||
is_content=em.is_content,
|
||||
message_roles=[m.get("role") or "" for m in new_messages],
|
||||
multi_modal_data=mm_data,
|
||||
)
|
||||
@@ -719,20 +805,22 @@ class Qwen3VLRenderer:
|
||||
# at inference the chat template emits these as the generation
|
||||
# prompt and the model never samples them. Splitting the text
|
||||
# at the ``\n`` after the role tag is safe: Qwen3 BPE treats
|
||||
# ``\n`` as a token boundary.
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("assistant\n", is_sampled=False)
|
||||
# ``\n`` as a token boundary. For the assistant role the
|
||||
# invariant ``is_content == sampled_mask`` holds — every sampled
|
||||
# token is body, every scaffold token isn't.
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("assistant\n", is_sampled=False, is_content=False)
|
||||
|
||||
# Body (content + tool calls) is the model-sampled portion.
|
||||
if not tool_calls:
|
||||
em.text(content, is_sampled=True)
|
||||
em.text(content, is_sampled=True, is_content=True)
|
||||
else:
|
||||
for tc_idx, tc in enumerate(tool_calls):
|
||||
if tc_idx == 0:
|
||||
separator = "\n" if original_content else ""
|
||||
em.text(content + separator, is_sampled=True)
|
||||
em.text(content + separator, is_sampled=True, is_content=True)
|
||||
else:
|
||||
em.text("\n", is_sampled=True)
|
||||
em.text("\n", is_sampled=True, is_content=True)
|
||||
|
||||
func = tc.get("function") or tc
|
||||
name = func.get("name", "")
|
||||
@@ -743,18 +831,19 @@ class Qwen3VLRenderer:
|
||||
else json.dumps(arguments, ensure_ascii=False)
|
||||
)
|
||||
|
||||
em.special(self._tool_call, is_sampled=True)
|
||||
em.special(self._tool_call, is_sampled=True, is_content=True)
|
||||
em.text(
|
||||
'\n{"name": "' + name + '", "arguments": ' + args_str + "}\n",
|
||||
is_sampled=True,
|
||||
is_content=True,
|
||||
)
|
||||
em.special(self._tool_call_end, is_sampled=True)
|
||||
em.special(self._tool_call_end, is_sampled=True, is_content=True)
|
||||
|
||||
# ``<|im_end|>`` is the model's stop signal — it samples this to
|
||||
# end its turn. The trailing ``\n`` is template-appended between
|
||||
# turns and never sampled.
|
||||
em.special(self._im_end, is_sampled=True)
|
||||
em.text("\n", is_sampled=False)
|
||||
# end its turn (and counts as part of its body). The trailing
|
||||
# ``\n`` is template-appended between turns and never sampled.
|
||||
em.special(self._im_end, is_sampled=True, is_content=True)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
|
||||
def _render_tool(
|
||||
self,
|
||||
@@ -765,24 +854,27 @@ class Qwen3VLRenderer:
|
||||
) -> None:
|
||||
# Tool messages are conversation history injected by the runtime
|
||||
# between assistant turns — the model never samples any of these
|
||||
# tokens, so every emission is is_sampled=False. (render_media_content
|
||||
# also stamps is_sampled=False on its emissions.)
|
||||
# tokens, so every emission is is_sampled=False. The
|
||||
# ``content`` body bytes get ``is_content=True`` (via
|
||||
# ``render_media_content``); everything else — the
|
||||
# ``<|im_start|>user`` wrap, inter-section ``\n``s, and the
|
||||
# ``<|tool_response>`` specials — is scaffold.
|
||||
prev_is_tool = msg_idx > 0 and messages[msg_idx - 1]["role"] == "tool"
|
||||
next_is_tool = (
|
||||
msg_idx + 1 < len(messages) and messages[msg_idx + 1]["role"] == "tool"
|
||||
)
|
||||
|
||||
if not prev_is_tool:
|
||||
em.special(self._im_start, is_sampled=False)
|
||||
em.text("user", is_sampled=False)
|
||||
em.special(self._im_start, is_sampled=False, is_content=False)
|
||||
em.text("user", is_sampled=False, is_content=False)
|
||||
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._tool_response, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
em.special(self._tool_response, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
render_media_content(messages[msg_idx].get("content"))
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._tool_response_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
em.special(self._tool_response_end, is_sampled=False, is_content=False)
|
||||
|
||||
if not next_is_tool:
|
||||
em.special(self._im_end, is_sampled=False)
|
||||
em.text("\n", is_sampled=False)
|
||||
em.special(self._im_end, is_sampled=False, is_content=False)
|
||||
em.text("\n", is_sampled=False, is_content=False)
|
||||
|
||||
+227
-16
@@ -1,8 +1,10 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import numpy as np
|
||||
|
||||
import pytest
|
||||
from renderers.base import (
|
||||
ParsedResponse,
|
||||
ParsedToolCall,
|
||||
@@ -19,7 +21,15 @@ class _FakeRenderer:
|
||||
assert messages == [{"role": "user", "content": "hi"}]
|
||||
assert tools == [{"type": "function", "function": {"name": "echo"}}]
|
||||
assert add_generation_prompt is True
|
||||
return RenderedTokens(token_ids=[1, 2, 3])
|
||||
# Populate the full attribution surface so the test can verify
|
||||
# ``generate`` threads it through to the result dict unchanged.
|
||||
return RenderedTokens(
|
||||
token_ids=[1, 2, 3],
|
||||
message_indices=[0, 0, -1],
|
||||
sampled_mask=[False, False, False],
|
||||
is_content=[False, True, False],
|
||||
message_roles=["user"],
|
||||
)
|
||||
|
||||
def render_ids(self, messages, *, tools=None, add_generation_prompt=False):
|
||||
return self.render(
|
||||
@@ -62,8 +72,8 @@ class _FakeClient:
|
||||
self.calls.append(
|
||||
{"path": path, "cast_to": cast_to, "body": body, "options": options}
|
||||
)
|
||||
routed_experts = np.array([[[1]], [[2]]], dtype=np.int32)
|
||||
return {
|
||||
routed_experts = np.array([[[1]], [[2]]], dtype=np.uint8)
|
||||
payload = {
|
||||
"request_id": "gen-test",
|
||||
"choices": [
|
||||
{
|
||||
@@ -77,7 +87,7 @@ class _FakeClient:
|
||||
},
|
||||
"finish_reason": "stop",
|
||||
"routed_experts": {
|
||||
"data": base64.b85encode(routed_experts.tobytes()).decode(
|
||||
"data": base64.b64encode(routed_experts.tobytes()).decode(
|
||||
"ascii"
|
||||
),
|
||||
"shape": list(routed_experts.shape),
|
||||
@@ -85,6 +95,10 @@ class _FakeClient:
|
||||
}
|
||||
],
|
||||
}
|
||||
return httpx.Response(
|
||||
200,
|
||||
content=json.dumps(payload, separators=(",", ":")).encode("utf-8"),
|
||||
)
|
||||
|
||||
|
||||
def test_generate_builds_request_body_and_parses_response():
|
||||
@@ -113,6 +127,7 @@ def test_generate_builds_request_body_and_parses_response():
|
||||
# /inference/v1/generate is mounted at the server root, so we post to
|
||||
# an absolute URL stripped of the OpenAI SDK's automatic /v1 prefix.
|
||||
assert client.calls[0]["path"] == "http://fake-host:8000/inference/v1/generate"
|
||||
assert client.calls[0]["cast_to"] is httpx.Response
|
||||
assert client.calls[0]["body"] == {
|
||||
"model": "test-model",
|
||||
"token_ids": [1, 2, 3],
|
||||
@@ -134,9 +149,22 @@ def test_generate_builds_request_body_and_parses_response():
|
||||
assert result["prompt_ids"] == [1, 2, 3]
|
||||
assert result["completion_ids"] == [7, 8]
|
||||
assert result["completion_logprobs"] == [-0.1, -0.2]
|
||||
assert result["routed_experts"] == [[[1]], [[2]]]
|
||||
assert result["routed_experts"]["shape"] == [2, 1, 1]
|
||||
assert isinstance(result["routed_experts"]["data"], memoryview)
|
||||
assert result["routed_experts"]["data"].tobytes() == base64.b64encode(b"\x01\x02")
|
||||
assert result["multi_modal_data"] is None
|
||||
assert result["request_id"] == "gen-test"
|
||||
# Per-token attribution from the renderer surfaces on the result so
|
||||
# downstream consumers (verifiers RendererClient → prime-rl) can
|
||||
# build selective loss masks without a second render pass.
|
||||
attr = result["prompt_attribution"]
|
||||
assert attr is not None
|
||||
assert isinstance(attr, RenderedTokens)
|
||||
assert attr.token_ids == [1, 2, 3]
|
||||
assert attr.is_content == [False, True, False]
|
||||
assert attr.sampled_mask == [False, False, False]
|
||||
assert attr.message_indices == [0, 0, -1]
|
||||
assert attr.message_roles == ["user"]
|
||||
assert len(result["tool_calls"]) == 1
|
||||
tc = result["tool_calls"][0]
|
||||
assert tc.name == "echo"
|
||||
@@ -207,6 +235,44 @@ def test_generate_uses_prebuilt_prompt_ids_without_rendering():
|
||||
|
||||
assert client.calls[0]["body"]["token_ids"] == [11, 12, 13]
|
||||
assert result["prompt_ids"] == [11, 12, 13]
|
||||
# Pre-built prompt without explicit attribution → ``None`` carried
|
||||
# through. Consumers fall back to whatever attribution-free path
|
||||
# they have (e.g. uniform completion mask).
|
||||
assert result["prompt_attribution"] is None
|
||||
|
||||
|
||||
def test_generate_threads_prompt_attribution_through_prebuilt_prompt_path():
|
||||
"""When the caller passes both ``prompt_ids`` and ``prompt_attribution``
|
||||
(the multi-turn bridge path in verifiers), ``generate`` must thread
|
||||
the attribution through to the result dict unchanged — no re-rendering,
|
||||
no per-token reshuffling. Lets downstream consumers carry the
|
||||
renderer's body/scaffold cut into the trajectory step without an
|
||||
extra render pass."""
|
||||
client = _FakeClient()
|
||||
# Caller-supplied attribution; mirrors what
|
||||
# ``RendererClient._get_incremental_prompt_ids`` returns from the
|
||||
# bridge_to_next_turn output.
|
||||
supplied = RenderedTokens(
|
||||
token_ids=[11, 12, 13],
|
||||
message_indices=[-1, 0, 0],
|
||||
sampled_mask=[False, False, False],
|
||||
is_content=[False, True, True],
|
||||
message_roles=["tool"],
|
||||
)
|
||||
|
||||
result = asyncio.run(
|
||||
generate(
|
||||
client=client,
|
||||
renderer=_NoRenderRenderer(),
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model="test-model",
|
||||
prompt_ids=[11, 12, 13],
|
||||
prompt_attribution=supplied,
|
||||
)
|
||||
)
|
||||
|
||||
# Exact passthrough — same object, no copy / no transform.
|
||||
assert result["prompt_attribution"] is supplied
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -214,30 +280,42 @@ def test_generate_uses_prebuilt_prompt_ids_without_rendering():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_generate_serializes_multimodal_features_for_qwen3_vl():
|
||||
@pytest.mark.parametrize(
|
||||
"model_id,renderer_class_path",
|
||||
[
|
||||
("Qwen/Qwen3-VL-4B-Instruct", "renderers.qwen3_vl:Qwen3VLRenderer"),
|
||||
("Qwen/Qwen3.5-2B", "renderers.qwen35:Qwen35Renderer"),
|
||||
],
|
||||
ids=["qwen3_vl", "qwen35"],
|
||||
)
|
||||
def test_generate_serializes_multimodal_features_for_qwen_vl_family(
|
||||
model_id, renderer_class_path
|
||||
):
|
||||
"""When the renderer emits ``MultiModalData``, ``generate`` translates
|
||||
it into vLLM's ``features`` payload (mm_hashes + mm_placeholders +
|
||||
base64-encoded kwargs_data) and sticks it in the request body."""
|
||||
import pytest as _pytest
|
||||
base64-encoded kwargs_data) and sticks it in the request body. Covers
|
||||
every renderer routed through ``_build_qwen_vl_features``."""
|
||||
import importlib
|
||||
|
||||
_pytest.importorskip("torch")
|
||||
_pytest.importorskip("vllm", reason="vllm needed for features serialization")
|
||||
pytest.importorskip("torch")
|
||||
pytest.importorskip("vllm", reason="vllm needed for features serialization")
|
||||
|
||||
import torch as _torch
|
||||
|
||||
from renderers.base import (
|
||||
MultiModalData,
|
||||
PlaceholderRange,
|
||||
load_tokenizer,
|
||||
)
|
||||
from renderers.qwen3_vl import Qwen3VLRenderer
|
||||
|
||||
# Build a minimal real Qwen3VLRenderer so type dispatch in
|
||||
mod_name, cls_name = renderer_class_path.split(":")
|
||||
renderer_cls = getattr(importlib.import_module(mod_name), cls_name)
|
||||
|
||||
# Build a minimal real renderer so type dispatch in
|
||||
# _build_mm_features hits the qwen branch. The tokenizer is only
|
||||
# touched in __init__ to grab special-token ids; render() / etc.
|
||||
# aren't called here because we pre-supply prompt_ids + mm_data.
|
||||
tokenizer = load_tokenizer("Qwen/Qwen3-VL-4B-Instruct")
|
||||
renderer = Qwen3VLRenderer(tokenizer)
|
||||
tokenizer = load_tokenizer(model_id)
|
||||
renderer = renderer_cls(tokenizer)
|
||||
|
||||
# Two synthetic 1×2×2 images. Field factory expects pixel_values
|
||||
# shape ``(sum_HW, embed_dim)`` and grid_thw shape ``(N, 3)``; the
|
||||
@@ -291,3 +369,136 @@ def test_generate_serializes_multimodal_features_for_qwen3_vl():
|
||||
# Items are base64 strings (encode_mm_kwargs_item output).
|
||||
for item in features["kwargs_data"]["image"]:
|
||||
assert isinstance(item, str) and len(item) > 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Prompt overflow handling.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _LongRenderer(_FakeRenderer):
|
||||
"""Renders a 10-token prompt regardless of input — enough to overflow a
|
||||
small ``max_prompt_len``."""
|
||||
|
||||
def render(self, messages, *, tools=None, add_generation_prompt=False):
|
||||
from renderers.base import RenderedTokens
|
||||
|
||||
return RenderedTokens(token_ids=list(range(10)))
|
||||
|
||||
|
||||
def test_generate_raises_overlong_prompt_when_explicit_cap_exceeded():
|
||||
"""Pre-flight overflow check: when an explicit ``max_prompt_len`` is set
|
||||
and the rendered prompt is longer, ``generate`` raises
|
||||
``OverlongPromptError`` without dispatching the request to the engine."""
|
||||
from renderers.client import OverlongPromptError
|
||||
|
||||
client = _FakeClient()
|
||||
renderer = _LongRenderer()
|
||||
|
||||
with pytest.raises(OverlongPromptError) as excinfo:
|
||||
asyncio.run(
|
||||
generate(
|
||||
client=client,
|
||||
renderer=renderer,
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model="test-model",
|
||||
max_prompt_len=4,
|
||||
)
|
||||
)
|
||||
|
||||
assert excinfo.value.prompt_len == 10
|
||||
assert excinfo.value.max_prompt_len == 4
|
||||
assert client.calls == [], "request must not be dispatched on pre-flight fail"
|
||||
|
||||
|
||||
def test_generate_allows_prompt_at_max_prompt_len():
|
||||
"""A prompt exactly equal to ``max_prompt_len`` is allowed (the check is
|
||||
strict ``>``); only longer prompts trip the pre-flight."""
|
||||
client = _FakeClient()
|
||||
renderer = _LongRenderer()
|
||||
|
||||
result = asyncio.run(
|
||||
generate(
|
||||
client=client,
|
||||
renderer=renderer,
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model="test-model",
|
||||
max_prompt_len=10,
|
||||
)
|
||||
)
|
||||
|
||||
assert len(client.calls) == 1
|
||||
assert result["prompt_ids"] == list(range(10))
|
||||
|
||||
|
||||
def test_generate_auto_discovers_max_prompt_len_from_models_endpoint():
|
||||
"""When ``max_prompt_len`` is ``None`` (default), ``generate`` discovers
|
||||
the cap via ``GET /v1/models`` and reads ``ModelCard.max_model_len``.
|
||||
The result is cached per ``(base_url, model)`` so subsequent calls
|
||||
don't re-query."""
|
||||
from renderers.client import OverlongPromptError, _max_prompt_len_cache
|
||||
|
||||
class _ClientWithModels(_FakeClient):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.base_url = "http://disco-host:8000/v1"
|
||||
self.models_calls = 0
|
||||
|
||||
async def get(self, path, *, cast_to):
|
||||
self.models_calls += 1
|
||||
assert path == "/models"
|
||||
return {
|
||||
"object": "list",
|
||||
"data": [
|
||||
{"id": "test-model", "max_model_len": 4},
|
||||
{"id": "other", "max_model_len": 999},
|
||||
],
|
||||
}
|
||||
|
||||
# Clear cache so this test isn't affected by earlier ones.
|
||||
_max_prompt_len_cache.clear()
|
||||
|
||||
client = _ClientWithModels()
|
||||
renderer = _LongRenderer()
|
||||
|
||||
with pytest.raises(OverlongPromptError) as excinfo:
|
||||
asyncio.run(
|
||||
generate(
|
||||
client=client,
|
||||
renderer=renderer,
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model="test-model",
|
||||
)
|
||||
)
|
||||
|
||||
assert excinfo.value.max_prompt_len == 4
|
||||
assert excinfo.value.prompt_len == 10
|
||||
assert client.models_calls == 1, "lookup must hit /models once"
|
||||
assert client.calls == [], "pre-flight must short-circuit the request"
|
||||
|
||||
|
||||
def test_generate_caches_max_prompt_len_lookup_failure():
|
||||
"""When ``GET /v1/models`` fails (e.g. mock client without ``.get``),
|
||||
the lookup result is cached as ``None`` and the pre-flight quietly
|
||||
disables — the request still goes through, callers fall back to
|
||||
whatever reactive overflow handling they have."""
|
||||
from renderers.client import _max_prompt_len_cache
|
||||
|
||||
# _FakeClient has no .get method → AttributeError → cached None.
|
||||
_max_prompt_len_cache.clear()
|
||||
client = _FakeClient()
|
||||
client.base_url = "http://no-models:8000/v1"
|
||||
|
||||
result = asyncio.run(
|
||||
generate(
|
||||
client=client,
|
||||
renderer=_LongRenderer(),
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model="test-model",
|
||||
)
|
||||
)
|
||||
|
||||
# Request was dispatched (no pre-flight rejection) and round-tripped.
|
||||
assert len(client.calls) == 1
|
||||
assert result["prompt_ids"] == list(range(10))
|
||||
assert _max_prompt_len_cache[("http://no-models:8000/v1", "test-model")] is None
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
"""Per-token ``RenderedTokens.is_content`` invariants.
|
||||
|
||||
``is_content[k]`` answers "does ``token_ids[k]`` come from message body
|
||||
bytes (caller-provided content / tool_calls / reasoning_content, or
|
||||
the model's sampled emission for the assistant role) or is it template
|
||||
scaffolding the renderer added around the body (role tags, special
|
||||
tokens, separators, tool-response wraps, generation prompt)?"
|
||||
|
||||
By design ``is_content`` is a superset of ``sampled_mask``:
|
||||
|
||||
- Equal on every token attributed to an assistant message (sampled ==
|
||||
body for that role by construction).
|
||||
- Carries new information on every other role: the model never samples
|
||||
user / tool / system tokens, so ``sampled_mask`` is uniformly
|
||||
``False`` over those — ``is_content`` differentiates body from wrap.
|
||||
|
||||
These tests parametrise across every renderer in
|
||||
``conftest.RENDERER_MODELS`` and verify the contract for hand-coded
|
||||
renderers. ``DefaultRenderer`` leaves ``is_content`` empty by design
|
||||
(the Jinja template is opaque, so the renderer cannot know the
|
||||
wrap/body split) and is exempt from the populated-length check.
|
||||
|
||||
The body-bytes decode invariant uses ``in`` (substring) rather than
|
||||
strict equality because some renderers normalise whitespace, strip
|
||||
trailing newlines, etc. within the message body emit — but the
|
||||
caller-provided content must always be recoverable as a substring of
|
||||
the decoded body run.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def _is_populated(rendered) -> bool:
|
||||
return len(rendered.is_content) == len(rendered.token_ids) and bool(
|
||||
rendered.is_content
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_length_or_empty(model_name, renderer):
|
||||
"""``is_content`` is either empty (opt-out) or matches token_ids
|
||||
length exactly. No partial fills."""
|
||||
msgs = [
|
||||
{"role": "user", "content": "Hi"},
|
||||
{"role": "assistant", "content": "Hello!"},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
n_tokens = len(rendered.token_ids)
|
||||
n_mask = len(rendered.is_content)
|
||||
assert n_mask == 0 or n_mask == n_tokens, (
|
||||
f"{model_name}: is_content length {n_mask} must be 0 or match "
|
||||
f"token_ids length {n_tokens}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_equals_sampled_on_assistant(model_name, renderer):
|
||||
"""On every token attributed to an assistant message,
|
||||
``is_content[k] == sampled_mask[k]``. The two signals collapse on
|
||||
that role by design — the model's sampled output IS the assistant
|
||||
message's body, and the surrounding scaffold is neither sampled
|
||||
nor body."""
|
||||
msgs = [
|
||||
{"role": "system", "content": "You are helpful."},
|
||||
{"role": "user", "content": "Hi"},
|
||||
{"role": "assistant", "content": "Hello world!"},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
if len(rendered.sampled_mask) != len(rendered.token_ids):
|
||||
return # renderer opts out of sampled_mask
|
||||
|
||||
mismatches = []
|
||||
for k, msg_idx in enumerate(rendered.message_indices):
|
||||
if msg_idx < 0:
|
||||
continue
|
||||
if msgs[msg_idx].get("role") != "assistant":
|
||||
continue
|
||||
if rendered.is_content[k] != rendered.sampled_mask[k]:
|
||||
mismatches.append((k, rendered.is_content[k], rendered.sampled_mask[k]))
|
||||
assert not mismatches, (
|
||||
f"{model_name}: is_content != sampled_mask on assistant tokens "
|
||||
f"(k, is_content, sampled): {mismatches[:8]}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_excludes_generation_prompt(model_name, renderer):
|
||||
"""All generation-prompt tokens (msg_idx=-1) are scaffold — the
|
||||
next-turn opener the chat template injects so the model can
|
||||
continue. ``is_content`` must be False over that entire span."""
|
||||
msgs = [{"role": "user", "content": "Hi"}]
|
||||
rendered = renderer.render(msgs, add_generation_prompt=True)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
bad = [
|
||||
k
|
||||
for k, (msg_idx, is_content) in enumerate(
|
||||
zip(rendered.message_indices, rendered.is_content)
|
||||
)
|
||||
if msg_idx == -1 and is_content
|
||||
]
|
||||
assert not bad, (
|
||||
f"{model_name}: generation-prompt tokens marked is_content=True "
|
||||
f"at positions {bad[:8]}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_recovers_user_body(model_name, tokenizer, renderer):
|
||||
"""The decoded run of is_content=True tokens within a user message
|
||||
contains the user's original content. ``in`` rather than equality
|
||||
because some templates normalise whitespace inside the body emit;
|
||||
the input substring must still be recoverable from the decoded
|
||||
body run."""
|
||||
user_text = "Hello, my name is Sebastian."
|
||||
msgs = [
|
||||
{"role": "user", "content": user_text},
|
||||
{"role": "assistant", "content": "Hi!"},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
user_body_ids = [
|
||||
tid
|
||||
for tid, mi, ic in zip(
|
||||
rendered.token_ids, rendered.message_indices, rendered.is_content
|
||||
)
|
||||
if mi == 0 and ic
|
||||
]
|
||||
assert user_body_ids, (
|
||||
f"{model_name}: no is_content=True tokens attributed to user message"
|
||||
)
|
||||
decoded = tokenizer.decode(user_body_ids).strip()
|
||||
assert user_text in decoded or decoded in user_text, (
|
||||
f"{model_name}: user body run decodes to {decoded!r}, "
|
||||
f"expected to contain {user_text!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_recovers_tool_body(model_name, tokenizer, renderer):
|
||||
"""The decoded run of is_content=True tokens within a tool message
|
||||
contains the tool response body. The whole point of the body/wrap
|
||||
cut: SFT on this run trains the model to anticipate tool outputs
|
||||
without learning to emit the surrounding ``<|tool_response>`` /
|
||||
role-tag scaffold (which would interrupt a real rollout)."""
|
||||
tool_text = "The capital of France is Paris."
|
||||
msgs = [
|
||||
{"role": "user", "content": "What's the capital of France?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"type": "function",
|
||||
"id": "call_1",
|
||||
"function": {
|
||||
"name": "lookup",
|
||||
"arguments": {"q": "capital of France"},
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "content": tool_text, "tool_call_id": "call_1"},
|
||||
{"role": "assistant", "content": "Paris."},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
tool_body_ids = [
|
||||
tid
|
||||
for tid, mi, ic in zip(
|
||||
rendered.token_ids, rendered.message_indices, rendered.is_content
|
||||
)
|
||||
if mi == 2 and ic
|
||||
]
|
||||
assert tool_body_ids, (
|
||||
f"{model_name}: no is_content=True tokens attributed to tool message"
|
||||
)
|
||||
decoded = tokenizer.decode(tool_body_ids).strip()
|
||||
assert tool_text in decoded, (
|
||||
f"{model_name}: tool body run decodes to {decoded!r}, "
|
||||
f"expected to contain {tool_text!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_recovers_system_body(model_name, tokenizer, renderer):
|
||||
"""The decoded run of is_content=True tokens within a system
|
||||
message contains the caller-provided system content. Tools header
|
||||
/ footer (if present) are scaffold and never appear as body."""
|
||||
sys_text = "You are an unusually precise assistant."
|
||||
msgs = [
|
||||
{"role": "system", "content": sys_text},
|
||||
{"role": "user", "content": "Hi"},
|
||||
{"role": "assistant", "content": "Hi!"},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
sys_body_ids = [
|
||||
tid
|
||||
for tid, mi, ic in zip(
|
||||
rendered.token_ids, rendered.message_indices, rendered.is_content
|
||||
)
|
||||
if mi == 0 and ic
|
||||
]
|
||||
assert sys_body_ids, (
|
||||
f"{model_name}: no is_content=True tokens attributed to system message"
|
||||
)
|
||||
decoded = tokenizer.decode(sys_body_ids).strip()
|
||||
assert sys_text in decoded, (
|
||||
f"{model_name}: system body run decodes to {decoded!r}, "
|
||||
f"expected to contain {sys_text!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_is_content_no_body_on_role_tag(model_name, renderer):
|
||||
"""The first token attributed to a user/system/tool message must
|
||||
have ``is_content=False`` — that's the leading role-tag run
|
||||
(``<|im_start|>`` / equivalent), which is template scaffold, never
|
||||
body."""
|
||||
msgs = [
|
||||
{"role": "user", "content": "Hi"},
|
||||
{"role": "assistant", "content": "Hello!"},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
user_positions = [k for k, idx in enumerate(rendered.message_indices) if idx == 0]
|
||||
assert user_positions, f"{model_name}: no tokens attributed to user message"
|
||||
first_k = user_positions[0]
|
||||
assert not rendered.is_content[first_k], (
|
||||
f"{model_name}: first user-attributed token at k={first_k} should "
|
||||
f"be is_content=False (role-tag scaffolding), but was True"
|
||||
)
|
||||
|
||||
|
||||
def test_content_token_spans_by_role_isolates_tool_body(
|
||||
model_name, tokenizer, renderer
|
||||
):
|
||||
"""``content_token_spans_by_role()["tool"]`` returns spans over
|
||||
which every token is the tool message body. Joining the decoded
|
||||
spans recovers the tool response. Adjacent scaffold tokens
|
||||
(``<|tool_response>``, role-tag openers) are never inside any
|
||||
returned span."""
|
||||
tool_text = "Result: 42"
|
||||
msgs = [
|
||||
{"role": "user", "content": "What's 6*7?"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"type": "function",
|
||||
"id": "call_x",
|
||||
"function": {"name": "calc", "arguments": {"e": "6*7"}},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "content": tool_text, "tool_call_id": "call_x"},
|
||||
{"role": "assistant", "content": "42."},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
spans = rendered.content_token_spans_by_role()
|
||||
tool_spans = spans.get("tool") or []
|
||||
assert tool_spans, f"{model_name}: no tool content spans returned"
|
||||
|
||||
pieces: list[str] = []
|
||||
for s, e in tool_spans:
|
||||
run_ids = rendered.token_ids[s:e]
|
||||
# All tokens in the span must be is_content=True by definition.
|
||||
for k in range(s, e):
|
||||
assert rendered.is_content[k], (
|
||||
f"{model_name}: span {(s, e)} contains is_content=False at k={k}"
|
||||
)
|
||||
pieces.append(tokenizer.decode(run_ids))
|
||||
joined = "".join(pieces).strip()
|
||||
assert tool_text in joined, (
|
||||
f"{model_name}: joined tool spans decode to {joined!r}, "
|
||||
f"expected to contain {tool_text!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_content_mask_for_roles_excludes_assistant_when_unset(model_name, renderer):
|
||||
"""``content_mask_for_roles({"tool"})`` returns a mask that's True
|
||||
only on tool body tokens — never on assistant tokens, even though
|
||||
those also have ``is_content=True``. The role filter is the whole
|
||||
point: SFT-on-tool-body must not bleed into the assistant span."""
|
||||
msgs = [
|
||||
{"role": "user", "content": "Hi"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"type": "function",
|
||||
"id": "call_a",
|
||||
"function": {"name": "ping", "arguments": {}},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "content": "pong", "tool_call_id": "call_a"},
|
||||
{"role": "assistant", "content": "OK."},
|
||||
]
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
tool_mask = rendered.content_mask_for_roles({"tool"})
|
||||
assert len(tool_mask) == len(rendered.token_ids)
|
||||
|
||||
for k, mi in enumerate(rendered.message_indices):
|
||||
if mi < 0:
|
||||
assert not tool_mask[k], (
|
||||
f"{model_name}: scaffold token k={k} (msg_idx=-1) marked in tool mask"
|
||||
)
|
||||
continue
|
||||
role = msgs[mi].get("role")
|
||||
if role != "tool" and tool_mask[k]:
|
||||
raise AssertionError(
|
||||
f"{model_name}: tool-role mask True on a {role!r} token at k={k}"
|
||||
)
|
||||
|
||||
|
||||
def test_build_training_sample_content_sft_roles_picks_up_tool_body(
|
||||
model_name, renderer
|
||||
):
|
||||
"""``build_training_sample(..., content_sft_roles={"tool"})``
|
||||
produces a loss mask that's True on tool body tokens AND assistant
|
||||
sampled tokens, but False on the tool-message scaffold (role tag,
|
||||
``<|tool_response>`` wraps, separators). The canonical
|
||||
SFT-on-tool-body + RL-on-assistant composition."""
|
||||
from renderers import build_training_sample
|
||||
|
||||
msgs = [
|
||||
{"role": "user", "content": "Hi"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"type": "function",
|
||||
"id": "call_z",
|
||||
"function": {"name": "noop", "arguments": {}},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "content": "done", "tool_call_id": "call_z"},
|
||||
{"role": "assistant", "content": "OK."},
|
||||
]
|
||||
ids, mask = build_training_sample(
|
||||
renderer,
|
||||
msgs,
|
||||
role_to_mask=lambda m: m["role"] == "assistant",
|
||||
content_sft_roles={"tool"},
|
||||
)
|
||||
assert len(mask) == len(ids)
|
||||
|
||||
# We need at least one trainable tool-body token if the renderer
|
||||
# populates is_content. Renderers that opt out (empty is_content)
|
||||
# fall back to the existing role_to_mask behaviour, which leaves
|
||||
# tool tokens False — that's the documented fallback and is fine.
|
||||
rendered = renderer.render(msgs)
|
||||
if not _is_populated(rendered):
|
||||
return
|
||||
|
||||
trainable_per_role: dict[str, int] = {}
|
||||
for k, mi in enumerate(rendered.message_indices):
|
||||
if mi < 0:
|
||||
continue
|
||||
if mask[k]:
|
||||
r = msgs[mi].get("role") or ""
|
||||
trainable_per_role[r] = trainable_per_role.get(r, 0) + 1
|
||||
|
||||
assert trainable_per_role.get("tool", 0) > 0, (
|
||||
f"{model_name}: build_training_sample with content_sft_roles={{'tool'}} "
|
||||
f"trained on zero tool tokens"
|
||||
)
|
||||
assert trainable_per_role.get("assistant", 0) > 0, (
|
||||
f"{model_name}: assistant tokens dropped from training mask"
|
||||
)
|
||||
assert trainable_per_role.get("user", 0) == 0, (
|
||||
f"{model_name}: user tokens leaked into training mask: {trainable_per_role}"
|
||||
)
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Kimi K2.5 tool-schema TypeScript rendering: bool/null literal correctness.
|
||||
|
||||
Regression for PR #55: ``_function_to_typescript`` used to leak Python
|
||||
literals (``True`` / ``False`` / ``None``) into output advertised as
|
||||
TypeScript. Both call sites — ``_EnumType.to_typescript_style`` (for
|
||||
non-string enum members) and ``_TypedParam.to_typescript_style`` (for
|
||||
bool defaults) — now route non-string values through ``json.dumps`` so
|
||||
booleans and ``None`` render as ``true`` / ``false`` / ``null``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from renderers.kimi_k25 import _function_to_typescript
|
||||
|
||||
|
||||
def test_bool_default_renders_as_json_literal():
|
||||
"""Bool defaults must render as ``true`` / ``false``, not ``True`` / ``False``."""
|
||||
out = _function_to_typescript(
|
||||
{
|
||||
"name": "set_logging",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"verbose": {"type": "boolean", "default": True},
|
||||
"quiet": {"type": "boolean", "default": False},
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
assert "// Default: true" in out
|
||||
assert "// Default: false" in out
|
||||
assert "Default: True" not in out
|
||||
assert "Default: False" not in out
|
||||
|
||||
|
||||
def test_enum_non_string_members_render_as_json_literals():
|
||||
"""``null`` / bool / int enum members must render as JSON literals, not Python ``repr``."""
|
||||
out = _function_to_typescript(
|
||||
{
|
||||
"name": "set_mode",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"mode": {"enum": ["auto", None, True, False, 0]},
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
assert '"auto" | null | true | false | 0' in out
|
||||
# Python literals must not leak through.
|
||||
assert "None" not in out
|
||||
assert "True" not in out
|
||||
assert "False" not in out
|
||||
@@ -3,8 +3,7 @@
|
||||
``load_tokenizer`` defaults to routing every supported model through
|
||||
``fastokens.patch_transformers()`` for ~10x faster encode. Models in
|
||||
``FASTOKENS_INCOMPATIBLE`` skip the patch (DeepSeek's Metaspace
|
||||
pretokenizer isn't supported; MiniMax tokenizers diverge on literal
|
||||
special-token text). Callers can opt out per-call with
|
||||
pretokenizer isn't supported). Callers can opt out per-call with
|
||||
``use_fastokens=False``.
|
||||
|
||||
These tests pin the policy:
|
||||
@@ -47,8 +46,6 @@ def test_fastokens_incompatible_is_explicit_set():
|
||||
{
|
||||
"deepseek-ai/DeepSeek-V3",
|
||||
"deepseek-ai/DeepSeek-V3-Base",
|
||||
"MiniMaxAI/MiniMax-M2",
|
||||
"MiniMaxAI/MiniMax-M2.5",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -108,7 +105,7 @@ def test_fast_and_vanilla_encode_identically_on_compatible_model():
|
||||
def test_incompat_model_loads_via_vanilla_backend(model):
|
||||
"""For models we know diverge / fail under fastokens, the fast path
|
||||
must be skipped so the load still succeeds with a vanilla backend."""
|
||||
if "DeepSeek" in model or "MiniMax" in model:
|
||||
if "DeepSeek" in model:
|
||||
# Skip if upstream gating / size makes the load impractical here.
|
||||
# We only care that the path doesn't try fastokens. Probe the
|
||||
# tokenizer_config to make sure the repo is reachable; if not,
|
||||
@@ -170,3 +167,45 @@ def test_fallback_on_fastokens_load_error(monkeypatch):
|
||||
assert "Shim" not in _backend_class_name(tok)
|
||||
# Still works.
|
||||
assert len(tok.encode("hi", add_special_tokens=False)) > 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Print suppression: fastokens itself prints "[fastokens]
|
||||
# patch_transformers: ..." on every patch/unpatch call. Building a
|
||||
# RendererPool of size N would emit ~N lines (the pool factory calls
|
||||
# load_tokenizer once per slot). load_tokenizer swallows that stdout
|
||||
# chatter and emits a single INFO log on the first patch instead.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_no_fastokens_stdout_chatter(capsys, caplog):
|
||||
"""``load_tokenizer`` must not leak ``[fastokens]`` prints onto
|
||||
stdout, and must emit exactly one INFO log per process announcing
|
||||
the fast path (not once per call)."""
|
||||
import logging
|
||||
|
||||
import renderers.base as rb
|
||||
|
||||
# Reset the process-wide "announced" flag so this test sees the
|
||||
# first-call log even if another test loaded a tokenizer earlier.
|
||||
rb._FASTOKENS_ANNOUNCED = False
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="renderers.base"):
|
||||
load_tokenizer(_FAST_MODEL)
|
||||
load_tokenizer(_FAST_MODEL)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "[fastokens]" not in captured.out, (
|
||||
f"fastokens print leaked to stdout: {captured.out!r}"
|
||||
)
|
||||
assert "[fastokens]" not in captured.err, (
|
||||
f"fastokens print leaked to stderr: {captured.err!r}"
|
||||
)
|
||||
|
||||
fastokens_info = [
|
||||
r for r in caplog.records if "fastokens enabled" in r.getMessage()
|
||||
]
|
||||
assert len(fastokens_info) == 1, (
|
||||
f"expected exactly one fastokens INFO log across two loads, "
|
||||
f"got {len(fastokens_info)}"
|
||||
)
|
||||
|
||||
@@ -695,3 +695,40 @@ def test_qwen3_vl_renderer_exposes_image_modality():
|
||||
renderer = create_renderer(tokenizer, renderer="auto")
|
||||
assert isinstance(renderer, Qwen3VLRenderer)
|
||||
assert "image" in MULTIMODAL_MODELS[model]
|
||||
|
||||
|
||||
def test_is_image_part_treats_type_field_as_authoritative():
|
||||
"""``Dataset.from_list`` unifies the Arrow schema across the elements
|
||||
of a list-typed column. A content list mixing text and image parts
|
||||
round-trips with ``image_url: None`` added to every text part (and
|
||||
``text: None`` added to every image part). The classifier must treat
|
||||
the ``type`` field as authoritative when present — falling back to
|
||||
a key-presence check on ``image_url`` would misclassify the text
|
||||
part and the renderer would later raise on ``_load_pil_image(None)``.
|
||||
"""
|
||||
from renderers.qwen3_vl import _is_image_part, _is_video_part
|
||||
|
||||
# Typed parts classify by their ``type``.
|
||||
assert _is_image_part(
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,XXX"}}
|
||||
)
|
||||
assert _is_image_part({"type": "image", "image": object()})
|
||||
assert _is_video_part(
|
||||
{"type": "video_url", "video_url": {"url": "data:video/mp4;base64,XXX"}}
|
||||
)
|
||||
|
||||
# Schema-unified text parts — typed as text, with a None zombie key
|
||||
# for the sibling modality — must NOT classify as image / video.
|
||||
schema_unified_text = {"type": "text", "text": "hello", "image_url": None}
|
||||
assert not _is_image_part(schema_unified_text)
|
||||
assert not _is_video_part(schema_unified_text)
|
||||
schema_unified_text_with_video = {"type": "text", "text": "hi", "video_url": None}
|
||||
assert not _is_video_part(schema_unified_text_with_video)
|
||||
|
||||
# Untyped fallback only fires when ``type`` is absent, and requires
|
||||
# a truthy value (mere key presence isn't enough).
|
||||
assert _is_image_part({"image_url": {"url": "data:..."}})
|
||||
assert _is_image_part({"image": object()})
|
||||
assert not _is_image_part({"image_url": None})
|
||||
assert not _is_image_part({"image": None})
|
||||
assert not _is_video_part({"video_url": None})
|
||||
|
||||
@@ -12,6 +12,9 @@ resolution-markers = [
|
||||
exclude-newer = "0001-01-01T00:00:00Z" # This has no effect and is included for backwards compatibility when using relative exclude-newer values.
|
||||
exclude-newer-span = "P7D"
|
||||
|
||||
[options.exclude-newer-package]
|
||||
fastokens = false
|
||||
|
||||
[[package]]
|
||||
name = "annotated-doc"
|
||||
version = "0.0.4"
|
||||
@@ -264,6 +267,35 @@ wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/8a/0e/97c33bf5009bdbac74fd2beace167cab3f978feb69cc36f1ef79360d6c4e/exceptiongroup-1.3.1-py3-none-any.whl", hash = "sha256:a7a39a3bd276781e98394987d3a5701d0c4edffb633bb7a5144577f82c773598", size = 16740, upload-time = "2025-11-21T23:01:53.443Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastokens"
|
||||
version = "0.2.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/14/8e/7e88ec1d48db5a6e8d8d44318ce285e38c04b81508bdc2a60e17045a116f/fastokens-0.2.0.tar.gz", hash = "sha256:ef0e175de5c8cb1b616b3210d75dce1fab78e35fc02f77f03f7847d4678be686", size = 675822, upload-time = "2026-05-17T10:32:55.642Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/e8/14/3d640cbe3c866ee6c113ea4ca37c16c5aa44be6412918928bbd3f3b739ef/fastokens-0.2.0-cp313-cp313t-macosx_10_12_x86_64.whl", hash = "sha256:e7db86c2785a502e3cd993c7d3a91c46e2751f94d1f446caa9600e9cf3dafbd1", size = 3078350, upload-time = "2026-05-17T10:32:41.313Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/bc/bf/33ca3798842fb8bdacf03a97c874454d50fe328d44e0d3c3ba7a633fd3ab/fastokens-0.2.0-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:fa8dcbc6ad3f7a7e9f5bf1ef3cfc9f0c04f0c8779be9e38864815dc567b27de4", size = 2983397, upload-time = "2026-05-17T10:32:38.823Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/c4/0a/1bfd13fb855bce3ce50faa644d6e0e19343035706c2628e17da1247bbf50/fastokens-0.2.0-cp313-cp313t-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:09b399600ae5beaa34e45afd05c6835b8569861955dfdc3e05afba25fe414e5a", size = 3309835, upload-time = "2026-05-17T10:32:33.427Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/32/ce/33292977a81011ffc59cee1c20b6f8d0dbd9cb39209c327dc74dc542178d/fastokens-0.2.0-cp313-cp313t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f363b1e89fe4ed979224c326b25b33b28e1172134f3e7959238276170f12328e", size = 3615173, upload-time = "2026-05-17T10:32:30.898Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/07/1c/1bef8b4831bf9220ae182fcee3e250273b7f537c81aec96041f6eb4bc990/fastokens-0.2.0-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5d79358faa11658d8908fbdfab321b6ae6a0ff52ae3cdce7a0fda5c30629a276", size = 3295485, upload-time = "2026-05-17T10:32:36.109Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/92/11/49afb739800b6a82af04fa1e991b68669c789ebeaa3bcbc96c2544c8ff9c/fastokens-0.2.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:cf6ae284b70d65989548a8143e1f463f31126f5650ad45ce6af05b83e4afb3e7", size = 3329524, upload-time = "2026-05-17T10:32:44.607Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f0/d2/f9b1c01535a0ce994d30e86cf49616023246134d2a406b74c5dd49df7825/fastokens-0.2.0-cp313-cp313t-musllinux_1_2_armv7l.whl", hash = "sha256:40fa091ff12aa8ac00fa8e2133c7256b96209827b88de5120eb1361600d7f68f", size = 3149194, upload-time = "2026-05-17T10:32:47.596Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/bd/0b/95b2e5d25efc684988918658c90445e0e5f1dc4fee7dae5edd586b4feaba/fastokens-0.2.0-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:f643f4739b20c86ed21e2c46e7fe203e6a621e21124074f138819423882e3aba", size = 3401011, upload-time = "2026-05-17T10:32:50.13Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/9b/31/80b74196524d7a9576fa96685a39ee1f333ffd12eb29dbeb5e65c6e3dcb2/fastokens-0.2.0-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:3cd4effdc6610cbee0be717032652a1997976f1a2f21949435d925c7982cf6f3", size = 3593240, upload-time = "2026-05-17T10:32:52.661Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/59/96/12814aa955b7277adb07a8b36176181f247f3b9cf973d05b34294df6a72c/fastokens-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl", hash = "sha256:d29eb1d608977d63fc4679d6ce2b360fdb8b0ea8d66a0eaf804f7ac31ba52a3a", size = 3089011, upload-time = "2026-05-17T10:32:43.16Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/c0/05/553b59c6a8542ad7d443306fc1fd7bb012e4aae459ccf9ab422ea4c681b7/fastokens-0.2.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:f8ec7a20322aafe245201727c7a507b87333c51dc580940b9ee5456dbfe3963c", size = 2992964, upload-time = "2026-05-17T10:32:39.992Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/13/66/a53c2309003510de7c189ba8a9d2ea5a1833e9b447d9f69b95449c3dfe34/fastokens-0.2.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:dc4c0a25726620b1cc4cfb7e9dc1a53b17bbe3f2f9adbaa57b8375b5f36ac5d4", size = 3316289, upload-time = "2026-05-17T10:32:34.557Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/b4/54/e0e4318ee1ad0b5196df72cf93615bba0b81f7869d659a44ccc475969151/fastokens-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:160253f8d30747cf66e7ed895c513e16f7b173dd9e644fa641e2eecbd43a616a", size = 3303534, upload-time = "2026-05-17T10:32:37.462Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/64/44/bfff90e4b1a43c17edf7305dafbd56dc992bbe832cc08da78f1f50104c2d/fastokens-0.2.0-cp39-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:b61b9fe5b41e0bb36ad86e7551dc53293c9833909ef07b1cdbaa2055b06c3b3e", size = 3254096, upload-time = "2026-05-17T10:32:28.489Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/df/89/bec1a0368100c5f1134ab1ce588d71f88697fadbbad5f26bb130eda00fe8/fastokens-0.2.0-cp39-abi3-manylinux_2_28_armv7l.whl", hash = "sha256:8f138fc64e355589be43068e6996ac0bfcfd872cd75fb5793d11d06cb9c0ac9b", size = 3000177, upload-time = "2026-05-17T10:32:29.745Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f2/fd/cbe8a033e2ef565ce5aef4e02316054b04adad96ee9805cba74a50a84ed9/fastokens-0.2.0-cp39-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:c1a6cd93d16e880d81deb12519cc2eb20149d7423321cf00a34ce25ea9c9ef15", size = 3626561, upload-time = "2026-05-17T10:32:32.087Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/05/bf/1cad7f0e8d03f5f5b2b417cda8859e4d968d2eebdca0cd336b23d7dbbdbb/fastokens-0.2.0-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:01b9bdba818d7b2c67d57d9917faf7a1dad32ece0734440130de94ad768b819f", size = 3336689, upload-time = "2026-05-17T10:32:46.21Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/05/56/0030c6e67c60ee88b74336d1bb03eb556b2afa60445268921ad02d9cb3b3/fastokens-0.2.0-cp39-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:9aec70b85a06bf2ac95dd76e07e404040dbcfb06eb165f29bc61ef2ab68dc87a", size = 3154666, upload-time = "2026-05-17T10:32:48.835Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/c1/5b/02cc5d501ccc2fbd4b068a7aa34a35347bdb3a4189b96af647ef0407af87/fastokens-0.2.0-cp39-abi3-musllinux_1_2_i686.whl", hash = "sha256:8c41f5278ac53853249c1170d653ee0afaa0906c9bc32c8d25c9443b6d667298", size = 3406657, upload-time = "2026-05-17T10:32:51.392Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/97/d7/f5fb2564e16b1f5733e05c41b090f95a3fe767f6b888ba7d864193bc5447/fastokens-0.2.0-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:d068bc50082ad67d5d542847075f1f7b8d10f703274e56e241312f18b4d9e772", size = 3598064, upload-time = "2026-05-17T10:32:54.109Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/0a/39/2098de2aa01c3e2ef62b066926d70015f88845e3ac1a976ba1cc3a363c05/fastokens-0.2.0-cp39-abi3-win_amd64.whl", hash = "sha256:5729c44ce1d60cb03e506731dcb17d8c2d69c267098c3ff35ca8be37d618714d", size = 2767905, upload-time = "2026-05-17T10:32:56.759Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filelock"
|
||||
version = "3.29.0"
|
||||
@@ -374,11 +406,11 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.13"
|
||||
version = "3.15"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/ce/cc/762dfb036166873f0059f3b7de4565e1b5bc3d6f28a414c13da27e442f99/idna-3.13.tar.gz", hash = "sha256:585ea8fe5d69b9181ec1afba340451fba6ba764af97026f92a91d4eef164a242", size = 194210, upload-time = "2026-04-22T16:42:42.314Z" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/82/77/7b3966d0b9d1d31a36ddf1746926a11dface89a83409bf1483f0237aa758/idna-3.15.tar.gz", hash = "sha256:ca962446ea538f7092a95e057da437618e886f4d349216d2b1e294abfdb65fdc", size = 199245, upload-time = "2026-05-12T22:45:57.011Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/5d/13/ad7d7ca3808a898b4612b6fe93cde56b53f3034dcde235acb1f0e1df24c6/idna-3.13-py3-none-any.whl", hash = "sha256:892ea0cde124a99ce773decba204c5552b69c3c67ffd5f232eb7696135bc8bb3", size = 68629, upload-time = "2026-04-22T16:42:40.909Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/d2/23/408243171aa9aaba178d3e2559159c24c1171a641aa83b67bdd3394ead8e/idna-3.15-py3-none-any.whl", hash = "sha256:048adeaf8c2d788c40fee287673ccaa74c24ffd8dcf09ffa555a2fbb59f10ac8", size = 72340, upload-time = "2026-05-12T22:45:55.733Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1335,6 +1367,7 @@ wheels = [
|
||||
name = "renderers"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "fastokens" },
|
||||
{ name = "jinja2" },
|
||||
{ name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
|
||||
{ name = "numpy", version = "2.4.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },
|
||||
@@ -1358,6 +1391,7 @@ dev = [
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "fastokens", specifier = ">=0.2.0" },
|
||||
{ name = "jinja2" },
|
||||
{ name = "numpy" },
|
||||
{ name = "openai", specifier = ">=1.108.1" },
|
||||
|
||||
Reference in New Issue
Block a user