|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import importlib |
| 5 | +import logging |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from utils.environment import Environment |
| 11 | +from utils.exception import ( |
| 12 | + InferenceCodeLoadException, |
| 13 | + RequirementsInstallException, |
| 14 | + ServerStartException, |
| 15 | +) |
| 16 | +from utils.logger import SAGEMAKER_DISTRIBUTION_INFERENCE_LOGGER |
| 17 | + |
| 18 | +logger = logging.getLogger(SAGEMAKER_DISTRIBUTION_INFERENCE_LOGGER) |
| 19 | + |
| 20 | + |
| 21 | +class TornadoServer: |
| 22 | + """Holds serving logic using the Tornado framework. |
| 23 | +
|
| 24 | + The serve.py script will invoke TornadoServer.serve() to start the serving process. |
| 25 | + The TornadoServer will install the runtime requirements specified through a requirements file. |
| 26 | + It will then load an handler function within an inference script and then front it will an /invocations |
| 27 | + route using the Tornado framework. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + """Initialize the serving behaviors. |
| 32 | +
|
| 33 | + Defines the serving behavior through Environment() and locate where |
| 34 | + the inference code is contained. |
| 35 | + """ |
| 36 | + |
| 37 | + self._environment = Environment() |
| 38 | + logger.setLevel(int(self._environment.logging_level)) |
| 39 | + logger.debug(f"Environment: {str(self._environment)}") |
| 40 | + |
| 41 | + self._path_to_inference_code = ( |
| 42 | + Path(self._environment.base_directory).joinpath(self._environment.code_directory) |
| 43 | + if self._environment.code_directory |
| 44 | + else Path(self._environment.base_directory) |
| 45 | + ) |
| 46 | + logger.debug(f"Path to inference code: `{str(self._path_to_inference_code)}`") |
| 47 | + |
| 48 | + def initialize(self): |
| 49 | + """Initialize the serving artifacts and dependencies. |
| 50 | +
|
| 51 | + Install the runtime requirements and then locate the handler function from |
| 52 | + the inference script. |
| 53 | + """ |
| 54 | + |
| 55 | + logger.info("Initializing inference server...") |
| 56 | + self._install_runtime_requirements() |
| 57 | + self._handler = self._load_inference_handler() |
| 58 | + |
| 59 | + def serve(self): |
| 60 | + """Orchestrate the initialization and server startup behavior. |
| 61 | +
|
| 62 | + Call the initalize() method, determine the right Tornado serving behavior (async or sync), |
| 63 | + and then start the Tornado server through asyncio |
| 64 | + """ |
| 65 | + |
| 66 | + logger.info("Serving inference requests using Tornado...") |
| 67 | + self.initialize() |
| 68 | + |
| 69 | + if asyncio.iscoroutinefunction(self._handler): |
| 70 | + import async_handler as inference_handler |
| 71 | + else: |
| 72 | + import sync_handler as inference_handler |
| 73 | + |
| 74 | + try: |
| 75 | + asyncio.run(inference_handler.handle(self._handler, self._environment)) |
| 76 | + except Exception as e: |
| 77 | + raise ServerStartException(e) |
| 78 | + |
| 79 | + def _install_runtime_requirements(self): |
| 80 | + """Install the runtime requirements.""" |
| 81 | + |
| 82 | + logger.info("Installing runtime requirements...") |
| 83 | + requirements_txt = self._path_to_inference_code.joinpath(self._environment.requirements) |
| 84 | + if requirements_txt.is_file(): |
| 85 | + try: |
| 86 | + subprocess.check_call(["micromamba", "install", "--yes", "--file", str(requirements_txt)]) |
| 87 | + except Exception as e: |
| 88 | + logger.error("Failed to install requirements using `micromamba install`. Falling back to `pip install`...") |
| 89 | + try: |
| 90 | + subprocess.check_call(["pip", "install", "-r", str(requirements_txt)]) |
| 91 | + except Exception as e: |
| 92 | + raise RequirementsInstallException(e) |
| 93 | + else: |
| 94 | + logger.debug(f"No requirements file was found at `{str(requirements_txt)}`") |
| 95 | + |
| 96 | + def _load_inference_handler(self) -> callable: |
| 97 | + """Load the handler function from the inference script.""" |
| 98 | + |
| 99 | + logger.info("Loading inference handler...") |
| 100 | + inference_module_name, handle_name = self._environment.code.split(".") |
| 101 | + if inference_module_name and handle_name: |
| 102 | + inference_module_file = f"{inference_module_name}.py" |
| 103 | + module_spec = importlib.util.spec_from_file_location( |
| 104 | + inference_module_file, str(self._path_to_inference_code.joinpath(inference_module_file)) |
| 105 | + ) |
| 106 | + if module_spec: |
| 107 | + sys.path.insert(0, str(self._path_to_inference_code.resolve())) |
| 108 | + module = importlib.util.module_from_spec(module_spec) |
| 109 | + module_spec.loader.exec_module(module) |
| 110 | + |
| 111 | + if hasattr(module, handle_name): |
| 112 | + handler = getattr(module, handle_name) |
| 113 | + else: |
| 114 | + raise InferenceCodeLoadException( |
| 115 | + f"Handler `{handle_name}` could not be found in module `{inference_module_file}`" |
| 116 | + ) |
| 117 | + logger.debug(f"Loaded handler `{handle_name}` from module `{inference_module_name}`") |
| 118 | + return handler |
| 119 | + else: |
| 120 | + raise InferenceCodeLoadException( |
| 121 | + f"Inference code could not be found at `{str(self._path_to_inference_code.joinpath(inference_module_file))}`" |
| 122 | + ) |
| 123 | + raise InferenceCodeLoadException( |
| 124 | + f"Inference code expected in the format of `<module>.<handler>` but was provided as {self._environment.code}" |
| 125 | + ) |
0 commit comments