Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
from __future__ import annotations
from typing_extensions import override
import math
import os
import torch
import torch.nn.functional as F
from .module import Module
from .linear import Linear
from .rmsnorm import RMSNorm
from .ngram_embedding import NGramEmbedding, WARM_TOKENS
from ..ext import exllamav3_ext as ext
from ..tokenizer.mm_embedding import FIRST_MM_EMBEDDING_INDEX
from ..model.config import Config
from ..util.tensor import get_for_device
"""
PLE (per-layer embedding) injection layer (Qwen3.8-Flash-Next): feeds hashed n-gram features into
every residual stream of the hyper-connection stack.
The n-gram embedding (2560) projects to one key per stream (10240) and one shared value (2560).
Each stream's normed activation gates the value through a signed-sqrt dot-product gate, and a
depthwise DILATED causal conv (kernel ple_conv_kernel_size, dilation ngram_size) over the normed
gated values adds local lexical context. The layer's output is added to the raw stream stack by
the caller: streams <- streams + ple(streams, token_history).
Recurrent state per sequence: the (kernel-1)*dilation trailing positions of the normed gated
values (conv_state), plus the ngram_size-1 previous token ids (carried in token_history by the
caller). Norms are grouped RMS (rms per 2560-stream, zero-init weight applied as 1 + w).
"""
def _ple_gate_torch(gate, value, gate_scale):
scaled = gate.float() * gate_scale
signed_sqrt = torch.sign(scaled) * torch.sqrt(torch.clamp(scaled.abs(), min = 1e-6))
return torch.sigmoid(signed_sqrt).unsqueeze(-1) * value.float().unsqueeze(-2)
try:
import triton
import triton.language as tl
@triton.jit
def _ple_tadd_kernel(g_ptr, y_ptr, T, C, y_sb, y_sc, BT: tl.constexpr, BC: tl.constexpr):
b = tl.program_id(2)
t = tl.program_id(0) * BT + tl.arange(0, BT)
c = tl.program_id(1) * BC + tl.arange(0, BC)
yv = tl.load(y_ptr + b.to(tl.int64) * y_sb + c[:, None] * y_sc + t[None, :], mask=(c[:, None] < C) & (t[None, :] < T), other=0.0)
yv = tl.trans(yv)
gp = g_ptr + b.to(tl.int64) * T * C + t[:, None] * C + c[None, :]
m = (t[:, None] < T) & (c[None, :] < C)
tl.store(gp, tl.load(gp, mask=m, other=0.0) + yv.to(tl.float32), mask=m)
except Exception: # no Triton: the transpose-copy fallback below
_ple_tadd_kernel = None
def _ple_tadd(gated: torch.Tensor, conv_out: torch.Tensor) -> bool:
"""gated (bsz, seq, H, D) fp32, contiguous, += conv_out (bsz, seq, ch = H * D) fp16, the transposed view of a (bsz, ch, seq) tensor: one Triton pass,
tiles transposed through shared memory. False when the layout / dtype / Triton is not the expected one (the caller adds the old way)."""
if _ple_tadd_kernel is None or not torch.version.hip or gated.dtype != torch.float or not gated.is_contiguous() \
or conv_out.dtype not in (torch.half, torch.bfloat16, torch.float) or conv_out.stride(1) != 1:
return False
bsz, seq, ch = conv_out.shape
if gated.numel() != bsz * seq * ch:
return False
BT = BC = 64
_ple_tadd_kernel[((seq + BT - 1) // BT, (ch + BC - 1) // BC, bsz)](gated, conv_out, seq, ch, conv_out.stride(0), conv_out.stride(2),
BT=BT, BC=BC, num_warps=4)
return True
class PLELayerState:
"""
Per-slot recurrent state for one PLE layer, following the ShortConvLayerState conventions:
the live state occupies the leading window columns, history forwards (speculative decoding)
write right-aligned trailing columns, and rewind restores the window from them. Two tensors:
the dilated-conv state (trailing normed gated values) and the carried token-id context for
the n-gram hashing (eos-filled at sequence start).
"""
def __init__(
self,
module: PLELayer,
max_batch_size: int,
max_history: int,
cache_id: int,
):
self.module = module
self.win = module.conv_state_len
self.ctx = module.ple_embedding.context_len
hc_hidden = module.hc_mult * module.hidden_size
self.conv_state = torch.empty(
(max_batch_size, hc_hidden, self.win + max_history),
dtype = torch.half,
device = "meta",
)
self.id_state = torch.empty(
(max_batch_size, self.ctx + max_history),
dtype = torch.long,
device = "meta",
)
self.device = None
self.max_history = max_history
self.max_batch_size = max_batch_size
self.cache_id = cache_id
def get_checkpoint_size(self):
return self.conv_state.shape[1] * self.win * 2 + self.ctx * 8
def storage_size(self):
return sum(t.numel() * t.element_size() for t in (self.conv_state, self.id_state))
def alloc(self, device):
self.conv_state = torch.zeros_like(self.conv_state, device = device)
# The carried token-id context stays on the CPU: the n-gram hashing runs host-side on
# the (pinned) input ids, so the id history must never round-trip through the device
self.id_state = torch.full_like(self.id_state, self.module.ple_embedding.eos_token_id,
device = "cpu")
self.device = device
def free(self):
self.conv_state = torch.empty_like(self.conv_state, device = "meta")
self.id_state = torch.empty_like(self.id_state, device = "meta")
self.device = None
def clear(self, idx: int):
if self.device is not None:
self.conv_state[idx].zero_()
self.id_state[idx].fill_(self.module.ple_embedding.eos_token_id)
def get_state_tensors(self):
return self.conv_state, self.id_state
def rewind(self, slot: int, last_history: int, num_tokens: int):
assert num_tokens <= last_history
if last_history > 0:
p = self.conv_state.shape[-1] - num_tokens
temp = self.conv_state[slot, :, p - self.win : p].clone()
self.conv_state[slot, :, :self.win].copy_(temp)
p = self.id_state.shape[-1] - num_tokens
temp = self.id_state[slot, p - self.ctx : p].clone()
self.id_state[slot, :self.ctx].copy_(temp)
def stash(self, slot, position: int = 0):
# id_state lives on the CPU: .cpu() would return a view of the live slot, so a stashed checkpoint
# changes whenever the slot advances; clone it
return (self.conv_state[slot, :, :self.win].cpu(), self.id_state[slot, :self.ctx].cpu().clone())
def unstash(self, slot, stashed, position: int = 0):
self.conv_state[slot, :, :self.win].copy_(stashed[0])
self.id_state[slot, :self.ctx].copy_(stashed[1])
def tp_export(self, plan):
return {
"cls": PLELayerState,
"args": {
"cache_id": self.cache_id,
"max_history": self.max_history,
"max_batch_size": self.max_batch_size,
}
}
class PLELayer(Module):
def __init__(
self,
config: Config | None,
key: str,
layer_idx: int,
hidden_size: int,
hc_mult: int,
ple_embed_dim: int,
ngram_size: int,
heads_per_ngram: int,
eos_token_id: int,
conv_kernel_size: int,
rms_norm_eps: float,
qmap: str | None = None,
stream_from_disk: bool | None = None,
out_dtype: torch.dtype | None = None,
mm_token_id: int | None = None,
):
super().__init__(config = config, key = key, qmap = None)
self.hidden_size = hidden_size
self.hc_mult = hc_mult
self.mm_token_id = mm_token_id
self.conv_kernel_size = conv_kernel_size
self.conv_dilation = ngram_size
self.conv_state_len = (conv_kernel_size - 1) * self.conv_dilation
self.gate_scale = 1.0 / math.sqrt(hidden_size)
self.out_dtype = out_dtype
hc_hidden = hc_mult * hidden_size
self.ple_embedding = NGramEmbedding(
config = config,
key = f"{key}.ple_embedding.ngram_embedding",
ngram_size = ngram_size,
heads_per_ngram = heads_per_ngram,
ple_embed_dim = ple_embed_dim,
eos_token_id = eos_token_id,
stream_from_disk = stream_from_disk,
)
self.key_proj = Linear(
config = config,
key = f"{key}.key_proj",
in_features = ple_embed_dim,
out_features = hc_hidden,
qmap = qmap,
out_dtype = torch.half,
)
self.value_proj = Linear(
config = config,
key = f"{key}.value_proj",
in_features = ple_embed_dim,
out_features = hidden_size,
qmap = qmap,
out_dtype = torch.half,
)
# Grouped RMS norms over the stream stack (weight is hc_mult rows of hidden channels,
# zero-init, applied as 1 + w)
def norm(name):
return RMSNorm(config, f"{key}.{name}", rms_norm_eps, constant_bias = 1.0,
groups = hc_mult)
self.norm_key = norm("norm_key")
self.norm_query = norm("norm_query")
self.norm_conv = norm("norm_conv")
self.register_submodule(self.ple_embedding)
self.register_submodule(self.key_proj)
self.register_submodule(self.value_proj)
self.register_submodule(self.norm_key)
self.register_submodule(self.norm_query)
self.register_submodule(self.norm_conv)
# Recurrent state registration: negative layer_idx keeps the state key distinct from the
# decoder block that shares this layer index in the cache's recurrent-layer map
assert layer_idx < 0
self.layer_idx = layer_idx
self.caps.update({"recurrent_cache": True, "prefetch_ids": True})
self.layer_state_cls = PLELayerState
self.recurrent_layers = []
self.tp_recurrent_lookup = {}
self.conv_w = None
@override
def load(self, device: torch.device, **kwargs):
super().load(device, **kwargs)
for rl in self.recurrent_layers:
rl.alloc(device)
self.conv_w = self.config.stc.get_tensor(f"{self.key}.conv1d.weight", device,
float2half = True, no_defer = True).contiguous()
@override
def unload(self):
for rl in self.recurrent_layers:
rl.free()
super().unload()
self.conv_w = None
@override
def get_tensors(self):
return {
f"{self.key}.conv1d.weight": self.conv_w.contiguous(),
}
@override
def weights_numel(self):
# Everything this module contributes to the converted shards: the (fp16) projections,
# the three grouped norm weights and the conv kernel — but NOT the n-gram embedding,
# whose table lives out-of-line in ngram_embedding.safetensors and accounts for itself.
# Shape-derived, since conversion reads the count after unload()
hc_hidden = self.hc_mult * self.hidden_size
return self.key_proj.weights_numel() + self.value_proj.weights_numel() \
+ 3 * hc_hidden + hc_hidden * self.conv_kernel_size
@override
def optimizer_targets(self):
return self.key_proj.optimizer_targets() + self.value_proj.optimizer_targets()
def _short_conv(self, x: torch.Tensor, conv_state: torch.Tensor | None):
"""
x: (bsz, seq, hc_hidden) normed gated values. conv_state: (bsz, hc_hidden,
conv_state_len) trailing positions from the previous chunk, or None at sequence start.
Returns (silu(conv(x)) (bsz, seq, hc_hidden), the full padded column stream
(bsz, hc_hidden, conv_state_len + seq) — its trailing conv_state_len columns are the
next chunk's conv state; the full stream supports history writes for rewind).
"""
bsz, seq, ch = x.shape
xt = x.transpose(1, 2) # (bsz, ch, seq)
if conv_state is None:
conv_state = xt.new_zeros((bsz, ch, self.conv_state_len))
xt = torch.cat((conv_state.to(xt.dtype), xt), dim = -1) # (bsz, ch, state + seq)
y = F.conv1d(xt, self.conv_w, groups = ch, dilation = self.conv_dilation)
return F.silu(y).transpose(1, 2), xt
def forward_streams(
self,
streams: torch.Tensor,
token_history: torch.Tensor,
params: dict,
conv_state: torch.Tensor | None = None,
):
"""
streams: (bsz, seq, hc_mult, hidden) fp32 residual stack;
token_history: (bsz, (ngram_size - 1) + seq) token ids (eos-padded at sequence start).
Returns (delta (bsz, seq, hc_mult, hidden) fp32 to add to the streams, full conv column
stream — see _short_conv). The layer sits at the front of the forward pass and was
host-bound issued op-by-op from python; the whole sequence runs as one ext call
(ple_forward_streams), with the op-by-op form kept below as the reference.
"""
bsz, seq = streams.shape[:2]
H, D = self.hc_mult, self.hidden_size
emb = self.ple_embedding.forward(token_history, params) # (bsz, seq, ple_dim)
if hasattr(ext, "ple_forward_streams") \
and self.key_proj.quant_type == "fp16" and self.value_proj.quant_type == "fp16" \
and streams.dtype == torch.float and streams.is_contiguous() \
and self.key_proj.inner.bias is None and self.value_proj.inner.bias is None:
delta = torch.empty_like(streams)
conv_stream = torch.empty((bsz, H * D, self.conv_state_len + seq),
dtype = torch.half, device = streams.device)
ext.ple_forward_streams(
streams, emb.contiguous(),
self.key_proj.inner.weight, self.value_proj.inner.weight,
self.norm_key.weight.data, self.norm_query.weight.data,
self.norm_conv.weight.data, self.conv_w,
conv_state.contiguous() if conv_state is not None else None,
self.norm_key.rms_norm_eps, self.gate_scale, self.conv_dilation,
delta, conv_stream,
)
return delta, conv_stream
return self.forward_streams_reference(streams, emb, params, conv_state)
def _streams_chain_hip(self, streams, key, value, conv_state):
"""ple_fn/ple_hip.hip chain; None when the shapes/dtypes are not the proved ones (caller keeps the torch chain)."""
try:
from .ple_fn import ple_hip
if not (torch.version.hip and self.norm_key.rms_norm_eps == self.norm_query.rms_norm_eps == self.norm_conv.rms_norm_eps
and self.norm_key.constant_bias == 1.0 and self.norm_key.constant_scale == 1.0
and self.norm_query.constant_bias == 1.0 and self.norm_query.constant_scale == 1.0
and self.norm_conv.constant_bias == 1.0 and self.norm_conv.constant_scale == 1.0
and self.conv_state_len == ple_hip.ST and self.conv_dilation == 3 and self.conv_kernel_size == 4):
return None
bsz, seq = streams.shape[:2]
key = key.reshape(bsz * seq, -1)
value = value.reshape(bsz * seq, -1)
if conv_state is not None:
conv_state = conv_state.contiguous()
wts = (self.norm_key.weight.data, self.norm_query.weight.data, self.norm_conv.weight.data)
if not ple_hip.supported(key, value, streams, conv_state, wts, self.conv_w, 1.0, self.hc_mult, self.hidden_size):
return None
return ple_hip.ple_chain(key, value, streams, conv_state, *wts, self.conv_w,
self.norm_key.rms_norm_eps, self.gate_scale)
except Exception:
return None
def forward_streams_reference(self, streams, emb, params, conv_state = None):
"""Op-by-op form of forward_streams (torch + individual ext kernels)."""
bsz, seq = streams.shape[:2]
H, D = self.hc_mult, self.hidden_size
key = self.key_proj.forward(emb, params).view(bsz, seq, H, D)
value = self.value_proj.forward(emb, params) # (bsz, seq, hidden) fp16
if os.environ.get("EXL3_PLE_HIP", "0") == "1":
# hand HIP kernels for everything after the projections (bitwise equal to the torch chain below)
out = self._streams_chain_hip(streams, key, value, conv_state)
if out is not None:
return out
key = self.norm_key.forward(key, params, out_dtype = torch.float)
query = self.norm_query.forward(streams, params, out_dtype = torch.float)
# per-stream key/query dots as a batched (1, D) x (D, 1) matmul, then the fused gate
# kernel: gated = sigmoid(signed_sqrt(dot * scale)) * value broadcast over streams
gate = torch.bmm(query.view(-1, 1, D), key.reshape(-1, D, 1)).view(bsz, seq, H)
ple_gate = getattr(ext, "ple_gate", None)
if ple_gate is not None:
gated = torch.empty((bsz, seq, H, D), dtype = torch.float, device = value.device)
ple_gate(gate, value, gated, self.gate_scale)
else:
gated = _ple_gate_torch(gate, value, self.gate_scale)
normed = self.norm_conv.forward(gated, params, out_dtype = torch.half).flatten(-2)
conv_out, conv_stream = self._short_conv(normed, conv_state)
if os.environ.get("EXL3_PFE_PLE", "1") != "0":
# gated is a local temp that is dead after the conv: add in place, no 84 MB fresh allocation
# (same fp32 elementwise sum, bit-identical)
co = conv_out.view(bsz, seq, H, D)
if os.environ.get("EXL3_PLE_CONTIG", "1" if torch.version.hip else "0") == "1" and not co.is_contiguous() and \
(not torch.version.hip or "gfx1151" in torch.cuda.get_device_properties(gated.device).gcnArchName):
# EXL3_PLE_CONTIG=1 (default on with ROCm, gfx1151 only): conv_out is the transposed view of the (bsz, ch, seq) conv result, so
# the in-place add below reads it with a ch-sized stride (uncoalesced, a strided broadcast kernel: ~100 ms per 8K chunk). A
# tiled transposed add (Triton, ple_tadd) does it in one pass at bandwidth; where Triton or the layout does not fit, one
# transpose copy first. Same fp32 sum (fp16 widened, added), bit-identical. 0 = the strided broadcast add.
try:
done = _ple_tadd(gated, conv_out) if conv_out.dim() == 3 else False
except Exception:
done = False
if not done:
delta = gated.add_(co.contiguous())
else:
delta = gated
else:
delta = gated.add_(co)
else:
delta = gated + conv_out.view(bsz, seq, H, D)
return delta, conv_stream
def _prepare_ids(self, ids: torch.Tensor) -> torch.Tensor:
# the hashing runs host-side; in the hot path the generator's ids are already CPU
ids = ids.to("cpu", torch.int64)
if self.mm_token_id is not None and int(ids.max()) >= FIRST_MM_EMBEDDING_INDEX:
# multimodal spans carry embedding alias ids; the reference hashes the literal
# placeholder token there, so substitute it (the carried id context then matches
# what HF's would hold as well)
ids = ids.clone()
ids[ids >= FIRST_MM_EMBEDDING_INDEX] = self.mm_token_id
return ids
def _history(self, ids: torch.Tensor) -> torch.Tensor:
"""(bsz, ctx + seq) hashing history at the start of a sequence: eos-padded context."""
pad = ids.new_full((ids.shape[0], self.ple_embedding.context_len), self.ple_embedding.eos_token_id)
return torch.cat((pad, ids), dim = 1)
def _state_history(self, ids: torch.Tensor, params: dict):
"""History for a forward with recurrent states in params: the carried context comes from
the state slots. Returns (history, layer state, slots), or (history, None, None) for a
stateless forward at position 0."""
rsg = params.get("recurrent_states")
if rsg:
layer_instance = (self.layer_idx, params.get("layer_instance", 0))
rsl = rsg[0].cache.get_recurrent_layer(layer_instance)
_, id_state = rsl.get_state_tensors()
slots = get_for_device(params, "recurrent_slots", "cpu").tolist()
assert len(slots) == ids.shape[0]
ctx = self.ple_embedding.context_len
prev_ids = torch.stack([id_state[s, :ctx] for s in slots])
return torch.cat((prev_ids, ids), dim = 1), rsl, slots
assert params.get("position", 0) == 0, \
"PLELayer requires recurrent states for forwards past position 0"
return self._history(ids), None, None
def prefetch(self, input_ids: torch.Tensor, params: dict):
"""
Called by the model before its first layers are issued: stage the n-gram rows for the
coming forward over input_ids on a worker thread (NGramEmbedding.prefetch), with the
carried id context read from the recurrent state slots the forward will use. The gather
then overlaps block 0. A history that doesn't match what forward() then builds is simply
not used, so this can only affect timing, never results.
"""
history, _, _ = self._state_history(self._prepare_ids(input_ids), params)
self.ple_embedding.prefetch(history)
ahead = params.get("pf_ahead")
if ahead is not None and WARM_TOKENS > 0 and self.ple_embedding.handles is not None:
self._warm_ahead(*ahead)
def _warm_ahead(self, seq, end: int):
"""Warm the page cache for the rows of the next WARM_TOKENS prompt positions after this chunk
(seq.sequence_ids, chunk ends at `end`), in windows of at most one 4096-row chunk; every
position is warmed once per sequence (high-water mark on seq)."""
total = len(seq.sequence_ids) - 1
lo = max(end, getattr(seq, "_ngram_warm_to", 0))
hi = min(total, end + WARM_TOKENS)
ctx = self.ple_embedding.context_len
while lo < hi and lo >= ctx:
nxt = min(hi, lo + 4096)
ids = self._prepare_ids(seq.sequence_ids.torch_slice(lo - ctx, nxt))
self.ple_embedding.warm(ids)
lo = nxt
seq._ngram_warm_to = max(lo, getattr(seq, "_ngram_warm_to", 0))
@override
def forward(self, x: torch.Tensor, params: dict, out_dtype: torch.dtype | None = None):
"""
Standalone-module form: x is the (bsz, seq, hc_mult, hidden) stream stack; token ids come
from params["input_ids"] (put there by the Embedding module). With recurrent states in
params, the carried token context and conv state make forwards incremental (same
conventions as ShortConv: state window in [:, ..., :width], history writes right-aligned
for rewind).
"""
pending = params.pop("hc_pending", None) # EXL3_PF_GR_FUSE / EXL3_PF_HC_FUSE: apply the deferred residual first
if pending is not None:
from .hyperconnections import HyperConnection
HyperConnection.flush_pending(x, pending)
bsz, seq = x.shape[:2]
ids = params.get("input_ids")
if ids is None:
# autosplit measuring forward: no token ids in params; a dummy history gives the
# same memory profile (the real n-gram gather is a few hundred rows at most)
ids = torch.zeros((bsz, seq), dtype = torch.long)
ids = self._prepare_ids(ids)
ctx = self.ple_embedding.context_len
win = self.conv_state_len
save_history = params.get("recurrent_history", False)
history, rsl, slots = self._state_history(ids, params)
if rsl is not None:
conv_state, id_state = rsl.get_state_tensors()
cs = torch.stack([conv_state[s, :, :win] for s in slots]).to(x.device)
delta, conv_stream = self.forward_streams(x, history, params, conv_state = cs)
for i, s in enumerate(slots):
if save_history:
w = min(conv_state.shape[-1], conv_stream.shape[-1])
conv_state[s, :, -w:].copy_(conv_stream[i, :, -w:])
wi = min(id_state.shape[-1], history.shape[-1])
id_state[s, -wi:].copy_(history[i, -wi:])
else:
conv_state[s, :, :win].copy_(conv_stream[i, :, -win:])
id_state[s, :ctx].copy_(history[i, -ctx:])
# EXL3_PF_NO_TAIL: the state after row r of this chunk (what stash() would hold if the forward had ended there) =
# conv columns [r, r + win) of the full conv stream and ids [r, r + ctx) of the history; the job stashes them
# (Job.stash_midchunk, 3-tuple form: pinned host copy + event)
rows = params.get("checkpoint_rows")
if rows and params.get("midchunk_mode") == 2 and not save_history and bsz == 1 and len(slots) == 1:
saved = params.setdefault("midchunk_states", {})
layer_instance = (self.layer_idx, params.get("layer_instance", 0))
for r in rows:
if not 0 < r < seq:
continue
hs = torch.empty((conv_stream.shape[1], win), dtype = conv_stream.dtype, pin_memory = True)
hs.copy_(conv_stream[0, :, r:r + win], non_blocking = True)
ev = torch.cuda.Event()
ev.record()
saved.setdefault(r, {})[layer_instance] = (hs, history[0, r:r + ctx].clone(), ev)
else:
delta, _ = self.forward_streams(x, history, params)
if os.environ.get("EXL3_PFE_PLE", "1") != "0":
# delta is a fresh tensor owned here; x stays untouched (conversion-safe). fp32 add commutes exactly
return delta.add_(x)
return x + delta