fix: Suppress all logs when verbose=False, use hardcoded fileno's to work in colab notebooks. Closes #796 Closes #729
This commit is contained in:
parent
945c62c567
commit
f116175a5a
2 changed files with 16 additions and 17 deletions
|
@ -15,6 +15,7 @@ import numpy.typing as npt
|
||||||
|
|
||||||
from .llama_types import *
|
from .llama_types import *
|
||||||
from .llama_grammar import LlamaGrammar
|
from .llama_grammar import LlamaGrammar
|
||||||
|
from ._utils import suppress_stdout_stderr
|
||||||
|
|
||||||
import llama_cpp.llama_cpp as llama_cpp
|
import llama_cpp.llama_cpp as llama_cpp
|
||||||
|
|
||||||
|
@ -47,6 +48,7 @@ class _LlamaModel:
|
||||||
if not os.path.exists(path_model):
|
if not os.path.exists(path_model):
|
||||||
raise ValueError(f"Model path does not exist: {path_model}")
|
raise ValueError(f"Model path does not exist: {path_model}")
|
||||||
|
|
||||||
|
with suppress_stdout_stderr(disable=verbose):
|
||||||
self.model = llama_cpp.llama_load_model_from_file(
|
self.model = llama_cpp.llama_load_model_from_file(
|
||||||
self.path_model.encode("utf-8"), self.params
|
self.path_model.encode("utf-8"), self.params
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import sys
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
|
# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
|
||||||
outnull_file = open(os.devnull, "w")
|
outnull_file = open(os.devnull, "w")
|
||||||
errnull_file = open(os.devnull, "w")
|
errnull_file = open(os.devnull, "w")
|
||||||
|
|
||||||
|
STDOUT_FILENO = 1
|
||||||
|
STDERR_FILENO = 2
|
||||||
|
|
||||||
class suppress_stdout_stderr(object):
|
class suppress_stdout_stderr(object):
|
||||||
# NOTE: these must be "saved" here to avoid exceptions when using
|
# NOTE: these must be "saved" here to avoid exceptions when using
|
||||||
# this context manager inside of a __del__ method
|
# this context manager inside of a __del__ method
|
||||||
|
@ -22,12 +24,8 @@ class suppress_stdout_stderr(object):
|
||||||
if self.disable:
|
if self.disable:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Check if sys.stdout and sys.stderr have fileno method
|
self.old_stdout_fileno_undup = STDOUT_FILENO
|
||||||
if not hasattr(self.sys.stdout, 'fileno') or not hasattr(self.sys.stderr, 'fileno'):
|
self.old_stderr_fileno_undup = STDERR_FILENO
|
||||||
return self # Return the instance without making changes
|
|
||||||
|
|
||||||
self.old_stdout_fileno_undup = self.sys.stdout.fileno()
|
|
||||||
self.old_stderr_fileno_undup = self.sys.stderr.fileno()
|
|
||||||
|
|
||||||
self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
|
self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
|
||||||
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)
|
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)
|
||||||
|
@ -47,7 +45,6 @@ class suppress_stdout_stderr(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check if sys.stdout and sys.stderr have fileno method
|
# Check if sys.stdout and sys.stderr have fileno method
|
||||||
if hasattr(self.sys.stdout, 'fileno') and hasattr(self.sys.stderr, 'fileno'):
|
|
||||||
self.sys.stdout = self.old_stdout
|
self.sys.stdout = self.old_stdout
|
||||||
self.sys.stderr = self.old_stderr
|
self.sys.stderr = self.old_stderr
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue