From aa71741db8165ea8f2060819be789bc60449cf68 Mon Sep 17 00:00:00 2001 From: Rahul Tuli Date: Fri, 26 Apr 2024 16:11:49 +0000 Subject: [PATCH] Style and some more changes --- src/deepsparse/evaluation/results.py | 2 +- src/deepsparse/loggers/config.py | 2 -- src/deepsparse/loggers/logger_manager.py | 2 +- src/deepsparse/loggers/root_logger.py | 2 +- src/deepsparse/pipelines/numpy_schemas.py | 6 +----- src/deepsparse/server/cli.py | 2 +- src/deepsparse/server/config_hot_reloading.py | 6 +++--- src/deepsparse/server/helpers.py | 2 +- src/deepsparse/server/server.py | 2 +- src/deepsparse/transformers/haystack/helpers.py | 5 +++-- src/deepsparse/transformers/pipelines/chat/chat.py | 2 -- src/deepsparse/transformers/pipelines_cli.py | 2 +- src/deepsparse/yolo/schemas.py | 4 ++-- tests/deepsparse/loggers/test_config.py | 2 +- tests/deepsparse/utils/test_data.py | 6 +++--- 15 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/deepsparse/evaluation/results.py b/src/deepsparse/evaluation/results.py index dd45ff2a2a..e045098863 100644 --- a/src/deepsparse/evaluation/results.py +++ b/src/deepsparse/evaluation/results.py @@ -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): diff --git a/src/deepsparse/loggers/config.py b/src/deepsparse/loggers/config.py index 86078e7b7d..74f7b7d9db 100644 --- a/src/deepsparse/loggers/config.py +++ b/src/deepsparse/loggers/config.py @@ -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: diff --git a/src/deepsparse/loggers/logger_manager.py b/src/deepsparse/loggers/logger_manager.py index adfad3a39a..0b3528c9fd 100644 --- a/src/deepsparse/loggers/logger_manager.py +++ b/src/deepsparse/loggers/logger_manager.py @@ -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( diff --git a/src/deepsparse/loggers/root_logger.py b/src/deepsparse/loggers/root_logger.py index f739b13aeb..93352ce3cf 100644 --- a/src/deepsparse/loggers/root_logger.py +++ b/src/deepsparse/loggers/root_logger.py @@ -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 """ diff --git a/src/deepsparse/pipelines/numpy_schemas.py b/src/deepsparse/pipelines/numpy_schemas.py index 6e29b1b106..fd9d2bbadd 100644 --- a/src/deepsparse/pipelines/numpy_schemas.py +++ b/src/deepsparse/pipelines/numpy_schemas.py @@ -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") @@ -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 @@ -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 diff --git a/src/deepsparse/server/cli.py b/src/deepsparse/server/cli.py index d402f616fd..37bc92353d 100644 --- a/src/deepsparse/server/cli.py +++ b/src/deepsparse/server/cli.py @@ -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( diff --git a/src/deepsparse/server/config_hot_reloading.py b/src/deepsparse/server/config_hot_reloading.py index b419b8cfed..f017f2a0d4 100644 --- a/src/deepsparse/server/config_hot_reloading.py +++ b/src/deepsparse/server/config_hot_reloading.py @@ -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 @@ -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 diff --git a/src/deepsparse/server/helpers.py b/src/deepsparse/server/helpers.py index 5c5103d039..0bbed5ed78 100644 --- a/src/deepsparse/server/helpers.py +++ b/src/deepsparse/server/helpers.py @@ -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, ) diff --git a/src/deepsparse/server/server.py b/src/deepsparse/server/server.py index 12757466de..0c52efc508 100644 --- a/src/deepsparse/server/server.py +++ b/src/deepsparse/server/server.py @@ -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 diff --git a/src/deepsparse/transformers/haystack/helpers.py b/src/deepsparse/transformers/haystack/helpers.py index d17890ed48..825804789e 100644 --- a/src/deepsparse/transformers/haystack/helpers.py +++ b/src/deepsparse/transformers/haystack/helpers.py @@ -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()) diff --git a/src/deepsparse/transformers/pipelines/chat/chat.py b/src/deepsparse/transformers/pipelines/chat/chat.py index b7c8658721..7a24bf070d 100644 --- a/src/deepsparse/transformers/pipelines/chat/chat.py +++ b/src/deepsparse/transformers/pipelines/chat/chat.py @@ -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 diff --git a/src/deepsparse/transformers/pipelines_cli.py b/src/deepsparse/transformers/pipelines_cli.py index 887af4c594..0e639f568b 100644 --- a/src/deepsparse/transformers/pipelines_cli.py +++ b/src/deepsparse/transformers/pipelines_cli.py @@ -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) diff --git a/src/deepsparse/yolo/schemas.py b/src/deepsparse/yolo/schemas.py index e47f43d9a1..3a256ca010 100644 --- a/src/deepsparse/yolo/schemas.py +++ b/src/deepsparse/yolo/schemas.py @@ -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 @@ -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( diff --git a/tests/deepsparse/loggers/test_config.py b/tests/deepsparse/loggers/test_config.py index ebf6b8a89a..37c0b93cc2 100644 --- a/tests/deepsparse/loggers/test_config.py +++ b/tests/deepsparse/loggers/test_config.py @@ -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 diff --git a/tests/deepsparse/utils/test_data.py b/tests/deepsparse/utils/test_data.py index b38012dd35..d601e267bb 100644 --- a/tests/deepsparse/utils/test_data.py +++ b/tests/deepsparse/utils/test_data.py @@ -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]) @@ -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)