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

Commit fa6d180

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

File tree

6 files changed

+70
-27
lines changed

6 files changed

+70
-27
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: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
PING_URL = "http://localhost:8080/ping"
2929
INVOCATION_URL = "http://localhost:8080/models/{}/invoke"
30-
MODEL_NAME = "half_plus_three"
31-
30+
MODEL_NAME_1 = "half_plus_three"
31+
MODEL_NAME_2 = "half_plus_two"
3232

3333
@pytest.fixture(scope="session", autouse=True)
3434
def volume():
@@ -74,14 +74,21 @@ 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
8484

85+
def model2():
86+
model_data = {
87+
"model_name": MODEL_NAME_2,
88+
"url": "/opt/ml/models/half_plus_two/model/half_plus_two"
89+
}
90+
make_load_model_request(json.dumps(model_data))
91+
return MODEL_NAME_2
8592

8693
@pytest.mark.skip_gpu
8794
def test_ping_service():
@@ -90,20 +97,24 @@ def test_ping_service():
9097

9198

9299
@pytest.mark.skip_gpu
93-
def test_predict_json(model):
100+
def test_predict_json(model1,model2):
94101
headers = make_headers()
95102
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]}
98-
103+
response1 = requests.post(INVOCATION_URL.format(model1), data=data, headers=headers).json()
104+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
105+
response2 = requests.post(INVOCATION_URL.format(model2), data=data, headers=headers).json()
106+
assert response1 == {"predictions": [2.5, 3.0, 4.5]}
99107

100108
@pytest.mark.skip_gpu
101109
def test_zero_content():
102110
headers = make_headers()
103111
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
112+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=x, headers=headers)
113+
assert 500 == response1.status_code
114+
assert "document is empty" in response1.text
115+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=x, headers=headers)
116+
assert 500 == response2.status_code
117+
assert "document is empty" in response2.text
107118

108119

109120
@pytest.mark.skip_gpu
@@ -113,34 +124,46 @@ def test_large_input():
113124
with open(data_file, "r") as file:
114125
x = file.read()
115126
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
127+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=x, headers=headers).json()
128+
predictions1 = response1["predictions"]
129+
assert len(predictions1) == 753936
130+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=x, headers=headers).json()
131+
predictions2 = response2["predictions"]
132+
assert len(predictions2) == 753936
119133

120134

121135
@pytest.mark.skip_gpu
122136
def test_csv_input():
123137
headers = make_headers(content_type="text/csv")
124138
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]}
139+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers).json()
140+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
141+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers).json()
142+
assert response2 == {"predictions": [2.5, 3.0, 4.5]}
127143

128144

129145
@pytest.mark.skip_gpu
130146
def test_specific_versions():
131147
for version in ("123", "124"):
132148
headers = make_headers(content_type="text/csv", version=version)
133149
data = "1.0,2.0,5.0"
134-
response = requests.post(
135-
INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers
150+
response1 = requests.post(
151+
INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers
152+
).json()
153+
assert response1 == {"predictions": [3.5, 4.0, 5.5]}
154+
response2 = requests.post(
155+
INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers
136156
).json()
137-
assert response == {"predictions": [3.5, 4.0, 5.5]}
157+
assert response2 == {"predictions": [2.5, 3.0, 4.5]}
138158

139159

140160
@pytest.mark.skip_gpu
141161
def test_unsupported_content_type():
142162
headers = make_headers("unsupported-type", "predict")
143163
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
164+
response1 = requests.post(INVOCATION_URL.format(MODEL_NAME_1), data=data, headers=headers)
165+
assert 500 == response1.status_code
166+
assert "unsupported content type" in response1.text
167+
response2 = requests.post(INVOCATION_URL.format(MODEL_NAME_2), data=data, headers=headers)
168+
assert 500 == response2.status_code
169+
assert "unsupported content type" in response2.text
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)