Skip to content
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

Pass environment variable SERVING_ENVIRONMENT to SageMaker model #5140

Merged
merged 2 commits into from Dec 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlflow/models/container/__init__.py
Expand Up @@ -38,6 +38,8 @@
DISABLE_NGINX = "DISABLE_NGINX"
ENABLE_MLSERVER = "ENABLE_MLSERVER"

SERVING_ENVIRONMENT = "SERVING_ENVIRONMENT"


def _init(cmd):
"""
Expand Down
9 changes: 7 additions & 2 deletions mlflow/sagemaker/__init__.py
Expand Up @@ -22,7 +22,7 @@
from mlflow.utils.annotations import experimental
from mlflow.utils.file_utils import TempDir
from mlflow.models.container import SUPPORTED_FLAVORS as SUPPORTED_DEPLOYMENT_FLAVORS
from mlflow.models.container import DEPLOYMENT_CONFIG_KEY_FLAVOR_NAME
from mlflow.models.container import DEPLOYMENT_CONFIG_KEY_FLAVOR_NAME, SERVING_ENVIRONMENT


DEFAULT_IMAGE_NAME = "mlflow-pyfunc"
Expand All @@ -41,6 +41,8 @@
DEFAULT_SAGEMAKER_INSTANCE_TYPE = "ml.m4.xlarge"
DEFAULT_SAGEMAKER_INSTANCE_COUNT = 1

SAGEMAKER_SERVING_ENVIRONMENT = "SageMaker"

_logger = logging.getLogger(__name__)

_full_template = "{account}.dkr.ecr.{region}.amazonaws.com/{image}:{version}"
Expand Down Expand Up @@ -1208,7 +1210,10 @@ def _get_deployment_config(flavor_name):
"""
:return: The deployment configuration as a dictionary
"""
deployment_config = {DEPLOYMENT_CONFIG_KEY_FLAVOR_NAME: flavor_name}
deployment_config = {
DEPLOYMENT_CONFIG_KEY_FLAVOR_NAME: flavor_name,
SERVING_ENVIRONMENT: SAGEMAKER_SERVING_ENVIRONMENT,
}
return deployment_config


Expand Down
36 changes: 32 additions & 4 deletions tests/sagemaker/test_deployment.py
Expand Up @@ -216,7 +216,7 @@ def test_attempting_to_deploy_in_asynchronous_mode_without_archiving_throws_exce

@pytest.mark.large
@mock_sagemaker_aws_services
def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_from_local(
def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_and_env_from_local(
pretrained_model, sagemaker_client
):
app_name = "test-app"
Expand Down Expand Up @@ -245,11 +245,18 @@ def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_from_loca
assert app_name in [
endpoint["EndpointName"] for endpoint in sagemaker_client.list_endpoints()["Endpoints"]
]
model_environment = sagemaker_client.describe_model(ModelName=model_name)["PrimaryContainer"][
"Environment"
]
assert model_environment == {
"MLFLOW_DEPLOYMENT_FLAVOR_NAME": "python_function",
"SERVING_ENVIRONMENT": "SageMaker",
}


@pytest.mark.large
@mock_sagemaker_aws_services
def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_from_local(
def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_and_env_from_local(
pretrained_model, sagemaker_client
):
app_name = "test-app"
Expand Down Expand Up @@ -288,11 +295,18 @@ def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_from_
assert app_name in [
endpoint["EndpointName"] for endpoint in sagemaker_client.list_endpoints()["Endpoints"]
]
model_environment = sagemaker_client.describe_model(ModelName=model_name)["PrimaryContainer"][
"Environment"
]
assert model_environment == {
"MLFLOW_DEPLOYMENT_FLAVOR_NAME": "python_function",
"SERVING_ENVIRONMENT": "SageMaker",
}


@pytest.mark.large
@mock_sagemaker_aws_services
def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_from_s3(
def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_and_env_from_s3(
pretrained_model, sagemaker_client
):
local_model_path = _download_artifact_from_uri(pretrained_model.model_uri)
Expand Down Expand Up @@ -328,11 +342,18 @@ def test_deploy_creates_sagemaker_and_s3_resources_with_expected_names_from_s3(
assert app_name in [
endpoint["EndpointName"] for endpoint in sagemaker_client.list_endpoints()["Endpoints"]
]
model_environment = sagemaker_client.describe_model(ModelName=model_name)["PrimaryContainer"][
"Environment"
]
assert model_environment == {
"MLFLOW_DEPLOYMENT_FLAVOR_NAME": "python_function",
"SERVING_ENVIRONMENT": "SageMaker",
}


@pytest.mark.large
@mock_sagemaker_aws_services
def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_from_s3(
def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_and_env_from_s3(
pretrained_model, sagemaker_client
):
local_model_path = _download_artifact_from_uri(pretrained_model.model_uri)
Expand Down Expand Up @@ -373,6 +394,13 @@ def test_deploy_cli_creates_sagemaker_and_s3_resources_with_expected_names_from_
assert app_name in [
endpoint["EndpointName"] for endpoint in sagemaker_client.list_endpoints()["Endpoints"]
]
model_environment = sagemaker_client.describe_model(ModelName=model_name)["PrimaryContainer"][
"Environment"
]
assert model_environment == {
"MLFLOW_DEPLOYMENT_FLAVOR_NAME": "python_function",
"SERVING_ENVIRONMENT": "SageMaker",
}


@pytest.mark.large
Expand Down