Skip to content

fix: enable model.register without 'inference' & 'transform' instances #3228

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 5 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4499,9 +4499,19 @@ def get_create_model_package_request(
"Containers": containers,
"SupportedContentTypes": content_types,
"SupportedResponseMIMETypes": response_types,
"SupportedRealtimeInferenceInstanceTypes": inference_instances,
"SupportedTransformInstanceTypes": transform_instances,
}
if inference_instances is not None:
inference_specification.update(
{
"SupportedRealtimeInferenceInstanceTypes": inference_instances,
}
)
if transform_instances is not None:
inference_specification.update(
{
"SupportedTransformInstanceTypes": transform_instances,
}
)
Copy link
Contributor

Choose a reason for hiding this comment

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

These are optional only if a model registry model package(ie model package group is present). If it is marketplace model package (ie model package group is None) it is mandatory field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a miss from my end. Thanks for pointing it out. I have addressed the same now.

request_dict["InferenceSpecification"] = inference_specification
request_dict["CertifyForMarketplace"] = marketplace_cert
request_dict["ModelApprovalStatus"] = approval_status
Expand Down
9 changes: 2 additions & 7 deletions src/sagemaker/workflow/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,11 @@ def __init__(
super(_RegisterModelStep, self).__init__(
name, StepTypeEnum.REGISTER_MODEL, display_name, description, depends_on, retry_policies
)
deprecated_args_missing = (
content_types is None
or response_types is None
or inference_instances is None
or transform_instances is None
)
deprecated_args_missing = content_types is None or response_types is None
if not (step_args is None) ^ deprecated_args_missing:
raise ValueError(
"step_args and the set of (content_types, response_types, "
"inference_instances, transform_instances) are mutually exclusive. "
") are mutually exclusive. "
"Either of them should be provided."
)

Expand Down
64 changes: 62 additions & 2 deletions tests/unit/sagemaker/workflow/test_pipeline_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,68 @@ def test_pipeline_session_context_for_model_step_without_instance_types(
],
"SupportedContentTypes": ["text/csv"],
"SupportedResponseMIMETypes": ["text/csv"],
"SupportedRealtimeInferenceInstanceTypes": None,
"SupportedTransformInstanceTypes": None,
},
"CertifyForMarketplace": False,
"ModelApprovalStatus": "PendingManualApproval",
"SamplePayloadUrl": "s3://test-bucket/model",
"Task": "IMAGE_CLASSIFICATION",
}

assert register_step_args.create_model_package_request == expected_output


def test_pipeline_session_context_for_model_step_with_one_instance_types(
pipeline_session_mock,
):
model = Model(
name="MyModel",
image_uri="fakeimage",
model_data=ParameterString(name="ModelData", default_value="s3://my-bucket/file"),
sagemaker_session=pipeline_session_mock,
entry_point=f"{DATA_DIR}/dummy_script.py",
source_dir=f"{DATA_DIR}",
role=_ROLE,
)
register_step_args = model.register(
content_types=["text/csv"],
response_types=["text/csv"],
inference_instances=["ml.t2.medium", "ml.m5.xlarge"],
model_package_group_name="MyModelPackageGroup",
task="IMAGE_CLASSIFICATION",
sample_payload_url="s3://test-bucket/model",
framework="TENSORFLOW",
framework_version="2.9",
nearest_model_name="resnet50",
data_input_configuration='{"input_1":[1,224,224,3]}',
)

expected_output = {
"ModelPackageGroupName": "MyModelPackageGroup",
"InferenceSpecification": {
"Containers": [
{
"Image": "fakeimage",
"Environment": {
"SAGEMAKER_PROGRAM": "dummy_script.py",
"SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code",
"SAGEMAKER_CONTAINER_LOG_LEVEL": "20",
"SAGEMAKER_REGION": "us-west-2",
},
"ModelDataUrl": ParameterString(
name="ModelData",
default_value="s3://my-bucket/file",
),
"Framework": "TENSORFLOW",
"FrameworkVersion": "2.9",
"NearestModelName": "resnet50",
"ModelInput": {
"DataInputConfig": '{"input_1":[1,224,224,3]}',
},
}
],
"SupportedContentTypes": ["text/csv"],
"SupportedResponseMIMETypes": ["text/csv"],
"SupportedRealtimeInferenceInstanceTypes": ["ml.t2.medium", "ml.m5.xlarge"],
},
"CertifyForMarketplace": False,
"ModelApprovalStatus": "PendingManualApproval",
Expand Down
66 changes: 64 additions & 2 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2485,8 +2485,70 @@ def test_create_model_package_from_containers_without_instance_types(sagemaker_s
"Containers": containers,
"SupportedContentTypes": content_types,
"SupportedResponseMIMETypes": response_types,
"SupportedRealtimeInferenceInstanceTypes": None,
"SupportedTransformInstanceTypes": None,
},
"ModelPackageDescription": description,
"ModelMetrics": model_metrics,
"MetadataProperties": metadata_properties,
"CertifyForMarketplace": marketplace_cert,
"ModelApprovalStatus": approval_status,
"DriftCheckBaselines": drift_check_baselines,
"CustomerMetadataProperties": customer_metadata_properties,
}
sagemaker_session.sagemaker_client.create_model_package.assert_called_with(**expected_args)


def test_create_model_package_from_containers_with_one_instance_types(sagemaker_session):
model_package_name = "sagemaker-model-package"
containers = ["dummy-container"]
content_types = ["application/json"]
response_types = ["application/json"]
transform_instances = ["ml.m5.xlarge"]
model_metrics = {
"Bias": {
"ContentType": "content-type",
"S3Uri": "s3://...",
}
}
drift_check_baselines = {
"Bias": {
"ConfigFile": {
"ContentType": "content-type",
"S3Uri": "s3://...",
}
}
}

metadata_properties = {
"CommitId": "test-commit-id",
"Repository": "test-repository",
"GeneratedBy": "sagemaker-python-sdk",
"ProjectId": "unit-test",
}
marketplace_cert = (True,)
approval_status = ("Approved",)
description = "description"
customer_metadata_properties = {"key1": "value1"}
sagemaker_session.create_model_package_from_containers(
containers=containers,
content_types=content_types,
response_types=response_types,
transform_instances=transform_instances,
model_package_name=model_package_name,
model_metrics=model_metrics,
metadata_properties=metadata_properties,
marketplace_cert=marketplace_cert,
approval_status=approval_status,
description=description,
drift_check_baselines=drift_check_baselines,
customer_metadata_properties=customer_metadata_properties,
)
expected_args = {
"ModelPackageName": model_package_name,
"InferenceSpecification": {
"Containers": containers,
"SupportedContentTypes": content_types,
"SupportedResponseMIMETypes": response_types,
"SupportedTransformInstanceTypes": transform_instances,
},
"ModelPackageDescription": description,
"ModelMetrics": model_metrics,
Expand Down