Commit 320a5d7
* feat: add explicit methods to free model
This commit introduces a `close` method to both `Llama` and `_LlamaModel`,
allowing users to explicitly free the model from RAM/VRAM.
The previous implementation relied on the destructor of `_LlamaModel` to free
the model. However, in Python, the timing of destructor calls is unclear—for
instance, the `del` statement does not guarantee immediate invocation of the
destructor.
This commit provides an explicit method to release the model, which works
immediately and allows the user to load another model without memory issues.
Additionally, this commit implements a context manager in the `Llama` class,
enabling the automatic closure of the `Llama` object when used with the `with`
statement.
* feat: Implement ContextManager in _LlamaModel, _LlamaContext, and _LlamaBatch
This commit enables automatic resource management by
implementing the `ContextManager` protocol in `_LlamaModel`,
`_LlamaContext`, and `_LlamaBatch`. This ensures that
resources are properly managed and released within a `with`
statement, enhancing robustness and safety in resource handling.
* feat: add ExitStack for Llama's internal class closure
This update implements ExitStack to manage and close internal
classes in Llama, enhancing efficient and safe resource
management.
* Use contextlib ExitStack and closing
* Explicitly free model when closing resources on server
---------
Co-authored-by: Andrei Betlen <abetlen@gmail.com>
1 parent dbcf64c commit 320a5d7
3 files changed
+50
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
| |||
40 | 38 | | |
41 | 39 | | |
42 | 40 | | |
43 | | - | |
44 | | - | |
| 41 | + | |
45 | 42 | | |
46 | 43 | | |
47 | 44 | | |
| |||
56 | 53 | | |
57 | 54 | | |
58 | 55 | | |
59 | | - | |
60 | | - | |
61 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
62 | 60 | | |
63 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
| |||
257 | 260 | | |
258 | 261 | | |
259 | 262 | | |
260 | | - | |
261 | | - | |
262 | 263 | | |
263 | 264 | | |
264 | 265 | | |
| |||
269 | 270 | | |
270 | 271 | | |
271 | 272 | | |
| 273 | + | |
272 | 274 | | |
273 | | - | |
274 | 275 | | |
275 | 276 | | |
276 | 277 | | |
277 | 278 | | |
278 | | - | |
279 | | - | |
280 | | - | |
| 279 | + | |
281 | 280 | | |
282 | 281 | | |
283 | 282 | | |
284 | 283 | | |
285 | | - | |
286 | | - | |
287 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
288 | 288 | | |
289 | 289 | | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
290 | 295 | | |
291 | 296 | | |
292 | 297 | | |
| |||
501 | 506 | | |
502 | 507 | | |
503 | 508 | | |
504 | | - | |
505 | | - | |
506 | 509 | | |
507 | 510 | | |
508 | 511 | | |
509 | 512 | | |
510 | 513 | | |
511 | 514 | | |
512 | 515 | | |
513 | | - | |
514 | | - | |
| 516 | + | |
515 | 517 | | |
516 | 518 | | |
517 | 519 | | |
518 | 520 | | |
519 | 521 | | |
520 | 522 | | |
521 | | - | |
522 | | - | |
523 | | - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
524 | 527 | | |
525 | 528 | | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
526 | 534 | | |
527 | 535 | | |
528 | 536 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
350 | 353 | | |
351 | 354 | | |
352 | 355 | | |
353 | | - | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
354 | 359 | | |
355 | | - | |
| 360 | + | |
356 | 361 | | |
357 | 362 | | |
358 | 363 | | |
| |||
364 | 369 | | |
365 | 370 | | |
366 | 371 | | |
367 | | - | |
| 372 | + | |
368 | 373 | | |
369 | 374 | | |
370 | 375 | | |
371 | | - | |
| 376 | + | |
372 | 377 | | |
373 | | - | |
| 378 | + | |
374 | 379 | | |
375 | 380 | | |
376 | 381 | | |
377 | 382 | | |
378 | | - | |
| 383 | + | |
379 | 384 | | |
380 | 385 | | |
381 | 386 | | |
| |||
1959 | 1964 | | |
1960 | 1965 | | |
1961 | 1966 | | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
1962 | 1971 | | |
1963 | 1972 | | |
1964 | 1973 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| |||
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| 70 | + | |
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| |||
0 commit comments