Skip to content
Merged
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
26 changes: 22 additions & 4 deletions mlx_lm/models/qwen3_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .activations import swiglu
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .pipeline import PipelineMixin
from .switch_layers import SwitchGLU


Expand Down Expand Up @@ -181,7 +182,7 @@ def __call__(
return out


class Qwen3MoeModel(nn.Module):
class Qwen3MoeModel(PipelineMixin, nn.Module):
def __init__(self, args: ModelArgs):
super().__init__()
self.args = args
Expand All @@ -206,14 +207,31 @@ def __call__(
else:
h = self.embed_tokens(inputs)

pipeline_rank = self.pipeline_rank
pipeline_size = self.pipeline_size

if cache is None:
cache = [None] * len(self.layers)
cache = [None] * len(self.pipeline_layers)

mask = create_attention_mask(h, cache[0])

for layer, c in zip(self.layers, cache):
# Receive from the previous process in the pipeline
if pipeline_rank < pipeline_size - 1:
h = mx.distributed.recv_like(h, (pipeline_rank + 1))

for layer, c in zip(self.pipeline_layers, cache):
h = layer(h, mask, c)

# Send to the next process in the pipeline
if pipeline_rank != 0:
h = mx.distributed.send(h, (pipeline_rank - 1) % pipeline_size)
if cache[-1] is not None:
cache[-1].keys = mx.depends(cache[-1].keys, h)

# Broadcast h while keeping it in the graph
if pipeline_size > 1:
h = mx.distributed.all_gather(h)[: h.shape[0]]

return self.norm(h)


Expand Down Expand Up @@ -312,4 +330,4 @@ def predicate(path, _):

@property
def layers(self):
return self.model.layers
return self.model.pipeline_layers
37 changes: 37 additions & 0 deletions tests/model_parallel_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ def test_shard(self):
out = model(x)
self.assertTrue(mx.allclose(expected, out, rtol=1e-3, atol=1e-3))

def test_pipeline(self):
test_configs = [
{
"model_type": "qwen3_moe",
"vocab_size": 128,
"hidden_size": 64,
"intermediate_size": 128,
"moe_intermediate_size": 32,
"num_hidden_layers": 4,
"num_attention_heads": 4,
"num_key_value_heads": 2,
"head_dim": 16,
"num_experts": 4,
"num_experts_per_tok": 2,
"decoder_sparse_step": 1,
"mlp_only_layers": [0],
"norm_topk_prob": True,
"rms_norm_eps": 1e-5,
"rope_theta": 10000.0,
"max_position_embeddings": 256,
"tie_word_embeddings": False,
},
]
mx.random.seed(0)
for config in test_configs:
model_type = config["model_type"]
with self.subTest(f"Testing {model_type}", model_type=model_type):
arch = importlib.import_module(f"mlx_lm.models.{model_type}")
args = arch.ModelArgs.from_dict(config)
model = arch.Model(args)
vocab_size = args.vocab_size
x = mx.random.randint(0, vocab_size, shape=(32, 4))
expected = model(x)
model.model.pipeline(mx.distributed.init())
out = model(x)
self.assertTrue(mx.allclose(expected, out, rtol=1e-3, atol=1e-3))


if __name__ == "__main__":
unittest.main()
Loading