[Model] Add Kimi K3 support: model files and kernels [1/N] (#50089)
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Signed-off-by: Isotr0py <Isotr0py@outlook.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Ziming Huang <zelda.huanghuang@gmail.com> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Isotr0py <mozf@inferact.ai> Co-authored-by: aoshen02 <aoshen02@users.noreply.github.com> Co-authored-by: Woosuk Kwon <woosuk@inferact.ai> Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: aoshen02 <aoshen@inferact.ai> Co-authored-by: Summer Yang <girasoleyang@gmail.com> Co-authored-by: Kevin H. Luu <khluu000@gmail.com> Co-authored-by: Bowen Wang <abmfy@icloud.com> Co-authored-by: gnovack <novackgm@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: xiaozhoupy <peiyuanzhou1994@gmail.com> Co-authored-by: Roy Wang <yasong.wang@inferact.ai> Co-authored-by: Jeff (Junze) Ma <93145857+majunze2001@users.noreply.github.com> Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
co-authored by
Isotr0py
Thien Tran
Bugen Zhao
Yongye Zhu
Ziming Huang
Roger Wang
Isotr0py
aoshen02
Woosuk Kwon
Jee Jee Li
aoshen02
Summer Yang
Kevin H. Luu
Bowen Wang
gnovack
Nick Hill
Yifan Qiao
xiaozhoupy
Roy Wang
Jeff Ma
Codex
parent
6f00a1ae3b
commit
7c6729b769
+131
-8
@@ -2733,6 +2733,52 @@ def fused_minimax_m3_qknorm_rope_kv_insert(
|
||||
)
|
||||
|
||||
|
||||
def fused_kda_decode(
|
||||
x: torch.Tensor,
|
||||
weight: torch.Tensor,
|
||||
bias: torch.Tensor | None,
|
||||
conv_state: torch.Tensor,
|
||||
raw_g: torch.Tensor,
|
||||
raw_beta: torch.Tensor,
|
||||
A_log: torch.Tensor,
|
||||
dt_bias: torch.Tensor,
|
||||
state_indices: torch.Tensor,
|
||||
state: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
lower_bound: float | None = None,
|
||||
output_gate: torch.Tensor | None = None,
|
||||
norm_weight: torch.Tensor | None = None,
|
||||
norm_eps: float = 1e-5,
|
||||
) -> torch.Tensor:
|
||||
if out is None:
|
||||
out = torch.empty(
|
||||
1,
|
||||
x.shape[0],
|
||||
raw_g.shape[2],
|
||||
raw_g.shape[3],
|
||||
dtype=x.dtype,
|
||||
device=x.device,
|
||||
)
|
||||
torch.ops._C.fused_kda_decode(
|
||||
x,
|
||||
weight,
|
||||
bias,
|
||||
conv_state,
|
||||
raw_g,
|
||||
raw_beta,
|
||||
A_log,
|
||||
dt_bias,
|
||||
state_indices,
|
||||
state,
|
||||
out,
|
||||
lower_bound,
|
||||
output_gate,
|
||||
norm_weight,
|
||||
norm_eps,
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def concat_and_cache_mla(
|
||||
kv_c: torch.Tensor,
|
||||
k_pe: torch.Tensor,
|
||||
@@ -2746,6 +2792,26 @@ def concat_and_cache_mla(
|
||||
)
|
||||
|
||||
|
||||
def concat_and_cache_mla_grouped(
|
||||
kv_c: torch.Tensor,
|
||||
k_pe: torch.Tensor,
|
||||
kv_cache_ptrs: torch.Tensor,
|
||||
slot_mapping: torch.Tensor,
|
||||
block_size: int,
|
||||
block_stride: int,
|
||||
entry_stride: int,
|
||||
) -> None:
|
||||
torch.ops._C_cache_ops.concat_and_cache_mla_grouped(
|
||||
kv_c,
|
||||
k_pe,
|
||||
kv_cache_ptrs,
|
||||
slot_mapping,
|
||||
block_size,
|
||||
block_stride,
|
||||
entry_stride,
|
||||
)
|
||||
|
||||
|
||||
def kimi_k3_attn_res(
|
||||
prefix: torch.Tensor,
|
||||
delta: torch.Tensor,
|
||||
@@ -3040,6 +3106,63 @@ def all_reduce(
|
||||
torch.ops._C_custom_ar.all_reduce(fa, inp, out, reg_buffer, reg_buffer_sz_bytes)
|
||||
|
||||
|
||||
def custom_all_gather(
|
||||
fa: int,
|
||||
inp: torch.Tensor,
|
||||
out: torch.Tensor,
|
||||
reg_buffer: int,
|
||||
reg_buffer_sz_bytes: int,
|
||||
) -> None:
|
||||
torch.ops._C_custom_ar.custom_all_gather(
|
||||
fa, inp, out, reg_buffer, reg_buffer_sz_bytes
|
||||
)
|
||||
|
||||
|
||||
def mnnvl_lamport_all_gather(
|
||||
fa: int,
|
||||
inp: torch.Tensor,
|
||||
out: torch.Tensor,
|
||||
local_buffer: int,
|
||||
multicast_buffer: int,
|
||||
epoch_buffer: int,
|
||||
stage_sz_bytes: int,
|
||||
) -> None:
|
||||
torch.ops._C_custom_ar.mnnvl_lamport_all_gather(
|
||||
fa,
|
||||
inp,
|
||||
out,
|
||||
local_buffer,
|
||||
multicast_buffer,
|
||||
epoch_buffer,
|
||||
stage_sz_bytes,
|
||||
)
|
||||
|
||||
|
||||
def custom_reduce_scatter(
|
||||
fa: int,
|
||||
inp: torch.Tensor,
|
||||
out: torch.Tensor,
|
||||
reg_buffer: int,
|
||||
reg_buffer_sz_bytes: int,
|
||||
) -> None:
|
||||
torch.ops._C_custom_ar.custom_reduce_scatter(
|
||||
fa, inp, out, reg_buffer, reg_buffer_sz_bytes
|
||||
)
|
||||
|
||||
|
||||
def mnnvl_lamport_reduce_scatter(
|
||||
fa: int,
|
||||
inp: torch.Tensor,
|
||||
out: torch.Tensor,
|
||||
local_buffer: int,
|
||||
epoch_buffer: int,
|
||||
stage_sz_bytes: int,
|
||||
) -> None:
|
||||
torch.ops._C_custom_ar.mnnvl_lamport_reduce_scatter(
|
||||
fa, inp, out, local_buffer, epoch_buffer, stage_sz_bytes
|
||||
)
|
||||
|
||||
|
||||
def dispose(fa: int) -> None:
|
||||
torch.ops._C_custom_ar.dispose(fa)
|
||||
|
||||
@@ -3144,18 +3267,18 @@ def dsv3_fused_a_gemm(
|
||||
output: torch.Tensor,
|
||||
mat_a: torch.Tensor,
|
||||
mat_b: torch.Tensor,
|
||||
enable_pdl: bool = False,
|
||||
) -> None:
|
||||
"""DeepSeek V3 fused A GEMM (SM 9.0+, bf16 only, 1-16 tokens).
|
||||
"""Low-latency fused-A-style GEMM (SM 9.0+, BF16, 1-16 tokens).
|
||||
|
||||
Computes output = mat_a @ mat_b.T where:
|
||||
mat_a: [num_tokens, 7168] row-major bf16 (hidden states)
|
||||
mat_b: [7168, 2112] column-major bf16 (weight transposed)
|
||||
output: [num_tokens, 2112] row-major bf16
|
||||
Computes ``output = mat_a @ mat_b`` for the compiled Kimi K3 and
|
||||
DeepSeek V3 projection shapes. ``mat_a`` and ``output`` are row-major;
|
||||
``mat_b`` is the column-major transposed weight. ``enable_pdl`` permits
|
||||
programmatic dependent launch for callers that have validated it.
|
||||
|
||||
Optimized for the DeepSeek V2/V3 QKV A-projection at small batch sizes.
|
||||
Requires SM 9.0+ (Hopper).
|
||||
Requires SM 9.0+.
|
||||
"""
|
||||
torch.ops._C.dsv3_fused_a_gemm(output, mat_a, mat_b)
|
||||
torch.ops._C.dsv3_fused_a_gemm(output, mat_a, mat_b, enable_pdl)
|
||||
|
||||
|
||||
if hasattr(torch.ops._C, "weight_packed_linear"):
|
||||
|
||||
Reference in New Issue
Block a user