Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0a6a0c5
perf(rollout): TP2 decode optimizations — GLU combo, custom-AR handsh…
delock Nov 2, 2026
4bcc86b
fix: CI compliance — arch guards, hook literals, mock-safe guard, scr…
delock Nov 2, 2026
9e7e8dd
fix: CI compliance — remove torch.distributed import, fix ignore-cuda…
delock Nov 2, 2026
9cc11f5
feat(segki): arena/reshard handshake — segki_refresh drops derived-we…
delock Nov 3, 2026
8b811a0
fix: import RolloutRequest in the arena tests
delock Nov 3, 2026
4e384a2
test: ZeRO-3 trainer -> arena-sync -> segKI rollout end-to-end
delock Nov 3, 2026
8111716
docs: mark the refresh_weights contract provisional pending final han…
delock Nov 3, 2026
bd736d3
fix: gate arena tests' graph capture on real accelerators; CI compliance
delock Nov 3, 2026
e3a4652
fix: route the arena e2e script through get_accelerator() instead of …
delock Nov 3, 2026
5f44d61
chore: drop dead accelerator graph-capture probes; use DeepSpeed lice…
delock Nov 3, 2026
1152551
refactor(comm): adopt the custom_collective from #8997, drop the loca…
delock Nov 3, 2026
cef3ec9
test: reference anchors for the post-original glue kernels
delock Nov 3, 2026
47d426d
refactor: drop the compile integration — segKI is hand-written kernels
delock Nov 3, 2026
d154866
revert: restore the TP dataloader consistency check in engine.py
delock Nov 4, 2026
1c7717e
Merge remote-tracking branch 'origin/master' into gma/qwen_ki_expr
delock Nov 4, 2026
38ee22c
refactor(interface): all Python call sites route through the portable…
delock Nov 4, 2026
7a44923
test: route kernel-reference tests through grouped_linear + fix asser…
delock Nov 4, 2026
1d3948e
refactor(reference): kernel_reference aligned to the 7 interface methods
delock Nov 4, 2026
e356342
fix: reference test alignment — all call sites through grouped_linear…
delock Nov 5, 2026
fb0351e
chore: drop accidentally committed pre-commit cache dirs
delock Nov 5, 2026
9c24dba
fix(kernels): decode_attn honors the q row stride
delock Nov 5, 2026
5712d44
fix(segment_ki): pack FLA recurrent bypass inputs; gate by parity probe
delock Nov 5, 2026
169577e
fix(cache): bridge the HF >=5.19 GDN cache protocol on the static cache
delock Nov 6, 2026
7b3ea17
sync(custom_collective): align with the validated #8997 implementation
delock Nov 6, 2026
a4edcac
fix(segment_ki): correct GDN TP layout — slice z/b/a per rank, valida…
delock Nov 6, 2026
2d834e9
merge: adopt master's _token_selector (#8939), drop _sample_from_logits
delock Nov 8, 2026
a9a6ac1
fix: merge fallout — drop duplicate get_query_offset, unused import
delock Nov 8, 2026
4fd31d6
fix(rollout): sample the prefill first token and the b>1 decode steps
delock Nov 8, 2026
39210bf
fix(cache): restore the transformers>=5.20 seq-length protocol on Dee…
delock Nov 9, 2026
300e5b8
test(cache): align seq-length expectations with the restored +1 protocol
delock Nov 9, 2026
43b624f
fix(rollout): route graph capture through the accelerator-gated variable
delock Nov 9, 2026
5b70a0b
fix(rollout): define the accelerator-gated use_graph_capture; drop is…
delock Nov 9, 2026
16b6fc6
fix(rollout): adopt master's _continuous_next_tokens with token selector
delock Nov 9, 2026
30ce8d7
fix: drop leftover @staticmethod on _continuous_next_tokens
delock Nov 9, 2026
d834039
fix: add module-level logger; defensive num_attention_heads access
delock Nov 9, 2026
e53e2dc
fix: _continuous_prefill_cache sampling param; drop greedy-only restr…
delock Nov 9, 2026
6d273de
fix: add sampling param to _continuous_prefill; update call sites
delock Nov 9, 2026
eff04b3
fix(rollout): restore master's graph-capture gates lost in the merge
delock Nov 9, 2026
e2ad943
Merge branch 'master' into gma/qwen_ki_expr
delock Nov 9, 2026
2553e95
Merge branch 'master' into gma/qwen_ki_expr
delock Nov 10, 2026
aed2898
Merge branch 'master' into gma/qwen_ki_expr
delock Nov 11, 2026
2d8f409
fix(comm): restrict the CPU shm inference all-reduce to world-wide gr…
delock Nov 11, 2026
00353b7
Merge remote-tracking branch 'origin/master' into gma/qwen_ki_expr
delock Nov 13, 2026
d43f5a2
test: keep GDN in_proj z/b/a replicated in the arena e2e specs
delock Nov 13, 2026
9b4b720
fix(rollout): drop fossil do_sample kwarg duplicated by _hf_sampling_…
delock Nov 13, 2026
f0001ab
Merge master: adopt the #8997 evolution of the custom-AR files
delock Nov 15, 2026
8b05554
perf(rollout): amortize the b>1 EOS check like the b=1 path
delock Nov 15, 2026
c77736d
fix(rollout): MRoPE position shape + visible fallback when installs d…
delock Nov 15, 2026
90175e9
fix(rollout): check EOS every step in the b>1 decode loop
delock Nov 15, 2026
b5298ed
perf(module_inject): bf16-native silu epilogue in the GLU reference
delock Nov 16, 2026
2603bd3
Merge master: adopt #8786's adaptive prefill cost model
delock Nov 16, 2026
de059c1
fix(rollout): keep use_segki out of the legacy positional config slots
delock Nov 16, 2026
7423b14
test: drop the scratch-repo sys.path insert from the arena e2e script
delock Nov 17, 2026
bd69fc7
fix(rollout): repair refresh_weights and wire it into sync_weights
delock Nov 17, 2026
9bf48c8
fix(rollout): release the continuous graph across weight syncs (inter…
delock Nov 17, 2026
01257b7
Merge branch 'master' into gma/qwen_ki_expr
delock Nov 17, 2026
ed86967
fix(refresh): reach the backend cat cache on GLU-only (dense) models
delock Nov 18, 2026
b4a571e
docs: segKI design guide (draft)
delock Nov 18, 2026
d5eeb87
docs: segKI design guide — boundary, refresh_weights, lineage, extens…
erkre Nov 18, 2026
e26cc13
docs: move SEGKI design guide next to its implementation
delock Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,861 changes: 1,861 additions & 0 deletions csrc/module_inject/fused_glu.cu

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions deepspeed/module_inject/kernel_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
"""Reference implementations (executable specifications) for segment-KI kernels.

Each function here defines the mathematically correct output for its
corresponding interface method (the m.def entries on the loaded extension).
Custom kernels (CUDA today, SYCL/XPU in the future) must produce results
equivalent to these functions within bf16 tolerance. Three roles in one:

1. **Specification**: an agent porting kernels to a new backend reads these
to understand what each interface method must compute.
2. **Runtime fallback**: the forward paths in segment_ki call these when no
custom kernel is available (e.g. CPU-only, XPU without compiled kernels).
3. **Test oracle**: unit tests compare custom kernel output against these
functions on identical inputs.

All implementations use only standard PyTorch ops — no custom kernels — so
they are correct by construction and run on any device.
"""

import math

import torch
import torch.nn.functional as F


def grouped_linear(x, weights, glu_epilogue=False):
"""Unified projection: matmul against each weight, optional GLU epilogue.

The portable interface's single entry point for all GEMV/GEMM paths
(flat single-row, small-batch grouped, large-batch composite). Returns
a list of per-weight outputs; the GLU variant folds silu(gate)*up into
the first output.
"""
outs = [F.linear(x, w) for w in weights]
if glu_epilogue:
# bf16-native silu: bitwise-equal to HF's activation (the fp32
# decomposition rounds differently) and skips its extra casts
outs = [F.silu(outs[0]) * outs[1]]
return outs


def gdn_gates(a, b, a_log, dt_bias):
"""GDN gating: beta = sigmoid(b), g = -exp(a_log) * softplus(a + dt_bias)."""
beta = torch.sigmoid(b)
g = -torch.exp(a_log.float()) * F.softplus(a.float() + dt_bias.float())
return beta, g.to(b.dtype)


def decode_attn(q, K, V, pos):
"""Single-query attention over valid KV positions, batched.

q: [b, num_heads, head_dim]
K, V: [b, num_kv_heads, max_len, head_dim]
pos: int or LongTensor [b] (valid positions, 0-indexed inclusive;
an int applies to every row)

Returns [b, num_heads, head_dim] with GQA (K/V heads shared across query
heads).
"""
num_q_heads = q.shape[-2]
num_kv_heads = K.shape[-3]
head_dim = q.shape[-1]
n_rep = num_q_heads // num_kv_heads
scale = 1.0 / math.sqrt(head_dim)

lens = (pos + 1).tolist() if torch.is_tensor(pos) else [pos + 1] * q.shape[0]
out = torch.empty(q.shape, dtype=torch.float32, device=q.device)
for r in range(q.shape[0]):
K_valid = K[r, :, :lens[r], :] # [nkv, len, hd]
V_valid = V[r, :, :lens[r], :]
if n_rep > 1:
K_valid = K_valid.repeat_interleave(n_rep, dim=0)
V_valid = V_valid.repeat_interleave(n_rep, dim=0)
scores = torch.einsum('hd,hld->hl', q[r].float(), K_valid.float()) * scale
weights = torch.softmax(scores, dim=-1)
out[r] = torch.einsum('hl,hld->hd', weights, V_valid.float())
return out.to(q.dtype)


def silu_mul(gu, n):
"""GLU epilogue on a [M, 2n] buffer: silu(g) * u."""
g = gu[:, :n].float()
u = gu[:, n:].float()
return (g / (1.0 + torch.exp(-g)) * u).to(gu.dtype)


def repeat_qk(q, k, ratio):
"""Head-repeat for the GDN scan's v-head layout: out[...,hv,:] = in[...,hv/ratio,:]."""
return q.repeat_interleave(ratio, dim=-2), k.repeat_interleave(ratio, dim=-2)


def gated_norm(x, z, weight, eps=1e-5):
"""GDN z-gated output norm: (weight * rms(x)) * silu(z), weight direct."""
xf = x.float()
xf = xf * torch.rsqrt(xf.pow(2).mean(-1, keepdim=True) + eps)
return (xf * weight.float() * torch.nn.functional.silu(z.float())).to(x.dtype)


def fused_add_norm(hidden, residual, weight, eps=1e-5):
"""Residual add + RMSNorm: rms_norm(hidden + residual) * weight."""
combined = hidden.float() + residual.float()
rms = torch.rsqrt(combined.pow(2).mean(dim=-1, keepdim=True) + eps)
return (combined * rms * weight.float()).to(hidden.dtype)
378 changes: 378 additions & 0 deletions deepspeed/module_inject/segki_design.md

Large diffs are not rendered by default.

Loading
Loading