mlxrs 0.1.0

Safe Rust bindings for Apple's MLX array framework, with LM, VLM, audio, and embeddings support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! @generated by `cargo xtask-codegen` from mlxrs/data/tokenizer/ (mlx-lm df1d3f3). DO NOT EDIT.
//!
//! Regenerate with `cargo xtask-codegen`; `cargo xtask-codegen --check` is the
//! drift guard. Source-of-truth: the files under `mlxrs/data/tokenizer/`
//! plus the deterministic GPT-2 `bytes_to_unicode` algorithm.
//!
//! `dead_code` is allowed because this module carries every codegen
//! table but a given feature subset consumes only some of them.
#![allow(missing_docs, dead_code)]

// GPT-2 bytes_to_unicode inverse (char -> byte), sorted by char for
// binary search. Provenance: openai/gpt-2 encoder.py / mlx-lm df1d3f3.
pub(crate) static BYTE_DECODER: &[(char, u8)] = &[
  ('!', 33),
  ('"', 34),
  ('#', 35),
  ('$', 36),
  ('%', 37),
  ('&', 38),
  ('\'', 39),
  ('(', 40),
  (')', 41),
  ('*', 42),
  ('+', 43),
  (',', 44),
  ('-', 45),
  ('.', 46),
  ('/', 47),
  ('0', 48),
  ('1', 49),
  ('2', 50),
  ('3', 51),
  ('4', 52),
  ('5', 53),
  ('6', 54),
  ('7', 55),
  ('8', 56),
  ('9', 57),
  (':', 58),
  (';', 59),
  ('<', 60),
  ('=', 61),
  ('>', 62),
  ('?', 63),
  ('@', 64),
  ('A', 65),
  ('B', 66),
  ('C', 67),
  ('D', 68),
  ('E', 69),
  ('F', 70),
  ('G', 71),
  ('H', 72),
  ('I', 73),
  ('J', 74),
  ('K', 75),
  ('L', 76),
  ('M', 77),
  ('N', 78),
  ('O', 79),
  ('P', 80),
  ('Q', 81),
  ('R', 82),
  ('S', 83),
  ('T', 84),
  ('U', 85),
  ('V', 86),
  ('W', 87),
  ('X', 88),
  ('Y', 89),
  ('Z', 90),
  ('[', 91),
  ('\\', 92),
  (']', 93),
  ('^', 94),
  ('_', 95),
  ('`', 96),
  ('a', 97),
  ('b', 98),
  ('c', 99),
  ('d', 100),
  ('e', 101),
  ('f', 102),
  ('g', 103),
  ('h', 104),
  ('i', 105),
  ('j', 106),
  ('k', 107),
  ('l', 108),
  ('m', 109),
  ('n', 110),
  ('o', 111),
  ('p', 112),
  ('q', 113),
  ('r', 114),
  ('s', 115),
  ('t', 116),
  ('u', 117),
  ('v', 118),
  ('w', 119),
  ('x', 120),
  ('y', 121),
  ('z', 122),
  ('{', 123),
  ('|', 124),
  ('}', 125),
  ('~', 126),
  ('¡', 161),
  ('¢', 162),
  ('£', 163),
  ('¤', 164),
  ('¥', 165),
  ('¦', 166),
  ('§', 167),
  ('¨', 168),
  ('©', 169),
  ('ª', 170),
  ('«', 171),
  ('¬', 172),
  ('®', 174),
  ('¯', 175),
  ('°', 176),
  ('±', 177),
  ('²', 178),
  ('³', 179),
  ('´', 180),
  ('µ', 181),
  ('', 182),
  ('·', 183),
  ('¸', 184),
  ('¹', 185),
  ('º', 186),
  ('»', 187),
  ('¼', 188),
  ('½', 189),
  ('¾', 190),
  ('¿', 191),
  ('À', 192),
  ('Á', 193),
  ('Â', 194),
  ('Ã', 195),
  ('Ä', 196),
  ('Å', 197),
  ('Æ', 198),
  ('Ç', 199),
  ('È', 200),
  ('É', 201),
  ('Ê', 202),
  ('Ë', 203),
  ('Ì', 204),
  ('Í', 205),
  ('Î', 206),
  ('Ï', 207),
  ('Ð', 208),
  ('Ñ', 209),
  ('Ò', 210),
  ('Ó', 211),
  ('Ô', 212),
  ('Õ', 213),
  ('Ö', 214),
  ('×', 215),
  ('Ø', 216),
  ('Ù', 217),
  ('Ú', 218),
  ('Û', 219),
  ('Ü', 220),
  ('Ý', 221),
  ('Þ', 222),
  ('ß', 223),
  ('à', 224),
  ('á', 225),
  ('â', 226),
  ('ã', 227),
  ('ä', 228),
  ('å', 229),
  ('æ', 230),
  ('ç', 231),
  ('è', 232),
  ('é', 233),
  ('ê', 234),
  ('ë', 235),
  ('ì', 236),
  ('í', 237),
  ('î', 238),
  ('ï', 239),
  ('ð', 240),
  ('ñ', 241),
  ('ò', 242),
  ('ó', 243),
  ('ô', 244),
  ('õ', 245),
  ('ö', 246),
  ('÷', 247),
  ('ø', 248),
  ('ù', 249),
  ('ú', 250),
  ('û', 251),
  ('ü', 252),
  ('ý', 253),
  ('þ', 254),
  ('ÿ', 255),
  ('Ā', 0),
  ('ā', 1),
  ('Ă', 2),
  ('ă', 3),
  ('Ą', 4),
  ('ą', 5),
  ('Ć', 6),
  ('ć', 7),
  ('Ĉ', 8),
  ('ĉ', 9),
  ('Ċ', 10),
  ('ċ', 11),
  ('Č', 12),
  ('č', 13),
  ('Ď', 14),
  ('ď', 15),
  ('Đ', 16),
  ('đ', 17),
  ('Ē', 18),
  ('ē', 19),
  ('Ĕ', 20),
  ('ĕ', 21),
  ('Ė', 22),
  ('ė', 23),
  ('Ę', 24),
  ('ę', 25),
  ('Ě', 26),
  ('ě', 27),
  ('Ĝ', 28),
  ('ĝ', 29),
  ('Ğ', 30),
  ('ğ', 31),
  ('Ġ', 32),
  ('ġ', 127),
  ('Ģ', 128),
  ('ģ', 129),
  ('Ĥ', 130),
  ('ĥ', 131),
  ('Ħ', 132),
  ('ħ', 133),
  ('Ĩ', 134),
  ('ĩ', 135),
  ('Ī', 136),
  ('ī', 137),
  ('Ĭ', 138),
  ('ĭ', 139),
  ('Į', 140),
  ('į', 141),
  ('İ', 142),
  ('ı', 143),
  ('IJ', 144),
  ('ij', 145),
  ('Ĵ', 146),
  ('ĵ', 147),
  ('Ķ', 148),
  ('ķ', 149),
  ('ĸ', 150),
  ('Ĺ', 151),
  ('ĺ', 152),
  ('Ļ', 153),
  ('ļ', 154),
  ('Ľ', 155),
  ('ľ', 156),
  ('Ŀ', 157),
  ('ŀ', 158),
  ('Ł', 159),
  ('ł', 160),
  ('Ń', 173),
];

// Source: mlxrs/data/tokenizer/bpe_space_matches.toml (mlx-lm df1d3f3).
pub(crate) static BPE_SPACE_MATCHES: &[&str] =
  &[".", "?", "!", ",", "n't", "'m", "'s", "'ve", "'re"];

// Source: mlxrs/data/tokenizer/tool_parsers.toml (mlx-lm df1d3f3).

/// One entry per tool parser: stable name + `tool_call_start` /
/// `tool_call_end` delimiters.
pub(crate) struct ToolParserMarkers {
  pub name: &'static str,
  pub start: &'static str,
  pub end: &'static str,
}

/// Ordered `_infer_tool_parser` selection rule (mirrors the Python
/// if/elif chain). A rule fires when every `all` substring is present
/// AND (`any_of` empty OR at least one `any_of` substring is present).
pub(crate) struct ToolParserSelect {
  pub name: &'static str,
  pub all: &'static [&'static str],
  pub any_of: &'static [&'static str],
}

pub(crate) static TOOL_PARSER_MARKERS: &[ToolParserMarkers] = &[
  ToolParserMarkers {
    name: "minimax_m2",
    start: "<minimax:tool_call>",
    end: "</minimax:tool_call>",
  },
  ToolParserMarkers {
    name: "gemma4",
    start: "<|tool_call>",
    end: "<tool_call|>",
  },
  ToolParserMarkers {
    name: "function_gemma",
    start: "<start_function_call>",
    end: "<end_function_call>",
  },
  ToolParserMarkers {
    name: "longcat",
    start: "<longcat_tool_call>",
    end: "</longcat_tool_call>",
  },
  ToolParserMarkers {
    name: "glm47",
    start: "<tool_call>",
    end: "</tool_call>",
  },
  ToolParserMarkers {
    name: "pythonic",
    start: "<|tool_call_start|>",
    end: "<|tool_call_end|>",
  },
  ToolParserMarkers {
    name: "qwen3_coder",
    start: "<tool_call>",
    end: "</tool_call>",
  },
  ToolParserMarkers {
    name: "kimi_k2",
    start: "<|tool_calls_section_begin|>",
    end: "<|tool_calls_section_end|>",
  },
  ToolParserMarkers {
    name: "mistral",
    start: "[TOOL_CALLS]",
    end: "",
  },
  ToolParserMarkers {
    name: "json_tools",
    start: "<tool_call>",
    end: "</tool_call>",
  },
];

pub(crate) static TOOL_PARSER_SELECT: &[ToolParserSelect] = &[
  ToolParserSelect {
    name: "minimax_m2",
    all: &["<minimax:tool_call>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "gemma4",
    all: &["<|tool_call>", "<tool_call|>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "function_gemma",
    all: &["<start_function_call>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "longcat",
    all: &["<longcat_tool_call>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "glm47",
    all: &["<arg_key>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "pythonic",
    all: &["<|tool_list_start|>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "qwen3_coder",
    all: &[],
    any_of: &["<tool_call>\\n<function=", "<tool_call>\n<function="],
  },
  ToolParserSelect {
    name: "kimi_k2",
    all: &["<|tool_calls_section_begin|>"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "mistral",
    all: &["[TOOL_CALLS]"],
    any_of: &[],
  },
  ToolParserSelect {
    name: "json_tools",
    all: &["<tool_call>", "tool_call.name"],
    any_of: &[],
  },
];

// Source: mlxrs/data/tokenizer/chat_template_overrides/deepseek_v32.jinja
// (mlx-lm df1d3f3). Byte-exact embed; substitution pipeline stays in chat.rs.
pub(crate) static DEEPSEEK_V32_TEMPLATE: &str = r#"## Tools

You have access to a set of tools you can use to answer the user's question.
You can invoke functions by writing a "<{dsml}function_calls>" block like the following as part of your reply to the user:
<{dsml}function_calls>
<{dsml}invoke name="$FUNCTION_NAME">
<{dsml}parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</{dsml}parameter>
...
</{dsml}invoke>
<{dsml}invoke name="$FUNCTION_NAME2">
...
</{dsml}invoke>
</{dsml}function_calls>

String and scalar parameters should be specified as is without any escaping or quotes, while lists and objects should use JSON format. The "string" attribute should be set to "true" for string type parameters and "false" for other types (numbers, booleans, arrays, objects).

If the thinking_mode is enabled, then after function results you should strongly consider outputting a thinking block. Here is an example:

<{dsml}function_calls>
...
</{dsml}function_calls>

<function_results>
...
</function_results>

{think_start}...thinking about results{think_end}

Here are the functions available in JSONSchema format:
<functions>
{tool_schemas}
</functions>
"#;