-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathllm_mlx.py
330 lines (289 loc) · 11.3 KB
/
llm_mlx.py
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
import click
import json
import llm
from huggingface_hub.utils import disable_progress_bars, enable_progress_bars
from pydantic import Field
from typing import Optional
import os
from pathlib import Path # local import to avoid adding to global namespace
import sys
disable_progress_bars()
# These defaults copied from llama.cpp
# https://github.com/ggml-org/llama.cpp/blob/68ff663a04ed92044a9937bcae353e9d9733f9cd/examples/main/README.md#generation-flags
DEFAULT_TEMPERATURE = 0.8
DEFAULT_TOP_P = 0.9
DEFAULT_MIN_P = 0.1
DEFAULT_MIN_TOKENS_TO_KEEP = 1
DEFAULT_MAX_TOKENS = 1024
def _ensure_models_file():
filepath = llm.user_dir() / "llm-mlx.json"
if not filepath.exists():
filepath.write_text("{}")
return filepath
@llm.hookimpl
def register_commands(cli):
@cli.group()
def mlx():
"Commands for working with MLX models"
@mlx.command()
def models_file():
"Display the path to the llm-mlx.json file"
click.echo(_ensure_models_file())
@mlx.command()
@click.argument("model_path")
@click.option(
"aliases",
"-a",
"--alias",
multiple=True,
help="Alias(es) to register the model under",
)
def download_model(model_path, aliases):
"Download and register a MLX model"
models_file = _ensure_models_file()
models = json.loads(models_file.read_text())
models[model_path] = {"aliases": aliases}
enable_progress_bars()
MlxModel(model_path).prompt("hi").text()
disable_progress_bars()
models_file.write_text(json.dumps(models, indent=2))
@mlx.command()
def models():
"List registered MLX models"
models_file = _ensure_models_file()
models = json.loads(models_file.read_text())
click.echo(json.dumps(models, indent=2))
@mlx.command()
def manage_models():
"Register MLX models from the Hugging Face cache on disk"
cache_dir = Path(
os.environ.get("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
)
found_models = set()
for root, dirs, _ in os.walk(cache_dir):
for d in dirs:
if "mlx-community" in d:
model_dir = Path(root) / d
snapshots_dir = model_dir / "snapshots"
if not snapshots_dir.exists():
continue
# Search for config.json in any subfolder under snapshots
config_found = False
for config_root, _, config_files in os.walk(snapshots_dir):
if "config.json" in config_files:
config_path = Path(config_root) / "config.json"
try:
with open(config_path) as f:
config = json.load(f)
model_type = config.get("model_type", "").lower()
if model_type in [
"whisper",
"llava",
"paligemma",
"qwen2_vl",
"qwen2_5_vl",
"florence2",
"florence",
]:
continue
config_found = True
break
except (json.JSONDecodeError, FileNotFoundError):
continue
if not config_found:
continue
parts = d.split("--")
model_name = "/".join(parts[1:])
if model_name:
# Store model_type along with model_name
found_models.add((model_type, model_name))
if not found_models:
click.echo("No MLX models found in Hugging Face cache")
return
models_file = _ensure_models_file()
existing_models = json.loads(models_file.read_text())
# Create list of models with their current registration status
model_choices = []
for model_type, model in sorted(found_models):
is_registered = model in existing_models
action = "Unregister" if is_registered else "Register"
# Include model_type in display name
display_name = f"{action} {model} ({model_type})"
model_choices.append((display_name, model, is_registered))
# Show interactive selection menu
selected = select_models(model_choices)
print("\nUpdating {}".format(models_file))
for display_name, model_name, is_registered in selected:
if is_registered:
# Unregister model
del existing_models[model_name]
models_file.write_text(json.dumps(existing_models, indent=2))
click.echo(f" Unregistered {model_name}")
else:
# Register new model
existing_models[model_name] = {"aliases": []}
models_file.write_text(json.dumps(existing_models, indent=2))
click.echo(f" Registered {model_name}")
@llm.hookimpl
def register_models(register):
for model_path, config in json.loads(_ensure_models_file().read_text()).items():
aliases = config.get("aliases", [])
register(MlxModel(model_path), aliases=aliases)
class MlxModel(llm.Model):
can_stream = True
class Options(llm.Options):
max_tokens: Optional[int] = Field(
description="Maximum number of tokens to generate",
ge=0,
default=None,
)
unlimited: Optional[bool] = Field(
description="Unlimited output tokens",
default=None,
)
temperature: Optional[float] = Field(
description="Sampling temperature",
ge=0,
default=None,
)
top_p: Optional[float] = Field(
description="Sampling top-p",
ge=0,
le=1,
default=None,
)
min_p: Optional[float] = Field(
description="Sampling min-p",
ge=0,
le=1,
default=None,
)
min_tokens_to_keep: Optional[int] = Field(
description="Minimum tokens to keep for min-p sampling",
ge=1,
default=None,
)
seed: Optional[int] = Field(
description="Random number seed",
default=None,
)
def __init__(self, model_path):
self.model_id = model_path
self.model_path = model_path
self._model = None
self._tokenizer = None
def _load(self):
from mlx_lm import load
if self._model is None:
self._model, self._tokenizer = load(self.model_path)
return self._model, self._tokenizer
def execute(self, prompt, stream, response, conversation):
import mlx.core as mx
from mlx_lm import stream_generate
from mlx_lm.sample_utils import make_sampler
model, tokenizer = self._load()
messages = []
current_system = None
if conversation is not None:
for prev_response in conversation.responses:
if (
prev_response.prompt.system
and prev_response.prompt.system != current_system
):
messages.append(
{"role": "system", "content": prev_response.prompt.system}
)
current_system = prev_response.prompt.system
messages.append(
{"role": "user", "content": prev_response.prompt.prompt}
)
messages.append({"role": "assistant", "content": prev_response.text()})
if prompt.system and prompt.system != current_system:
messages.append({"role": "system", "content": prompt.system})
messages.append({"role": "user", "content": prompt.prompt})
chat_prompt = tokenizer.apply_chat_template(
messages, add_generation_prompt=True
)
sampler = make_sampler(
(
DEFAULT_TEMPERATURE
if prompt.options.temperature is None
else prompt.options.temperature
),
DEFAULT_TOP_P if prompt.options.top_p is None else prompt.options.top_p,
DEFAULT_MIN_P if prompt.options.min_p is None else prompt.options.min_p,
(
DEFAULT_MIN_TOKENS_TO_KEEP
if prompt.options.min_tokens_to_keep is None
else prompt.options.min_tokens_to_keep
),
)
if prompt.options.seed:
mx.random.seed(prompt.options.seed)
max_tokens = DEFAULT_MAX_TOKENS
if prompt.options.max_tokens is not None:
max_tokens = prompt.options.max_tokens
if prompt.options.unlimited:
max_tokens = -1
# Always use stream_generate() because generate() in mlx_lm calls it under the hood
for chunk in stream_generate(
model,
tokenizer,
chat_prompt,
sampler=sampler,
max_tokens=max_tokens,
):
yield chunk.text
response.set_usage(input=chunk.prompt_tokens, output=chunk.generation_tokens)
response.response_json = {
"prompt_tps": chunk.prompt_tps,
"generation_tps": chunk.generation_tps,
"peak_memory": chunk.peak_memory,
"finish_reason": chunk.finish_reason,
}
def select_models(model_choices):
selected = [False] * len(model_choices)
idx = 0
window_size = os.get_terminal_size().lines - 5
while True:
print("\033[H\033[J", end="")
print(
"❯ llm mlx manage-models\nAvailable model files (↑/↓ to navigate, SPACE to select, ENTER to confirm, Ctrl+C to quit):"
)
window_start = max(
0, min(idx - window_size + 3, len(model_choices) - window_size)
)
window_end = min(window_start + window_size, len(model_choices))
for i in range(window_start, window_end):
display_name, _, _ = model_choices[i]
print(
f"{'>' if i == idx else ' '} {'◉' if selected[i] else '○'} {display_name}"
)
key = get_key()
if key == "\x1b[A": # Up arrow
idx = max(0, idx - 1)
elif key == "\x1b[B": # Down arrow
idx = min(len(model_choices) - 1, idx + 1)
elif key == " ":
selected[idx] = not selected[idx]
elif key == "\r": # Enter key
break
elif key == "\x03": # Ctrl+C
print("\nNo changes made.")
sys.exit(0)
return [
choice for choice, is_selected in zip(model_choices, selected) if is_selected
]
def get_key():
"""Get a single keypress from the user."""
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
if ch == "\x1b":
ch += sys.stdin.read(2)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch