FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
llama-cpp-python/llama_cpp/_logger.py at main · etjson/llama-cpp-python · GitHub
etjson
/
llama-cpp-python
Public
forked from
JamePeng/llama-cpp-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
llama-cpp-python
/
llama_cpp
/
_logger.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
413 lines (318 loc) · 10.3 KB
Breadcrumbs
llama-cpp-python
/
llama_cpp
/
_logger.py
Copy path
File metadata and controls
413 lines (318 loc) · 10.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import
sys
import
ctypes
import
logging
from
dataclasses
import
dataclass
,
field
from
typing
import
Iterable
,
Optional
,
TextIO
,
Union
import
llama_cpp
.
_ggml
as
_ggml
import
llama_cpp
.
llama_cpp
as
llama_cpp_lib
# enum ggml_log_level {
# GGML_LOG_LEVEL_NONE = 0,
# GGML_LOG_LEVEL_INFO = 1,
# GGML_LOG_LEVEL_WARN = 2,
# GGML_LOG_LEVEL_ERROR = 3,
# GGML_LOG_LEVEL_DEBUG = 4,
# GGML_LOG_LEVEL_CONT = 5, // continue previous log
# };
GGML_LOG_LEVEL_NONE
=
0
GGML_LOG_LEVEL_INFO
=
1
GGML_LOG_LEVEL_WARN
=
2
GGML_LOG_LEVEL_ERROR
=
3
GGML_LOG_LEVEL_DEBUG
=
4
GGML_LOG_LEVEL_CONT
=
5
# common/log.h model:
#
# LOG_LEVEL_OUTPUT = 0
# LOG_LEVEL_ERROR = 1
# LOG_LEVEL_WARN = 2
# LOG_LEVEL_INFO = 3
# LOG_LEVEL_TRACE = 4
# LOG_LEVEL_DEBUG = 5
#
# Rule:
#
# event_verbosity <= verbosity_threshold => print
#
# Larger threshold means more verbose output.
#
LOG_LEVEL_OUTPUT
=
0
LOG_LEVEL_ERROR
=
1
LOG_LEVEL_WARN
=
2
LOG_LEVEL_INFO
=
3
LOG_LEVEL_TRACE
=
4
LOG_LEVEL_DEBUG
=
5
LOG_DEFAULT_LLAMA
=
LOG_LEVEL_INFO
LOG_DEFAULT_DEBUG
=
LOG_LEVEL_DEBUG
# Match the updated common_log_default_callback behavior:
# INFO -> TRACE
# CONT -> TRACE
#
# This is slightly more conservative for verbosity=3:
# if the backend emits INFO through ggml_log_callback, Python will hide it unless
# verbosity >= 4. This mirrors the current upstream default callback behavior.
GGML_LEVEL_TO_VERBOSITY
=
{
GGML_LOG_LEVEL_NONE
:
LOG_LEVEL_OUTPUT
,
GGML_LOG_LEVEL_ERROR
:
LOG_LEVEL_ERROR
,
GGML_LOG_LEVEL_WARN
:
LOG_LEVEL_WARN
,
GGML_LOG_LEVEL_INFO
:
LOG_LEVEL_TRACE
,
GGML_LOG_LEVEL_DEBUG
:
LOG_LEVEL_DEBUG
,
GGML_LOG_LEVEL_CONT
:
LOG_LEVEL_TRACE
,
# fallback only; CONT inherits previous
}
GGML_LEVEL_TO_PYTHON_LEVEL
=
{
GGML_LOG_LEVEL_NONE
:
logging
.
INFO
,
GGML_LOG_LEVEL_ERROR
:
logging
.
ERROR
,
GGML_LOG_LEVEL_WARN
:
logging
.
WARNING
,
GGML_LOG_LEVEL_INFO
:
logging
.
INFO
,
GGML_LOG_LEVEL_DEBUG
:
logging
.
DEBUG
,
GGML_LOG_LEVEL_CONT
:
logging
.
INFO
,
# fallback only; CONT inherits previous
}
# Default substring filters.
#
# These are intentionally simple substring filters instead of hard-coded
# special branches. Users can replace or clear them with set_log_filters().
DEFAULT_LOG_FILTERS
=
[
"CUDA Graph"
,
"CUDA graph"
]
VerbosityLike
=
Union
[
bool
,
int
,
str
,
None
]
logger
=
logging
.
getLogger
(
"llama-cpp-python"
)
@
dataclass
class
LoggerConfig
:
# 0=output, 1=error, 2=warn, 3=info, 4=trace, 5=debug
verbosity
:
int
=
LOG_DEFAULT_LLAMA
show_output
:
bool
=
True
stdout
:
TextIO
=
sys
.
stdout
stderr
:
TextIO
=
sys
.
stderr
# If any substring is contained in a log message, the message is dropped.
log_filters
:
list
[
str
]
=
field
(
default_factory
=
lambda
:
list
(
DEFAULT_LOG_FILTERS
))
log_filters_case_sensitive
:
bool
=
True
_config
=
LoggerConfig
()
_last_verbosity
=
LOG_LEVEL_INFO
def
_normalize_verbosity
(
value
:
VerbosityLike
,
*
,
default
:
int
=
LOG_DEFAULT_LLAMA
,
)
->
int
:
"""
Convert user input to llama.cpp-style verbosity 0..5.
Compatibility:
verbose=False -> ERROR (1)
verbose=True -> DEBUG (5)
Numeric levels:
0 = output
1 = error
2 = warn
3 = info
4 = trace
5 = debug
"""
if
value
is
None
:
return
default
if
isinstance
(
value
,
bool
):
return
LOG_LEVEL_DEBUG
if
value
else
LOG_LEVEL_ERROR
if
isinstance
(
value
,
int
):
return
max
(
LOG_LEVEL_OUTPUT
,
min
(
LOG_LEVEL_DEBUG
,
value
))
if
isinstance
(
value
,
str
):
key
=
value
.
strip
().
lower
()
aliases
=
{
"0"
:
LOG_LEVEL_OUTPUT
,
"output"
:
LOG_LEVEL_OUTPUT
,
"none"
:
LOG_LEVEL_OUTPUT
,
"1"
:
LOG_LEVEL_ERROR
,
"error"
:
LOG_LEVEL_ERROR
,
"err"
:
LOG_LEVEL_ERROR
,
"silent"
:
LOG_LEVEL_ERROR
,
"2"
:
LOG_LEVEL_WARN
,
"warn"
:
LOG_LEVEL_WARN
,
"warning"
:
LOG_LEVEL_WARN
,
"quiet"
:
LOG_LEVEL_WARN
,
"3"
:
LOG_LEVEL_INFO
,
"info"
:
LOG_LEVEL_INFO
,
"default"
:
LOG_DEFAULT_LLAMA
,
"normal"
:
LOG_DEFAULT_LLAMA
,
"4"
:
LOG_LEVEL_TRACE
,
"trace"
:
LOG_LEVEL_TRACE
,
"trc"
:
LOG_LEVEL_TRACE
,
"5"
:
LOG_LEVEL_DEBUG
,
"debug"
:
LOG_LEVEL_DEBUG
,
"verbose"
:
LOG_LEVEL_DEBUG
,
}
if
key
in
aliases
:
return
aliases
[
key
]
try
:
parsed
=
int
(
key
)
except
ValueError
as
exc
:
raise
ValueError
(
"_logger._normalize_verbosity: "
"verbosity must be one of 0..5, bool, None, or "
"'silent'/'quiet'/'info'/'trace'/'debug'"
)
from
exc
return
max
(
LOG_LEVEL_OUTPUT
,
min
(
LOG_LEVEL_DEBUG
,
parsed
))
raise
TypeError
(
f"_logger._normalize_verbosity: unsupported verbosity type:
{
type
(
value
)!r
}
"
)
def
_verbosity_to_python_level
(
verbosity
:
int
)
->
int
:
if
verbosity
>=
LOG_LEVEL_DEBUG
:
return
logging
.
DEBUG
if
verbosity
>=
LOG_LEVEL_INFO
:
return
logging
.
INFO
if
verbosity
>=
LOG_LEVEL_WARN
:
return
logging
.
WARNING
return
logging
.
ERROR
def
_get_verbosity
(
level
:
int
)
->
int
:
"""
Map ggml log level to Python-side verbosity.
GGML_LOG_LEVEL_INFO maps to LOG_LEVEL_INFO so that verbosity=3 remains
useful as the default info level.
"""
if
level
==
GGML_LOG_LEVEL_NONE
:
return
LOG_LEVEL_OUTPUT
if
level
==
GGML_LOG_LEVEL_ERROR
:
return
LOG_LEVEL_ERROR
if
level
==
GGML_LOG_LEVEL_WARN
:
return
LOG_LEVEL_WARN
if
level
==
GGML_LOG_LEVEL_INFO
:
return
LOG_LEVEL_INFO
if
level
==
GGML_LOG_LEVEL_DEBUG
:
return
LOG_LEVEL_DEBUG
if
level
==
GGML_LOG_LEVEL_CONT
:
return
LOG_LEVEL_INFO
return
LOG_LEVEL_DEBUG
def
_decode_log_text
(
text
:
bytes
)
->
str
:
return
text
.
decode
(
"utf-8"
,
errors
=
"replace"
)
def
_matches_log_filter
(
msg
:
str
)
->
bool
:
filters
=
_config
.
log_filters
if
not
filters
:
return
False
if
_config
.
log_filters_case_sensitive
:
return
any
(
item
and
item
in
msg
for
item
in
filters
)
msg_lower
=
msg
.
lower
()
return
any
(
item
and
item
.
lower
()
in
msg_lower
for
item
in
filters
)
def
_should_drop
(
level
:
int
,
verbosity
:
int
,
msg
:
str
)
->
bool
:
if
verbosity
>
_config
.
verbosity
:
return
True
if
level
==
GGML_LOG_LEVEL_NONE
and
not
_config
.
show_output
:
return
True
if
_matches_log_filter
(
msg
):
return
True
return
False
@
_ggml
.
ggml_log_callback
def
ggml_log_callback
(
level
:
int
,
text
:
bytes
,
user_data
:
ctypes
.
c_void_p
,
):
global
_last_verbosity
msg
=
_decode_log_text
(
text
)
if
level
==
GGML_LOG_LEVEL_CONT
:
verbosity
=
_last_verbosity
else
:
verbosity
=
_get_verbosity
(
level
)
_last_verbosity
=
verbosity
if
_should_drop
(
level
,
verbosity
,
msg
):
return
out
=
_config
.
stdout
if
level
==
GGML_LOG_LEVEL_NONE
else
_config
.
stderr
print
(
msg
,
end
=
""
,
flush
=
True
,
file
=
out
)
# Keep a global reference to avoid ctypes callback being garbage-collected.
_ggml_log_callback_ref
=
ggml_log_callback
llama_cpp_lib
.
llama_log_set
(
_ggml_log_callback_ref
,
ctypes
.
c_void_p
(
0
))
def
configure_logging
(
*
,
verbosity
:
VerbosityLike
=
None
,
verbose
:
Optional
[
bool
]
=
None
,
quiet
:
Optional
[
bool
]
=
None
,
silent
:
Optional
[
bool
]
=
None
,
show_output
:
Optional
[
bool
]
=
None
,
log_filters
:
Optional
[
Iterable
[
str
]]
=
None
,
append_log_filters
:
Optional
[
Iterable
[
str
]]
=
None
,
log_filters_case_sensitive
:
Optional
[
bool
]
=
None
,
):
"""
Configure native ggml/llama.cpp runtime logging.
Priority:
silent > quiet > verbosity > verbose > current config
Compatibility:
verbose=False -> ERROR
verbose=True -> DEBUG
Numeric levels:
0 = output
1 = error
2 = warn
3 = info
4 = trace
5 = debug
"""
if
silent
is
True
:
v
=
LOG_LEVEL_ERROR
elif
quiet
is
True
:
v
=
LOG_LEVEL_WARN
elif
verbosity
is
not
None
:
v
=
_normalize_verbosity
(
verbosity
)
elif
verbose
is
not
None
:
v
=
_normalize_verbosity
(
verbose
)
else
:
v
=
_config
.
verbosity
_config
.
verbosity
=
v
logger
.
setLevel
(
_verbosity_to_python_level
(
v
))
if
show_output
is
not
None
:
_config
.
show_output
=
show_output
if
log_filters
is
not
None
:
_config
.
log_filters
=
[
s
for
s
in
log_filters
if
s
]
if
append_log_filters
is
not
None
:
_config
.
log_filters
.
extend
(
s
for
s
in
append_log_filters
if
s
)
if
log_filters_case_sensitive
is
not
None
:
_config
.
log_filters_case_sensitive
=
log_filters_case_sensitive
def
set_verbose
(
verbose
:
bool
):
"""
Backward-compatible bool API.
False -> ERROR
True -> DEBUG
"""
configure_logging
(
verbose
=
verbose
)
def
set_verbosity
(
verbosity
:
VerbosityLike
):
configure_logging
(
verbosity
=
verbosity
)
def
get_verbosity
()
->
int
:
return
_config
.
verbosity
def
set_quiet
(
quiet
:
bool
=
True
):
configure_logging
(
quiet
=
quiet
)
def
set_silent
(
silent
:
bool
=
True
):
configure_logging
(
silent
=
silent
)
def
set_log_filters
(
filters
:
Iterable
[
str
],
*
,
case_sensitive
:
bool
=
True
,
):
"""
Replace all substring log filters.
Example:
set_log_filters(["CUDA Graph id", "clip_model_loader: tensor"])
"""
configure_logging
(
log_filters
=
filters
,
log_filters_case_sensitive
=
case_sensitive
,
)
def
get_log_filters
()
->
list
[
str
]:
return
list
(
_config
.
log_filters
)
def
add_log_filters
(
filters
:
Iterable
[
str
]):
"""
Append substring log filters.
"""
configure_logging
(
append_log_filters
=
filters
)
def
clear_log_filters
():
"""
Clear all substring log filters, including default filters.
"""
_config
.
log_filters
.
clear
()
def
reset_log_filters
():
"""
Restore default substring log filters.
"""
_config
.
log_filters
=
list
(
DEFAULT_LOG_FILTERS
)
def
get_log_filters_case_sensitive
()
->
bool
:
return
_config
.
log_filters_case_sensitive
def
reset_logging
():
"""
Reset logging to default llama.cpp-style INFO verbosity and default filters.
"""
_config
.
verbosity
=
LOG_DEFAULT_LLAMA
_config
.
show_output
=
True
_config
.
log_filters
=
list
(
DEFAULT_LOG_FILTERS
)
_config
.
log_filters_case_sensitive
=
True
logger
.
setLevel
(
_verbosity_to_python_level
(
_config
.
verbosity
))
Back
|
FazBrowse Home
|
New Git URL