File tree Expand file tree Collapse file tree 2 files changed +57
-4
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +57
-4
lines changed
Original file line number Diff line number Diff line change 27
27
import numpy as np
28
28
import numpy .typing as npt
29
29
30
+ from .utils import suppress_stdout_stderr
31
+
30
32
class BaseLlamaCache (ABC ):
31
33
"""Base cache class for a llama.cpp model."""
32
34
@@ -308,12 +310,25 @@ def __init__(
308
310
if not os .path .exists (model_path ):
309
311
raise ValueError (f"Model path does not exist: { model_path } " )
310
312
311
- self .model = llama_cpp .llama_load_model_from_file (
312
- self .model_path .encode ("utf-8" ), self .params
313
- )
313
+ if verbose :
314
+ self .model = llama_cpp .llama_load_model_from_file (
315
+ self .model_path .encode ("utf-8" ), self .params
316
+ )
317
+ else :
318
+ with suppress_stdout_stderr ():
319
+ self .model = llama_cpp .llama_load_model_from_file (
320
+ self .model_path .encode ("utf-8" ), self .params
321
+ )
314
322
assert self .model is not None
315
323
316
- self .ctx = llama_cpp .llama_new_context_with_model (self .model , self .params )
324
+ if verbose :
325
+ self .ctx = llama_cpp .llama_new_context_with_model (self .model , self .params )
326
+ else :
327
+ with suppress_stdout_stderr ():
328
+ print ("here" )
329
+ self .ctx = llama_cpp .llama_new_context_with_model (
330
+ self .model , self .params
331
+ )
317
332
318
333
assert self .ctx is not None
319
334
Original file line number Diff line number Diff line change
1
+ import os
2
+ import sys
3
+
4
+
5
+ class suppress_stdout_stderr (object ):
6
+ # Oddly enough this works better than the contextlib version
7
+ def __enter__ (self ):
8
+ self .outnull_file = open (os .devnull , "w" )
9
+ self .errnull_file = open (os .devnull , "w" )
10
+
11
+ self .old_stdout_fileno_undup = sys .stdout .fileno ()
12
+ self .old_stderr_fileno_undup = sys .stderr .fileno ()
13
+
14
+ self .old_stdout_fileno = os .dup (sys .stdout .fileno ())
15
+ self .old_stderr_fileno = os .dup (sys .stderr .fileno ())
16
+
17
+ self .old_stdout = sys .stdout
18
+ self .old_stderr = sys .stderr
19
+
20
+ os .dup2 (self .outnull_file .fileno (), self .old_stdout_fileno_undup )
21
+ os .dup2 (self .errnull_file .fileno (), self .old_stderr_fileno_undup )
22
+
23
+ sys .stdout = self .outnull_file
24
+ sys .stderr = self .errnull_file
25
+ return self
26
+
27
+ def __exit__ (self , * _ ):
28
+ sys .stdout = self .old_stdout
29
+ sys .stderr = self .old_stderr
30
+
31
+ os .dup2 (self .old_stdout_fileno , self .old_stdout_fileno_undup )
32
+ os .dup2 (self .old_stderr_fileno , self .old_stderr_fileno_undup )
33
+
34
+ os .close (self .old_stdout_fileno )
35
+ os .close (self .old_stderr_fileno )
36
+
37
+ self .outnull_file .close ()
38
+ self .errnull_file .close ()
You can’t perform that action at this time.
0 commit comments