Skip to content

Commit

Permalink
Style and some more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-tuli committed Apr 26, 2024
1 parent ec16222 commit aa71741
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/deepsparse/evaluation/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _save_to_json(result: Result, save_path: str):


def _save_to_yaml(result: Result, save_path: str):
_save(yaml.dump(result.dict()), save_path, expected_ext=".yaml")
_save(yaml.dump(result.model_dump()), save_path, expected_ext=".yaml")


def _save(data: str, save_path: str, expected_ext: str):
Expand Down
2 changes: 0 additions & 2 deletions src/deepsparse/loggers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ class LoggingConfig(BaseModel):
description="Metric level config",
)

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("loggers", always=True)
def always_include_python_logger(cls, value):
if "default" not in value:
Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/loggers/logger_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LoggerManager(AsyncExecutor, LoggerFactory):
"""

def __init__(self, config: str = ""):
self.config = LoggingConfig.from_config(config).dict()
self.config = LoggingConfig.from_config(config).model_dump()
super().__init__(config=self.config)

def log(
Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/loggers/root_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RootLogger(FrequencyFilter):
its own FrequencyFilter
:param config: config with respect to
the log_type (LoggerConfig().dict().get(log_type))
the log_type (LoggerConfig().model_dump().get(log_type))
:param leaf_logger: leaf logger singleton shared among other RootLogger
"""
Expand Down
6 changes: 1 addition & 5 deletions src/deepsparse/pipelines/numpy_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Generic, Type, TypeVar

import numpy
from pydantic.fields import ModelField
from pydantic.v1.fields import ModelField


Dtype = TypeVar("Dtype")
Expand All @@ -39,8 +39,6 @@ class Model(BaseModel):
"""

@classmethod
# TODO[pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __get_validators__(cls):
yield cls.validate

Expand Down Expand Up @@ -78,8 +76,6 @@ class _NumpyDtypeValidator:
expected: Type[numpy.dtype]

@classmethod
# TODO[pydantic]: We couldn't refactor `__get_validators__`, please create the `__get_pydantic_core_schema__` manually.
# Check https://docs.pydantic.dev/latest/migration/#defining-custom-types for more information.
def __get_validators__(cls):
yield cls.validate

Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def main(
with TemporaryDirectory() as tmp_dir:
config_path = os.path.join(tmp_dir, "server-config.yaml")
with open(config_path, "w") as fp:
yaml.dump(cfg.dict(), fp)
yaml.dump(cfg.model_dump(), fp)

server = _fetch_server(integration=integration, config=config_path)
server.start_server(
Expand Down
6 changes: 3 additions & 3 deletions src/deepsparse/server/config_hot_reloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _diff_generator(
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
with open(old_path, "w") as fp:
fp.write(f"# Version {version} saved at {timestamp} by deepsparse.server\n")
yaml.safe_dump(old_config.dict(), fp)
yaml.safe_dump(old_config.model_dump(), fp)
_LOGGER.info(f"Saved old version of config to {old_path}")
version += 1

Expand Down Expand Up @@ -147,10 +147,10 @@ def _update_endpoints(

for endpoint in removed:
_LOGGER.info(f"Requesting removal of endpoint '{endpoint.route}'")
requests.delete(url, json=endpoint.dict()).raise_for_status()
requests.delete(url, json=endpoint.model_dump()).raise_for_status()

for endpoint in added:
_LOGGER.info(f"Requesting addition of endpoint '{endpoint.route}'")
requests.post(url, json=endpoint.dict()).raise_for_status()
requests.post(url, json=endpoint.model_dump()).raise_for_status()

return added, removed
2 changes: 1 addition & 1 deletion src/deepsparse/server/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

def create_error_response(status_code: HTTPStatus, message: str) -> JSONResponse:
return JSONResponse(
ErrorResponse(message=message, type="invalid_request_error").dict(),
ErrorResponse(message=message, type="invalid_request_error").model_dump(),
status_code=status_code.value,
)

Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async def benchmark(
json_params = await raw_request.json()
benchmark_config = PipelineBenchmarkConfig(**json_params)
results = benchmark_from_pipeline(
pipeline=proxy_pipeline.pipeline, **benchmark_config.dict()
pipeline=proxy_pipeline.pipeline, **benchmark_config.model_dump()
)

return results
Expand Down
5 changes: 3 additions & 2 deletions src/deepsparse/transformers/haystack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def print_pipeline_documents(
if isinstance(haystack_pipeline_output.query, list):
for i in range(len(haystack_pipeline_output.query)):
results_dict = {
key: value[i] for key, value in haystack_pipeline_output.dict().items()
key: value[i]
for key, value in haystack_pipeline_output.model_dump().items()
}
print_documents(results_dict)
else:
print_documents(haystack_pipeline_output.dict())
print_documents(haystack_pipeline_output.model_dump())
2 changes: 0 additions & 2 deletions src/deepsparse/transformers/pipelines/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class ChatInput(TextGenerationInput):
"will be set to a random uuid.",
)

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("session_ids")
def validate_session_ids(cls, value, values) -> Union[None, List[str]]:
# make sure that the that format session_ids confirms with the
Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/transformers/pipelines_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def response_to_json(response: Any):
elif isinstance(response, dict):
return {key: response_to_json(val) for key, val in response.items()}
elif isinstance(response, BaseModel):
return response.dict()
return response.model_dump()
return json.dumps(response)


Expand Down
4 changes: 2 additions & 2 deletions src/deepsparse/yolo/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

from collections import namedtuple
from typing import Any, Iterable, List, Optional, TextIO
from typing import Any, Iterable, List, Optional, TextIO, Union

import numpy
from PIL import Image
Expand Down Expand Up @@ -106,7 +106,7 @@ class YOLOOutput(BaseModel):
scores: List[List[float]] = Field(
description="List of scores, one for each prediction"
)
labels: List[List[str]] = Field(
labels: List[Union[List[str], List[float]]] = Field(
description="List of labels, one for each prediction"
)
intermediate_outputs: Optional[Any] = Field(
Expand Down
2 changes: 1 addition & 1 deletion tests/deepsparse/loggers/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_config_generates_default_json():
"""
expected_dict = yaml.safe_load(expected_config)
default_dict = LoggingConfig().dict()
default_dict = LoggingConfig().model_dump()
assert expected_dict == default_dict


Expand Down
6 changes: 3 additions & 3 deletions tests/deepsparse/utils/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_prep_for_serialization(unserialized_output, target_serialized_output):
def check_dict_equality(dict_1, dict_2):
for key, value in dict_1.items():
if isinstance(value, BaseModel):
value = value.dict()
value = value.model_dump()
check_dict_equality(value, dict_2[key].dict())
elif isinstance(value, dict):
check_dict_equality(value, dict_2[key])
Expand All @@ -97,6 +97,6 @@ def check_dict_equality(dict_1, dict_2):
assert value == dict_2[key]

serialized_output = prep_for_serialization(unserialized_output)
serialized_output = serialized_output.dict()
target_serialized_output = target_serialized_output.dict()
serialized_output = serialized_output.model_dump()
target_serialized_output = target_serialized_output.model_dump()
check_dict_equality(target_serialized_output, serialized_output)

0 comments on commit aa71741

Please sign in to comment.