18
18
from typing import Any , Dict , Optional
19
19
20
20
from deepsparse .loggers_v2 .filters .frequency_filter import FrequencyFilter
21
- from deepsparse .loggers_v2 .filters .pattern import is_match_found
21
+ from deepsparse .loggers_v2 .filters .pattern import (
22
+ is_match_found ,
23
+ unravel_value_as_generator ,
24
+ )
22
25
23
26
from .utils import import_from_registry
24
27
@@ -43,7 +46,7 @@ def __init__(self, config: Dict, leaf_logger: Dict):
43
46
super ().__init__ ()
44
47
self .config = config
45
48
self .leaf_logger = leaf_logger
46
- self .run_args = defaultdict (lambda : defaultdict (list ))
49
+ self .run_args = defaultdict (lambda : defaultdict (lambda : defaultdict ( list ) ))
47
50
self .create ()
48
51
49
52
def create (self ):
@@ -53,13 +56,15 @@ def create(self):
53
56
Note:
54
57
55
58
self.run_args = {
56
- tag: { # tag from config
57
- func: [ # func from config
58
- (freq, leaf_loggers), # freq from config
59
- (freq, leaf_loggers),
60
- (freq, leaf_loggers),
61
- ],
62
- func2: [...]
59
+ tag: {
60
+ func: {
61
+ freq: [
62
+ ([loggers], [capture]),
63
+ ([loggers2], [capture2]),
64
+ ...
65
+ ]
66
+ },
67
+ func2: {...}
63
68
},
64
69
tag2: {...}
65
70
}
@@ -73,58 +78,115 @@ def create(self):
73
78
for logger_id in func_arg .get ("uses" , []):
74
79
leaf_loggers .append (self .leaf_logger [logger_id ])
75
80
76
- self .run_args [tag ][func ].append (
77
- (func_arg .get ("freq" , 1 ), leaf_loggers ),
81
+ self .run_args [tag ][func ][ func_arg . get ( "freq" , 1 )] .append (
82
+ (leaf_loggers , func_arg .get ("capture" ))
78
83
)
79
84
80
85
def log (
81
- self , value : Any , log_type : str , tag : Optional [str ] = None , * args , ** kwargs
86
+ self ,
87
+ value : Any ,
88
+ log_type : str ,
89
+ tag : str ,
90
+ capture : Optional [str ] = None ,
91
+ * args ,
92
+ ** kwargs ,
82
93
):
83
94
"""
84
95
Send args to the leaf loggers if the given tag, func, freq are accpeted. Need to
85
- pass two filters to be accepted.
96
+ pass three filters to be accepted.
86
97
87
- 1. The inputted arg must be found in the config file
98
+ 1. The provided tag must be a subset or regex match with the tags in the
99
+ root logger config file
88
100
2. The number of calls to the current self.log(...) must be a multiple of
89
101
freq from the config file wrt tag and func
102
+ 3. If capture is speficied in the config file (only for metric log), it must be
103
+ a subset or have a regex match
90
104
91
- If accepted, log to leaf loggers wrt the leaf loggers that was configured with
92
- tag and func
105
+ If accepted, value=func(value) if func is provided, and pass this value to the
106
+ leaf loggers
93
107
94
108
:param value: Any value to log, may be dimentionally reduced by func
95
109
:param log_type: String representing the root logger level
96
110
:param tag: Candidate id that will be used to filter out only the wanted log
111
+ :param capture: The property or dict key to record if match exists. If set to None
112
+ no capture filter will be applied even if set in config
97
113
98
114
"""
99
115
for tag_from_config , tag_run_args in self .run_args .items ():
100
116
if is_match_found (tag_from_config , tag ):
101
117
102
- # key: func_name, value: [( freq, leaf_loggers)]
118
+ # key: func_name, value: { freq: {...}}
103
119
for func_from_config , func_execute_args in tag_run_args .items ():
104
120
105
- for execute_arg in func_execute_args :
106
- freq_from_config , leaf_loggers = execute_arg
121
+ # key: freq, value = [ ([loggers], [capture]), ... ]
122
+ for freq_from_config , execute_args in func_execute_args . items ():
107
123
108
124
# increment the counter
109
125
self .inc (tag_from_config , func_from_config )
110
126
111
- # check if the given tag.func is a multiple of the counter
112
- if self .should_execute_on_frequency (
113
- tag_from_config , func_from_config , freq_from_config
114
- ):
115
- if func_from_config is not None :
116
- func_callable = import_from_registry (func_from_config )
117
- value = func_callable (value )
118
-
119
- for leaf_logger in leaf_loggers :
120
- leaf_logger .log (
121
- value = value ,
122
- tag = tag ,
123
- func = func_from_config ,
124
- log_type = log_type ,
125
- * args ,
126
- ** kwargs ,
127
- )
127
+ # execute_arg = ([loggers], [capture])
128
+ for execute_arg in execute_args :
129
+
130
+ leaf_loggers , captures_from_config = execute_arg
131
+
132
+ # check if the given tag.func is a multiple of the counter
133
+ if self .should_execute_on_frequency (
134
+ tag_from_config , func_from_config , freq_from_config
135
+ ):
136
+ # Capture filter (filter by class prop or dict key)
137
+ if capture is None :
138
+ self ._apply_func_and_log (
139
+ value = value ,
140
+ tag = tag ,
141
+ log_type = log_type ,
142
+ func_from_config = func_from_config ,
143
+ leaf_loggers = leaf_loggers ,
144
+ * args ,
145
+ ** kwargs ,
146
+ )
147
+
148
+ else :
149
+ for capture_from_config in captures_from_config :
150
+ if is_match_found (capture_from_config , capture ):
151
+ for (
152
+ captured ,
153
+ value ,
154
+ ) in unravel_value_as_generator (value ):
155
+
156
+ self ._apply_func_and_log (
157
+ value = value ,
158
+ tag = tag ,
159
+ log_type = log_type ,
160
+ func_from_config = func_from_config ,
161
+ leaf_loggers = leaf_loggers ,
162
+ capture = captured ,
163
+ * args ,
164
+ ** kwargs ,
165
+ )
166
+
167
+ def _apply_func_and_log (
168
+ self ,
169
+ value : Any ,
170
+ log_type : str ,
171
+ tag : str ,
172
+ func_from_config : str ,
173
+ leaf_loggers : list ,
174
+ * args ,
175
+ ** kwargs ,
176
+ ):
177
+ if func_from_config is not None :
178
+ func_callable = import_from_registry (func_from_config )
179
+ value = func_callable (value )
180
+
181
+ for leaf_logger in leaf_loggers :
182
+ leaf_logger .log (
183
+ value = value ,
184
+ tag = tag ,
185
+ func = func_from_config ,
186
+ log_type = log_type ,
187
+ * args ,
188
+ ** kwargs ,
189
+ )
128
190
129
191
130
192
class SystemLogger (RootLogger ):
@@ -159,17 +221,7 @@ class MetricLogger(RootLogger):
159
221
LOG_TYPE = "metric"
160
222
161
223
def __init__ (self , config : Dict , leaf_logger ):
162
- self .capture = set ()
163
224
super ().__init__ (config , leaf_logger )
164
225
165
- def create (self ):
166
- super ().create ()
167
- for func_args in self .config .values ():
168
- for func_arg in func_args :
169
- for capture in func_arg ["capture" ]:
170
- self .capture .add (capture )
171
-
172
- def log (self , capture : str , * args , ** kwargs ):
173
- for pattern in self .capture :
174
- if is_match_found (pattern , capture ):
175
- super ().log (log_type = self .LOG_TYPE , capture = capture , * args , ** kwargs )
226
+ def log (self , * args , ** kwargs ):
227
+ super ().log (log_type = self .LOG_TYPE , * args , ** kwargs )
0 commit comments