Skip to content

Commit fa861bc

Browse files
icywang86ruiRui Wang Napieralski
and
Rui Wang Napieralski
authored
feature: upgrade Neo MxNet to 1.7 (#1934)
This reverts commit b3f8abc. Co-authored-by: Rui Wang Napieralski <[email protected]>
1 parent 8fff159 commit fa861bc

File tree

6 files changed

+51
-45
lines changed

6 files changed

+51
-45
lines changed

src/sagemaker/image_uri_config/neo-mxnet.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
"processors": ["cpu", "gpu"],
33
"scope": ["inference"],
44
"version_aliases": {
5-
"0.12.1": "1.5",
6-
"1.0.0": "1.5",
7-
"1.1.0": "1.5",
8-
"1.2": "1.5",
9-
"1.2.0": "1.5",
10-
"1.2.1": "1.5",
11-
"1.3": "1.5",
12-
"1.3.0": "1.5",
13-
"1.4": "1.5",
14-
"1.4.0": "1.5",
15-
"1.4.1": "1.5"
5+
"0.12.1": "1.7",
6+
"1.0.0": "1.7",
7+
"1.1.0": "1.7",
8+
"1.2": "1.7",
9+
"1.2.0": "1.7",
10+
"1.2.1": "1.7",
11+
"1.3": "1.7",
12+
"1.3.0": "1.7",
13+
"1.4": "1.7",
14+
"1.4.0": "1.7",
15+
"1.4.1": "1.7"
1616
},
1717
"versions": {
18-
"1.5": {
18+
"1.7": {
1919
"py_versions": ["py3"],
2020
"registries": {
2121
"af-south-1": "774647643957",
@@ -42,7 +42,7 @@
4242
"us-west-1": "710691900526",
4343
"us-west-2": "301217895009"
4444
},
45-
"repository": "sagemaker-neo-mxnet"
45+
"repository": "sagemaker-inference-mxnet"
4646
}
4747
}
4848
}

src/sagemaker/image_uri_config/neo-pytorch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"us-west-1": "710691900526",
3737
"us-west-2": "301217895009"
3838
},
39-
"repository": "sagemaker-neo-pytorch"
39+
"repository": "sagemaker-inference-pytorch"
4040
}
4141
}
4242
}

tests/data/mxnet_mnist/mnist_neo.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,32 @@ def train(
104104
save(model_dir, mlp_model)
105105

106106

107-
def neo_preprocess(payload, content_type):
108-
logging.info("Invoking user-defined pre-processing function")
107+
def model_fn(path_to_model_files):
108+
import neomxnet # noqa: F401
109109

110-
if content_type != "application/vnd+python.numpy+binary":
111-
raise RuntimeError("Content type must be application/vnd+python.numpy+binary")
110+
ctx = mx.cpu()
111+
sym, arg_params, aux_params = mx.model.load_checkpoint(
112+
os.path.join(path_to_model_files, "compiled"), 0
113+
)
114+
mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
115+
mod.bind(
116+
for_training=False, data_shapes=[("data", (1, 1, 28, 28))], label_shapes=mod._label_shapes
117+
)
118+
mod.set_params(arg_params, aux_params, allow_missing=True)
119+
return mod
112120

113-
return np.asarray(json.loads(payload.decode("utf-8")))
114121

122+
def transform_fn(mod, payload, input_content_type, requested_output_content_type):
123+
import neomxnet # noqa: F401
115124

116-
# NOTE: this function cannot use MXNet
117-
def neo_postprocess(result):
118-
logging.info("Invoking user-defined post-processing function")
125+
if input_content_type != "application/vnd+python.numpy+binary":
126+
raise RuntimeError("Input content type must be application/vnd+python.numpy+binary")
119127

120-
# Softmax (assumes batch size 1)
128+
inference_payload = np.asarray(json.loads(payload.decode("utf-8")))
129+
result = mod.predict(inference_payload)
121130
result = np.squeeze(result)
122-
result_exp = np.exp(result - np.max(result))
123-
result = result_exp / np.sum(result_exp)
124-
125-
response_body = json.dumps(result.tolist())
131+
response_body = json.dumps(result.asnumpy().tolist())
126132
content_type = "application/json"
127-
128133
return response_body, content_type
129134

130135

@@ -135,7 +140,7 @@ def neo_postprocess(result):
135140
parser = argparse.ArgumentParser()
136141

137142
parser.add_argument("--batch-size", type=int, default=100)
138-
parser.add_argument("--epochs", type=int, default=10)
143+
parser.add_argument("--epochs", type=int, default=1)
139144
parser.add_argument("--learning-rate", type=float, default=0.1)
140145

141146
parser.add_argument("--model-dir", type=str, default=os.environ["SM_MODEL_DIR"])

tests/integ/test_neo_mxnet.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ def mxnet_training_job(
5858

5959

6060
@pytest.mark.canary_quick
61-
@pytest.mark.skip(
62-
reason="This test is failing because the image uri and the training script format has changed."
63-
)
6461
def test_attach_deploy(
6562
mxnet_training_job, sagemaker_session, cpu_instance_type, cpu_instance_family
6663
):
@@ -71,7 +68,7 @@ def test_attach_deploy(
7168

7269
estimator.compile_model(
7370
target_instance_family=cpu_instance_family,
74-
input_shape={"data": [1, 1, 28, 28]},
71+
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},
7572
output_path=estimator.output_path,
7673
)
7774

@@ -89,9 +86,6 @@ def test_attach_deploy(
8986
predictor.predict(data)
9087

9188

92-
@pytest.mark.skip(
93-
reason="This test is failing because the image uri and the training script format has changed."
94-
)
9589
def test_deploy_model(
9690
mxnet_training_job,
9791
sagemaker_session,
@@ -123,7 +117,7 @@ def test_deploy_model(
123117

124118
model.compile(
125119
target_instance_family=cpu_instance_family,
126-
input_shape={"data": [1, 1, 28, 28]},
120+
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},
127121
role=role,
128122
job_name=unique_name_from_base("test-deploy-model-compilation-job"),
129123
output_path="/".join(model_data.split("/")[:-1]),
@@ -165,7 +159,7 @@ def test_inferentia_deploy_model(
165159

166160
model.compile(
167161
target_instance_family=inf_instance_family,
168-
input_shape={"data": [1, 1, 28, 28]},
162+
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},
169163
role=role,
170164
job_name=unique_name_from_base("test-deploy-model-compilation-job"),
171165
output_path="/".join(model_data.split("/")[:-1]),

tests/unit/sagemaker/image_uris/test_neo.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,26 @@ def test_algo_uris(algo):
6262

6363

6464
def _test_neo_framework_uris(framework, version):
65-
framework = "neo-{}".format(framework)
65+
framework_in_config = f"neo-{framework}"
66+
framework_in_uri = f"neo-{framework}" if framework == "tensorflow" else f"inference-{framework}"
6667

6768
for region in regions.regions():
6869
if region in ACCOUNTS:
69-
uri = image_uris.retrieve(framework, region, instance_type="ml_c5", version=version)
70-
assert _expected_framework_uri(framework, version, region=region) == uri
70+
uri = image_uris.retrieve(
71+
framework_in_config, region, instance_type="ml_c5", version=version
72+
)
73+
assert _expected_framework_uri(framework_in_uri, version, region=region) == uri
7174
else:
7275
with pytest.raises(ValueError) as e:
73-
image_uris.retrieve(framework, region, instance_type="ml_c5", version=version)
76+
image_uris.retrieve(
77+
framework_in_config, region, instance_type="ml_c5", version=version
78+
)
7479
assert "Unsupported region: {}.".format(region) in str(e.value)
7580

76-
uri = image_uris.retrieve(framework, "us-west-2", instance_type="ml_p2", version=version)
77-
assert _expected_framework_uri(framework, version, processor="gpu") == uri
81+
uri = image_uris.retrieve(
82+
framework_in_config, "us-west-2", instance_type="ml_p2", version=version
83+
)
84+
assert _expected_framework_uri(framework_in_uri, version, processor="gpu") == uri
7885

7986

8087
def test_neo_mxnet(neo_mxnet_version):

tests/unit/test_mxnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _create_compilation_job(input_shape, output_location):
175175

176176

177177
def _neo_inference_image(mxnet_version):
178-
return "301217895009.dkr.ecr.us-west-2.amazonaws.com/sagemaker-neo-{}:{}-cpu-py3".format(
178+
return "301217895009.dkr.ecr.us-west-2.amazonaws.com/sagemaker-inference-{}:{}-cpu-py3".format(
179179
FRAMEWORK.lower(), mxnet_version
180180
)
181181

0 commit comments

Comments
 (0)