mlflow.sagemaker

mlflow.sagemaker模块提供用于将 MLflow 模型部署到 Amazon SageMaker 的 API。

class mlflow.sagemaker.SageMakerDeploymentClient(target_uri)[source]

基类: mlflow.deployments.base.BaseDeploymentClient

为 SageMaker 初始化部署客户端。默认区域和假定角色 ARN 将根据 target_uri 的值设置。

这个类旨在取代其他 mlflow.sagemaker 实时服务 API。 它也被设计为通过 mlflow.deployments 模块使用。 这意味着你可以使用 mlflow deployments CLI 将模型部署到 SageMaker,并通过 mlflow.deployments.get_deploy_client 函数获取客户端。

Parameters

target_uri

一个符合以下格式之一的 URI:

  • sagemaker:这将把默认区域设置为us-west-2,并将默认的假设角色 ARN 设置为None

  • sagemaker:/region_name:这将把默认区域设置为region_name,并将默认的假设角色 ARN 设置为None

  • sagemaker:/region_name/assumed_role_arn:这将把默认区域设置为region_name,并将默认的假设角色 ARN 设置为assumed_role_arn

当提供了assumed_role_arn但未提供region_name时,将引发 MlflowException。

create_deployment(name, model_uri, flavor=None, config=None, endpoint=None)[source]

在 AWS SageMaker 上部署 MLflow 模型。当前活动的 AWS 帐户必须已配置正确的权限。

此函数创建一个 SageMaker 端点。有关此端点接受的输入数据格式的更多信息,请参见 MLflow deployment tools documentation

Parameters
  • name – 已部署应用程序的名称。

  • model_uri

    以 URI 格式表示,要部署到 SageMaker 的 MLflow 模型的位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs://run-relative/path/to/model

    • models://

    • models://

    有关受支持的 URI 方案的更多信息,请参见 Referencing Artifacts

  • flavor – 模型用于部署的 flavor 的名称。必须是 None 或 mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS 之一。如果 None,将从模型的可用 flavor 中自动选择一个。如果指定的 flavor 不存在或不支持部署,则会抛出异常。

  • config

    Configuration parameters. The supported parameters are:

    • assume_role_arn: The name of an IAM cross-account role to be assumed to deploy SageMaker to another AWS account. If this parameter is not specified, the role given in the target_uri will be used. If the role is not given in the target_uri, defaults to us-west-2.

    • execution_role_arn: The name of an IAM role granting the SageMaker service permissions to access the specified Docker image and S3 bucket containing MLflow model artifacts. If unspecified, the currently-assumed role will be used. This execution role is passed to the SageMaker service when creating a SageMaker model from the specified MLflow model. It is passed as the ExecutionRoleArn parameter of the SageMaker CreateModel API call. This role is not assumed for any other call. For more information about SageMaker execution roles for model creation, see https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html.

    • bucket: S3 bucket where model artifacts will be stored. Defaults to a SageMaker-compatible bucket name.

    • image_url: URL of the ECR-hosted Docker image the model should be deployed into, produced by mlflow sagemaker build-and-push-container. This parameter can also be specified by the environment variable MLFLOW_SAGEMAKER_DEPLOY_IMG_URL.

    • region_name: Name of the AWS region to which to deploy the application. If unspecified, use the region name given in the target_uri. If it is also not specified in the target_uri, defaults to us-west-2.

    • archive: If True, any pre-existing SageMaker application resources that become inactive (i.e. as a result of deploying in mlflow.sagemaker.DEPLOYMENT_MODE_REPLACE mode) are preserved. These resources may include unused SageMaker models and endpoint configurations that were associated with a prior version of the application endpoint. If False, these resources are deleted. In order to use archive=False, create_deployment() must be executed synchronously with synchronous=True. Defaults to False.

    • instance_type: The type of SageMaker ML instance on which to deploy the model. For a list of supported instance types, see https://aws.amazon.com/sagemaker/pricing/instance-types/. Defaults to ml.m4.xlarge.

    • instance_count: The number of SageMaker ML instances on which to deploy the model. Defaults to 1.

    • synchronous: If True, this function will block until the deployment process succeeds or encounters an irrecoverable failure. If False, this function will return immediately after starting the deployment process. It will not wait for the deployment process to complete; in this case, the caller is responsible for monitoring the health and status of the pending deployment via native SageMaker APIs or the AWS console. Defaults to True.

    • timeout_seconds: If synchronous is True, the deployment process will return after the specified number of seconds if no definitive result (success or failure) is achieved. Once the function returns, the caller is responsible for monitoring the health and status of the pending deployment using native SageMaker APIs or the AWS console. If synchronous is False, this parameter is ignored. Defaults to 300.

    • vpc_config: A dictionary specifying the VPC configuration to use when creating the new SageMaker model associated with this application. The acceptable values for this parameter are identical to those of the VpcConfig parameter in the SageMaker boto3 client’s create_model method. For more information, see https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html. Defaults to None.

    • data_capture_config: A dictionary specifying the data capture configuration to use when creating the new SageMaker model associated with this application. For more information, see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DataCaptureConfig.html. Defaults to None.

    • variant_name: A string specifying the desired name when creating a production variant. Defaults to None.

    • async_inference_config: A dictionary specifying the async_inference_configuration

    • serverless_config: A dictionary specifying the serverless_configuration

    • env: A dictionary specifying environment variables as key-value pairs to be set for the deployed model. Defaults to None.

    • tags: A dictionary of key-value pairs representing additional tags to be set for the deployed model. Defaults to None.

  • endpoint – (可选) 用于在其下创建部署的 endpoint。当前不受支持

Python example
from mlflow.deployments import get_deploy_client

vpc_config = {
    "SecurityGroupIds": [
        "sg-123456abc",
    ],
    "Subnets": [
        "subnet-123456abc",
    ],
}
config = dict(
    assume_role_arn="arn:aws:123:role/assumed_role",
    execution_role_arn="arn:aws:456:role/execution_role",
    bucket_name="my-s3-bucket",
    image_url="1234.dkr.ecr.us-east-1.amazonaws.com/mlflow-test:1.23.1",
    region_name="us-east-1",
    archive=False,
    instance_type="ml.m5.4xlarge",
    instance_count=1,
    synchronous=True,
    timeout_seconds=300,
    vpc_config=vpc_config,
    variant_name="prod-variant-1",
    env={"DISABLE_NGINX": "true", "GUNICORN_CMD_ARGS": '"--timeout 60"'},
    tags={"training_timestamp": "2022-11-01T05:12:26"},
)
client = get_deploy_client("sagemaker")
client.create_deployment(
    "my-deployment",
    model_uri="/mlruns/0/abc/model",
    flavor="python_function",
    config=config,
)
Command-line example
mlflow deployments create --target sagemaker:/us-east-1/arn:aws:123:role/assumed_role \
        --name my-deployment \
        --model-uri /mlruns/0/abc/model \
        --flavor python_function\
        -C execution_role_arn=arn:aws:456:role/execution_role \
        -C bucket_name=my-s3-bucket \
        -C image_url=1234.dkr.ecr.us-east-1.amazonaws.com/mlflow-test:1.23.1 \
        -C region_name=us-east-1 \
        -C archive=False \
        -C instance_type=ml.m5.4xlarge \
        -C instance_count=1 \
        -C synchronous=True \
        -C timeout_seconds=300 \
        -C variant_name=prod-variant-1 \
        -C vpc_config='{"SecurityGroupIds": ["sg-123456abc"], \
        "Subnets": ["subnet-123456abc"]}' \
        -C data_capture_config='{"EnableCapture": True, \
        'InitialSamplingPercentage': 100, 'DestinationS3Uri": 's3://my-bucket/path', \
        'CaptureOptions': [{'CaptureMode': 'Output'}]}'
        -C env='{"DISABLE_NGINX": "true", "GUNICORN_CMD_ARGS": ""--timeout 60""}' \
        -C tags='{"training_timestamp": "2022-11-01T05:12:26"}' \
create_endpoint(name, config=None)[source]

使用指定的目标创建一个端点。默认情况下,该方法应阻塞直到创建完成(即直到可以在该端点中创建部署)。在发生冲突的情况下(例如由于与现有端点冲突而无法创建指定的端点),会引发mlflow.exceptions.MlflowException。有关对异步创建支持和其他配置的更多详细信息,请参阅针对目标的插件文档。

Parameters
  • name – 用于端点的唯一名称。如果存在另一个具有相同名称的端点,则会引发 mlflow.exceptions.MlflowException

  • config – (可选)Dict,包含针对该端点的特定配置。

Returns

Dict 对应于已创建的 endpoint,必须包含 ‘name’ 键。

delete_deployment(name, config=None, endpoint=None)[source]

删除一个 SageMaker 应用程序。

Parameters
  • name – 已部署应用的名称。

  • config

    配置参数。支持的参数包括:

    • assume_role_arn: 要被假定以删除 SageMaker 部署的 IAM 角色名称。

    • region_name: 应用部署所在的 AWS 区域名称。默认值为 us-west-2target_uri 中提供的区域。

    • archive: 如果 True,与指定应用关联的资源(例如其相关模型和端点配置)将被保留。如果 False,这些资源将被删除。要使用 archive=Falsedelete() 必须以 synchronous=True 同步执行。默认值为 False

    • synchronous: 如果 True,此函数将阻塞直到删除过程成功或遇到不可恢复的失败。如果 False,此函数在启动删除过程后立即返回。它不会等待删除过程完成;在这种情况下,调用方负责通过原生 SageMaker API 或 AWS 控制台监控删除过程的状态。默认值为 True

    • timeout_seconds: 如果 synchronousTrue,在未获得明确结果(成功或失败)的情况下,删除过程将在指定的秒数后返回。一旦函数返回,调用方负责通过原生 SageMaker API 或 AWS 控制台监控删除过程的状态。如果 synchronous 为 False,则忽略此参数。默认值为 300

  • endpoint – (可选) Endpoint 包含要删除的部署。目前不支持

Python example
from mlflow.deployments import get_deploy_client

config = dict(
    assume_role_arn="arn:aws:123:role/assumed_role",
    region_name="us-east-1",
    archive=False,
    synchronous=True,
    timeout_seconds=300,
)
client = get_deploy_client("sagemaker")
client.delete_deployment("my-deployment", config=config)
Command-line example
mlflow deployments delete --target sagemaker \
        --name my-deployment \
        -C assume_role_arn=arn:aws:123:role/assumed_role \
        -C region_name=us-east-1 \
        -C archive=False \
        -C synchronous=True \
        -C timeout_seconds=300
delete_endpoint(endpoint)[source]

从指定目标删除该端点。删除操作应幂等(即在不存在的部署上重试删除时不应失败)。

Parameters

endpoint – 要删除的 endpoint 名称

explain(deployment_name=None, df=None, endpoint=None)[source]

此功能尚未实现,将在未来推出。

get_deployment(name, endpoint=None)[source]

返回一个描述指定部署的字典。

如果需要指定区域名称,必须在 target_uri 中使用 AWS 区域来初始化该插件,例如 sagemaker:/us-east-1

要假设 IAM 角色,插件必须使用 AWS 区域和角色 ARN 在 target_uri 中进行初始化,例如 sagemaker:/us-east-1/arn:aws:1234:role/assumed_role

在检索部署时发生错误,也会抛出 mlflow.exceptions.MlflowException

Parameters
  • name – 要检索的部署的名称

  • endpoint – (可选) 包含要获取的部署的端点。当前不支持

Returns

A dictionary that describes the specified deployment

Python example
from mlflow.deployments import get_deploy_client

client = get_deploy_client("sagemaker:/us-east-1/arn:aws:123:role/assumed_role")
client.get_deployment("my-deployment")
Command-line example
mlflow deployments get --target sagemaker:/us-east-1/arn:aws:1234:role/assumed_role \
    --name my-deployment
get_endpoint(endpoint)[source]

返回一个描述指定端点的字典,如果不存在具有提供名称的端点,则抛出 py:class:mlflow.exception.MlflowException。该字典保证包含一个键 ‘name’,其值为端点名称。返回字典的其他字段及其类型可能因目标不同而有所不同。

Parameters

endpoint – 要获取的 endpoint 的名称

list_deployments(endpoint=None)[source]

列出部署。此方法返回描述每个部署的字典列表。

如果需要指定区域名称,必须在 target_uri 中使用 AWS 区域来初始化该插件,例如 sagemaker:/us-east-1

要假设一个 IAM 角色,插件必须在 target_uri 中使用 AWS 区域和角色 ARN 进行初始化,例如 sagemaker:/us-east-1/arn:aws:1234:role/assumed_role

Parameters

endpoint – (可选) 列出指定 endpoint 中的部署。当前不受支持

Returns

对应于部署的字典列表。

Python example
from mlflow.deployments import get_deploy_client

client = get_deploy_client("sagemaker:/us-east-1/arn:aws:123:role/assumed_role")
client.list_deployments()
Command-line example
mlflow deployments list --target sagemaker:/us-east-1/arn:aws:1234:role/assumed_role
list_endpoints()[source]

列出指定目标中的 endpoints。此方法预计返回所有 endpoints 的非分页列表(另一种方法是返回一个包含实际 endpoints 的 ‘endpoints’ 字段的 dict,插件可以在返回的字典中指定其他字段,例如用于分页的 next_page_token 字段,并接受一个 pagination_args 参数以将与分页相关的参数传递给此方法)。

Returns

对应端点的字典列表。每个字典都保证包含一个‘name’键,该键包含端点的名称。返回字典的其他字段及其类型可能在不同目标之间有所不同。

predict(deployment_name=None, inputs=None, endpoint=None, params: Optional[dict[str, typing.Any]] = None)[source]

使用提供的 PyFunc 输入,从指定的部署中计算预测结果。

该方法的输入/输出类型与 MLflow PyFunc prediction interface 匹配。

如果需要指定区域名称,必须在 target_uri 中使用 AWS 区域来初始化该插件,例如 sagemaker:/us-east-1

要假设一个 IAM 角色,插件必须在 target_uri 中使用 AWS 区域和角色 ARN 初始化,例如 sagemaker:/us-east-1/arn:aws:1234:role/assumed_role

Parameters
  • deployment_name – 要进行预测的部署的名称。

  • inputs – 用于传递给部署或模型端点以进行推理的输入数据(或参数)。有关支持的输入类型的完整列表,请参见 Inference API

  • endpoint – 用于进行预测的端点。当前不支持

  • params – 可选参数,用于调用该端点。

Returns

PyFunc 的输出,例如 Pandas DataFrame、Pandas Series 或 NumPy 数组。 有关受支持输出类型的完整列表,请参见 Inference API

Python example
import pandas as pd
from mlflow.deployments import get_deploy_client

df = pd.DataFrame(data=[[1, 2, 3]], columns=["feat1", "feat2", "feat3"])
client = get_deploy_client("sagemaker:/us-east-1/arn:aws:123:role/assumed_role")
client.predict("my-deployment", df)
Command-line example
cat > ./input.json <<- input
{"feat1": {"0": 1}, "feat2": {"0": 2}, "feat3": {"0": 3}}
input

mlflow deployments predict \
    --target sagemaker:/us-east-1/arn:aws:1234:role/assumed_role \
    --name my-deployment \
    --input-path ./input.json
update_deployment(name, model_uri, flavor=None, config=None, endpoint=None)[source]

在 AWS SageMaker 上更新部署。此函数可以将新模型替换或添加到现有 SageMaker endpoint。默认情况下,此函数会用新模型替换现有模型。当前激活的 AWS 帐户必须已设置正确的权限。

Parameters
  • name – 已部署应用程序的名称。

  • model_uri

    以 URI 格式表示,要部署到 SageMaker 的 MLflow 模型的位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs:/<mlflow_run_id>/run-relative/path/to/model

    • models:/<model_name>/<model_version>

    • models:/<model_name>/<stage>

    有关受支持的 URI 方案的更多信息,请参见 Referencing Artifacts

  • flavor – 用于部署的模型 flavor 的名称。必须为 None 或 mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS 之一。如果 None,将从模型可用的 flavor 中自动选择一个。如果指定的 flavor 不存在或不支持部署,则会抛出异常。

  • config

    Configuration parameters. The supported parameters are:

    • assume_role_arn: The name of an IAM cross-account role to be assumed to deploy SageMaker to another AWS account. If this parameter is not specified, the role given in the target_uri will be used. If the role is not given in the target_uri, defaults to us-west-2.

    • execution_role_arn: The name of an IAM role granting the SageMaker service permissions to access the specified Docker image and S3 bucket containing MLflow model artifacts. If unspecified, the currently-assumed role will be used. This execution role is passed to the SageMaker service when creating a SageMaker model from the specified MLflow model. It is passed as the ExecutionRoleArn parameter of the SageMaker CreateModel API call. This role is not assumed for any other call. For more information about SageMaker execution roles for model creation, see https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html.

    • bucket: S3 bucket where model artifacts will be stored. Defaults to a SageMaker-compatible bucket name.

    • image_url: URL of the ECR-hosted Docker image the model should be deployed into, produced by mlflow sagemaker build-and-push-container. This parameter can also be specified by the environment variable MLFLOW_SAGEMAKER_DEPLOY_IMG_URL.

    • region_name: Name of the AWS region to which to deploy the application. If unspecified, use the region name given in the target_uri. If it is also not specified in the target_uri, defaults to us-west-2.

    • mode: The mode in which to deploy the application. Must be one of the following:

      mlflow.sagemaker.DEPLOYMENT_MODE_REPLACE

      If an application of the specified name exists, its model(s) is replaced with the specified model. If no such application exists, it is created with the specified name and model. This is the default mode.

      mlflow.sagemaker.DEPLOYMENT_MODE_ADD

      Add the specified model to a pre-existing application with the specified name, if one exists. If the application does not exist, a new application is created with the specified name and model. NOTE: If the application already exists, the specified model is added to the application’s corresponding SageMaker endpoint with an initial weight of zero (0). To route traffic to the model, update the application’s associated endpoint configuration using either the AWS console or the UpdateEndpointWeightsAndCapacities function defined in https://docs.aws.amazon.com/sagemaker/latest/dg/API_UpdateEndpointWeightsAndCapacities.html.

    • archive: If True, any pre-existing SageMaker application resources that become inactive (i.e. as a result of deploying in mlflow.sagemaker.DEPLOYMENT_MODE_REPLACE mode) are preserved. These resources may include unused SageMaker models and endpoint configurations that were associated with a prior version of the application endpoint. If False, these resources are deleted. In order to use archive=False, update_deployment() must be executed synchronously with synchronous=True. Defaults to False.

    • instance_type: The type of SageMaker ML instance on which to deploy the model. For a list of supported instance types, see https://aws.amazon.com/sagemaker/pricing/instance-types/. Defaults to ml.m4.xlarge.

    • instance_count: The number of SageMaker ML instances on which to deploy the model. Defaults to 1.

    • synchronous: If True, this function will block until the deployment process succeeds or encounters an irrecoverable failure. If False, this function will return immediately after starting the deployment process. It will not wait for the deployment process to complete; in this case, the caller is responsible for monitoring the health and status of the pending deployment via native SageMaker APIs or the AWS console. Defaults to True.

    • timeout_seconds: If synchronous is True, the deployment process will return after the specified number of seconds if no definitive result (success or failure) is achieved. Once the function returns, the caller is responsible for monitoring the health and status of the pending deployment using native SageMaker APIs or the AWS console. If synchronous is False, this parameter is ignored. Defaults to 300.

    • variant_name: A string specifying the desired name when creating a production variant. Defaults to None.

    • vpc_config: A dictionary specifying the VPC configuration to use when creating the new SageMaker model associated with this application. The acceptable values for this parameter are identical to those of the VpcConfig parameter in the SageMaker boto3 client’s create_model method. For more information, see https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html. Defaults to None.

    • data_capture_config: A dictionary specifying the data capture configuration to use when creating the new SageMaker model associated with this application. For more information, see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DataCaptureConfig.html. Defaults to None.

    • async_inference_config: A dictionary specifying the async config configuration. Defaults to None.

    • env: A dictionary specifying environment variables as key-value pairs to be set for the deployed model. Defaults to None.

    • tags: A dictionary of key-value pairs representing additional tags to be set for the deployed model. Defaults to None.

  • endpoint – (可选) 包含要更新的部署的端点。当前不支持

Python example
from mlflow.deployments import get_deploy_client

vpc_config = {
    "SecurityGroupIds": [
        "sg-123456abc",
    ],
    "Subnets": [
        "subnet-123456abc",
    ],
}
data_capture_config = {
    "EnableCapture": True,
    "InitialSamplingPercentage": 100,
    "DestinationS3Uri": "s3://my-bucket/path",
    "CaptureOptions": [{"CaptureMode": "Output"}],
}
config = dict(
    assume_role_arn="arn:aws:123:role/assumed_role",
    execution_role_arn="arn:aws:456:role/execution_role",
    bucket_name="my-s3-bucket",
    image_url="1234.dkr.ecr.us-east-1.amazonaws.com/mlflow-test:1.23.1",
    region_name="us-east-1",
    mode="replace",
    archive=False,
    instance_type="ml.m5.4xlarge",
    instance_count=1,
    synchronous=True,
    timeout_seconds=300,
    variant_name="prod-variant-1",
    vpc_config=vpc_config,
    data_capture_config=data_capture_config,
    env={"DISABLE_NGINX": "true", "GUNICORN_CMD_ARGS": '"--timeout 60"'},
    tags={"training_timestamp": "2022-11-01T05:12:26"},
)
client = get_deploy_client("sagemaker")
client.update_deployment(
    "my-deployment",
    model_uri="/mlruns/0/abc/model",
    flavor="python_function",
    config=config,
)
Command-line example
mlflow deployments update --target sagemaker:/us-east-1/arn:aws:123:role/assumed_role \
        --name my-deployment \
        --model-uri /mlruns/0/abc/model \
        --flavor python_function\
        -C execution_role_arn=arn:aws:456:role/execution_role \
        -C bucket_name=my-s3-bucket \
        -C image_url=1234.dkr.ecr.us-east-1.amazonaws.com/mlflow-test:1.23.1 \
        -C region_name=us-east-1 \
        -C mode=replace \
        -C archive=False \
        -C instance_type=ml.m5.4xlarge \
        -C instance_count=1 \
        -C synchronous=True \
        -C timeout_seconds=300 \
        -C variant_name=prod-variant-1 \
        -C vpc_config='{"SecurityGroupIds": ["sg-123456abc"], \
        "Subnets": ["subnet-123456abc"]}' \
        -C data_capture_config='{"EnableCapture": True, \
        "InitialSamplingPercentage": 100, "DestinationS3Uri": "s3://my-bucket/path", \
        "CaptureOptions": [{"CaptureMode": "Output"}]}'
        -C env='{"DISABLE_NGINX": "true", "GUNICORN_CMD_ARGS": ""--timeout 60""}' \
        -C tags='{"training_timestamp": "2022-11-01T05:12:26"}' \
update_endpoint(endpoint, config=None)[source]

使用指定名称更新端点。您可以更新端点的任何特定目标属性(通过 config)。默认情况下,此方法应阻塞直到更新完成(即直到可以在该端点中创建部署)。有关对异步更新及其他配置支持的更多详细信息,请参阅特定目标的插件文档。

Parameters
  • endpoint – 要更新的 endpoint 的唯一名称

  • config – (可选) dict,包含针对该端点的目标特定配置

mlflow.sagemaker.deploy_transform_job(job_name, model_uri, s3_input_data_type, s3_input_uri, content_type, s3_output_path, compression_type='None', split_type='Line', accept='text/csv', assemble_with='Line', input_filter='$', output_filter='$', join_resource='None', execution_role_arn=None, assume_role_arn=None, bucket=None, image_url=None, region_name='us-west-2', instance_type='ml.m4.xlarge', instance_count=1, vpc_config=None, flavor=None, archive=False, synchronous=True, timeout_seconds=1200)[source]

在 AWS SageMaker 上部署一个 MLflow 模型并创建相应的批量转换作业。当前处于活动状态的 AWS 账户必须已设置正确的权限。

Parameters
  • job_name – 已部署的 Sagemaker 批量转换作业的名称。

  • model_uri

    以 URI 格式表示,要部署到 SageMaker 的 MLflow 模型的位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs:/<mlflow_run_id>/run-relative/path/to/model

    • models:/<model_name>/<model_version>

    • models:/<model_name>/<stage>

    有关受支持的 URI 方案的更多信息,请参见 Referencing Artifacts

  • s3_input_data_type – 转换作业的输入数据类型。

  • s3_input_uri – S3 键名前缀或输入数据的清单。

  • content_type – 数据的多用途互联网邮件扩展(MIME)类型。

  • s3_output_path – 用于存储 Sagemaker 转换作业输出结果的 S3 路径。

  • compression_type – 转换数据的压缩类型。

  • split_type – 将转换作业的数据文件拆分为更小批次的方法。

  • accept – 输出数据的多用途互联网邮件扩展(MIME)类型。

  • assemble_with – 将转换作业的结果组装为单个 S3 对象的方法。

  • input_filter – 一个 JSONPath 表达式,用于为转换作业选择部分输入数据。

  • output_filter – 一个用于从转换作业的输出数据中选择一部分的 JSONPath 表达式。

  • join_resource – 要与转换后的数据连接的数据源。

  • execution_role_arn

    IAM 角色的名称,授予 SageMaker 服务权限以访问指定的包含 MLflow 模型工件的 Docker 镜像和 S3 存储桶。如果未指定,则将使用当前所假定的角色。此执行角色在从指定的 MLflow 模型创建 SageMaker 模型时传递给 SageMaker 服务。它作为 ExecutionRoleArn 参数传递给 SageMaker CreateModel API call。该角色 不会 被用于任何其他调用。如需有关用于模型创建的 SageMaker 执行角色的更多信息,请参见 https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html

  • assume_role_arn – IAM 跨账户角色的名称,用于在将 SageMaker 部署到另一个 AWS 账户时进行假定。如果未指定,SageMaker 将部署到当前活动的 AWS 账户。

  • bucket – 存放模型工件的 S3 存储桶。默认为与 SageMaker 兼容的存储桶名称。

  • image_url – 模型应部署到的由 ECR 托管的 Docker 镜像的 URL, 由 mlflow sagemaker build-and-push-container 生成。该参数也可以通过环境变量 MLFLOW_SAGEMAKER_DEPLOY_IMG_URL 指定。

  • region_name – 将应用程序部署到的 AWS 区域名称。

  • instance_type – 部署模型所使用的 SageMaker ML 实例的类型。有关受支持实例类型的列表,请参阅 https://aws.amazon.com/sagemaker/pricing/instance-types/.

  • instance_count – 在其上部署模型的 SageMaker ML 实例数量。

  • vpc_config

    一个字典,指定在为此批量转换作业创建关联的新 SageMaker 模型时要使用的 VPC 配置。此参数可接受的值与 VpcConfig 参数在 SageMaker boto3 client’s create_model method 中的取值相同。欲了解更多信息,请参见 https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html

    示例
    import mlflow.sagemaker as mfs
    
    vpc_config = {
        "SecurityGroupIds": [
            "sg-123456abc",
        ],
        "Subnets": [
            "subnet-123456abc",
        ],
    }
    mfs.deploy_transform_job(..., vpc_config=vpc_config)
    

  • flavor – 用于部署的模型 flavor 的名称。必须是 None 或 mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS 之一。如果 None,则会从模型可用的 flavors 中自动选择一个 flavor。如果指定的 flavor 不存在或不支持部署,将抛出异常。

  • archive – 如果 True,诸如 Sagemaker 模型和存储在 S3 的模型工件等资源会在批量转换作业完成后被保留。如果 False,这些资源会被删除。为了使用 archive=False,必须以 deploy_transform_job() 同步执行,并设置 synchronous=True

  • synchronous – 如果 True,此函数将阻塞直到部署过程成功或遇到不可恢复的失败。如果 False,此函数将在启动部署过程后立即返回。它不会等待部署过程完成;在这种情况下,调用方负责通过原生 SageMaker APIs 或 AWS console 监控挂起部署的健康状况和状态。

  • timeout_seconds – 如果 synchronousTrue,则部署过程将在指定的秒数后返回,如果未取得明确结果(成功或失败)。函数返回后,调用方负责使用原生 SageMaker APIs 或 AWS 控制台监控挂起部署的运行状况和状态。如果 synchronousFalse,则忽略此参数。

mlflow.sagemaker.push_image_to_ecr(image='mlflow-pyfunc')[source]

将本地 Docker 镜像推送到 AWS ECR。

镜像会被推送到当前活动的 AWS 账号下以及当前活动的 AWS 区域。

Parameters

image – Docker 镜像名称。

mlflow.sagemaker.push_model_to_sagemaker(model_name, model_uri, execution_role_arn=None, assume_role_arn=None, bucket=None, image_url=None, region_name='us-west-2', vpc_config=None, flavor=None)[source]

从 MLflow 模型工件创建一个 SageMaker 模型。 当前活动的 AWS 帐户必须具有正确的权限设置。

Parameters
  • model_name – Sagemaker 模型的名称。

  • model_uri

    以 URI 格式表示,要部署到 SageMaker 的 MLflow 模型的位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs:/<mlflow_run_id>/run-relative/path/to/model

    • models:/<model_name>/<model_version>

    • models:/<model_name>/<stage>

    有关受支持的 URI 方案的更多信息,请参见 Referencing Artifacts

  • execution_role_arn

    一个 IAM 角色的名称,该角色授予 SageMaker 服务访问指定的 Docker 镜像和包含 MLflow 模型工件的 S3 存储桶的权限。如果未指定,则使用当前所假定的角色。 在基于指定的 MLflow 模型创建 SageMaker 模型时,此执行角色会传递给 SageMaker 服务。它作为 ExecutionRoleArn 参数传递给 SageMaker CreateModel API call。此角色 不会 被用于任何其他调用。有关用于模型创建的 SageMaker 执行角色的更多信息,请参见 https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html

  • assume_role_arn – 指定要假设的 IAM 跨账号角色的名称,用于将 SageMaker 部署到另一个 AWS 账户。如果未指定,SageMaker 将部署到当前活动的 AWS 账户。

  • bucket – 存储模型工件的 S3 存储桶。默认是一个与 SageMaker 兼容的存储桶名称。

  • image_url – 模型应部署到的由 ECR 托管的 Docker 镜像的 URL,由 mlflow sagemaker build-and-push-container 生成。该参数也可以通过环境变量 MLFLOW_SAGEMAKER_DEPLOY_IMG_URL 指定。

  • region_name – 要将应用部署到的 AWS 区域的名称。

  • vpc_config

    一个字典,用于指定在创建新的 SageMaker 模型时使用的 VPC 配置。此参数可接受的值与 VpcConfig 参数在 SageMaker boto3 client’s create_model method 中的值相同。有关更多信息,请参见 https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html

    示例
    import mlflow.sagemaker as mfs
    
    vpc_config = {
        "SecurityGroupIds": [
            "sg-123456abc",
        ],
        "Subnets": [
            "subnet-123456abc",
        ],
    }
    mfs.push_model_to_sagemaker(..., vpc_config=vpc_config)
    

  • flavor – 用于部署的模型 flavor 的名称。必须为 None 或 mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS 之一。如果 None,则会从模型的可用 flavors 中自动选择一种 flavor。如果指定的 flavor 不存在或不支持部署,将抛出异常。

mlflow.sagemaker.run_local(name, model_uri, flavor=None, config=None)[source]

在本地以与 SageMaker 兼容的 Docker 容器中提供模型服务。

请注意,本地部署的模型不能通过其他部署 API(例如 update_deploymentdelete_deployment 等)进行管理。

Parameters
  • name – 本地服务应用的名称。

  • model_uri

    以 URI 格式表示的本地部署 MLflow 模型的位置。 例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs://run-relative/path/to/model

    • models://

    • models://

    有关受支持的 URI 方案的更多信息,请参阅 Referencing Artifacts.

  • flavor – 用于模型部署的 flavor 名称。必须是 None 或 mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS 之一。 如果 None,则会从模型可用的 flavors 中自动选择一个 flavor。 如果指定的 flavor 不存在或不支持部署,将抛出异常。

  • config

    配置参数。支持的参数有:

    • image:用于模型服务的 Docker 镜像名称。默认值

      "mlflow-pyfunc"

    • port:在本地主机上用于暴露模型服务器的端口。

      默认值为 5000

Python example
from mlflow.models import build_docker
from mlflow.deployments import get_deploy_client

build_docker(name="mlflow-pyfunc")

client = get_deploy_client("sagemaker")
client.run_local(
    name="my-local-deployment",
    model_uri="/mlruns/0/abc/model",
    flavor="python_function",
    config={
        "port": 5000,
        "image": "mlflow-pyfunc",
    },
)
Command-line example
mlflow models build-docker --name "mlflow-pyfunc"
mlflow deployments run-local --target sagemaker \
        --name my-local-deployment \
        --model-uri "/mlruns/0/abc/model" \
        --flavor python_function \
        -C port=5000 \
        -C image="mlflow-pyfunc"
mlflow.sagemaker.target_help()[source]

为 SageMaker 部署客户端提供帮助信息。

mlflow.sagemaker.terminate_transform_job(job_name, region_name='us-west-2', assume_role_arn=None, archive=False, synchronous=True, timeout_seconds=300)[source]

终止一个 SageMaker 批量转换作业。

Parameters
  • job_name – 已部署的 Sagemaker 批量转换作业的名称。

  • region_name – 部署批量转换作业的 AWS 区域的名称。

  • assume_role_arn – 要被假设以将 SageMaker 部署到另一个 AWS 账户的 IAM 跨账户角色的名称。如果未指定,SageMaker 将部署到当前活动的 AWS 账户。

  • archive – 如果 True,与指定批量转换作业关联的资源(例如其关联的模型和模型工件)会被保留。如果 False,这些资源会被删除。为了使用 archive=False,必须同步执行 terminate_transform_job() 并设置 synchronous=True

  • synchronous – 如果 True,此函数会阻塞,直到终止过程成功或遇到无法恢复的故障。 如果 False,此函数在启动终止过程后会立即返回。 它不会等待终止过程完成;在这种情况下,调用方需通过原生 SageMaker APIs 或 AWS 控制台 监控终止过程的状态。

  • timeout_seconds – 如果 synchronousTrue,且在指定的秒数内未获得明确的结果(成功或失败),则终止过程在指定秒数后返回。一旦函数返回,调用者需通过原生 SageMaker APIs 或 AWS console 监控终止过程的状态。如果 synchronous 为 False,则忽略此参数。