Skip to content
This repository was archived by the owner on May 23, 2024. It is now read-only.

Commit d50072a

Browse files
committed
Feature: Support multiple inference.py files and universal inference.py file along with universal requirements.txt file
1 parent 1a265db commit d50072a

File tree

6 files changed

+100
-26
lines changed

6 files changed

+100
-26
lines changed

docker/build_artifacts/sagemaker/python_service.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import subprocess
1919
import grpc
20+
import sys
2021

2122
import falcon
2223
import requests
@@ -143,6 +144,7 @@ def _handle_load_model_post(self, res, data): # noqa: C901
143144
# validate model files are in the specified base_path
144145
if self.validate_model_dir(base_path):
145146
try:
147+
self._import_custom_modules(model_name)
146148
tfs_config = tfs_utils.create_tfs_config_individual_model(model_name, base_path)
147149
tfs_config_file = "/sagemaker/tfs-config/{}/model-config.cfg".format(model_name)
148150
log.info("tensorflow serving model config: \n%s\n", tfs_config)
@@ -221,6 +223,21 @@ def _handle_load_model_post(self, res, data): # noqa: C901
221223
}
222224
)
223225

226+
def _import_custom_modules(self, model_name):
227+
inference_script_path = "/opt/ml/models/{}/model/code/inference.py".format(model_name)
228+
python_lib_path = "/opt/ml/models/{}/model/code/lib".format(model_name)
229+
230+
if os.path.exists(python_lib_path):
231+
log.info("add Python code library path")
232+
sys.path.append(python_lib_path)
233+
234+
if os.path.exists(inference_script_path):
235+
handler, input_handler, output_handler = self._import_handlers(inference_script_path)
236+
model_handlers = self._make_handler(handler, input_handler, output_handler)
237+
self.model_handlers[model_name] = model_handlers
238+
else:
239+
self.model_handlers[model_name] = default_handler
240+
224241
def _cleanup_config_file(self, config_file):
225242
if os.path.exists(config_file):
226243
os.remove(config_file)
@@ -264,8 +281,11 @@ def _handle_invocation_post(self, req, res, model_name=None):
264281

265282
try:
266283
res.status = falcon.HTTP_200
284+
handlers = self._handlers
285+
if SAGEMAKER_MULTI_MODEL_ENABLED and self.model_handlers:
286+
handlers = self.model_handlers[model_name]
287+
res.body, res.content_type = handlers(data, context)
267288

268-
res.body, res.content_type = self._handlers(data, context)
269289
except Exception as e: # pylint: disable=broad-except
270290
log.exception("exception handling request: {}".format(e))
271291
res.status = falcon.HTTP_500
@@ -276,8 +296,7 @@ def _setup_channel(self, grpc_port):
276296
log.info("Creating grpc channel for port: %s", grpc_port)
277297
self._channels[grpc_port] = grpc.insecure_channel("localhost:{}".format(grpc_port))
278298

279-
def _import_handlers(self):
280-
inference_script = INFERENCE_SCRIPT_PATH
299+
def _import_handlers(self, inference_script=INFERENCE_SCRIPT_PATH):
281300
spec = importlib.util.spec_from_file_location("inference", inference_script)
282301
inference = importlib.util.module_from_spec(spec)
283302
spec.loader.exec_module(inference)

docker/build_artifacts/sagemaker/serve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def __init__(self):
134134
os.environ["TFS_REST_PORTS"] = self._tfs_rest_concat_ports
135135

136136
def _need_python_service(self):
137-
if os.path.exists(INFERENCE_PATH):
137+
if (os.path.exists(INFERENCE_PATH) or os.path.exists(REQUIREMENTS_PATH)
138+
or os.path.exists(PYTHON_LIB_PATH)):
138139
self._enable_python_service = True
139140
if os.environ.get("SAGEMAKER_MULTI_MODEL_UNIVERSAL_BUCKET") and os.environ.get(
140141
"SAGEMAKER_MULTI_MODEL_UNIVERSAL_PREFIX"

test/integration/local/test_pre_post_processing_mme.py

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
from multi_model_endpoint_test_utils import make_load_model_request, make_headers
2626

27-
2827
PING_URL = "http://localhost:8080/ping"
2928
INVOCATION_URL = "http://localhost:8080/models/{}/invoke"
30-
MODEL_NAME = "half_plus_three"
29+
MODEL_NAME_1 = "half_plus_three"
30+
MODEL_NAME_2 = "half_plus_two"
3131

3232

3333
@pytest.fixture(scope="session", autouse=True)
@@ -74,13 +74,22 @@ def container(docker_base_name, tag, runtime_config):
7474

7575

7676
@pytest.fixture
77-
def model():
77+
def model1():
7878
model_data = {
79-
"model_name": MODEL_NAME,
79+
"model_name": MODEL_NAME_1,
8080
"url": "/opt/ml/models/half_plus_three/model/half_plus_three"
8181
}
8282
make_load_model_request(json.dumps(model_data))
83-
return MODEL_NAME
83+
return MODEL_NAME_1
84+
85+
@pytest.fixture
86+
def model2():
87+
model_data = {
88+
"model_name": MODEL_NAME_2,
89+
"url": "/opt/ml/models/half_plus_two/model/half_plus_two"
90+
}
91+
make_load_model_request(json.dumps(model_data))
92+
return MODEL_NAME_2
8493

8594

8695
@pytest.mark.skip_gpu
@@ -90,20 +99,33 @@ def test_ping_service():
9099

91100

92101
@pytest.mark.skip_gpu
93-
def test_predict_json(model):
102+
def test_predict_json(model1, model2):
94103
headers = make_headers()
95104
data = "{\"instances\": [1.0, 2.0, 5.0]}"
96-
response = requests.post(INVOCATION_URL.format(model), data=data, headers=headers).json()
97-
assert response == {"predictions": [3.5, 4.0, 5.5]}
105+
response1 = requests.post(INVOCATION_URL.format(model1), data=data, headers=headers).json()
106+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
107+
response2 = requests.post(INVOCATION_URL.format(model2), data=data, headers=headers).json()
108+
assert response2 == {"predictions": [2.5, 3.0, 4.5]}
98109

99110

100111
@pytest.mark.skip_gpu
101112
def test_zero_content():
102113
headers = make_headers()
103114
x = ""
104-
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=x, headers=headers)
105-
assert 500 == response.status_code
106-
assert "document is empty" in response.text
115+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=x, headers=headers)
116+
print("Response 1 status code:")
117+
print(response1.status_code)
118+
print("Response 1 text:")
119+
print(response1.text)
120+
assert 500 == response1.status_code
121+
assert "document is empty" in response1.text
122+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=x, headers=headers)
123+
print("Response 2 status code:")
124+
print(response2.status_code)
125+
print("Response 2 text:")
126+
print(response2.text)
127+
assert 500 == response2.status_code
128+
assert "document is empty" in response2.text
107129

108130

109131
@pytest.mark.skip_gpu
@@ -113,34 +135,66 @@ def test_large_input():
113135
with open(data_file, "r") as file:
114136
x = file.read()
115137
headers = make_headers(content_type="text/csv")
116-
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=x, headers=headers).json()
117-
predictions = response["predictions"]
118-
assert len(predictions) == 753936
138+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=x, headers=headers).json()
139+
predictions1 = response1["predictions"]
140+
print("Response 1:")
141+
print(response1)
142+
assert len(predictions1) == 753936
143+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=x, headers=headers).json()
144+
print("Response 2:")
145+
print(response2)
146+
predictions2 = response2["predictions"]
147+
assert len(predictions2) == 753936
119148

120149

121150
@pytest.mark.skip_gpu
122151
def test_csv_input():
123152
headers = make_headers(content_type="text/csv")
124153
data = "1.0,2.0,5.0"
125-
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers).json()
126-
assert response == {"predictions": [3.5, 4.0, 5.5]}
154+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers).json()
155+
print("Response 1:")
156+
print(response1)
157+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
158+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers).json()
159+
print("Response 2:")
160+
print(response2)
161+
assert response2 == {"predictions": [2.5, 3.0, 4.5]}
127162

128163

129164
@pytest.mark.skip_gpu
130165
def test_specific_versions():
131166
for version in ("123", "124"):
132167
headers = make_headers(content_type="text/csv", version=version)
133168
data = "1.0,2.0,5.0"
134-
response = requests.post(
135-
INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers
169+
response1 = requests.post(
170+
INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers
171+
).json()
172+
print("Response 1")
173+
print(response1)
174+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
175+
response2 = requests.post(
176+
INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers
136177
).json()
137-
assert response == {"predictions": [3.5, 4.0, 5.5]}
178+
print("Response 2:")
179+
print(response2)
180+
assert response2 == {"predictions": [2.5, 3.0, 4.5]}
138181

139182

140183
@pytest.mark.skip_gpu
141184
def test_unsupported_content_type():
142185
headers = make_headers("unsupported-type", "predict")
143186
data = "aW1hZ2UgYnl0ZXM="
144-
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers)
145-
assert 500 == response.status_code
146-
assert "unsupported content type" in response.text
187+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers)
188+
print("Response 1 status code:")
189+
print(response1.status_code)
190+
print("Response 1 text:")
191+
print(response1.text)
192+
assert 500 == response1.status_code
193+
assert "unsupported content type" in response1.text
194+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers)
195+
print("Response 2 status code:")
196+
print(response2.status_code)
197+
print("Response 2 text:")
198+
print(response2.text)
199+
assert 500 == response2.status_code
200+
assert "unsupported content type" in response2.text

0 commit comments

Comments
 (0)