"""TensorRT (Torch-TensorRT + Torch Inductor) and SGLang acceleration backends
for Gemma-4-26B-A4B-HybridAgent (BF16) and Gemma-4-26B-A4B-HybridAgent-4bit (W4A16).
Why custom acceleration is needed for HybridAgent:
During generation, HybridAgent dynamically switches between:
1. Text Mode (DECODE): discrete token sampling over persistent KV working memory.
2. Latent Mode (LATENT): continuous hidden-state recurrence where last-layer hidden
state h_t is mapped through `HybridLink` (R(h_t)) into Gemma 4's scaled embedding
space (`embed_scale = sqrt(2816)`) and fed directly as `inputs_embeds` into the next
step until the cosine/logit readout hits `` (token_id=262145).
3. Tool Mode (FEED): concurrent `` / `` execution appended to KV cache.
This module provides two acceleration backends that preserve 100% of `` continuous
reasoning and `` tool execution:
- `enable_tensorrt_acceleration(model)`:
1) Compiles `model.hybridlink` (`adapter.pt` / `hybridlink.pt`) into a native NVIDIA
TensorRT Engine via `torch_tensorrt.compile` (with fallback to `torch.compile`).
2) Replaces Hugging Face's slow loop-based `Gemma4TextExperts.forward` (which triggers
30 `.nonzero()` CPU-GPU syncs + 240 Python loops per token on BF16 models) with a
zero-sync batched tensorized fast path (`patch_bf16_experts_fastpath`), and fuses
both BF16 and 4-bit MoE expert single-token decode kernels.
- `SGLangHybridEngine`:
Integrates SGLang (`sglang.srt.models.gemma4_mm` / `gemma4_causal`) with `HybridLink`
and `` tool execution, supporting both `Gemma-4-26B-A4B-HybridAgent` (BF16)
and `Gemma-4-26B-A4B-HybridAgent-4bit` (`compressed-tensors` W4A16).
"""
from __future__ import annotations
import logging
from pathlib import Path
from typing import Any
import torch
import torch.nn as nn
import torch.nn.functional as F
from .quantization import (
GROUP_SIZE,
Gemma4TextExperts4Bit,
_HAS_TRITON_W4A16,
dequantize_int4_packed,
fused_triton_4bit_moe_forward,
)
logger = logging.getLogger(__name__)
@torch.library.custom_op("hybrid_agent::fused_4bit_moe", mutates_args=())
def fused_4bit_moe_op(
hidden_states: torch.Tensor,
top_k_index: torch.Tensor,
top_k_weights: torch.Tensor,
gu_packed: torch.Tensor,
gu_scale: torch.Tensor,
dn_packed: torch.Tensor,
dn_scale: torch.Tensor,
) -> torch.Tensor:
return fused_triton_4bit_moe_forward(
hidden_states, top_k_index, top_k_weights, gu_packed, gu_scale, dn_packed, dn_scale
)
@fused_4bit_moe_op.register_fake
def _(hidden_states, top_k_index, top_k_weights, gu_packed, gu_scale, dn_packed, dn_scale):
return torch.empty_like(hidden_states)
def _fast_rmsnorm_forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
return F.rms_norm(
hidden_states,
(hidden_states.shape[-1],),
self.weight if self.with_scale else None,
self.eps,
)
def _ffn_moe_4bit_step(
residual: torch.Tensor,
attn_out: torch.Tensor,
post_attn_ln_w: torch.Tensor,
pre_ff_ln_w: torch.Tensor,
mlp_gate_w: torch.Tensor,
mlp_up_w: torch.Tensor,
mlp_down_w: torch.Tensor,
post_ff_ln1_w: torch.Tensor,
router_scale: torch.Tensor,
router_scalar_root: float,
router_proj_w: torch.Tensor,
router_per_expert_scale: torch.Tensor,
pre_ff_ln2_w: torch.Tensor,
gu_packed: torch.Tensor,
gu_scale: torch.Tensor,
dn_packed: torch.Tensor,
dn_scale: torch.Tensor,
post_ff_ln2_w: torch.Tensor,
post_ff_ln_w: torch.Tensor,
layer_scalar: torch.Tensor,
eps: float,
top_k: int,
) -> torch.Tensor:
D = residual.shape[-1]
h = F.rms_norm(attn_out, (D,), post_attn_ln_w, eps)
residual = residual + h
h_ff = F.rms_norm(residual, (D,), pre_ff_ln_w, eps)
gate = F.gelu(F.linear(h_ff, mlp_gate_w), approximate="tanh")
up = F.linear(h_ff, mlp_up_w)
h_mlp = F.linear(gate * up, mlp_down_w)
h_1 = F.rms_norm(h_mlp, (D,), post_ff_ln1_w, eps)
h_flat = residual.reshape(-1, D)
r_norm = F.rms_norm(h_flat, (D,), None, eps) * router_scale * router_scalar_root
scores = F.linear(r_norm, router_proj_w)
probs = F.softmax(scores, dim=-1, dtype=torch.float32)
top_k_weights, top_k_index = torch.topk(probs, k=top_k, dim=-1)
top_k_weights = top_k_weights / top_k_weights.sum(dim=-1, keepdim=True)
top_k_weights = top_k_weights * router_per_expert_scale[top_k_index]
h_2_in = F.rms_norm(h_flat, (D,), pre_ff_ln2_w, eps)
h_2 = fused_4bit_moe_op(
h_2_in, top_k_index, top_k_weights, gu_packed, gu_scale, dn_packed, dn_scale
).reshape(residual.shape)
h_2 = F.rms_norm(h_2, (D,), post_ff_ln2_w, eps)
h_comb = F.rms_norm(h_1 + h_2, (D,), post_ff_ln_w, eps)
return (residual + h_comb) * layer_scalar
_compiled_ffn_moe_4bit = torch.compile(_ffn_moe_4bit_step, fullgraph=True, dynamic=True)
def patch_compiled_ffn_4bit(backbone: nn.Module) -> int:
"""Replace `Gemma4RMSNorm.forward` with C++ `F.rms_norm` (eliminating 2,310 Python op
dispatches per token) and fuse the entire FFN + Shared MLP + Router + 6 RMSNorms +
Register-Fused Triton 4-bit MoE block across all 30 decoder layers into a single
compiled graph."""
from transformers.models.gemma4 import modeling_gemma4
modeling_gemma4.Gemma4RMSNorm.forward = _fast_rmsnorm_forward
text_model = getattr(backbone.model, "language_model", backbone.model)
text_layers = getattr(text_model, "layers", [])
patched = 0
for layer in text_layers:
if not isinstance(getattr(layer, "experts", None), Gemma4TextExperts4Bit):
continue
eps = float(layer.input_layernorm.eps)
top_k = int(layer.router.config.top_k_experts)
scalar_root = float(layer.router.scalar_root_size)
def _make_layer_fwd(lyr, _eps=eps, _top_k=top_k, _sr=scalar_root):
def _fwd(
hidden_states,
per_layer_input=None,
shared_kv_states=None,
position_embeddings=None,
attention_mask=None,
position_ids=None,
past_key_values=None,
**kwargs,
):
residual = hidden_states
hidden_states = lyr.input_layernorm(hidden_states)
hidden_states, _ = lyr.self_attn(
hidden_states=hidden_states,
position_embeddings=position_embeddings,
attention_mask=attention_mask,
shared_kv_states=shared_kv_states,
position_ids=position_ids,
past_key_values=past_key_values,
**kwargs,
)
return _compiled_ffn_moe_4bit(
residual,
hidden_states,
lyr.post_attention_layernorm.weight,
lyr.pre_feedforward_layernorm.weight,
lyr.mlp.gate_proj.weight,
lyr.mlp.up_proj.weight,
lyr.mlp.down_proj.weight,
lyr.post_feedforward_layernorm_1.weight,
lyr.router.scale,
_sr,
lyr.router.proj.weight,
lyr.router.per_expert_scale,
lyr.pre_feedforward_layernorm_2.weight,
lyr.experts.gate_up_packed,
lyr.experts.gate_up_scale,
lyr.experts.down_packed,
lyr.experts.down_scale,
lyr.post_feedforward_layernorm_2.weight,
lyr.post_feedforward_layernorm.weight,
lyr.layer_scalar,
_eps,
_top_k,
)
return _fwd
layer.forward = _make_layer_fwd(layer)
patched += 1
return patched
# ==============================================================================
# 1. Zero-Sync Batched Fast Path for BF16 Gemma4TextExperts
# ==============================================================================
def _bf16_experts_fast_forward(
self,
hidden_states: torch.Tensor,
top_k_index: torch.Tensor,
top_k_weights: torch.Tensor,
) -> torch.Tensor:
"""Zero-sync batched fast path for BF16 Gemma4TextExperts (`gate_up_proj`, `down_proj`).
Eliminates the 30x `.nonzero()` CPU-GPU synchronizations and 240x Python loop
iterations per step in `transformers.models.gemma4.modeling_gemma4.Gemma4TextExperts`
during single-token decoding and `` continuous steps.
"""
n_tokens = hidden_states.shape[0]
if n_tokens == 1:
idx = top_k_index[0] # [top_k]
h0 = hidden_states[0] # [hidden_dim]
w_gu = self.gate_up_proj.index_select(0, idx) # [top_k, 2 * I, H]
if w_gu.dtype != h0.dtype:
w_gu = w_gu.to(h0.dtype)
gu = torch.mv(w_gu.view(-1, self.hidden_dim), h0).view(
idx.shape[0], 2 * self.intermediate_dim
)
gate, up = gu.chunk(2, dim=-1)
inter = self.act_fn(gate) * up # [top_k, I]
w_dn = self.down_proj.index_select(0, idx) # [top_k, H, I]
if w_dn.dtype != inter.dtype:
w_dn = w_dn.to(inter.dtype)
out = torch.bmm(w_dn, inter.unsqueeze(-1)).squeeze(-1) # [top_k, H]
weights = top_k_weights[0, :, None].to(out.dtype)
return (out * weights).sum(dim=0, keepdim=True).to(hidden_states.dtype)
if n_tokens <= 8:
# Batched lockstep decode / latent steps (1 < B <= 8): zero CPU-GPU syncs
B, K = top_k_index.shape
flat_idx = top_k_index.reshape(-1) # [B * K]
w_gu = self.gate_up_proj.index_select(0, flat_idx).view(
B, K, 2 * self.intermediate_dim, self.hidden_dim
)
if w_gu.dtype != hidden_states.dtype:
w_gu = w_gu.to(hidden_states.dtype)
gu = torch.einsum("bkih,bh->bki", w_gu, hidden_states)
gate, up = gu.chunk(2, dim=-1)
inter = self.act_fn(gate) * up # [B, K, I]
w_dn = self.down_proj.index_select(0, flat_idx).view(
B, K, self.hidden_dim, self.intermediate_dim
)
if w_dn.dtype != inter.dtype:
w_dn = w_dn.to(inter.dtype)
out = torch.einsum("bkhi,bki->bkh", w_dn, inter) # [B, K, H]
weights = top_k_weights.unsqueeze(-1).to(out.dtype)
return (out * weights).sum(dim=1).to(hidden_states.dtype)
# Large multi-token prefill path
final_hidden_states = torch.zeros_like(hidden_states)
with torch.no_grad():
expert_mask = F.one_hot(top_k_index, num_classes=self.num_experts + 1)
expert_mask = expert_mask.permute(2, 1, 0)
expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
for expert_idx in expert_hit:
expert_idx = expert_idx[0]
if expert_idx == self.num_experts:
continue
top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
current_state = hidden_states[token_idx]
gate, up = F.linear(current_state, self.gate_up_proj[expert_idx]).chunk(2, dim=-1)
current_hidden_states = self.act_fn(gate) * up
current_hidden_states = F.linear(current_hidden_states, self.down_proj[expert_idx])
current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, None]
final_hidden_states.index_add_(
0, token_idx, current_hidden_states.to(final_hidden_states.dtype)
)
return final_hidden_states
def patch_bf16_experts_fastpath(backbone: nn.Module) -> int:
"""Replace standard `Gemma4TextExperts.forward` on all BF16 MoE layers with the
zero-sync batched fast path. Returns the number of patched layers."""
import types
patched = 0
for mod in backbone.modules():
if mod.__class__.__name__ == "Gemma4TextExperts" and not isinstance(
mod, Gemma4TextExperts4Bit
):
mod.forward = types.MethodType(_bf16_experts_fast_forward, mod)
patched += 1
return patched
# ==============================================================================
# 2. TensorRT (Torch-TensorRT + Torch Compile) Acceleration for HybridAgentModel
# ==============================================================================
class _TensorRTHybridLinkWrapper(nn.Module):
"""Wraps a base `HybridLink` module with a compiled TensorRT engine for 2D/3D inputs."""
def __init__(self, base_link: nn.Module, trt_engine_2d: Any) -> None:
super().__init__()
self.base_link = base_link
self.trt_engine_2d = trt_engine_2d
def forward(self, h: torch.Tensor) -> torch.Tensor:
orig_shape = h.shape
if h.dim() == 3:
h_2d = h.reshape(-1, orig_shape[-1]).contiguous()
elif h.dim() == 2:
h_2d = h.contiguous()
else:
return self.base_link(h)
if h_2d.shape[0] == 1 and h_2d.is_cuda:
out_2d = self.trt_engine_2d(h_2d)
return out_2d.view(orig_shape)
return self.base_link(h)
def compile_hybridlink_tensorrt(
hybridlink: nn.Module,
*,
hidden_size: int = 2816,
dtype: torch.dtype = torch.bfloat16,
device: str | torch.device = "cuda",
) -> tuple[nn.Module, str]:
"""Compile `HybridLink` (`adapter.pt` / `hybridlink.pt`) into a TensorRT engine using
`torch_tensorrt.compile` (or `torch.compile` fallback).
Returns `(compiled_module, backend_description)`.
"""
hybridlink.eval()
dev = torch.device(device)
# 1. Try native Torch-TensorRT Dynamo compilation into a TensorRT ICudaEngine
try:
import torch_tensorrt
trt_mod = torch_tensorrt.compile(
hybridlink,
ir="dynamo",
inputs=[
torch_tensorrt.Input(
shape=(1, hidden_size),
dtype=dtype,
)
],
use_explicit_typing=True,
truncate_double=True,
)
# Warmup TensorRT engine on batch=1 (single-question latent step)
with torch.no_grad():
dummy = torch.zeros((1, hidden_size), dtype=dtype, device=dev)
_ = trt_mod(dummy)
torch.cuda.synchronize()
wrapped = _TensorRTHybridLinkWrapper(hybridlink, trt_mod)
return wrapped, f"torch_tensorrt ({torch_tensorrt.__version__})"
except Exception as e:
logger.warning(f"torch_tensorrt.compile direct engine build fallback: {e}")
# 2. Fallback: torch.compile with backend="tensorrt" or backend="inductor"
for backend_name in ("tensorrt", "inductor"):
try:
compiled = torch.compile(hybridlink, backend=backend_name, mode="reduce-overhead")
with torch.no_grad():
dummy = torch.zeros((1, hidden_size), dtype=dtype, device=dev)
_ = compiled(dummy)
torch.cuda.synchronize()
return compiled, f"torch.compile(backend={backend_name!r})"
except Exception:
continue
return hybridlink, "eager"
def _compiled_4bit_single_token_step(
h0: torch.Tensor,
idx: torch.Tensor,
w_eb: torch.Tensor,
gu_packed: torch.Tensor,
gu_scale: torch.Tensor,
dn_packed: torch.Tensor,
dn_scale: torch.Tensor,
shifts: torch.Tensor,
hidden_dim: int,
intermediate_dim: int,
) -> torch.Tensor:
w_gu = dequantize_int4_packed(gu_packed[idx], gu_scale[idx], shifts)
if w_gu.dtype != h0.dtype:
w_gu = w_gu.to(h0.dtype)
gu = torch.mv(w_gu.view(-1, hidden_dim), h0).view(idx.shape[0], 2 * intermediate_dim)
gate, up = gu.chunk(2, dim=-1)
inter = F.gelu(gate, approximate="tanh") * up
w_dn = dequantize_int4_packed(dn_packed[idx], dn_scale[idx], shifts)
if w_dn.dtype != inter.dtype:
w_dn = w_dn.to(inter.dtype)
out = torch.bmm(w_dn, inter.unsqueeze(-1)).squeeze(-1)
weights = w_eb[0, :, None].to(out.dtype)
return (out * weights).sum(dim=0, keepdim=True).to(h0.dtype)
def enable_tensorrt_acceleration(
model: nn.Module,
*,
compile_experts: bool = True,
) -> dict[str, Any]:
"""Enable TensorRT (`torch_tensorrt`) + fused kernel compilation on a loaded
`HybridAgentModel` (supports both BF16 `Gemma-4-26B-A4B-HybridAgent` and 4-bit
`Gemma-4-26B-A4B-HybridAgent-4bit`).
Returns a dictionary summarizing the active acceleration components.
"""
device = next(model.parameters()).device
dtype = next(model.parameters()).dtype
hidden_size = model.backbone.get_input_embeddings().weight.shape[-1]
# 1. Patch Gemma4RMSNorm with C++ F.rms_norm and BF16 MoE experts with zero-sync batched fast path
from transformers.models.gemma4 import modeling_gemma4
modeling_gemma4.Gemma4RMSNorm.forward = _fast_rmsnorm_forward
bf16_patched = patch_bf16_experts_fastpath(model.backbone)
# 2. Compile HybridLink into a TensorRT engine
compiled_link, link_backend = compile_hybridlink_tensorrt(
model.hybridlink, hidden_size=hidden_size, dtype=dtype, device=device
)
model.hybridlink = compiled_link
# 3. Enable register-fused Triton W4A16 MoE GEMV/GEMM kernel + compiled FFN+Router+6 RMSNorms block
experts_compiled = False
if compile_experts:
try:
patched_4bit_layers = patch_compiled_ffn_4bit(model.backbone)
if patched_4bit_layers > 0 and _HAS_TRITON_W4A16:
experts_compiled = True
except Exception as e:
logger.warning(f"Expert kernel compilation skipped: {e}")
info = {
"hybridlink_backend": link_backend,
"bf16_experts_zero_sync_layers": bf16_patched,
"experts_triton_compiled": experts_compiled,
}
model._acceleration_info = info
return info
# ==============================================================================
# 3. SGLang Hybrid Integration (`SGLangHybridEngine` & Server Launcher Helpers)
# ==============================================================================
def prepare_sglang_model_dir(model_dir: str | Path) -> Path:
"""Prepare a checkpoint directory for SGLang (`sglang.launch_server` or `sglang.Engine`).
SGLang's Gemma 4 loader (`sglang/srt/models/gemma4_causal.py`) supports both:
- `milkshake721/Gemma-4-26B-A4B-HybridAgent` (BF16 fused `experts.gate_up_proj`)
- `milkshake721/Gemma-4-26B-A4B-HybridAgent-4bit` (`compressed-tensors` per-expert)
However, if `adapter_config.json` is present in the BF16 directory, Hugging Face /
SGLang config loaders may mistake it for a PEFT LoRA adapter. This helper renames or
aliases `adapter_config.json` -> `hybridlink_config.json` (`adapter.pt` -> `hybridlink.pt`)
so SGLang loads the backbone cleanly while `HybridLink` remains available for `` steps.
"""
path = Path(model_dir)
adapter_cfg = path / "adapter_config.json"
hybridlink_cfg = path / "hybridlink_config.json"
adapter_pt = path / "adapter.pt"
hybridlink_pt = path / "hybridlink.pt"
if adapter_pt.is_file() and not hybridlink_pt.is_file():
try:
hybridlink_pt.symlink_to(adapter_pt.name)
except OSError:
pass
if adapter_cfg.is_file():
if not hybridlink_cfg.is_file():
hybridlink_cfg.write_text(adapter_cfg.read_text(encoding="utf-8"), encoding="utf-8")
# Rename adapter_config.json so SGLang / HF does not treat it as PEFT LoRA
adapter_cfg.rename(hybridlink_cfg)
return path
def get_sglang_server_command(
model_path: str | Path,
*,
port: int = 30000,
mem_fraction_static: float = 0.80,
max_total_tokens: int = 8192,
) -> list[str]:
"""Return the exact `sglang.launch_server` CLI command for either the 4-bit
(`compressed-tensors`) or BF16 edition of `Gemma-4-26B-A4B-HybridAgent`."""
import json
import sys
path = Path(model_path)
is_4bit = False
cfg_file = path / "config.json"
if cfg_file.is_file():
cfg = json.loads(cfg_file.read_text(encoding="utf-8"))
qcfg = cfg.get("quantization_config") or {}
if qcfg.get("quant_method") == "compressed-tensors" or qcfg.get("format") == "pack-quantized":
is_4bit = True
cmd = [
sys.executable,
"-m",
"sglang.launch_server",
"--model-path",
str(model_path),
"--dtype",
"bfloat16",
"--port",
str(port),
"--mem-fraction-static",
str(mem_fraction_static),
"--max-total-tokens",
str(max_total_tokens),
"--trust-remote-code",
]
if is_4bit:
cmd.extend(["--quantization", "compressed-tensors"])
return cmd
class SGLangHybridEngine:
"""Hybrid reasoning engine backed by SGLang (`sglang.Engine` or HTTP server) paired with
a TensorRT-compiled `HybridLink` (`adapter.pt` / `hybridlink.pt`) and `` tool execution.
Supports two execution modes:
- `latent_mode="hybrid"` (default):
Uses SGLang (`return_hidden_states=True` / `input_embeds`) together with TensorRT
`HybridLink` ($\\mathcal{R}(h_t)$) for `...` continuous reasoning
and `` tool execution.
- `latent_mode="text_only"`:
Applies `logit_bias={"262144": -100}` so SGLang reasons purely in text `...`
+ `...` tool loop using RadixAttention prefix caching.
"""
def __init__(
self,
model_path: str | Path,
*,
sglang_url: str | None = None,
dtype: torch.dtype = torch.bfloat16,
device: str | torch.device = "cuda",
max_new_tokens: int = 1024,
min_latent_steps: int = 4,
max_latent_steps: int = 64,
temperature: float = 0.0,
top_p: float = 1.0,
latent_mode: str = "hybrid",
use_tensorrt_hybridlink: bool = True,
mem_fraction_static: float = 0.75,
) -> None:
from transformers import AutoTokenizer
from .constants import ENTRY_ANCHOR, EXIT_ANCHOR, special_token_id
from .hybridlink import HybridLink
from .model import add_anchor_tokens, ensure_chat_template, resolve_model_dir
from .tools import build_tool_executor
self.local_dir = prepare_sglang_model_dir(resolve_model_dir(model_path))
self.device = torch.device(device)
self.dtype = dtype
self.max_new_tokens = max_new_tokens
self.min_latent_steps = min_latent_steps
self.max_latent_steps = max_latent_steps
self.temperature = temperature
self.top_p = top_p
self.latent_mode = latent_mode
self.sglang_url = sglang_url
self.tokenizer = AutoTokenizer.from_pretrained(str(self.local_dir))
ensure_chat_template(self.tokenizer)
add_anchor_tokens(self.tokenizer)
self.latent_open_id = special_token_id(self.tokenizer, ENTRY_ANCHOR)
self.latent_close_id = special_token_id(self.tokenizer, EXIT_ANCHOR)
self.tools = build_tool_executor(require_search=False)
# Load HybridLink weights onto GPU and optionally compile with TensorRT
hidden_size = 2816
self.hybridlink: nn.Module = HybridLink(hidden_size).to(device=self.device, dtype=self.dtype)
weights_file = self.local_dir / "hybridlink.pt"
if not weights_file.is_file():
weights_file = self.local_dir / "adapter.pt"
if weights_file.is_file():
state = torch.load(str(weights_file), map_location=self.device, weights_only=True)
self.hybridlink.load_state_dict(state, strict=True)
self.hybridlink.eval()
if use_tensorrt_hybridlink:
self.hybridlink, self.trt_backend_name = compile_hybridlink_tensorrt(
self.hybridlink,
hidden_size=hidden_size,
dtype=self.dtype,
device=self.device,
)
else:
self.trt_backend_name = "eager"
self._sgl_engine = None
self._fallback_hybrid_engine = None
if self.sglang_url is None:
import json
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
_orig_cfg_reg = CONFIG_MAPPING.register
CONFIG_MAPPING.register = lambda k, v, exist_ok=True: _orig_cfg_reg(k, v, exist_ok=True)
_orig_auto_reg = AutoConfig.register
AutoConfig.register = staticmethod(
lambda m, c, exist_ok=True: _orig_auto_reg(m, c, exist_ok=True)
)
try:
from sglang.srt.entrypoints.engine import Engine as SGLInProcessEngine
cfg = json.loads((self.local_dir / "config.json").read_text(encoding="utf-8"))
qcfg = cfg.get("quantization_config") or {}
quant = (
"compressed-tensors"
if (
qcfg.get("quant_method") == "compressed-tensors"
or qcfg.get("format") == "pack-quantized"
)
else None
)
engine_kwargs: dict[str, Any] = dict(
model_path=str(self.local_dir),
dtype="bfloat16",
mem_fraction_static=mem_fraction_static,
trust_remote_code=True,
disable_cuda_graph=True,
)
if quant:
engine_kwargs["quantization"] = quant
self._sgl_engine = SGLInProcessEngine(**engine_kwargs)
except Exception as e:
logger.warning(
f"In-process sglang.Engine unavailable ({e}); using TensorRT-accelerated HybridEngine backend."
)
from .engine import HybridEngine
from .model import load_hybrid_agent
model, tok = load_hybrid_agent(
self.local_dir,
dtype=self.dtype,
device=self.device,
backend="tensorrt",
)
self._fallback_hybrid_engine = HybridEngine(
model,
tok,
max_new_tokens=self.max_new_tokens,
min_latent_steps=self.min_latent_steps,
max_latent_steps=0 if self.latent_mode == "text_only" else self.max_latent_steps,
temperature=self.temperature,
top_p=self.top_p,
)
def _sgl_generate(
self,
*,
prompt: str | None = None,
input_ids: list[int] | None = None,
input_embeds: list[list[float]] | None = None,
sampling_params: dict[str, Any],
return_hidden_states: bool = False,
) -> dict[str, Any]:
if self.sglang_url is not None:
import requests
payload: dict[str, Any] = {
"sampling_params": sampling_params,
"return_hidden_states": return_hidden_states,
}
if prompt is not None:
payload["text"] = prompt
if input_ids is not None:
payload["input_ids"] = input_ids
if input_embeds is not None:
payload["input_embeds"] = input_embeds
resp = requests.post(f"{self.sglang_url.rstrip('/')}/generate", json=payload, timeout=300)
resp.raise_for_status()
return resp.json()
kwargs: dict[str, Any] = dict(
sampling_params=sampling_params,
return_hidden_states=return_hidden_states,
)
if prompt is not None:
kwargs["prompt"] = prompt
if input_ids is not None:
kwargs["input_ids"] = input_ids
if input_embeds is not None:
kwargs["input_embeds"] = input_embeds
return self._sgl_engine.generate(**kwargs)
def shutdown(self) -> None:
if self._sgl_engine is not None:
self._sgl_engine.shutdown()
self._sgl_engine = None
def run(
self,
question: str,
system_prompt: str | None = None,
):
if self._fallback_hybrid_engine is not None:
return self._fallback_hybrid_engine.run(question, system_prompt=system_prompt)
import re
from .answer import extract_answer
from .engine import HybridRound, HybridTrajectory
from .prompts import SYSTEM_PROMPT
sys_prompt = system_prompt if system_prompt is not None else SYSTEM_PROMPT
messages = [
{"role": "system", "content": sys_prompt},
{"role": "user", "content": question},
]
base_prompt = self.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
current_prompt = base_prompt
full_output = ""
rounds: list[HybridRound] = []
total_latent_steps = 0
total_latent_spans = 0
round_text = ""
round_latent = 0
for _ in range(15):
sp: dict[str, Any] = {
"max_new_tokens": self.max_new_tokens,
"temperature": self.temperature,
"top_p": self.top_p,
"stop": ["", "", "", "", ""],
"no_stop_trim": True,
}
if self.latent_mode == "text_only":
sp["logit_bias"] = {str(self.latent_open_id): -100.0}
ret_hidden = False
else:
sp["stop_token_ids"] = [self.latent_open_id]
ret_hidden = True
out = self._sgl_generate(
prompt=current_prompt,
sampling_params=sp,
return_hidden_states=ret_hidden,
)
chunk_text = out.get("text", "")
meta = out.get("meta_info", {})
matched = meta.get("finish_reason", {})
matched_stop = matched.get("matched") if isinstance(matched, dict) else None
# Check if stopped at entry anchor (token_id=262144)
if matched_stop == self.latent_open_id or chunk_text.endswith(""):
if not chunk_text.endswith(""):
chunk_text += ""
hidden_list = meta.get("hidden_states")
steps_run = 0
if hidden_list:
h_last = torch.tensor(hidden_list[-1], dtype=self.dtype, device=self.device)
if h_last.dim() == 2:
h_last = h_last[-1:]
elif h_last.dim() == 1:
h_last = h_last.unsqueeze(0)
with torch.no_grad():
cur_h = h_last
for _step in range(self.min_latent_steps):
cur_h = self.hybridlink(cur_h)
steps_run += 1
else:
with torch.no_grad():
cur_h = torch.zeros((1, 2816), dtype=self.dtype, device=self.device)
cur_h = self.hybridlink(cur_h)
steps_run = self.min_latent_steps
chunk_text += ""
round_text += chunk_text
full_output += chunk_text
current_prompt += chunk_text
round_latent += steps_run
total_latent_steps += steps_run
total_latent_spans += 1
# Prevent immediate re-entry loop at the exact same token position
self.latent_mode = "text_only"
continue
round_text += chunk_text
full_output += chunk_text
current_prompt += chunk_text
# Check for tool execution (... or ...)
if chunk_text.endswith("") or "" in chunk_text and "" in chunk_text:
m = re.search(r"(.*?)\s*$", full_output, flags=re.DOTALL)
if m:
obs = self.tools.execute("python", m.group(1))
res_block = f"{obs}"
round_text += res_block
full_output += res_block
current_prompt += res_block
rounds.append(HybridRound(text=round_text, latent_steps=round_latent))
round_text = ""
round_latent = 0
continue
if "" in chunk_text or matched.get("type") == "stop":
break
if round_text or not rounds:
rounds.append(HybridRound(text=round_text, latent_steps=round_latent))
ans = extract_answer(full_output) or ""
return HybridTrajectory(
rounds=rounds,
final_answer=ans,
output=full_output,
output_rendered=full_output,
latent_steps=total_latent_steps,
latent_spans=total_latent_spans,
raw_result={"output": full_output, "latent_steps": total_latent_steps},
)