Show HN: Cactus Hybrid:我们教会了 Gemma 4 如何判断自己是否出错
Show HN: Cactus Hybrid: We taught Gemma 4 to know when it's wrong

原始链接: https://github.com/cactus-compute/cactus-hybrid

Cactus 推出了一款经过后训练的“Gemma 4 E2B Hybrid”模型,旨在解决小型端侧 AI 固有的可靠性问题。通过嵌入可为每次响应生成结构化置信度评分(0 到 1)的内部探针,该模型允许开发人员实施智能路由:如果置信度低于阈值(例如 0.85),查询将自动卸载至规模更大、能力更强的模型。 这种混合方法使最小的 Gemma 模型能够在仅重定向 15%–35% 查询的情况下,在各项主要基准测试中达到与 Gemini 3.1 Flash-Lite 相当的性能。值得注意的是,这些内部探针并非依赖表层模式,而是通过隐藏状态检测正确性信号;这一点已得到验证,即使在音频等未经过专门训练的模态下,该模型也能高精度地识别自身的错误。 Cactus 为这一工作流程提供了跨主流框架(包括 Hugging Face、MLX 以及打过补丁的 `llama.cpp`)的开源支持。这使开发人员能够在速度、隐私和高保真性能之间取得平衡,确保系统在必要时能够自动调用“援军”。

Cactus Compute 发布了开源工具“Cactus Hybrid”,该工具使端侧模型能够准确识别其生成内容可能出现错误的时机。通过在 Gemma 4 模型的隐藏层上训练一个仅 68k 参数的探测层,该团队创建了一个可靠的置信度评分(0–1),其表现显著优于传统的 Token 熵方法。 这种方法允许开发者运行快速且保护隐私的端侧模型,并智能地仅将 15%–35% 的不确定性查询路由至成本更高的云端模型(如 Gemini 3.1 Flash-Lite)。测试表明,这种混合架构在仅需一小部分成本的情况下,即可实现与大型云端模型相当的基准测试性能。 主要亮点包括: * **高准确度:** 该探测器在文本、视觉和音频基准测试中实现了平均 0.814 的 AUROC 值。 * **模态独立性:** 探测器通过隐藏状态识别正确性信号,而非依赖记忆模式。 * **可访问性:** 代码采用 MIT 许可证,权重已发布于 Hugging Face,并兼容 Llama.cpp、MLX 和 Transformers 等主流框架。 Cactus 目前正在优化该工具以实现更复杂的层级化路由,并计划近期发布关于其机制研究的详细报告。
相关文章

原文

A small, on-device model is fast and private, but sometimes wrong. At Cactus we post-train models to know when they are wrong: we ship probes inside the checkpoint that score every answer with a confidence between 0 and 1, returned as structured data (never parsed out of the answer text). Answer on-device when confidence is high; you can re-route to a bigger model when it's low:

if confidence < 0.85:
    answer = ask_a_bigger_model(prompt)

We start the rollout with Gemma 4 E2B Hybrid, all builds live in the Cactus Hybrid collection on Hugging Face.

Gemma 4 E2B hybrid, the smallest Gemma model, matches Gemini 3.1 Flash-Lite on most benchmarks by routing only 15–35% of queries to the Gemini 3.1 Flash-Lite and running the remnant itself.

Benchmark Handoff to match Flash-Lite (FP16) At 4-bit At 3-bit
ChartQA 15–20% 25–30% 40–50%
MMBench 30–35% 40–45% 50–55%
LibriSpeech 25–30% 35–40% 55–65%
GigaSpeech 30–35% 40–45% 50–55%
MMAU 30–35% 35–40% 50–55%
MMLU-Pro 45–55% ~90% n/a
  • N/B: Quantisation quality is measured on Cactus Quants which performs well at uniform quantization.
  • Developers are encouraged to benchmark for Unsloth, GGUF, and MLX quantization independently.
# pip install cactus-compute
import json
from cactus.bindings.cactus import cactus_complete, cactus_init
from cactus.cli.download import download_bundle

lm = cactus_init(str(download_bundle("Cactus-Compute/gemma-4-E2B-it")))
result = cactus_complete(
    lm,
    [{"role": "user", "content": "What is the capital of France?"}],
    json.dumps({"max_tokens": 512, "auto_handoff": False}),
    None,
    lambda *_: None,
)
print(result["response"].strip())
print("confidence:", result["confidence"])
# pip install mlx-lm
import re
from mlx_lm import load, generate

model, tokenizer = load(
    "Cactus-Compute/gemma-4-e2b-it-hybrid-mlx",
    tokenizer_config={"trust_remote_code": True},
)

messages = [{"role": "user", "content": "What is the capital of France?"}]
answer = generate(
    model,
    tokenizer,
    prompt=tokenizer.apply_chat_template(messages, add_generation_prompt=True),
    max_tokens=512,
)
# the checkpoint reasons before answering; keep only the final answer
answer = re.split(r"<\|?channel\|?>", answer)[-1]
answer = re.sub(r"^(thought|final)\b\s*", "", answer).strip()
print(answer)
print("confidence:", model.last_confidence)
# pip install "transformers>=5.5.4,<5.6" torch   (5.14+ segfaults on this checkpoint)
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "Cactus-Compute/gemma-4-e2b-it-hybrid"
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, dtype="auto").to(device)

messages = [{"role": "user", "content": "What is the capital of France?"}]
inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(device)
out = model.generate(**inputs, return_confidence=True, max_new_tokens=512)

print(tokenizer.decode(out.sequences[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
print("confidence:", out.confidence)

Load the model with an explicit .to(device), not device_map="auto": the probe scores generations outside the module forward() path, so weights that accelerate offloads (left on the meta device) crash the confidence read.

llama.cpp is C++, so the probe is a patch you compile into the engine (see patches/llama.cpp/). Build the patched server once:

git clone https://github.com/cactus-compute/cactus-hybrid && cd cactus-hybrid
./patches/llama.cpp/install.sh && rehash

Then serve and query it like any llama-server — the response carries a top-level confidence field:

llama-server -hf Cactus-Compute/gemma-4-e2b-it-hybrid-GGUF:Q4_K_M --jinja
curl -s http://localhost:8080/v1/chat/completions \
  -d '{"messages":[{"role":"user","content":"What is the capital of France?"}],"max_tokens":512}' \
  | jq '{answer: .choices[0].message.content, confidence}'

Gemma 4 E2B Hybrid AUROC measures how well the the separates wrong answers from right ones (higher = better, 0.5 is random, 1.0 is perfect):

Hold-out Modality Cactus Hybrid Token Entropy
MMLU text MCQ 0.770 0.697
MMLU-Pro text MCQ 0.771 0.692
ARC-Easy text MCQ 0.888 0.655
ARC-Challenge text MCQ 0.834 0.646
GSM8K (3-shot) text gen 0.782 0.731
MMBench-EN-Dev vision MCQ 0.840 0.435
ChartQA vision QA 0.779 0.615
DocVQA vision QA 0.781 0.512
MMAU audio MCQ 0.789 0.517
GigaSpeech audio 0.876 0.343
Earnings-22 audio 0.839 0.323
LibriSpeech audio 0.822 0.427
Mean 0.814 0.549

The strongest result: the probe was trained on zero audio data, yet achieves 0.79–0.88 AUROC on four audio benchmarks (two transcription, one audio MCQ, one out-of-domain transcription).

This rules out surface-level explanations, the probe is reading a modality-independent correctness signal from the hidden state, not memorizing patterns from training data.


MIT-licensed. Gemma model use is subject to the Gemma terms.

联系我们 contact @ memedata.com