[Spec Decode][DSpark] Add Gemma4-12B DSpark draft model (#47216)

Signed-off-by: DiegoCao <DiegoCao@users.noreply.github.com>
Co-authored-by: DiegoCao <DiegoCao@users.noreply.github.com>
This commit is contained in:
Hangrui Cao
2026-07-16 21:51:47 +00:00
committed by GitHub
co-authored by DiegoCao
parent c95c663049
commit 4a394bfcda
6 changed files with 417 additions and 4 deletions
+12 -4
View File
@@ -283,28 +283,36 @@ def evaluate_gsm8k_offline(
max_tokens: int = 256,
temperature: float = 0.0,
gen_prefix: str = "",
use_chat_completions: bool = False,
) -> dict[str, float | int]:
"""Evaluate GSM8K accuracy using an offline vllm.LLM object.
Same prompts and scoring as evaluate_gsm8k(), but runs generation
directly via llm.generate() instead of calling a server over HTTP.
When ``use_chat_completions=True``, prompts go through the chat template via
``llm.chat()`` instead of raw completion (for instruction-tuned models).
"""
from vllm import SamplingParams
prompts, labels = _build_gsm8k_prompts(num_questions, num_shots, gen_prefix)
sampling_params = SamplingParams(
temperature=temperature,
max_tokens=max_tokens,
stop=["Question", "Assistant:", "<|separator|>"],
)
mode = "chat" if use_chat_completions else "completion"
print(
f"Running offline GSM8K evaluation: {len(prompts)} questions, {num_shots}-shot"
f"Running offline GSM8K evaluation: {len(prompts)} questions, "
f"{num_shots}-shot, {mode}"
)
tic = time.perf_counter()
outputs = llm.generate(prompts, sampling_params)
if use_chat_completions:
conversations = [[{"role": "user", "content": p}] for p in prompts]
outputs = llm.chat(conversations, sampling_params)
else:
outputs = llm.generate(prompts, sampling_params)
latency = time.perf_counter() - tic
states = [o.outputs[0].text for o in outputs]
+6
View File
@@ -1477,6 +1477,12 @@ _SPECULATIVE_DECODING_EXAMPLE_MODELS = {
is_available_online=False,
use_original_num_layers=True, # DSpark backbone requires all layers
),
"Gemma4DSparkModel": _HfExamplesInfo(
"google/gemma-4-12B-it",
speculative_model="deepseek-ai/dspark_gemma4_12b_block7",
is_available_online=False,
use_original_num_layers=True,
),
# [Eagle]
"EagleCohereForCausalLM": _HfExamplesInfo(
"/host/engines/cohere-moe",
@@ -286,6 +286,73 @@ def test_suffix_decoding_acceptance(
cleanup_dist_env_and_memory()
@pytest.mark.slow_test
@large_gpu_mark(min_gb=80)
def test_gemma4_dspark_correctness_and_acceptance_rate(
monkeypatch: pytest.MonkeyPatch,
):
"""
E2E test for Gemma4 DSpark speculative decoding: acceptance rate/length
regression coverage plus GSM8K correctness, at temperature=1.0 to exercise
the probabilistic draft-sampling/rejection-sampling path (not just greedy).
Uses google/gemma-4-12B-it as target with the dspark_gemma4_12b_block7 draft
model. Exercises the full Gemma4 DSpark path (draft build over the reused
base classes DFlashQwen3Model / Qwen3DSparkForCausalLM / Gemma4MTP* /
DSparkMarkovHead, the fused context-KV precompute, the Markov head, and
rejection sampling), so it fails if any base class drifts out of sync.
gemma-4-12B is instruct-tuned, so GSM8K is run through the chat template
(use_chat_completions=True; raw few-shot completion collapses to a few
percent). Reference: measured over 5 runs of 200 GSM8K questions at
temperature=1.0 (prefix caching disabled):
accuracy: min=0.900 max=0.955 mean=0.937
acceptance_rate: min=0.578 max=0.595 mean=0.588
acceptance_len: min=5.044 max=5.167 mean=5.116
Thresholds set conservatively to 10% to avoid flaking due to unlucky sampling
"""
monkeypatch.setenv("VLLM_USE_FLASHINFER_SAMPLER", "0")
spec_llm = LLM(
model="google/gemma-4-12B-it",
trust_remote_code=True,
speculative_config={
"method": "dspark",
"model": "deepseek-ai/dspark_gemma4_12b_block7",
"num_speculative_tokens": 7,
"draft_sample_method": "probabilistic",
},
max_model_len=8192,
max_num_seqs=32,
gpu_memory_utilization=0.8,
enforce_eager=True,
enable_prefix_caching=False,
disable_log_stats=False,
)
try:
results = evaluate_gsm8k_offline(
spec_llm, num_questions=200, temperature=1.0, use_chat_completions=True
)
gsm8k_accuracy = results["accuracy"]
metrics = spec_llm.get_metrics()
acceptance_rate = compute_acceptance_rate(metrics)
acceptance_len = compute_acceptance_len(metrics)
print(
f"Gemma4 DSpark acceptance_rate={acceptance_rate:.2f}, "
f"acceptance_len={acceptance_len:.2f}, "
f"gsm8k_accuracy={gsm8k_accuracy:.3f}"
)
assert acceptance_rate >= 0.588 * 0.9
assert acceptance_len >= 5.116 * 0.9
assert gsm8k_accuracy >= 0.937 * 0.9
finally:
del spec_llm
torch.accelerator.empty_cache()
cleanup_dist_env_and_memory()
@pytest.mark.parametrize(
["model_path", "expected_accuracy_threshold"],
[