|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +import requests |
| 10 | +import test_utils |
| 11 | +from model_archiver import ModelArchiverConfig |
| 12 | + |
| 13 | +CURR_FILE_PATH = Path(__file__).parent |
| 14 | +REPO_ROOT_DIR = CURR_FILE_PATH.parent.parent |
| 15 | +ROOT_DIR = os.path.join(tempfile.gettempdir(), "workspace") |
| 16 | +REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../") |
| 17 | +data_file_zero = os.path.join(REPO_ROOT, "test/pytest/test_data/0.png") |
| 18 | +config_file = os.path.join(REPO_ROOT, "test/resources/config_token.properties") |
| 19 | +mnist_scriptes_py = os.path.join(REPO_ROOT, "examples/image_classifier/mnist/mnist.py") |
| 20 | + |
| 21 | +HANDLER_PY = """ |
| 22 | +from ts.torch_handler.base_handler import BaseHandler |
| 23 | +
|
| 24 | +class deviceHandler(BaseHandler): |
| 25 | +
|
| 26 | + def initialize(self, context): |
| 27 | + super().initialize(context) |
| 28 | + assert self.get_device().type == "mps" |
| 29 | +""" |
| 30 | + |
| 31 | +MODEL_CONFIG_YAML = """ |
| 32 | + #frontend settings |
| 33 | + # TorchServe frontend parameters |
| 34 | + minWorkers: 1 |
| 35 | + batchSize: 4 |
| 36 | + maxWorkers: 4 |
| 37 | + """ |
| 38 | + |
| 39 | +MODEL_CONFIG_YAML_GPU = """ |
| 40 | + #frontend settings |
| 41 | + # TorchServe frontend parameters |
| 42 | + minWorkers: 1 |
| 43 | + batchSize: 4 |
| 44 | + maxWorkers: 4 |
| 45 | + deviceType: "gpu" |
| 46 | + """ |
| 47 | + |
| 48 | +MODEL_CONFIG_YAML_CPU = """ |
| 49 | + #frontend settings |
| 50 | + # TorchServe frontend parameters |
| 51 | + minWorkers: 1 |
| 52 | + batchSize: 4 |
| 53 | + maxWorkers: 4 |
| 54 | + deviceType: "cpu" |
| 55 | + """ |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="module") |
| 59 | +def model_name(): |
| 60 | + yield "mnist" |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture(scope="module") |
| 64 | +def work_dir(tmp_path_factory, model_name): |
| 65 | + return Path(tmp_path_factory.mktemp(model_name)) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(scope="module") |
| 69 | +def model_config_name(request): |
| 70 | + def get_config(param): |
| 71 | + if param == "cpu": |
| 72 | + return MODEL_CONFIG_YAML_CPU |
| 73 | + elif param == "gpu": |
| 74 | + return MODEL_CONFIG_YAML_GPU |
| 75 | + else: |
| 76 | + return MODEL_CONFIG_YAML |
| 77 | + |
| 78 | + return get_config(request.param) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture(scope="module", name="mar_file_path") |
| 82 | +def create_mar_file(work_dir, model_archiver, model_name, model_config_name): |
| 83 | + mar_file_path = work_dir.joinpath(model_name + ".mar") |
| 84 | + |
| 85 | + model_config_yaml_file = work_dir / "model_config.yaml" |
| 86 | + model_config_yaml_file.write_text(model_config_name) |
| 87 | + |
| 88 | + model_py_file = work_dir / "model.py" |
| 89 | + |
| 90 | + model_py_file.write_text(mnist_scriptes_py) |
| 91 | + |
| 92 | + handler_py_file = work_dir / "handler.py" |
| 93 | + handler_py_file.write_text(HANDLER_PY) |
| 94 | + |
| 95 | + config = ModelArchiverConfig( |
| 96 | + model_name=model_name, |
| 97 | + version="1.0", |
| 98 | + serialized_file=None, |
| 99 | + model_file=mnist_scriptes_py, # model_py_file.as_posix(), |
| 100 | + handler=handler_py_file.as_posix(), |
| 101 | + extra_files=None, |
| 102 | + export_path=work_dir, |
| 103 | + requirements_file=None, |
| 104 | + runtime="python", |
| 105 | + force=False, |
| 106 | + archive_format="default", |
| 107 | + config_file=model_config_yaml_file.as_posix(), |
| 108 | + ) |
| 109 | + |
| 110 | + with patch("archiver.ArgParser.export_model_args_parser", return_value=config): |
| 111 | + model_archiver.generate_model_archive() |
| 112 | + |
| 113 | + assert mar_file_path.exists() |
| 114 | + |
| 115 | + yield mar_file_path.as_posix() |
| 116 | + |
| 117 | + # Clean up files |
| 118 | + |
| 119 | + mar_file_path.unlink(missing_ok=True) |
| 120 | + |
| 121 | + # Clean up files |
| 122 | + |
| 123 | + |
| 124 | +@pytest.fixture(scope="module", name="model_name") |
| 125 | +def register_model(mar_file_path, model_store, torchserve): |
| 126 | + """ |
| 127 | + Register the model in torchserve |
| 128 | + """ |
| 129 | + shutil.copy(mar_file_path, model_store) |
| 130 | + |
| 131 | + file_name = Path(mar_file_path).name |
| 132 | + |
| 133 | + model_name = Path(file_name).stem |
| 134 | + |
| 135 | + params = ( |
| 136 | + ("model_name", model_name), |
| 137 | + ("url", file_name), |
| 138 | + ("initial_workers", "1"), |
| 139 | + ("synchronous", "true"), |
| 140 | + ("batch_size", "1"), |
| 141 | + ) |
| 142 | + |
| 143 | + test_utils.reg_resp = test_utils.register_model_with_params(params) |
| 144 | + |
| 145 | + yield model_name |
| 146 | + |
| 147 | + test_utils.unregister_model(model_name) |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.skipif(platform.machine() != "arm64", reason="Skip on Mac M1") |
| 151 | +@pytest.mark.parametrize("model_config_name", ["gpu"], indirect=True) |
| 152 | +def test_m1_device(model_name, model_config_name): |
| 153 | + response = requests.get(f"http://localhost:8081/models/{model_name}") |
| 154 | + assert response.status_code == 200, "Describe Failed" |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.skipif(platform.machine() != "arm64", reason="Skip on Mac M1") |
| 158 | +@pytest.mark.parametrize("model_config_name", ["cpu"], indirect=True) |
| 159 | +def test_m1_device_cpu(model_name, model_config_name): |
| 160 | + response = requests.get(f"http://localhost:8081/models/{model_name}") |
| 161 | + assert response.status_code == 404, "Describe Worked" |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.skipif(platform.machine() != "arm64", reason="Skip on Mac M1") |
| 165 | +@pytest.mark.parametrize("model_config_name", ["default"], indirect=True) |
| 166 | +def test_m1_device_default(model_name, model_config_name): |
| 167 | + response = requests.get(f"http://localhost:8081/models/{model_name}") |
| 168 | + assert response.status_code == 200, "Describe Failed" |
0 commit comments