From f116175a5a7c84569c88cad231855c1e6e59ff6e Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Tue, 30 Apr 2024 15:45:34 -0400 Subject: [PATCH] fix: Suppress all logs when verbose=False, use hardcoded fileno's to work in colab notebooks. Closes #796 Closes #729 --- llama_cpp/_internals.py | 8 +++++--- llama_cpp/_utils.py | 25 +++++++++++-------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index cc3d989..d7409f6 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -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}") diff --git a/llama_cpp/_utils.py b/llama_cpp/_utils.py index 4a10647..781b265 100644 --- a/llama_cpp/_utils.py +++ b/llama_cpp/_utils.py @@ -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):