Skip to content

feature: upgrade Neo MxNet to 1.7 #1934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/sagemaker/image_uri_config/neo-mxnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
"processors": ["cpu", "gpu"],
"scope": ["inference"],
"version_aliases": {
"0.12.1": "1.5",
"1.0.0": "1.5",
"1.1.0": "1.5",
"1.2": "1.5",
"1.2.0": "1.5",
"1.2.1": "1.5",
"1.3": "1.5",
"1.3.0": "1.5",
"1.4": "1.5",
"1.4.0": "1.5",
"1.4.1": "1.5"
"0.12.1": "1.7",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now all versions will map to 1.7?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. the older version has different repo now. If you compile a model with neo service now. The only container you can use to serve the model is the 1.7 one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤕

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats right, Neo doesnt support compiling with different versions of any Framework (MX, TF, or PT) yet. Whatever version of the framework used to train the model, only the one version in Neo (i.e MX-1.7, PT-1.4, TF-1.15) will be used to load that model. backward compatibility is assumed

"1.0.0": "1.7",
"1.1.0": "1.7",
"1.2": "1.7",
"1.2.0": "1.7",
"1.2.1": "1.7",
"1.3": "1.7",
"1.3.0": "1.7",
"1.4": "1.7",
"1.4.0": "1.7",
"1.4.1": "1.7"
},
"versions": {
"1.5": {
"1.7": {
"py_versions": ["py3"],
"registries": {
"af-south-1": "774647643957",
Expand All @@ -42,7 +42,7 @@
"us-west-1": "710691900526",
"us-west-2": "301217895009"
},
"repository": "sagemaker-neo-mxnet"
"repository": "sagemaker-inference-mxnet"
}
}
}
2 changes: 1 addition & 1 deletion src/sagemaker/image_uri_config/neo-pytorch.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"us-west-1": "710691900526",
"us-west-2": "301217895009"
},
"repository": "sagemaker-neo-pytorch"
"repository": "sagemaker-inference-pytorch"
}
}
}
35 changes: 20 additions & 15 deletions tests/data/mxnet_mnist/mnist_neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,32 @@ def train(
save(model_dir, mlp_model)


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

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

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

def transform_fn(mod, payload, input_content_type, requested_output_content_type):
import neomxnet # noqa: F401

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

# Softmax (assumes batch size 1)
inference_payload = np.asarray(json.loads(payload.decode("utf-8")))
result = mod.predict(inference_payload)
result = np.squeeze(result)
result_exp = np.exp(result - np.max(result))
result = result_exp / np.sum(result_exp)

response_body = json.dumps(result.tolist())
response_body = json.dumps(result.asnumpy().tolist())
content_type = "application/json"

return response_body, content_type


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

parser.add_argument("--batch-size", type=int, default=100)
parser.add_argument("--epochs", type=int, default=10)
parser.add_argument("--epochs", type=int, default=1)
parser.add_argument("--learning-rate", type=float, default=0.1)

parser.add_argument("--model-dir", type=str, default=os.environ["SM_MODEL_DIR"])
Expand Down
12 changes: 3 additions & 9 deletions tests/integ/test_neo_mxnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ def mxnet_training_job(


@pytest.mark.canary_quick
@pytest.mark.skip(
reason="This test is failing because the image uri and the training script format has changed."
)
def test_attach_deploy(
mxnet_training_job, sagemaker_session, cpu_instance_type, cpu_instance_family
):
Expand All @@ -71,7 +68,7 @@ def test_attach_deploy(

estimator.compile_model(
target_instance_family=cpu_instance_family,
input_shape={"data": [1, 1, 28, 28]},
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},
output_path=estimator.output_path,
)

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


@pytest.mark.skip(
reason="This test is failing because the image uri and the training script format has changed."
)
def test_deploy_model(
mxnet_training_job,
sagemaker_session,
Expand Down Expand Up @@ -123,7 +117,7 @@ def test_deploy_model(

model.compile(
target_instance_family=cpu_instance_family,
input_shape={"data": [1, 1, 28, 28]},
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},
role=role,
job_name=unique_name_from_base("test-deploy-model-compilation-job"),
output_path="/".join(model_data.split("/")[:-1]),
Expand Down Expand Up @@ -165,7 +159,7 @@ def test_inferentia_deploy_model(

model.compile(
target_instance_family=inf_instance_family,
input_shape={"data": [1, 1, 28, 28]},
input_shape={"data": [1, 1, 28, 28], "softmax_label": [1]},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isnt needed for Inferentia compilation flow. But if it works thats ok, it doesnt hurt anything.

role=role,
job_name=unique_name_from_base("test-deploy-model-compilation-job"),
output_path="/".join(model_data.split("/")[:-1]),
Expand Down
19 changes: 13 additions & 6 deletions tests/unit/sagemaker/image_uris/test_neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,26 @@ def test_algo_uris(algo):


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

for region in regions.regions():
if region in ACCOUNTS:
uri = image_uris.retrieve(framework, region, instance_type="ml_c5", version=version)
assert _expected_framework_uri(framework, version, region=region) == uri
uri = image_uris.retrieve(
framework_in_config, region, instance_type="ml_c5", version=version
)
assert _expected_framework_uri(framework_in_uri, version, region=region) == uri
else:
with pytest.raises(ValueError) as e:
image_uris.retrieve(framework, region, instance_type="ml_c5", version=version)
image_uris.retrieve(
framework_in_config, region, instance_type="ml_c5", version=version
)
assert "Unsupported region: {}.".format(region) in str(e.value)

uri = image_uris.retrieve(framework, "us-west-2", instance_type="ml_p2", version=version)
assert _expected_framework_uri(framework, version, processor="gpu") == uri
uri = image_uris.retrieve(
framework_in_config, "us-west-2", instance_type="ml_p2", version=version
)
assert _expected_framework_uri(framework_in_uri, version, processor="gpu") == uri


def test_neo_mxnet(neo_mxnet_version):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_mxnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def _create_compilation_job(input_shape, output_location):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there similar tests for pytorch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we only have unit tests for pytorch. It's debatable if we have a gap there since the mxnet test is already testing the part of the logic that calls the neo service to compile model. We are planning to revamp our build system to handle more parallel integ testing. Once the system is more scalable we will be in a better position to add integ tests.


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

Expand Down