Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce57920

Browse filesBrowse files
committed
Suppress llama.cpp output when loading model.
1 parent a9b9f03 commit ce57920
Copy full SHA for ce57920

File tree

Expand file treeCollapse file tree

2 files changed

+57
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+57
-4
lines changed

‎llama_cpp/llama.py

Copy file name to clipboardExpand all lines: llama_cpp/llama.py
+19-4Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import numpy as np
2828
import numpy.typing as npt
2929

30+
from .utils import suppress_stdout_stderr
31+
3032
class BaseLlamaCache(ABC):
3133
"""Base cache class for a llama.cpp model."""
3234

@@ -308,12 +310,25 @@ def __init__(
308310
if not os.path.exists(model_path):
309311
raise ValueError(f"Model path does not exist: {model_path}")
310312

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+
)
314322
assert self.model is not None
315323

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+
)
317332

318333
assert self.ctx is not None
319334

‎llama_cpp/utils.py

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.