Skip to content

Commit

Permalink
Merge branch 'main' into rename-llm-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
horheynm authored Aug 23, 2023
2 parents 7664eba + 2cf112a commit 97532b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/deepsparse/server/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

import numpy
from pydantic import BaseModel

from deepsparse import (
BaseLogger,
Expand All @@ -24,7 +27,7 @@
from deepsparse.server.config import EndpointConfig, ServerConfig


__all__ = ["server_logger_from_config"]
__all__ = ["server_logger_from_config", "prep_outputs_for_serialization"]


def server_logger_from_config(config: ServerConfig) -> BaseLogger:
Expand Down Expand Up @@ -58,6 +61,26 @@ def server_logger_from_config(config: ServerConfig) -> BaseLogger:
)


def prep_outputs_for_serialization(pipeline_outputs: Any):
"""
Prepares a pipeline output for JSON serialization by converting any numpy array
field to a list. For large numpy arrays, this operation will take a while to run.
:param pipeline_outputs: output data to clean
:return: cleaned pipeline_outputs
"""
if isinstance(pipeline_outputs, BaseModel):
for field_name in pipeline_outputs.__fields__.keys():
field_value = getattr(pipeline_outputs, field_name)
if isinstance(field_value, numpy.ndarray):
# numpy arrays aren't JSON serializable
setattr(pipeline_outputs, field_name, field_value.tolist())
elif isinstance(pipeline_outputs, numpy.ndarray):
pipeline_outputs = pipeline_outputs.tolist()

return pipeline_outputs


def _extract_system_logging_from_endpoints(
endpoints: List[EndpointConfig],
) -> Dict[str, SystemLoggingGroup]:
Expand Down
6 changes: 5 additions & 1 deletion src/deepsparse/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
SystemLoggingConfig,
)
from deepsparse.server.config_hot_reloading import start_config_watcher
from deepsparse.server.helpers import server_logger_from_config
from deepsparse.server.helpers import (
prep_outputs_for_serialization,
server_logger_from_config,
)
from deepsparse.server.system_logging import (
SystemLoggingMiddleware,
log_system_information,
Expand Down Expand Up @@ -261,6 +264,7 @@ def _predict(request: pipeline.input_schema):
server_logger=server_logger,
system_logging_config=system_logging_config,
)
pipeline_outputs = prep_outputs_for_serialization(pipeline_outputs)
return pipeline_outputs

def _predict_from_files(request: List[UploadFile]):
Expand Down

0 comments on commit 97532b1

Please sign in to comment.