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_grammar import LlamaGrammar
|
||||
from ._utils import suppress_stdout_stderr
|
||||
|
||||
import llama_cpp.llama_cpp as llama_cpp
|
||||
|
||||
|
@ -47,9 +48,10 @@ class _LlamaModel:
|
|||
if not os.path.exists(path_model):
|
||||
raise ValueError(f"Model path does not exist: {path_model}")
|
||||
|
||||
self.model = llama_cpp.llama_load_model_from_file(
|
||||
self.path_model.encode("utf-8"), self.params
|
||||
)
|
||||
with suppress_stdout_stderr(disable=verbose):
|
||||
self.model = llama_cpp.llama_load_model_from_file(
|
||||
self.path_model.encode("utf-8"), self.params
|
||||
)
|
||||
|
||||
if self.model is None:
|
||||
raise ValueError(f"Failed to load model from file: {path_model}")
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
import sys
|
||||
from typing import Any, Dict
|
||||
|
||||
# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
|
||||
outnull_file = open(os.devnull, "w")
|
||||
errnull_file = open(os.devnull, "w")
|
||||
|
||||
STDOUT_FILENO = 1
|
||||
STDERR_FILENO = 2
|
||||
|
||||
class suppress_stdout_stderr(object):
|
||||
# NOTE: these must be "saved" here to avoid exceptions when using
|
||||
# this context manager inside of a __del__ method
|
||||
|
@ -22,12 +24,8 @@ class suppress_stdout_stderr(object):
|
|||
if self.disable:
|
||||
return self
|
||||
|
||||
# Check if sys.stdout and sys.stderr have fileno method
|
||||
if not hasattr(self.sys.stdout, 'fileno') or not hasattr(self.sys.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_undup = STDOUT_FILENO
|
||||
self.old_stderr_fileno_undup = STDERR_FILENO
|
||||
|
||||
self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
|
||||
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)
|
||||
|
@ -47,15 +45,14 @@ class suppress_stdout_stderr(object):
|
|||
return
|
||||
|
||||
# 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.stderr = self.old_stderr
|
||||
self.sys.stdout = self.old_stdout
|
||||
self.sys.stderr = self.old_stderr
|
||||
|
||||
self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
|
||||
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)
|
||||
self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
|
||||
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)
|
||||
|
||||
self.os.close(self.old_stdout_fileno)
|
||||
self.os.close(self.old_stderr_fileno)
|
||||
self.os.close(self.old_stdout_fileno)
|
||||
self.os.close(self.old_stderr_fileno)
|
||||
|
||||
|
||||
class MetaSingleton(type):
|
||||
|
|
Loading…
Reference in a new issue