Skip to content

Commit 505c86f

Browse files
authored
feat: implement gemma3n text model in MLXLLM (#346)
* feat: implement gemma3n text model in MLXLLM * added to LLMModelFactory * added to MLXService * added to MLXChatExample
1 parent 073e657 commit 505c86f

File tree

4 files changed

+1046
-11
lines changed

4 files changed

+1046
-11
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ iOSInjectionProject/
9292
# OS
9393
.DS_Store
9494

95-
.idea
95+
.idea
96+
.vscode
97+

Applications/MLXChatExample/Services/MLXService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MLXService {
3030
LMModel(name: "qwen2VL:2b", configuration: VLMRegistry.qwen2VL2BInstruct4Bit, type: .vlm),
3131
LMModel(name: "smolVLM", configuration: VLMRegistry.smolvlminstruct4bit, type: .vlm),
3232
LMModel(name: "acereason:7B", configuration: LLMRegistry.acereason_7b_4bit, type: .llm),
33+
LMModel(name: "gemma3n:E2B", configuration: LLMRegistry.gemma3n_E2B_it_lm_4bit, type: .llm),
34+
LMModel(name: "gemma3n:E4B", configuration: LLMRegistry.gemma3n_E4B_it_lm_4bit, type: .llm),
3335
]
3436

3537
/// Cache to store loaded model containers to avoid reloading.

Libraries/MLXLLM/LLMModelFactory.swift

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ public class LLMTypeRegistry: ModelTypeRegistry, @unchecked Sendable {
3535
"phimoe": create(PhiMoEConfiguration.self, PhiMoEModel.init),
3636
"gemma": create(GemmaConfiguration.self, GemmaModel.init),
3737
"gemma2": create(Gemma2Configuration.self, Gemma2Model.init),
38+
"gemma3": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
39+
"gemma3_text": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
40+
"gemma3n": create(Gemma3nTextConfiguration.self, Gemma3nTextModel.init),
3841
"qwen2": create(Qwen2Configuration.self, Qwen2Model.init),
3942
"qwen3": create(Qwen3Configuration.self, Qwen3Model.init),
4043
"qwen3_moe": create(Qwen3MoEConfiguration.self, Qwen3MoEModel.init),
4144
"starcoder2": create(Starcoder2Configuration.self, Starcoder2Model.init),
4245
"cohere": create(CohereConfiguration.self, CohereModel.init),
4346
"openelm": create(OpenElmConfiguration.self, OpenELMModel.init),
4447
"internlm2": create(InternLM2Configuration.self, InternLM2Model.init),
45-
"gemma3_text": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
46-
"gemma3": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
4748
"granite": create(GraniteConfiguration.self, GraniteModel.init),
4849
"mimo": create(MiMoConfiguration.self, MiMoModel.init),
4950
"glm4": create(GLM4Configuration.self, GLM4Model.init),
@@ -56,7 +57,6 @@ public class LLMTypeRegistry: ModelTypeRegistry, @unchecked Sendable {
5657
"exaone4": create(Exaone4Configuration.self, Exaone4Model.init),
5758
]
5859
}
59-
6060
}
6161

6262
/// Registry of models and any overrides that go with them, e.g. prompt augmentation.
@@ -138,6 +138,40 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
138138
defaultPrompt: "What is the difference between lettuce and cabbage?"
139139
)
140140

141+
static public let gemma3_1B_qat_4bit = ModelConfiguration(
142+
id: "mlx-community/gemma-3-1b-it-qat-4bit",
143+
defaultPrompt: "What is the difference between a fruit and a vegetable?",
144+
extraEOSTokens: ["<end_of_turn>"]
145+
)
146+
147+
static public let gemma3n_E4B_it_lm_bf16 = ModelConfiguration(
148+
id: "mlx-community/gemma-3n-E4B-it-lm-bf16",
149+
defaultPrompt: "What is the difference between a fruit and a vegetable?",
150+
// https://ai.google.dev/gemma/docs/core/prompt-structure
151+
extraEOSTokens: ["<end_of_turn>"]
152+
)
153+
154+
static public let gemma3n_E2B_it_lm_bf16 = ModelConfiguration(
155+
id: "mlx-community/gemma-3n-E2B-it-lm-bf16",
156+
defaultPrompt: "What is the difference between a fruit and a vegetable?",
157+
// https://ai.google.dev/gemma/docs/core/prompt-structure
158+
extraEOSTokens: ["<end_of_turn>"]
159+
)
160+
161+
static public let gemma3n_E4B_it_lm_4bit = ModelConfiguration(
162+
id: "mlx-community/gemma-3n-E4B-it-lm-4bit",
163+
defaultPrompt: "What is the difference between a fruit and a vegetable?",
164+
// https://ai.google.dev/gemma/docs/core/prompt-structure
165+
extraEOSTokens: ["<end_of_turn>"]
166+
)
167+
168+
static public let gemma3n_E2B_it_lm_4bit = ModelConfiguration(
169+
id: "mlx-community/gemma-3n-E2B-it-lm-4bit",
170+
defaultPrompt: "What is the difference between a fruit and a vegetable?",
171+
// https://ai.google.dev/gemma/docs/core/prompt-structure
172+
extraEOSTokens: ["<end_of_turn>"]
173+
)
174+
141175
static public let qwen205b4bit = ModelConfiguration(
142176
id: "mlx-community/Qwen1.5-0.5B-Chat-4bit",
143177
overrideTokenizer: "PreTrainedTokenizer",
@@ -205,12 +239,6 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
205239
defaultPrompt: "What is the difference between a fruit and a vegetable?"
206240
)
207241

208-
static public let gemma3_1B_qat_4bit = ModelConfiguration(
209-
id: "mlx-community/gemma-3-1b-it-qat-4bit",
210-
defaultPrompt: "What is the difference between a fruit and a vegetable?",
211-
extraEOSTokens: ["<end_of_turn>"]
212-
)
213-
214242
static public let granite3_3_2b_4bit = ModelConfiguration(
215243
id: "mlx-community/granite-3.3-2b-instruct-4bit",
216244
defaultPrompt: ""
@@ -268,6 +296,11 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
268296
gemma2bQuantized,
269297
gemma_2_2b_it_4bit,
270298
gemma_2_9b_it_4bit,
299+
gemma3_1B_qat_4bit,
300+
gemma3n_E4B_it_lm_bf16,
301+
gemma3n_E2B_it_lm_bf16,
302+
gemma3n_E4B_it_lm_4bit,
303+
gemma3n_E2B_it_lm_4bit,
271304
granite3_3_2b_4bit,
272305
llama3_1_8B_4bit,
273306
llama3_2_1B_4bit,
@@ -288,7 +321,6 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
288321
qwen3_8b_4bit,
289322
qwen3MoE_30b_a3b_4bit,
290323
smolLM_135M_4bit,
291-
gemma3_1B_qat_4bit,
292324
mimo_7b_sft_4bit,
293325
glm4_9b_4bit,
294326
acereason_7b_4bit,

0 commit comments

Comments
 (0)