From f1572d4586032bb2030b147b3f4ad996bfe67dcd Mon Sep 17 00:00:00 2001 From: Awni Hannun Date: Fri, 9 May 2025 19:42:53 -0700 Subject: [PATCH] mistral3 (#166) --- mlx_lm/models/mistral3.py | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 mlx_lm/models/mistral3.py diff --git a/mlx_lm/models/mistral3.py b/mlx_lm/models/mistral3.py new file mode 100644 index 0000000..ef7d5a0 --- /dev/null +++ b/mlx_lm/models/mistral3.py @@ -0,0 +1,46 @@ +# Copyright © 2025 Apple Inc. + +from dataclasses import dataclass +from typing import Optional + +import mlx.core as mx +import mlx.nn as nn +from mlx.utils import tree_flatten, tree_unflatten + +from . import llama +from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention + + +@dataclass +class ModelArgs(BaseModelArgs): + model_type: str + text_config: dict + + def __post_init__(self): + self.text_config["tie_word_embeddings"] = False + + +class Model(nn.Module): + def __init__(self, args: ModelArgs): + super().__init__() + self.args = args + self.model_type = args.model_type + self.language_model = llama.Model(llama.ModelArgs.from_dict(args.text_config)) + + def __call__( + self, + inputs: mx.array, + cache=None, + mask: Optional[mx.array] = None, + ): + return self.language_model(inputs, cache=cache, mask=mask) + + def sanitize(self, weights): + weights = tree_unflatten(list(weights.items())) + weights.pop("vision_tower", None) + weights.pop("multi_modal_projector", None) + return dict(tree_flatten(weights)) + + @property + def layers(self): + return self.language_model.model.layers