|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""This module is for SageMaker inference.py.""" |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | +import asyncio |
| 17 | +import os |
| 18 | +import platform |
| 19 | +import cloudpickle |
| 20 | +import logging |
| 21 | +from pathlib import Path |
| 22 | +from sagemaker.serve.validations.check_integrity import perform_integrity_check |
| 23 | + |
| 24 | +logger = LOGGER = logging.getLogger("sagemaker") |
| 25 | + |
| 26 | + |
| 27 | +def initialize_custom_orchestrator(): |
| 28 | + """Initializes the custom orchestrator.""" |
| 29 | + code_dir = os.getenv("SAGEMAKER_INFERENCE_CODE_DIRECTORY", None) |
| 30 | + serve_path = Path(code_dir).joinpath("serve.pkl") |
| 31 | + with open(str(serve_path), mode="rb") as pkl_file: |
| 32 | + return cloudpickle.load(pkl_file) |
| 33 | + |
| 34 | + |
| 35 | +def _run_preflight_diagnostics(): |
| 36 | + _py_vs_parity_check() |
| 37 | + _pickle_file_integrity_check() |
| 38 | + |
| 39 | + |
| 40 | +def _py_vs_parity_check(): |
| 41 | + container_py_vs = platform.python_version() |
| 42 | + local_py_vs = os.getenv("LOCAL_PYTHON") |
| 43 | + |
| 44 | + if not local_py_vs or container_py_vs.split(".")[1] != local_py_vs.split(".")[1]: |
| 45 | + logger.warning( |
| 46 | + f"The local python version {local_py_vs} differs from the python version " |
| 47 | + f"{container_py_vs} on the container. Please align the two to avoid unexpected behavior" |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _pickle_file_integrity_check(): |
| 52 | + with open("/opt/ml/model/code/serve.pkl", "rb") as f: |
| 53 | + buffer = f.read() |
| 54 | + |
| 55 | + metadata_path = Path("/opt/ml/model/code/metadata.json") |
| 56 | + perform_integrity_check(buffer=buffer, metadata_path=metadata_path) |
| 57 | + |
| 58 | + |
| 59 | +_run_preflight_diagnostics() |
| 60 | +custom_orchestrator, _ = initialize_custom_orchestrator() |
| 61 | + |
| 62 | + |
| 63 | +async def handler(request): |
| 64 | + """Custom service entry point function. |
| 65 | +
|
| 66 | + :param request: raw input from request |
| 67 | + :return: outputs to be send back to client |
| 68 | + """ |
| 69 | + if asyncio.iscoroutinefunction(custom_orchestrator.handle): |
| 70 | + return await custom_orchestrator.handle(request) |
| 71 | + else: |
| 72 | + return custom_orchestrator.handle(request) |
0 commit comments