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

Commit 32d6b9a

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

File tree

64 files changed

+968
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+968
-36
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ For example:
164164

165165
## Pre/Post-Processing
166166

167-
**NOTE: There is currently no support for pre-/post-processing with multi-model containers.**
168-
169167
SageMaker TensorFlow Serving Container supports the following Content-Types for requests:
170168

171169
* `application/json` (default)
@@ -672,7 +670,7 @@ Only 90% of the ports will be utilized and each loaded model will be allocated w
672670
For example, if the ``SAGEMAKER_SAFE_PORT_RANGE`` is between 9000 to 9999, the maximum number of models that can be loaded to the endpoint at the same time would be 499 ((9999 - 9000) * 0.9 / 2).
673671

674672
### Using Multi-Model Endpoint with Pre/Post-Processing
675-
Multi-Model Endpoint can be used together with Pre/Post-Processing. Each model will need its own ``inference.py`` otherwise default handlers will be used. An example of the directory structure of Multi-Model Endpoint and Pre/Post-Processing would look like this:
673+
Multi-Model Endpoint can be used together with Pre/Post-Processing. Each model can either have its own ``inference.py`` or use a universal ``inference.py``. If both model-specific and universal ``inference.py`` files are provided, then the model-specific ``inference.py`` file is used. If both files are absent, then the default handlers will be used. An example of the directory structure of Multi-Model Endpoint with a model-specific ``inference.py`` file would look like this:
676674

677675
/opt/ml/models/model1/model
678676
|--[model_version_number]
@@ -687,7 +685,20 @@ Multi-Model Endpoint can be used together with Pre/Post-Processing. Each model w
687685
|--lib
688686
|--external_module
689687
|--inference.py
688+
Another example with of the directory structure of Multi-Model Endpoint with a universal ``inference.py`` file is as follows:
690689

690+
/opt/ml/models/model1/model
691+
|--[model_version_number]
692+
|--variables
693+
|--saved_model.pb
694+
/opt/ml/models/model2/model
695+
|--[model_version_number]
696+
|--assets
697+
|--variables
698+
|--saved_model.pb
699+
code
700+
|--requirements.txt
701+
|--inference.py
691702
## Contributing
692703

693704
Please read [CONTRIBUTING.md](https://github.com/aws/sagemaker-tensorflow-serving-container/blob/master/CONTRIBUTING.md)

docker/build_artifacts/sagemaker/python_service.py

Lines changed: 18 additions & 4 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,17 @@ 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+
if os.path.exists(python_lib_path):
230+
log.info("add Python code library path")
231+
sys.path.append(python_lib_path)
232+
if os.path.exists(inference_script_path):
233+
handler, input_handler, output_handler = self._import_handlers(inference_script_path)
234+
model_handlers = self._make_handler(handler, input_handler, output_handler)
235+
self.model_handlers[model_name] = model_handlers
236+
224237
def _cleanup_config_file(self, config_file):
225238
if os.path.exists(config_file):
226239
os.remove(config_file)
@@ -264,8 +277,10 @@ def _handle_invocation_post(self, req, res, model_name=None):
264277

265278
try:
266279
res.status = falcon.HTTP_200
267-
268-
res.body, res.content_type = self._handlers(data, context)
280+
handlers = self._handlers
281+
if SAGEMAKER_MULTI_MODEL_ENABLED and model_name in self.model_handlers:
282+
handlers = self.model_handlers[model_name]
283+
res.body, res.content_type = handlers(data, context)
269284
except Exception as e: # pylint: disable=broad-except
270285
log.exception("exception handling request: {}".format(e))
271286
res.status = falcon.HTTP_500
@@ -276,8 +291,7 @@ def _setup_channel(self, grpc_port):
276291
log.info("Creating grpc channel for port: %s", grpc_port)
277292
self._channels[grpc_port] = grpc.insecure_channel("localhost:{}".format(grpc_port))
278293

279-
def _import_handlers(self):
280-
inference_script = INFERENCE_SCRIPT_PATH
294+
def _import_handlers(self, inference_script=INFERENCE_SCRIPT_PATH):
281295
spec = importlib.util.spec_from_file_location("inference", inference_script)
282296
inference = importlib.util.module_from_spec(spec)
283297
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 renamed to test/integration/local/test_pre_post_processing_mme1.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313

14+
# In this test, only a universal inference.py file is provided. It's expected the handlers from the universal
15+
# inference.py file should be used by both models.
16+
1417
import json
1518
import os
16-
import shutil
1719
import subprocess
1820
import sys
1921
import time
@@ -27,27 +29,27 @@
2729

2830
PING_URL = "http://localhost:8080/ping"
2931
INVOCATION_URL = "http://localhost:8080/models/{}/invoke"
30-
MODEL_NAME = "half_plus_three"
32+
MODEL_NAMES = ["half_plus_three","half_plus_two"]
3133

3234

3335
@pytest.fixture(scope="session", autouse=True)
3436
def volume():
3537
try:
36-
model_dir = os.path.abspath("test/resources/mme_universal_script")
38+
model_dir = os.path.abspath("test/resources/mme1")
3739
subprocess.check_call(
38-
"docker volume create --name model_volume_mme --opt type=none "
40+
"docker volume create --name model_volume_mme1 --opt type=none "
3941
"--opt device={} --opt o=bind".format(model_dir).split())
4042
yield model_dir
4143
finally:
42-
subprocess.check_call("docker volume rm model_volume_mme".split())
44+
subprocess.check_call("docker volume rm model_volume_mme1".split())
4345

4446

4547
@pytest.fixture(scope="module", autouse=True)
4648
def container(docker_base_name, tag, runtime_config):
4749
try:
4850
command = (
4951
"docker run {}--name sagemaker-tensorflow-serving-test -p 8080:8080"
50-
" --mount type=volume,source=model_volume_mme,target=/opt/ml/models,readonly"
52+
" --mount type=volume,source=model_volume_mme1,target=/opt/ml/models,readonly"
5153
" -e SAGEMAKER_TFS_NGINX_LOGLEVEL=info"
5254
" -e SAGEMAKER_BIND_TO_PORT=8080"
5355
" -e SAGEMAKER_SAFE_PORT_RANGE=9000-9999"
@@ -74,13 +76,14 @@ def container(docker_base_name, tag, runtime_config):
7476

7577

7678
@pytest.fixture
77-
def model():
78-
model_data = {
79-
"model_name": MODEL_NAME,
80-
"url": "/opt/ml/models/half_plus_three/model/half_plus_three"
81-
}
82-
make_load_model_request(json.dumps(model_data))
83-
return MODEL_NAME
79+
def models():
80+
for MODEL_NAME in MODEL_NAMES:
81+
model_data = {
82+
"model_name": MODEL_NAME,
83+
"url": "/opt/ml/models/{}/model/{}".format(MODEL_NAME,MODEL_NAME)
84+
}
85+
make_load_model_request(json.dumps(model_data))
86+
return MODEL_NAMES
8487

8588

8689
@pytest.mark.skip_gpu
@@ -90,20 +93,25 @@ def test_ping_service():
9093

9194

9295
@pytest.mark.skip_gpu
93-
def test_predict_json(model):
96+
def test_predict_json(models):
9497
headers = make_headers()
9598
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]}
99+
responses = []
100+
for model in models:
101+
response = requests.post(INVOCATION_URL.format(model), data=data, headers=headers).json()
102+
responses.append(response)
103+
assert responses[0] == {"predictions": [3.5, 4.0, 5.5]}
104+
assert responses[1] == {"predictions": [2.5, 3.0, 4.5]}
98105

99106

100107
@pytest.mark.skip_gpu
101108
def test_zero_content():
102109
headers = make_headers()
103110
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
111+
for MODEL_NAME in MODEL_NAMES:
112+
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=x, headers=headers)
113+
assert 500 == response.status_code
114+
assert "document is empty" in response.text
107115

108116

109117
@pytest.mark.skip_gpu
@@ -113,21 +121,26 @@ def test_large_input():
113121
with open(data_file, "r") as file:
114122
x = file.read()
115123
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
124+
for MODEL_NAME in MODEL_NAMES:
125+
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=x, headers=headers).json()
126+
predictions = response["predictions"]
127+
assert len(predictions) == 753936
119128

120129

121130
@pytest.mark.skip_gpu
122131
def test_csv_input():
123132
headers = make_headers(content_type="text/csv")
124133
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]}
127-
134+
responses = []
135+
for MODEL_NAME in MODEL_NAMES:
136+
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers).json()
137+
responses.append(response)
138+
assert responses[0] == {"predictions": [3.5, 4.0, 5.5]}
139+
assert responses[1] == {"predictions": [2.5, 3.0, 4.5]}
128140

129141
@pytest.mark.skip_gpu
130142
def test_specific_versions():
143+
MODEL_NAME = MODEL_NAMES[0]
131144
for version in ("123", "124"):
132145
headers = make_headers(content_type="text/csv", version=version)
133146
data = "1.0,2.0,5.0"
@@ -141,6 +154,7 @@ def test_specific_versions():
141154
def test_unsupported_content_type():
142155
headers = make_headers("unsupported-type", "predict")
143156
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
157+
for MODEL_NAME in MODEL_NAMES:
158+
response = requests.post(INVOCATION_URL.format(MODEL_NAME), data=data, headers=headers)
159+
assert 500 == response.status_code
160+
assert "unsupported content type" in response.text

0 commit comments

Comments
 (0)