-
Notifications
You must be signed in to change notification settings - Fork 187
[V2 Loggers] config file #1533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[V2 Loggers] config file #1533
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from typing import Dict, List, Optional | ||
|
||
import yaml | ||
from pydantic import BaseModel, Extra, Field, validator | ||
|
||
|
||
class LoggerConfig(BaseModel): | ||
class Config: | ||
extra = Extra.allow | ||
|
||
name: str = Field( | ||
default="PythonLogger", | ||
description=( | ||
"Path (/path/to/file:FooLogger) or name of loggers in " | ||
"deepsparse/loggers/registry/__init__ path" | ||
), | ||
) | ||
handler: Optional[Dict] = None | ||
|
||
|
||
class TargetConfig(BaseModel): | ||
func: str = Field( | ||
default="identity", | ||
description=( | ||
( | ||
"Callable to apply to 'value' for dimensionality reduction. " | ||
"func can be a path /path/to/file:func) or name of func in " | ||
"deepsparse/loggers/registry/__init__ path" | ||
) | ||
), | ||
) | ||
|
||
freq: int = Field( | ||
default=1, | ||
description="The rate to log. Log every N occurances", | ||
) | ||
uses: List[str] = Field(default=["default"], description="") | ||
|
||
|
||
class MetricTargetConfig(TargetConfig): | ||
capture: List[str] = Field( | ||
[".*"], | ||
horheynm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description=( | ||
"Key of the output dict. Corresponding value will be logged. " | ||
"The value can be a regex pattern" | ||
), | ||
) | ||
|
||
|
||
class LoggingConfig(BaseModel): | ||
|
||
version: int = Field( | ||
default=2, | ||
horheynm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description="Pipeline logger version", | ||
) | ||
|
||
logger: Dict[str, LoggerConfig] = Field( | ||
horheynm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default=dict(default=LoggerConfig()), | ||
description="Loggers to be Used", | ||
) | ||
|
||
system: Dict[str, List[TargetConfig]] = Field( | ||
default={".*": [TargetConfig()]}, | ||
description="Default python logging module logger", | ||
) | ||
|
||
performance: Dict[str, List[TargetConfig]] = Field( | ||
default={"cpu": [TargetConfig()]}, | ||
description="Performance level config", | ||
) | ||
|
||
metric: Dict[str, List[MetricTargetConfig]] = Field( | ||
default={r"(?i)operator": [MetricTargetConfig()]}, | ||
description="Metric level config", | ||
) | ||
|
||
@validator("logger", always=True) | ||
def always_include_python_logger(cls, value): | ||
if "default" not in value: | ||
value["default"] = LoggerConfig() | ||
return value | ||
|
||
@classmethod | ||
def from_yaml(cls, yaml_path: str): | ||
"""Load from yaml file""" | ||
with open(yaml_path, "r") as file: | ||
yaml_content = yaml.safe_load(file) | ||
return cls(**yaml_content) | ||
|
||
@classmethod | ||
def from_str(cls, stringified_yaml: str): | ||
"""Load from stringified yaml""" | ||
yaml_content = yaml.safe_load(stringified_yaml) | ||
|
||
return cls(**yaml_content) | ||
|
||
@classmethod | ||
def from_config(cls, config: Optional[str] = None): | ||
# """Helper to load from file or string""" | ||
if config: | ||
if config.endswith(".yaml"): | ||
return cls.from_yaml(config) | ||
return cls.from_str(config) | ||
return LoggingConfig() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
|
||
import yaml | ||
|
||
from deepsparse.loggers_v2.config import LoggerConfig, LoggingConfig | ||
|
||
|
||
def test_config_generates_default_json(): | ||
"""Check the default LoggingConfig""" | ||
|
||
expected_config = """ | ||
version: 2 | ||
logger: | ||
default: | ||
name: PythonLogger | ||
handler: null | ||
system: | ||
".*": | ||
- func: identity | ||
freq: 1 | ||
uses: | ||
- default | ||
performance: | ||
cpu: | ||
- func: identity | ||
freq: 1 | ||
uses: | ||
- default | ||
metric: | ||
"(?i)operator": | ||
- func: identity | ||
freq: 1 | ||
uses: | ||
- default | ||
capture: | ||
- .* | ||
""" | ||
expected_dict = yaml.safe_load(expected_config) | ||
default_dict = LoggingConfig().dict() | ||
assert expected_dict == default_dict | ||
|
||
|
||
def test_logger_config_accepts_kwargs(): | ||
expected_config = """ | ||
name: PythonLogger | ||
foo: 1 | ||
bar: "2024" | ||
baz: | ||
one: 1 | ||
two: 2 | ||
boston: | ||
- one | ||
- two | ||
""" | ||
config = LoggerConfig(**yaml.safe_load(expected_config)).dict() | ||
|
||
assert config["name"] == "PythonLogger" | ||
assert config["handler"] is None | ||
assert config["baz"] == dict(one=1, two=2) | ||
assert config["foo"] == 1 | ||
assert config["boston"] == ["one", "two"] | ||
assert config["bar"] == "2024" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.