Skip to content

Commit 8f4cc2e

Browse files
committed
clena up
1 parent cc1c1dc commit 8f4cc2e

File tree

2 files changed

+145
-94
lines changed

2 files changed

+145
-94
lines changed

src/deepsparse/loggers_v2/root_logger.py

+100-48
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from typing import Any, Dict, Optional
1919

2020
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+
)
2225

2326
from .utils import import_from_registry
2427

@@ -43,7 +46,7 @@ def __init__(self, config: Dict, leaf_logger: Dict):
4346
super().__init__()
4447
self.config = config
4548
self.leaf_logger = leaf_logger
46-
self.run_args = defaultdict(lambda: defaultdict(list))
49+
self.run_args = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
4750
self.create()
4851

4952
def create(self):
@@ -53,13 +56,15 @@ def create(self):
5356
Note:
5457
5558
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: {...}
6368
},
6469
tag2: {...}
6570
}
@@ -73,58 +78,115 @@ def create(self):
7378
for logger_id in func_arg.get("uses", []):
7479
leaf_loggers.append(self.leaf_logger[logger_id])
7580

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"))
7883
)
7984

8085
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,
8293
):
8394
"""
8495
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.
8697
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
88100
2. The number of calls to the current self.log(...) must be a multiple of
89101
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
90104
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
93107
94108
:param value: Any value to log, may be dimentionally reduced by func
95109
:param log_type: String representing the root logger level
96110
: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
97113
98114
"""
99115
for tag_from_config, tag_run_args in self.run_args.items():
100116
if is_match_found(tag_from_config, tag):
101117

102-
# key: func_name, value: [(freq, leaf_loggers)]
118+
# key: func_name, value: {freq: {...}}
103119
for func_from_config, func_execute_args in tag_run_args.items():
104120

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():
107123

108124
# increment the counter
109125
self.inc(tag_from_config, func_from_config)
110126

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+
)
128190

129191

130192
class SystemLogger(RootLogger):
@@ -159,17 +221,7 @@ class MetricLogger(RootLogger):
159221
LOG_TYPE = "metric"
160222

161223
def __init__(self, config: Dict, leaf_logger):
162-
self.capture = set()
163224
super().__init__(config, leaf_logger)
164225

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)

tests/logger_v2/test_root_logger.py

+45-46
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,65 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import unittest
15+
1616
from unittest.mock import Mock
1717

1818
from deepsparse.loggers_v2.root_logger import RootLogger
1919

2020

21-
class TestRootLogger(unittest.TestCase):
22-
def test_log_method(self):
21+
def test_log_method():
2322

24-
mock_leaf_1 = Mock()
25-
mock_leaf_1.log = Mock()
23+
mock_leaf_1 = Mock()
24+
mock_leaf_1.log = Mock()
2625

27-
mock_leaf_2 = Mock()
28-
mock_leaf_2.log = Mock()
26+
mock_leaf_2 = Mock()
27+
mock_leaf_2.log = Mock()
2928

30-
mock_leaf_logger = {
31-
"logger_id_1": mock_leaf_1,
32-
"logger_id_2": mock_leaf_2,
33-
}
29+
mock_leaf_logger = {
30+
"logger_id_1": mock_leaf_1,
31+
"logger_id_2": mock_leaf_2,
32+
}
3433

35-
mock_config = {
36-
"tag1": [{"func": "identity", "freq": 2, "uses": ["logger_id_1"]}],
37-
"tag2": [
38-
{"func": "identity", "freq": 3, "uses": ["logger_id_2", "logger_id_1"]}
39-
],
40-
}
34+
mock_config = {
35+
"tag1": [{"func": "identity", "freq": 2, "uses": ["logger_id_1"]}],
36+
"tag2": [
37+
{"func": "identity", "freq": 3, "uses": ["logger_id_2", "logger_id_1"]}
38+
],
39+
}
4140

42-
root_logger = RootLogger(mock_config, mock_leaf_logger)
41+
root_logger = RootLogger(mock_config, mock_leaf_logger)
4342

44-
root_logger.log("log_value", "log_type", "tag1")
45-
assert mock_leaf_1.log.call_count == 0
43+
root_logger.log("log_value", "log_type", "tag1")
44+
assert mock_leaf_1.log.call_count == 0
4645

47-
root_logger.log("log_value", "log_type", "tag1")
48-
assert mock_leaf_1.log.call_count == 1
46+
root_logger.log("log_value", "log_type", "tag1")
47+
assert mock_leaf_1.log.call_count == 1
4948

50-
mock_leaf_logger["logger_id_1"].log.assert_called_with(
51-
value="log_value",
52-
tag="tag1",
53-
func="identity",
54-
log_type="log_type",
55-
)
49+
mock_leaf_logger["logger_id_1"].log.assert_called_with(
50+
value="log_value",
51+
tag="tag1",
52+
func="identity",
53+
log_type="log_type",
54+
)
5655

57-
root_logger.log("log_value", "log_type", "tag2")
58-
root_logger.log("log_value", "log_type", "tag2")
59-
assert mock_leaf_2.log.call_count == 0
56+
root_logger.log("log_value", "log_type", "tag2")
57+
root_logger.log("log_value", "log_type", "tag2")
58+
assert mock_leaf_2.log.call_count == 0
6059

61-
root_logger.log("log_value", "log_type", "tag2")
62-
assert mock_leaf_2.log.call_count == 1
63-
assert mock_leaf_1.log.call_count == 2
60+
root_logger.log("log_value", "log_type", "tag2")
61+
assert mock_leaf_2.log.call_count == 1
62+
assert mock_leaf_1.log.call_count == 2
6463

65-
mock_leaf_logger["logger_id_1"].log.assert_called_with(
66-
value="log_value",
67-
tag="tag2",
68-
func="identity",
69-
log_type="log_type",
70-
)
64+
mock_leaf_logger["logger_id_1"].log.assert_called_with(
65+
value="log_value",
66+
tag="tag2",
67+
func="identity",
68+
log_type="log_type",
69+
)
7170

72-
mock_leaf_logger["logger_id_2"].log.assert_called_with(
73-
value="log_value",
74-
tag="tag2",
75-
func="identity",
76-
log_type="log_type",
77-
)
71+
mock_leaf_logger["logger_id_2"].log.assert_called_with(
72+
value="log_value",
73+
tag="tag2",
74+
func="identity",
75+
log_type="log_type",
76+
)

0 commit comments

Comments
 (0)