Files
FreeToken/tests/tokenizer/test_effort.py
Xiaoze Fanandandy-yang-1 127c85665b feat(deepseek-v41): support text and vision serving (#603)
* test(deepseek-v41): add independent text and vision reference

* feat(quant): support block-32 and unrounded inference outputs

* feat(attention): add csa2 packed cache and sparse backend

* feat(deepseek-v41): serve text and images through shared runtime

* perf(mhc): tune batch-stable reduction tiles

* perf(moe): autotune fp4 decode launch geometry

* perf(moe): admit recurring misses against actual lru victims

* revert: perf(moe): admit recurring misses against actual lru victims

This reverts commit 1652d200f0ebb825379cf44c28baf52971e3a461.

* perf(mhc): derive the reduction tile from the width, not a timing

* perf(moe): replace the ds_fp4 decode autotune with a fixed two-warp launch

* fix(deepseek-v41): fence engram staging against in-flight lookups

* fix(moe): return the cpu routed sum in fp32 and round it in python

* fix(deepseek-v41): keep the vision tower and aligner unquantized

* fix(scheduler): admit bounded-replay hits by the history their resume reads

* refactor(deepseek-v41): name the attention family after the model, not csa2

* refactor(kvcache): import the dsv41 pool only for dsv41 models

* refactor(deepseek-v41): name the replay switch --swa-decoder-replay

* fix(server): keep reasoning_effort a string on the wire

* refactor(models): keep the prefill replay hooks off BaseLLMModel

* refactor(deepseek-v41): drop the fp32 routed sum and batch-invariant prefill

* fix(moe): weight ds_fp4 routes in the down epilogue again

* docs(models): anchor the v4.1 replay paragraph on the tech report

* refactor(deepseek-v41): run the decoder source's narrowed prefill on Block

* refactor(kvcache): let the dsv41 pool own the history a resume keeps live

* refactor(deepseek-v41): resume bounded-replay hits by capping the prefix match

* refactor(deepseek-v41): describe the attention group as a dsv4 variant and derive its geometry from the args

* refactor(deepseek-v41): keep the engram table locator in the model package

* refactor(deepseek-v41): drop the fp32 lm head output

- return the head's bf16 logits, as DeepSeek-V4 does
- revert ParallelLMHead(out_dtype=) and LinearKernel.apply_out_dtype, which only this call used
- remove test_head_preserves_fp32_logit_margin
- cast test_vision's prefill logits to fp32 before comparing, as the harness does

* refactor(quant): carry the w4a8 activation block on MoEConfig

- drop ActDesc and QuantScheme.act; the scheme only describes stored tensors
- Fp8BlockConfig sets MoEConfig.act_block from weight_block_size for its fp4 experts via a layer_config hook
- the ds_fp4 GPU kernel and CPU executor read the block from the layer config and require it

* refactor(deepseek-v41): size the kv pool from the args, not a geometry copy

- delete kvcache/dsv41_geometry.py; the pool and cost model read dsv41_args by attribute, as the DSV4 pool reads dsv4_args
- source / ratio aliasing reads args.roles; the duplicated source and ratio validation is gone
- the cost model owns the tier row formats, row bytes and the private window layers
- the pool keeps the format and window/ratio checks, and exposes the formats and is_private_window to the attention backend

* refactor(kvcache): move the dsv4 pool into kvcache/dsv4

* refactor(kvcache): move the dsv41 pool into kvcache/dsv4

* chore(benchmarks): drop the dsv41 selection and fp8 linear benchmarks

* test(deepseek-v41): drop tests that repeat other coverage

* test(deepseek-v41): load the tiny model's resident experts as banks

* fix(server): parse deepseek-v4.1's space-led dsml tool calls

---------

Co-authored-by: andy-yang-1 <andy_yang@berkeley.edu>
2026-10-07 00:15:11 -07:00

170 lines
6.4 KiB
Python

"""Unit tests for the reasoning-effort dialect layer (tokenizer/effort.py)."""
from __future__ import annotations
import pytest
from freetoken.tokenizer.effort import (
EFFORT_SCALE,
EffortProfile,
KNOWN_REASONING_EFFORTS,
probe_effort_profile,
quantize_effort,
)
QWEN38 = EffortProfile(
supported=frozenset({"xhigh", "medium", "low"}), default="xhigh", consumes_effort=True
)
DSV4_OFFICIAL = EffortProfile(
supported=frozenset({"low", "high", "max"}), default="low", consumes_effort=True
)
IGNORES = EffortProfile(supported=frozenset(KNOWN_REASONING_EFFORTS), default=None, consumes_effort=False)
def test_in_vocabulary_values_pass_through():
for name in ("xhigh", "medium", "low"):
assert quantize_effort(name, QWEN38) == name
for name in ("low", "high", "max"):
assert quantize_effort(name, DSV4_OFFICIAL) == name
def test_deepseek_dialect_high_lands_on_qwen_xhigh():
# high (0.9) is nearer xhigh (0.99) than medium (0.7)
assert quantize_effort("high", QWEN38) == "xhigh"
def test_openai_dialect_medium_drops_to_the_dsv4_default():
# medium (0.7) is 0.2 from the nearest gear -- beyond the quantize
# threshold, so nothing is sent and the encoder default (low) applies;
# anything else would silently escalate OpenAI-default traffic to the
# encoder's absolute-maximum "high" prompt. Matches vLLM's DSV4 mapping.
assert quantize_effort("medium", DSV4_OFFICIAL) is None
def test_xhigh_lands_on_dsv4_high_never_max():
# "max" is an extreme opt-in gear, reachable only by its own name
# (vLLM's DSV4 rule) -- rounding must not enter it.
assert quantize_effort("xhigh", DSV4_OFFICIAL) == "high"
def test_max_lands_on_qwen_xhigh():
assert quantize_effort("max", QWEN38) == "xhigh"
def test_far_values_drop_instead_of_rounding():
profile = EffortProfile(
supported=frozenset({"low", "xhigh"}), default=None, consumes_effort=True
)
assert quantize_effort("minimal", profile) == "low" # 0.1 away
assert quantize_effort("medium", profile) is None # 0.29 to the nearest gear
def test_unknown_and_non_string_values_drop_to_template_default():
assert quantize_effort("banana", QWEN38) is None
assert quantize_effort(3, QWEN38) is None
assert quantize_effort(None, QWEN38) is None
def test_effort_ignoring_template_sends_nothing():
assert quantize_effort("high", IGNORES) is None
assert quantize_effort("xhigh", IGNORES) is None
# --------------------------------------------------------------------------- #
# Probe tests: fake render callables standing in for real templates/encoders.
# --------------------------------------------------------------------------- #
def _qwen38_render(kwargs, tools):
# Validates unconditionally (enable_thinking undefined counts as on) and
# renders a distinct effort preamble per gear, default xhigh.
effort = kwargs.get("reasoning_effort", "xhigh")
if effort not in ("xhigh", "medium", "low"):
raise ValueError(f"Unexpected reasoning effort {effort}")
preamble = {"xhigh": "think hard", "medium": "", "low": "think briefly"}[effort]
return f"{preamble}|tools={bool(tools)}"
def test_probe_learns_the_qwen38_vocabulary():
profile = probe_effort_profile(_qwen38_render)
assert profile.supported == frozenset({"xhigh", "medium", "low"})
assert profile.default == "xhigh"
assert profile.consumes_effort
assert profile.validates # rejections observed -> the vocabulary is real
def _dsv4_render(kwargs, tools):
# Grades effort only in thinking mode (tools force it); asserts on unknown
# values there; "low" renders the empty preamble, matching the default.
effort = kwargs.get("reasoning_effort") or "low"
if not tools:
return "chat prompt"
assert effort in ("low", "high", "max"), f"Invalid reasoning effort: {effort}"
preamble = {"low": "", "high": "absolute maximum", "max": "beyond maximum"}[effort]
return f"{preamble}|thinking"
def test_probe_learns_the_dsv4_vocabulary_through_the_tools_round():
profile = probe_effort_profile(_dsv4_render)
assert profile.supported == frozenset({"low", "high", "max"})
assert profile.default == "low"
assert profile.consumes_effort
assert profile.validates
def test_probe_marks_an_ignoring_template_as_not_consuming():
profile = probe_effort_profile(lambda kwargs, tools: f"same|tools={bool(tools)}")
assert not profile.consumes_effort
assert not profile.validates
assert profile.default is None
def test_probe_marks_an_interpolating_template_as_not_validating():
# Grades effort (renders differ) but rejects nothing: consumes without a
# trustworthy vocabulary.
profile = probe_effort_profile(lambda kwargs, tools: f"p|{kwargs.get('reasoning_effort')}")
assert profile.consumes_effort
assert not profile.validates
def test_probe_skips_rounds_whose_baseline_fails():
def render(kwargs, tools):
if tools: # template rejects tools outright -- round is uninformative
raise RuntimeError("no tools supported")
return _qwen38_render(kwargs, tools)
profile = probe_effort_profile(render)
assert profile.supported == frozenset({"xhigh", "medium", "low"})
assert profile.consumes_effort
def _numeric_render(kwargs, tools):
if not tools and not kwargs.get("enable_thinking"):
return "chat"
effort = kwargs.get("reasoning_effort", "high")
if isinstance(effort, str):
effort = {"low": 50, "high": 75, "max": 100}[effort]
if type(effort) is not int or not 1 <= effort <= 100:
raise ValueError("invalid budget")
return f"Reasoning Effort: {effort}"
def test_probe_preserves_validated_numeric_budgets():
profile = probe_effort_profile(_numeric_render)
assert profile.integer_range == (1, 100)
assert profile.default == "high"
for budget in range(1, 101):
assert quantize_effort(budget, profile) == budget
for bad in (0, 101, -1, True, False, 1.0, 1.5):
with pytest.raises(ValueError, match="integer"):
quantize_effort(bad, profile)
def test_numeric_capability_requires_validation_and_the_whole_range():
assert probe_effort_profile(_dsv4_render).integer_range is None
assert probe_effort_profile(lambda kw, tools: str(kw)).integer_range is None
def holes(kwargs, tools):
if kwargs.get("reasoning_effort") == 42:
raise ValueError("unsupported")
return _numeric_render(kwargs, tools)
assert probe_effort_profile(holes).integer_range is None