Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
103 changes: 96 additions & 7 deletions mlx_lm/models/deepseek_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import math
from dataclasses import dataclass, field
from functools import lru_cache
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

import mlx.core as mx
import mlx.nn as nn
Expand Down Expand Up @@ -71,6 +71,9 @@ class ModelArgs(BaseModelArgs):
hc_sinkhorn_iters: int = 20
hc_eps: float = 1e-6

# Multi-token prediction (MTP) heads; built when > 0.
num_nextn_predict_layers: int = 1

# RoPE / YaRN
max_position_embeddings: int = 1048576
rope_theta: float = 10000.0
Expand All @@ -95,9 +98,11 @@ def __post_init__(self):
"`compress_ratios` must have at least one entry per hidden layer, "
f"got {len(self.compress_ratios)} for {self.num_hidden_layers} layers."
)
# Extra trailing entries (the checkpoint lists them for its MTP layers) are
# ignored, matching the dropped MTP weights.
self.compress_ratios = list(self.compress_ratios[: self.num_hidden_layers])
# Cover the base layers plus the MTP layers, which default to 0.
total_layers = self.num_hidden_layers + self.num_nextn_predict_layers
self.compress_ratios = list(self.compress_ratios[:total_layers])
while len(self.compress_ratios) < total_layers:
self.compress_ratios.append(0)
bad = [r for r in self.compress_ratios if r not in (0, 4, 128)]
if bad:
raise ValueError(f"Unsupported DeepSeek-V4 compress ratios: {bad}")
Expand Down Expand Up @@ -1630,6 +1635,44 @@ def __call__(self, h: mx.array, mask, cache, input_ids: mx.array) -> mx.array:
return h


# --------------------------------------------------------------------------- #
# MTP Block (next-N-token prediction head, from Blaizzy/mlx-lm PR #15) #
# --------------------------------------------------------------------------- #


class MTPBlock(nn.Module):
"""Next-N-token prediction head: re-mixes the previous hidden state with the
embedded "next" token, then runs a copy of the V4 block + hc_head. Present as
scaffolding for MTP decoding, which is wired up in a follow-up."""

def __init__(self, args: ModelArgs, layer_idx: int):
super().__init__()
dim = args.hidden_size
self.block = DeepseekV4Block(args, layer_idx)
self.e_proj = nn.Linear(dim, dim, bias=False)
self.h_proj = nn.Linear(dim, dim, bias=False)
self.enorm = nn.RMSNorm(dim, eps=args.rms_norm_eps)
self.hnorm = nn.RMSNorm(dim, eps=args.rms_norm_eps)
self.norm = nn.RMSNorm(dim, eps=args.rms_norm_eps)
self.hc_head = HyperHead(
args.hidden_size, args.hc_mult, args.rms_norm_eps, args.hc_eps
)

def __call__(
self,
h: mx.array,
embed_tokens: nn.Embedding,
input_ids: mx.array,
mask: Optional[mx.array],
cache: Optional[Any],
) -> mx.array:
e = self.enorm(embed_tokens(input_ids))
h_norm = self.hnorm(h)
x = self.e_proj(e)[:, :, None, :] + self.h_proj(h_norm)
x = mx.contiguous(x)
return self.block(x, mask, cache, input_ids)


# --------------------------------------------------------------------------- #
# Model #
# --------------------------------------------------------------------------- #
Expand Down Expand Up @@ -1726,6 +1769,11 @@ def __init__(self, args: ModelArgs):
self.model_type = args.model_type
self.model = DeepseekV4Model(args)
self.lm_head = nn.Linear(args.hidden_size, args.vocab_size, bias=False)
if args.num_nextn_predict_layers > 0:
n = args.num_hidden_layers
self.mtp = [
MTPBlock(args, n + i) for i in range(args.num_nextn_predict_layers)
]

def __call__(
self,
Expand Down Expand Up @@ -1755,6 +1803,44 @@ def pred(k: str):

return pred

def make_mtp_cache(self):
if not hasattr(self, "mtp"):
return None
caches = []
for mtp_block in self.mtp:
attn = mtp_block.block.attn
if attn.compress_ratio:
caches.append(CompressedKVCache(max_size=self.args.sliding_window))
else:
caches.append(RotatingKVCache(max_size=self.args.sliding_window))
return caches

def mtp_forward(
self,
h: mx.array,
input_ids: mx.array,
cache: Optional[List[Any]] = None,
) -> mx.array:
if cache is None:
cache = [None] * len(self.mtp)
first_cache = cache[0]
mask_cache = (
first_cache.local
if isinstance(first_cache, CompressedKVCache)
else first_cache
)
mask = create_attention_mask(
h[:, :, 0, :] if h.ndim == 4 else h,
mask_cache,
window_size=self.args.sliding_window,
return_array=True,
)
for mtp_block, layer_cache in zip(self.mtp, cache):
h = mtp_block(h, self.model.embed_tokens, input_ids, mask, layer_cache)
out = mtp_block.hc_head(h)
out = mtp_block.norm(out)
return self.lm_head(out)

def make_cache(self):
caches = []
for layer in self.layers:
Expand Down Expand Up @@ -1794,9 +1880,12 @@ def sanitize(self, weights: Dict[str, mx.array]) -> Dict[str, mx.array]:
"""
n_layers = self.args.num_hidden_layers

# 1) Drop MTP weights: this model does base-model inference only, so the
# multi-token-prediction heads some checkpoints carry are not consumed
# here. Base layers beyond num_hidden_layers are dropped below.
# 1) Drop MTP weights and the (unused) MTP module. The MTP module here is
# scaffolding for MTP decoding; the heads real checkpoints ship do not map
# to it yet, so they are dropped and the base model is loaded. Base layers
# beyond num_hidden_layers are dropped below.
if hasattr(self, "mtp"):
del self.mtp
new_weights = {}
for k, v in weights.items():
if k.startswith("mtp."):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,48 @@ def test_deepseek_v4_indexer_topk(self):
mx.eval(outputs)
self.assertEqual(outputs.shape, (1, 1, args.vocab_size))

def test_deepseek_v4_mtp_forward(self):
# MTP scaffolding: build a model with an MTP head and run mtp_forward on a
# freshly-built (zero-init) module to confirm it wires up and emits logits.
from mlx_lm.models import deepseek_v4

args = deepseek_v4.ModelArgs(
model_type="deepseek_v4",
vocab_size=1024,
hidden_size=128,
num_hidden_layers=4,
num_attention_heads=4,
num_key_value_heads=1,
q_lora_rank=32,
o_lora_rank=16,
o_groups=2,
head_dim=32,
qk_rope_head_dim=8,
sliding_window=16,
compress_ratios=[0, 0, 4, 0],
index_n_heads=4,
index_head_dim=16,
index_topk=4,
moe_intermediate_size=32,
n_routed_experts=4,
n_shared_experts=1,
num_experts_per_tok=2,
num_hash_layers=1,
hc_mult=2,
hc_sinkhorn_iters=2,
max_position_embeddings=256,
num_nextn_predict_layers=1,
)
model = deepseek_v4.Model(args)
self.assertTrue(hasattr(model, "mtp"))
self.assertEqual(len(model.mtp), args.num_nextn_predict_layers)

inputs = mx.array([[0, 1, 2, 3, 4]], dtype=mx.int32)
_, h_raw = model(inputs, return_hidden=True)
mtp_out = model.mtp_forward(h_raw, inputs, cache=model.make_mtp_cache())
mx.eval(mtp_out)
self.assertEqual(mtp_out.shape, (1, inputs.shape[1], args.vocab_size))

def test_deepseek_v4_sparse_gather_path(self):
# With index_gate_rows lowered, a modest prompt crosses the gate so the
# top-k indexer fires and V4Attention exercises the gather + compressed-
Expand Down
Loading