Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'main' into inkling2
  • Loading branch information
jundot committed Nov 20, 2026
commit f9a42a54268dcb88f52925501c63c3def04c1524
2 changes: 1 addition & 1 deletion mlx_lm/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ def main():
if processor is None:
raise ValueError(f"The model {model_path} does not take images.")
images = [load_image(processor, f) for f in args.image]
prompt, pixel_values = processor(prompt, images=images)
prompt, media_inputs = processor(prompt, images=images)
else:
media_inputs = {}
prompt = tokenizer.encode(prompt, add_special_tokens=not use_chat_template)
Expand Down
25 changes: 1 addition & 24 deletions mlx_lm/kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
# Copyright © 2026 Apple Inc.

"""Fused Metal kernels, one kernel per file."""

from .depth_attention import depth_attention
from .expert_routing import expert_routing
from .norm_add_norm import norm_add_norm, pre_norm
from .post_expert_norm_combine import post_expert_norm_combine
from .qk_norm import qk_norm
from .qk_norm_rope import qk_norm_rope
from .relative_attention import relative_attention
from .rms_norm_sigmoid_gate import rms_norm_sigmoid_gate
from .sparse_mla import sparse_mla_attention

__all__ = [
"depth_attention",
"expert_routing",
"norm_add_norm",
"post_expert_norm_combine",
"pre_norm",
"qk_norm",
"qk_norm_rope",
"relative_attention",
"rms_norm_sigmoid_gate",
"sparse_mla_attention",
]
"""Fused Metal kernels that more than one model uses, one kernel per file."""
55 changes: 33 additions & 22 deletions mlx_lm/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,48 @@

import importlib
from collections.abc import Sequence
from typing import Any, Protocol

from typing import BinaryIO
from typing import Any, BinaryIO, Protocol

import numpy as np
from mlx import nn

from . import inkling2, muse_spark
from mlx_lm.utils import model_package

__all__ = [
"ENCODER_CONFIGS",
"PROCESSORS",
"Processor",
"load_image",
"load_processor",
]

PROCESSORS = ("inkling2", "muse_spark")

# The processor of each model module.
PROCESSORS = {
"inkling2": inkling2.Processor,
"muse_spark": muse_spark.Processor,
}
# A checkpoint converted without its encoders has none of these, and takes
# only text.
ENCODER_CONFIGS = ("vision_config", "audio_config")


def load_processor(
model: nn.Module, config: dict
) -> inkling2.Processor | muse_spark.Processor | None:
"""The processor of a model, or None."""
if not config.get("vision_config"):
class Processor(Protocol):
def __call__(self, text: str, **media: Sequence[Any]) -> tuple[str, dict[str, Any]]:
"""The text with the placeholders expanded, and the encoder inputs."""


def load_processor(config: dict) -> Processor | None:
"""The processor of the model of a configuration, or None if it takes only text."""
if not any(config.get(key) for key in ENCODER_CONFIGS):
return None
if "processor_type" in config:
name = config["processor_type"]
else:
name = model_package(config)
if name not in PROCESSORS:
return None
model_module = type(model).__module__.rsplit(".", 1)[-1]
name = config.get("processor_type", model_module)
processor = PROCESSORS.get(name)
return processor.from_config(config) if processor else None
# Imported here: the package holds the whole model.
module = importlib.import_module(f"mlx_lm.models.{name}.processor")
return module.Processor.from_config(config)


def load_image(
processor: inkling2.Processor | muse_spark.Processor, fp: str | BinaryIO
) -> np.ndarray:
def load_image(processor: Processor, fp: str | BinaryIO) -> np.ndarray:
"""A uint8 (H, W, 3) image for the processor."""
if hasattr(processor, "load_image"):
return processor.load_image(fp)
Expand Down
3 changes: 2 additions & 1 deletion mlx_lm/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ def _tokenize(self, tokenizer, request, args):
add_generation_prompt=True,
**{**template_kwargs, "tokenize": False},
)
text, pixel_values = processor(
# The prompt cache of this server tracks images only.
text, media_inputs = processor(
text, images=[_decode_image(processor, u) for u in urls]
)
prompt = tokenizer.encode(text, add_special_tokens=False)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import mlx.core as mx
import mlx.nn as nn

from mlx_lm.kernels import (
depth_attention,
expert_routing,
from mlx_lm.kernels import sparse_mla
from mlx_lm.kernels.depth_attention import depth_attention
from mlx_lm.kernels.expert_routing import expert_routing
from mlx_lm.kernels.relative_attention import relative_attention
from mlx_lm.kernels.sparse_mla import sparse_mla_attention
from mlx_lm.models.muse_spark.kernels import (
norm_add_norm,
post_expert_norm_combine,
pre_norm,
qk_norm,
qk_norm_rope,
relative_attention,
rms_norm_sigmoid_gate,
)
from mlx_lm.models.inkling2 import _attention_reference
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.