Skip to content

Commit 2a14634

Browse files
authored
Port of bitnet1.58 with custom metal kernel (#331)
* Port mlx-lm bitnet1.58 ml-explore/mlx-lm#219 * update: Update relu2 function to use compile for shapeless input * refactor: Update BitLinear & Format code * update: Add quantization parameters to BaseConfiguration * update: Improve error handling during weight pre-loading in ContentView * update: Add bitnet_b1_58_2b_4t_4bit model configuration to LLMModelFactory * update: Rename relu2 function to reluSquared and refactor implementation * update: ACKNOWLEDGMENTS.md * Improve the bitnet kernel * remove: eliminate reluSquared function from Bitnet.swift * refactor: update kernel
1 parent 2fcd726 commit 2a14634

File tree

5 files changed

+529
-7
lines changed

5 files changed

+529
-7
lines changed

ACKNOWLEDGMENTS.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Individual Contributors
22

3-
If you wish to be acknowledged for your contributions, please list your name
4-
with a short description of your contribution(s) below. For example:
5-
6-
- Jane Smith: Added the `foo` and `bar` ops.
3+
> If you wish to be acknowledged for your contributions, please list your name
4+
> with a short description of your contribution(s) below. For example:
5+
> - Jane Smith: Added the `foo` and `bar` ops.
76
87
MLX Swift was developed with contributions from the following individuals:
98

9+
- [John Mai](https://github.com/johnmai-dev): Added support for multiple models (Qwen2, Starcoder2, InternLM2, Qwen3, Qwen3 MoE, GLM-4, MiMo, BitNet).
10+
11+
1012
<a href="https://github.com/ml-explore/mlx-swift-examples/graphs/contributors">
11-
<img class="dark-light" src="https://contrib.rocks/image?repo=ml-explore/-swift-examples&anon=0&columns=20&max=100&r=true" />
13+
<img class="dark-light" src="https://contrib.rocks/image?repo=ml-explore/mlx-swift-examples&anon=0&columns=20&max=100&r=true" />
1214
</a>
1315

1416

Applications/LLMEval/ContentView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,12 @@ struct ContentView: View {
133133

134134
}
135135
.task {
136-
// pre-load the weights on launch to speed up the first generation
137-
_ = try? await llm.load()
136+
do {
137+
// pre-load the weights on launch to speed up the first generation
138+
_ = try await llm.load()
139+
} catch {
140+
llm.output = "Failed: \(error)"
141+
}
138142
}
139143
}
140144

Libraries/MLXLLM/LLMModelFactory.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class LLMTypeRegistry: ModelTypeRegistry, @unchecked Sendable {
4848
"mimo": create(MiMoConfiguration.self, MiMoModel.init),
4949
"glm4": create(GLM4Configuration.self, GLM4Model.init),
5050
"acereason": create(Qwen2Configuration.self, Qwen2Model.init),
51+
"bitnet": create(BitnetConfiguration.self, BitnetModel.init),
5152
]
5253
}
5354

@@ -225,6 +226,11 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
225226
defaultPrompt: ""
226227
)
227228

229+
static public let bitnet_b1_58_2b_4t_4bit = ModelConfiguration(
230+
id: "mlx-community/bitnet-b1.58-2B-4T-4bit",
231+
defaultPrompt: "Why is the sky blue?"
232+
)
233+
228234
private static func all() -> [ModelConfiguration] {
229235
[
230236
codeLlama13b4bit,
@@ -256,6 +262,7 @@ public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
256262
mimo_7b_sft_4bit,
257263
glm4_9b_4bit,
258264
acereason_7b_4bit,
265+
bitnet_b1_58_2b_4t_4bit,
259266
]
260267
}
261268

0 commit comments

Comments
 (0)