Commit d1b1478
Decouple from tokenizer and downloader packages (#118)
* Decouple from tokenizer and downloader packages
MLX Swift LM currently has two fundamental problems:
- Model loading is tightly coupled to the Hugging Face Hub. A Hub client is required even when loading models from a local directory.
- Model loading performance with Swift Transformers lags far behind the Python equivalent, typically taking several seconds in Swift versus a few hundred milliseconds in Python.
This PR implements the following solutions:
- Swift Transformers is replaced with Swift Tokenizers, a streamlined and optimized fork that focuses purely on tokenizer functionality, with no Hugging Face dependency and no extraneous Core ML code. This unlocks a 10x to 15x speedup in model loading times.
- The Downloader protocol abstracts away the model hosting provider, making it easy to use other providers such as ModelScope or define custom providers such as downloading from storage buckets.
- Swift Hugging Face, a dedicated client for the Hub, is used in an optional module. No Hugging Face Hub code is bundled for users who don't need it.
The `hub` parameter (previously `HubApi`) has been replaced with `from` (any `Downloader` or `URL` for a local directory). Functions that previously defaulted to `defaultHubApi` no longer have a default – callers must either pass a `Downloader` explicitly or use the convenience methods in `MLXLMHuggingFace` / `MLXEmbeddersHuggingFace`, which default to `HubClient.default`.
For most users who were using the default Hub client, adding `import MLXLMHuggingFace` or `import MLXEmbeddersHuggingFace` and using the convenience overloads is sufficient.
Users who were passing a custom `HubApi` instance should create a `HubClient` instead and pass it as the `from` parameter. `HubClient` conforms to `Downloader` via `MLXLMHuggingFace`.
- `tokenizerId` and `overrideTokenizer` have been replaced by `tokenizerSource: TokenizerSource?`, which supports `.id(String)` for remote sources and `.directory(URL)` for local paths.
- `preparePrompt` has been removed. This shouldn't be used anyway, since support for chat templates is available.
- `modelDirectory(hub:)` has been removed. For local directories, pass the `URL` directly to the loading functions. For remote models, the `Downloader` protocol handles resolution.
`loadTokenizer(configuration:hub:)` has been removed. Tokenizer loading now uses `AutoTokenizer.from(directory:)` from Swift Tokenizers directly.
`replacementTokenizers` (the `TokenizerReplacementRegistry`) has been removed. Use `AutoTokenizer.register(_:for:)` from Swift Tokenizers instead.
The `defaultHubApi` global has been removed. Hugging Face Hub access is now provided by `HubClient.default` from the `HuggingFace` module.
- `downloadModel(hub:configuration:progressHandler:)` → `Downloader.download(id:revision:matching:useLatest:progressHandler:)`
- `loadTokenizerConfig(configuration:hub:)` → `AutoTokenizer.from(directory:)`
- `ModelFactory._load(hub:configuration:progressHandler:)` → `_load(configuration: ResolvedModelConfiguration)`
- `ModelFactory._loadContainer`: removed (base `loadContainer` now builds the container from `_load`)
---------
Co-authored-by: David Koski <dkoski@apple.com>1 parent 25b00d4 commit d1b1478
File tree
79 files changed
+2927
-1687
lines changed- Libraries
- BenchmarkHelpers
- IntegrationTestHelpers
- MLXEmbedders
- MLXHuggingFaceMacros
- MLXHuggingFace
- MLXLLM
- Documentation.docc
- Models
- MLXLMCommon
- Adapters
- Documentation.docc
- Registries
- Tool
- Parsers
- MLXVLM
- Models
- Tests
- Benchmarks
- MLXLMIntegrationTests
- MLXLMTests
- skills/mlx-swift-lm
- references
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
79 files changed
+2927
-1687
lines changedLarge diffs are not rendered by default.
Lines changed: 614 additions & 0 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
47 | | - | |
| 46 | + | |
48 | 47 | | |
49 | | - | |
50 | 48 | | |
51 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
52 | 52 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 53 | + | |
| 54 | + | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 60 | + | |
64 | 61 | | |
65 | 62 | | |
66 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | | - | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
| |||
26 | 24 | | |
27 | 25 | | |
28 | 26 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | 27 | | |
33 | 28 | | |
34 | 29 | | |
| |||
39 | 34 | | |
40 | 35 | | |
41 | 36 | | |
42 | | - | |
43 | | - | |
44 | 37 | | |
45 | 38 | | |
46 | 39 | | |
| |||
70 | 63 | | |
71 | 64 | | |
72 | 65 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 66 | + | |
| 67 | + | |
78 | 68 | | |
79 | 69 | | |
80 | | - | |
| 70 | + | |
81 | 71 | | |
| 72 | + | |
82 | 73 | | |
83 | | - | |
84 | | - | |
85 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
86 | 77 | | |
| 78 | + | |
87 | 79 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 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 | + | |
109 | 105 | | |
| 106 | + | |
| 107 | + | |
110 | 108 | | |
111 | 109 | | |
112 | 110 | | |
| |||
116 | 114 | | |
117 | 115 | | |
118 | 116 | | |
119 | | - | |
| 117 | + | |
120 | 118 | | |
| 119 | + | |
121 | 120 | | |
122 | 121 | | |
123 | 122 | | |
124 | | - | |
| 123 | + | |
| 124 | + | |
125 | 125 | | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | | - | |
129 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
130 | 132 | | |
131 | | - | |
| 133 | + | |
132 | 134 | | |
133 | 135 | | |
134 | 136 | | |
| |||
213 | 215 | | |
214 | 216 | | |
215 | 217 | | |
216 | | - | |
| 218 | + | |
217 | 219 | | |
| 220 | + | |
218 | 221 | | |
219 | 222 | | |
220 | 223 | | |
221 | | - | |
| 224 | + | |
| 225 | + | |
222 | 226 | | |
| 227 | + | |
223 | 228 | | |
224 | 229 | | |
225 | | - | |
226 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
227 | 234 | | |
228 | | - | |
| 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 | + | |
229 | 279 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
56 | 52 | | |
57 | 53 | | |
58 | 54 | | |
59 | 55 | | |
60 | | - | |
61 | | - | |
| 56 | + | |
| 57 | + | |
62 | 58 | | |
63 | 59 | | |
64 | | - | |
65 | | - | |
| 60 | + | |
| 61 | + | |
66 | 62 | | |
67 | | - | |
68 | | - | |
69 | | - | |
| 63 | + | |
| 64 | + | |
70 | 65 | | |
71 | 66 | | |
72 | 67 | | |
73 | 68 | | |
74 | 69 | | |
75 | | - | |
76 | | - | |
| 70 | + | |
77 | 71 | | |
78 | 72 | | |
79 | | - | |
80 | | - | |
| 73 | + | |
81 | 74 | | |
82 | 75 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 76 | + | |
100 | 77 | | |
101 | 78 | | |
102 | 79 | | |
| |||
0 commit comments