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 f116175

Browse filesBrowse files
committed
fix: Suppress all logs when verbose=False, use hardcoded fileno's to work in colab notebooks. Closes abetlen#796 Closes abetlen#729
1 parent 945c62c commit f116175
Copy full SHA for f116175

File tree

Expand file treeCollapse file tree

2 files changed

+16
-17
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-17
lines changed

‎llama_cpp/_internals.py

Copy file name to clipboardExpand all lines: llama_cpp/_internals.py
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from .llama_types import *
1717
from .llama_grammar import LlamaGrammar
18+
from ._utils import suppress_stdout_stderr
1819

1920
import llama_cpp.llama_cpp as llama_cpp
2021

@@ -47,9 +48,10 @@ def __init__(
4748
if not os.path.exists(path_model):
4849
raise ValueError(f"Model path does not exist: {path_model}")
4950

50-
self.model = llama_cpp.llama_load_model_from_file(
51-
self.path_model.encode("utf-8"), self.params
52-
)
51+
with suppress_stdout_stderr(disable=verbose):
52+
self.model = llama_cpp.llama_load_model_from_file(
53+
self.path_model.encode("utf-8"), self.params
54+
)
5355

5456
if self.model is None:
5557
raise ValueError(f"Failed to load model from file: {path_model}")

‎llama_cpp/_utils.py

Copy file name to clipboardExpand all lines: llama_cpp/_utils.py
+11-14Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import os
22
import sys
33

4-
import sys
54
from typing import Any, Dict
65

76
# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
87
outnull_file = open(os.devnull, "w")
98
errnull_file = open(os.devnull, "w")
109

10+
STDOUT_FILENO = 1
11+
STDERR_FILENO = 2
12+
1113
class suppress_stdout_stderr(object):
1214
# NOTE: these must be "saved" here to avoid exceptions when using
1315
# this context manager inside of a __del__ method
@@ -22,12 +24,8 @@ def __enter__(self):
2224
if self.disable:
2325
return self
2426

25-
# Check if sys.stdout and sys.stderr have fileno method
26-
if not hasattr(self.sys.stdout, 'fileno') or not hasattr(self.sys.stderr, 'fileno'):
27-
return self # Return the instance without making changes
28-
29-
self.old_stdout_fileno_undup = self.sys.stdout.fileno()
30-
self.old_stderr_fileno_undup = self.sys.stderr.fileno()
27+
self.old_stdout_fileno_undup = STDOUT_FILENO
28+
self.old_stderr_fileno_undup = STDERR_FILENO
3129

3230
self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
3331
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)
@@ -47,15 +45,14 @@ def __exit__(self, *_):
4745
return
4846

4947
# Check if sys.stdout and sys.stderr have fileno method
50-
if hasattr(self.sys.stdout, 'fileno') and hasattr(self.sys.stderr, 'fileno'):
51-
self.sys.stdout = self.old_stdout
52-
self.sys.stderr = self.old_stderr
48+
self.sys.stdout = self.old_stdout
49+
self.sys.stderr = self.old_stderr
5350

54-
self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
55-
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)
51+
self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
52+
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)
5653

57-
self.os.close(self.old_stdout_fileno)
58-
self.os.close(self.old_stderr_fileno)
54+
self.os.close(self.old_stdout_fileno)
55+
self.os.close(self.old_stderr_fileno)
5956

6057

6158
class MetaSingleton(type):

0 commit comments

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