-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy path__init__.py
More file actions
458 lines (374 loc) · 13.7 KB
/
__init__.py
File metadata and controls
458 lines (374 loc) · 13.7 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
quantcpp -- Compress AI's memory 3x. It gets faster.
Quick start:
from quantcpp import Model
m = Model.from_pretrained("Llama-3.2-1B")
print(m.ask("What is gravity?"))
Note: SmolLM2-135M downloads faster but produces low-quality output.
Use Llama-3.2-1B (~750 MB, one-time download) for good results.
"""
try:
from importlib.metadata import version as _pkg_version
__version__ = _pkg_version("quantcpp")
except Exception:
__version__ = "0.12.1" # fallback for editable / source-tree imports
import os
import sys
import threading
from pathlib import Path
from typing import Iterator, Optional
from quantcpp._binding import (
QuantConfig,
ON_TOKEN_CB,
get_lib,
load_model,
new_context,
free_ctx,
free_model,
version as _c_version,
)
# -----------------------------------------------------------------------
# Model registry — small GGUF models auto-downloaded from HuggingFace
# -----------------------------------------------------------------------
_CACHE_DIR = Path(os.environ.get("QUANTCPP_CACHE",
Path.home() / ".cache" / "quantcpp"))
# name → (HuggingFace repo, filename, approx size in MB)
_MODEL_REGISTRY = {
"SmolLM2-135M": (
"Felladrin/gguf-Q8_0-SmolLM2-135M-Instruct",
"smollm2-135m-instruct-q8_0.gguf",
135,
),
"Qwen3.5-0.8B": (
"unsloth/Qwen3.5-0.8B-GGUF",
"Qwen3.5-0.8B-Q4_K_M.gguf",
508,
),
"Llama-3.2-1B": (
"hugging-quants/Llama-3.2-1B-Instruct-Q4_K_M-GGUF",
"llama-3.2-1b-instruct-q4_k_m.gguf",
750,
),
}
def available_models():
"""List available model names for ``from_pretrained``."""
return sorted(_MODEL_REGISTRY.keys())
def _download_with_progress(url: str, dest: Path, desc: str) -> None:
"""Download a file with a tqdm-free progress bar (stdlib only)."""
import urllib.request
dest.parent.mkdir(parents=True, exist_ok=True)
tmp = dest.with_suffix(".part")
req = urllib.request.Request(url, headers={"User-Agent": f"quantcpp/{__version__}"})
with urllib.request.urlopen(req) as resp:
total = int(resp.headers.get("Content-Length", 0))
downloaded = 0
block = 1024 * 256 # 256 KB chunks
with open(tmp, "wb") as f:
while True:
chunk = resp.read(block)
if not chunk:
break
f.write(chunk)
downloaded += len(chunk)
if total > 0:
pct = downloaded * 100 // total
mb = downloaded / (1024 * 1024)
total_mb = total / (1024 * 1024)
bar_len = 30
filled = bar_len * downloaded // total
bar = "#" * filled + "-" * (bar_len - filled)
print(f"\r [{bar}] {pct:3d}% ({mb:.0f}/{total_mb:.0f} MB) {desc}",
end="", flush=True, file=sys.stderr)
print(file=sys.stderr)
tmp.rename(dest)
def download(name: str) -> str:
"""Download a model from HuggingFace Hub and return its local path.
Parameters
----------
name : str
Model name from the registry. Currently available:
``"SmolLM2-135M"`` (~135 MB, good for testing).
Returns
-------
str
Path to the downloaded ``.gguf`` file.
Examples
--------
>>> path = quantcpp.download("SmolLM2-135M")
>>> m = quantcpp.Model(path)
"""
if name not in _MODEL_REGISTRY:
avail = ", ".join(sorted(_MODEL_REGISTRY))
raise ValueError(
f"Unknown model {name!r}. Available: {avail}. "
"Or pass a local .gguf path to Model() directly."
)
repo, filename, _mb = _MODEL_REGISTRY[name]
dest = _CACHE_DIR / filename
if dest.is_file():
print(f" Using cached {dest}", file=sys.stderr)
return str(dest)
url = f"https://huggingface.co/{repo}/resolve/main/{filename}"
print(f" Downloading {name} (~{_mb} MB) ...", file=sys.stderr)
_download_with_progress(url, dest, name)
return str(dest)
class Model:
"""High-level Python interface to quant.cpp inference.
Parameters
----------
path : str
Path to a GGUF model file. Use ``Model.from_pretrained("SmolLM2-135M")``
to auto-download a small model for quick testing.
temperature : float
Sampling temperature (default 0.7). Use 0.0 for greedy.
top_p : float
Nucleus sampling threshold (default 0.9).
max_tokens : int
Maximum tokens per generation (default 256).
n_threads : int
CPU thread count (default 4).
kv_compress : int
KV cache compression: 0=off (default in v0.8.x).
Examples
--------
>>> m = Model.from_pretrained("SmolLM2-135M")
>>> m.ask("What is gravity?")
'Gravity is a force that attracts ...'
>>> with Model("model.gguf") as m:
... for tok in m.generate("Once upon a time"):
... print(tok, end="")
"""
@classmethod
def from_pretrained(cls, name: str, **kwargs) -> "Model":
"""Download a model and create a Model instance in one call.
Parameters
----------
name : str
Model name (e.g. ``"SmolLM2-135M"``). See ``quantcpp.download()``.
**kwargs
Forwarded to ``Model.__init__`` (temperature, max_tokens, etc.).
"""
path = download(name)
return cls(path, **kwargs)
def __init__(
self,
path: str,
*,
temperature: float = 0.7,
top_p: float = 0.9,
max_tokens: int = 256,
n_threads: int = 4,
kv_compress: int = 1,
context_length: int = 0,
progressive: bool = True,
aggressive: bool = False,
):
"""
Parameters
----------
progressive : bool
Progressive KV compression (default True). Keeps last 128
tokens' keys at FP32 while compressing the rest. Verified
on 3 models: +0% to +3% PPL improvement at 1.75 MB cost.
No reason to disable — it's strictly better.
aggressive : bool
Maximum memory savings (default False). Uses 4-bit KV with
last 512 tokens at FP32. Ideal for very long context.
At 128K context: 4.6 GB instead of 9.2 GB KV cache.
"""
if not os.path.isfile(path):
raise FileNotFoundError(f"Model file not found: {path}")
self._path = path
self._temperature = temperature
self._top_p = top_p
self._max_tokens = max_tokens
self._n_threads = n_threads
self._context_length = context_length
self._progressive = progressive
self._aggressive = aggressive
if aggressive:
# 4-bit KV + 512-token FP32 window: best memory/quality ratio.
# Measured: same PPL as flat 4-bit, attention-aware precision.
# TODO: add uniform_2b (kv_compress=3) for 48% more savings.
k_win = 512
elif progressive:
k_win = 128
else:
k_win = 0
self._kv_compress = kv_compress
self._model = load_model(path)
self._ctx = new_context(
self._model,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
n_threads=n_threads,
kv_compress=kv_compress,
context_length=context_length,
k_highres_window=k_win,
)
self._chat = True # auto-wrap with chat template for instruct models
self._lock = threading.Lock()
# -- Chat template -----------------------------------------------------
@staticmethod
def _apply_chat_template(prompt: str) -> str:
"""Wrap a user prompt with a generic ChatML-style template.
Works with SmolLM2, Llama 3.x Instruct, and most HuggingFace
instruct models that use the ``<|im_start|>`` / ``<|begin_of_text|>``
token convention. Simpler models may ignore the template tokens and
still generate correctly.
"""
return (
"<|im_start|>user\n"
f"{prompt}<|im_end|>\n"
"<|im_start|>assistant\n"
)
# -- Context manager ---------------------------------------------------
def __enter__(self):
return self
def __exit__(self, *exc):
self.close()
def __del__(self):
self.close()
# -- Core API ----------------------------------------------------------
def ask(self, prompt: str) -> str:
"""Send a prompt and return the full response as a string.
Parameters
----------
prompt : str
The input prompt / question.
Returns
-------
str
The model's complete response.
"""
self._ensure_open()
lib = get_lib()
import ctypes
import sys
if self._chat:
prompt = self._apply_chat_template(prompt)
with self._lock:
ptr = lib.quant_ask(self._ctx, prompt.encode("utf-8"))
if not ptr:
return ""
result = ctypes.cast(ptr, ctypes.c_char_p).value
text = result.decode("utf-8", errors="replace") if result else ""
# Free via the dylib's own free wrapper (added in v0.8.2). Falls back
# to a leak if the loaded library is an older single-header that
# doesn't export quant_free_string — preserves binary compat.
if hasattr(lib, "quant_free_string"):
lib.quant_free_string(ptr)
return text
def generate(self, prompt: str) -> Iterator[str]:
"""Stream tokens from a prompt. Yields token strings one at a time.
Parameters
----------
prompt : str
The input prompt.
Yields
------
str
Individual token strings as they are generated.
Examples
--------
>>> for token in m.generate("Hello"):
... print(token, end="", flush=True)
"""
self._ensure_open()
lib = get_lib()
if self._chat:
prompt = self._apply_chat_template(prompt)
tokens = []
done = threading.Event()
error_box = [None]
def _on_token(text_ptr, _user_data):
if text_ptr:
tokens.append(text_ptr.decode("utf-8", errors="replace"))
# prevent GC of the callback during generation
cb = ON_TOKEN_CB(_on_token)
def _run():
try:
with self._lock:
lib.quant_generate(
self._ctx,
prompt.encode("utf-8"),
cb,
None,
)
except Exception as e:
error_box[0] = e
finally:
done.set()
thread = threading.Thread(target=_run, daemon=True)
thread.start()
yielded = 0
while not done.is_set() or yielded < len(tokens):
if yielded < len(tokens):
yield tokens[yielded]
yielded += 1
else:
done.wait(timeout=0.01)
# Drain remaining tokens
while yielded < len(tokens):
yield tokens[yielded]
yielded += 1
if error_box[0] is not None:
raise error_box[0]
def save_context(self, path: str) -> None:
"""Save the current KV cache to disk.
Enables "read once, query forever": process a long document
once (slow prefill), save the context, then reload instantly
for follow-up questions without re-processing.
Parameters
----------
path : str
File path to write the context to (.kv extension recommended).
"""
self._ensure_open()
lib = get_lib()
rc = lib.quant_save_context(self._ctx, path.encode("utf-8"))
if rc != 0:
raise RuntimeError(f"Failed to save context to {path}")
def load_context(self, path: str) -> None:
"""Load a previously saved KV cache from disk.
Restores the exact conversation state — the model can immediately
answer follow-up questions about a previously processed document
without re-reading it.
Parameters
----------
path : str
Path to a context file saved by ``save_context``.
"""
self._ensure_open()
lib = get_lib()
rc = lib.quant_load_context(self._ctx, path.encode("utf-8"))
if rc != 0:
raise RuntimeError(f"Failed to load context from {path}")
def close(self) -> None:
"""Release model and context resources.
Safe to call multiple times. Called automatically when used
as a context manager or when garbage collected.
"""
if hasattr(self, "_ctx") and self._ctx:
free_ctx(self._ctx)
self._ctx = None
if hasattr(self, "_model") and self._model:
free_model(self._model)
self._model = None
# -- Properties --------------------------------------------------------
@property
def path(self) -> str:
"""Path to the loaded model file."""
return self._path
# -- Internals ---------------------------------------------------------
def _ensure_open(self):
if not getattr(self, "_ctx", None) or not getattr(self, "_model", None):
raise RuntimeError("Model has been closed")
def __repr__(self) -> str:
state = "open" if getattr(self, "_ctx", None) else "closed"
return f"quantcpp.Model({self._path!r}, state={state})"
def load(path: str, **kwargs) -> Model:
"""Shorthand for Model(path, **kwargs)."""
return Model(path, **kwargs)
__all__ = ["Model", "load", "download", "__version__"]