mlflow.sagemaker
The mlflow.sagemaker module provides an API for deploying MLflow models to Amazon SageMaker。
- class mlflow.sagemaker.SageMakerDeploymentClient(target_uri)[source]
Bases:
mlflow.deployments.base.BaseDeploymentClientInitialize a deployment client for SageMaker. The default region and assumed role ARN will be set according to the value of the target_uri。
This class is meant to supersede the other
mlflow.sagemakerreal-time serving API’s. It is also designed to be used through themlflow.deploymentsmodule. This means that you can deploy to SageMaker using the mlflow deployments CLI and get a client through themlflow.deployments.get_deploy_clientfunction。- 参数
target_uri –
A URI that follows one of the following formats
sagemaker: This will set the default region to us-west-2 and the default assumed role ARN to None。sagemaker:/region_name: This will set the default region to region_name and the default assumed role ARN to None。sagemaker:/region_name/assumed_role_arn: This will set the default region to region_name and the default assumed role ARN to assumed_role_arn。
When an assumed_role_arn is provided without a region_name, an MlflowException will be raised。
- create_deployment(name, model_uri, flavor=None, config=None, endpoint=None)[source]
Deploy an MLflow model on AWS SageMaker. The currently active AWS account must have correct permissions set up。
This function creates a SageMaker endpoint. For more information about the input data formats accepted by this endpoint, see the MLflow deployment tools documentation。
- 参数
name – Name of the deployed application。
model_uri –
The location, in URI format, of the MLflow model to deploy to SageMaker. For example
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see Referencing Artifacts.
flavor – The name of the flavor of the model to use for deployment. Must be either
Noneor one of mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS. IfNone, a flavor is automatically selected from the model’s available flavors. If the specified flavor is not present or not supported for deployment, an exception will be thrown。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 thetarget_uriwill be used. If the role is not given in thetarget_uri, defaults tous-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 theExecutionRoleArnparameter 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 bymlflow sagemaker build-and-push-container. This parameter can also be specified by the environment variableMLFLOW_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 thetarget_uri. If it is also not specified in thetarget_uri, defaults tous-west-2。archive: IfTrue, any pre-existing SageMaker application resources that become inactive (i.e. as a result of deploying inmlflow.sagemaker.DEPLOYMENT_MODE_REPLACEmode) are preserved. These resources may include unused SageMaker models and endpoint configurations that were associated with a prior version of the application endpoint. IfFalse, these resources are deleted. In order to usearchive=False,create_deployment()must be executed synchronously withsynchronous=True. Defaults toFalse。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 toml.m4.xlarge。instance_count: The number of SageMaker ML instances on which to deploy the model. Defaults to1。synchronous: IfTrue, this function will block until the deployment process succeeds or encounters an irrecoverable failure. IfFalse, 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 toTrue。timeout_seconds: IfsynchronousisTrue, 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. IfsynchronousisFalse, this parameter is ignored. Defaults to300。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 theVpcConfigparameter 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 toNone。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 toNone。variant_name: A string specifying the desired name when creating a production variant. Defaults toNone。async_inference_config: A dictionary specifying the async_inference_configurationserverless_config: A dictionary specifying the serverless_configurationenv: A dictionary specifying environment variables as key-value pairs to be set for the deployed model. Defaults toNone。tags: A dictionary of key-value pairs representing additional tags to be set for the deployed model. Defaults toNone。
endpoint – (optional) Endpoint to create the deployment under. Currently unsupported
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, )
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]
Create an endpoint with the specified target. By default, this method should block until creation completes (i.e. until it’s possible to create a deployment within the endpoint). In the case of conflicts (e.g. if it’s not possible to create the specified endpoint due to conflict with an existing endpoint), raises a
mlflow.exceptions.MlflowException. See target-specific plugin documentation for additional detail on support for asynchronous creation and other configuration。- 参数
name – Unique name to use for endpoint. If another endpoint exists with the same name, raises a
mlflow.exceptions.MlflowException。config – (optional) Dict containing target-specific configuration for the endpoint。
- 返回
Dict corresponding to created endpoint, which must contain the ‘name’ key。
- delete_deployment(name, config=None, endpoint=None)[source]
Delete a SageMaker application。
- 参数
name – Name of the deployed application。
config –
Configuration parameters. The supported parameters are
assume_role_arn: The name of an IAM role to be assumed to delete the SageMaker deployment。region_name: Name of the AWS region in which the application is deployed. Defaults tous-west-2or the region provided in the target_uri。archive: If True, resources associated with the specified application, such as its associated models and endpoint configuration, are preserved. If False, these resources are deleted. In order to usearchive=False,delete()must be executed synchronously withsynchronous=True. Defaults toFalse。synchronous: If True, this function blocks until the deletion process succeeds or encounters an irrecoverable failure. IfFalse, this function returns immediately after starting the deletion process. It will not wait for the deletion process to complete; in this case, the caller is responsible for monitoring the status of the deletion process via native SageMaker APIs or the AWS console. Defaults toTrue。timeout_seconds: If synchronous is True, the deletion process returns 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 status of the deletion process via native SageMaker APIs or the AWS console. If synchronous is False, this parameter is ignored. Defaults to300。
endpoint – (optional) Endpoint containing the deployment to delete. Currently unsupported
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)
- delete_endpoint(endpoint)[source]
Delete the endpoint from the specified target. Deletion should be idempotent (i.e. deletion should not fail if retried on a non-existent deployment)。
- 参数
endpoint – Name of endpoint to delete
- explain(deployment_name=None, df=None, endpoint=None)[source]
This function has not been implemented and will be coming in the future。
- get_deployment(name, endpoint=None)[source]
Returns a dictionary describing the specified deployment。
If a region name needs to be specified, the plugin must be initialized with the AWS region in the
target_urisuch assagemaker:/us-east-1。To assume an IAM role, the plugin must be initialized with the AWS region and the role ARN in the
target_urisuch assagemaker:/us-east-1/arn:aws:1234:role/assumed_role。A
mlflow.exceptions.MlflowExceptionwill also be thrown when an error occurs while retrieving the deployment。- 参数
name – Name of deployment to retrieve
endpoint – (optional) Endpoint containing the deployment to get. Currently unsupported
- 返回
A dictionary that describes the specified deployment
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")
- get_endpoint(endpoint)[source]
Returns a dictionary describing the specified endpoint, throwing a py:class:mlflow.exception.MlflowException if no endpoint exists with the provided name. The dict is guaranteed to contain an ‘name’ key containing the endpoint name. The other fields of the returned dictionary and their types may vary across targets。
- 参数
endpoint – Name of endpoint to fetch
- list_deployments(endpoint=None)[source]
List deployments. This method returns a list of dictionaries that describes each deployment。
If a region name needs to be specified, the plugin must be initialized with the AWS region in the
target_urisuch assagemaker:/us-east-1。To assume an IAM role, the plugin must be initialized with the AWS region and the role ARN in the
target_urisuch assagemaker:/us-east-1/arn:aws:1234:role/assumed_role。- 参数
endpoint – (optional) List deployments in the specified endpoint. Currently unsupported
- 返回
A list of dictionaries corresponding to deployments。
from mlflow.deployments import get_deploy_client client = get_deploy_client("sagemaker:/us-east-1/arn:aws:123:role/assumed_role") client.list_deployments()
- list_endpoints()[source]
List endpoints in the specified target. This method is expected to return an unpaginated list of all endpoints (an alternative would be to return a dict with an ‘endpoints’ field containing the actual endpoints, with plugins able to specify other fields, e.g. a next_page_token field, in the returned dictionary for pagination, and to accept a pagination_args argument to this method for passing pagination-related args)。
- 返回
A list of dicts corresponding to endpoints. Each dict is guaranteed to contain a ‘name’ key containing the endpoint name. The other fields of the returned dictionary and their types may vary across targets。
- predict(deployment_name=None, inputs=None, endpoint=None, params: Optional[dict[str, typing.Any]] = None)[source]
Compute predictions from the specified deployment using the provided PyFunc input。
The input/output types of this method match the MLflow PyFunc prediction interface。
If a region name needs to be specified, the plugin must be initialized with the AWS region in the
target_urisuch assagemaker:/us-east-1。To assume an IAM role, the plugin must be initialized with the AWS region and the role ARN in the
target_urisuch assagemaker:/us-east-1/arn:aws:1234:role/assumed_role。- 参数
deployment_name – Name of the deployment to predict against。
inputs – Input data (or arguments) to pass to the deployment or model endpoint for inference. For a complete list of supported input types, see Inference API。
endpoint – Endpoint to predict against. Currently unsupported
params – Optional parameters to invoke the endpoint with。
- 返回
A PyFunc output, such as a Pandas DataFrame, Pandas Series, or NumPy array. For a complete list of supported output types, see Inference API。
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)
- update_deployment(name, model_uri, flavor=None, config=None, endpoint=None)[source]
Update a deployment on AWS SageMaker. This function can replace or add a new model to an existing SageMaker endpoint. By default, this function replaces the existing model with the new one. The currently active AWS account must have correct permissions set up。
- 参数
name – Name of the deployed application。
model_uri –
The location, in URI format, of the MLflow model to deploy to SageMaker. For example
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see Referencing Artifacts.
flavor – The name of the flavor of the model to use for deployment. Must be either
Noneor one of mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS. IfNone, a flavor is automatically selected from the model’s available flavors. If the specified flavor is not present or not supported for deployment, an exception will be thrown。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 thetarget_uriwill be used. If the role is not given in thetarget_uri, defaults tous-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 theExecutionRoleArnparameter 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 bymlflow sagemaker build-and-push-container. This parameter can also be specified by the environment variableMLFLOW_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 thetarget_uri. If it is also not specified in thetarget_uri, defaults tous-west-2。mode: The mode in which to deploy the application. Must be one of the followingmlflow.sagemaker.DEPLOYMENT_MODE_REPLACEIf 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_ADDAdd 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
UpdateEndpointWeightsAndCapacitiesfunction defined in https://docs.aws.amazon.com/sagemaker/latest/dg/API_UpdateEndpointWeightsAndCapacities.html.
archive: IfTrue, any pre-existing SageMaker application resources that become inactive (i.e. as a result of deploying inmlflow.sagemaker.DEPLOYMENT_MODE_REPLACEmode) are preserved. These resources may include unused SageMaker models and endpoint configurations that were associated with a prior version of the application endpoint. IfFalse, these resources are deleted. In order to usearchive=False,update_deployment()must be executed synchronously withsynchronous=True. Defaults toFalse.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 toml.m4.xlarge。instance_count: The number of SageMaker ML instances on which to deploy the model. Defaults to1。synchronous: IfTrue, this function will block until the deployment process succeeds or encounters an irrecoverable failure. IfFalse, 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 toTrue。timeout_seconds: IfsynchronousisTrue, 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. IfsynchronousisFalse, this parameter is ignored. Defaults to300。variant_name: A string specifying the desired name when creating a production variant. Defaults toNone。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 theVpcConfigparameter 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 toNone。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 toNone。async_inference_config: A dictionary specifying the async config configuration. Defaults toNone.env: A dictionary specifying environment variables as key-value pairs to be set for the deployed model. Defaults toNone。tags: A dictionary of key-value pairs representing additional tags to be set for the deployed model. Defaults toNone。
endpoint – (optional) Endpoint containing the deployment to update. Currently unsupported
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, )
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]
Update the endpoint with the specified name. You can update any target-specific attributes of the endpoint (via config). By default, this method should block until the update completes (i.e. until it’s possible to create a deployment within the endpoint). See target-specific plugin documentation for additional detail on support for asynchronous update and other configuration。
- 参数
endpoint – Unique name of endpoint to update
config – (optional) dict containing target-specific configuration for the endpoint
- 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 账户必须具有正确的权限设置。
- 参数
job_name – 已部署的 SageMaker 批量转换作业的名称。
model_uri –
The location, in URI format, of the MLflow model to deploy to SageMaker. For example
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see 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 服务访问指定的 Docker 镜像和包含 MLflow 模型构件的 S3 存储桶的权限。如果未指定,则使用当前假定的角色。此执行角色在创建 SageMaker 模型(从指定的 MLflow 模型)时传递给 SageMaker 服务。它作为 SageMaker CreateModel API 调用的
ExecutionRoleArn参数传递。此角色不会用于任何其他调用。有关 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 配置。此参数的可接受值与 SageMaker boto3 客户端的 create_model 方法中的
VpcConfig参数完全相同。有关更多信息,请参阅 https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html。flavor – The name of the flavor of the model to use for deployment. Must be either
Noneor one of mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS. IfNone, a flavor is automatically selected from the model’s available flavors. If the specified flavor is not present or not supported for deployment, an exception will be thrown。archive – 如果为
True,则在批量转换作业完成后,将保留 SageMaker 模型和 S3 中的模型构件等资源。如果为False,则会删除这些资源。要使用archive=False,必须以同步方式(synchronous=True)执行deploy_transform_job()。synchronous – 如果为
True,则此函数将阻塞,直到部署过程成功或遇到不可恢复的失败。如果为False,则此函数将在启动部署过程后立即返回。它不会等待部署过程完成;在这种情况下,调用者负责通过本地 SageMaker API 或 AWS 控制台监控待定部署的健康状况和状态。timeout_seconds – 如果
synchronous为True,则如果未获得明确的结果(成功或失败),部署过程将在指定秒数后返回。函数返回后,调用者负责使用本地 SageMaker API 或 AWS 控制台监控待定部署的健康状况和状态。如果synchronous为False,则忽略此参数。
- mlflow.sagemaker.push_image_to_ecr(image='mlflow-pyfunc')[source]
将本地 Docker 镜像推送到 AWS ECR。
镜像将推送到当前活动的 AWS 账户和当前活动的 AWS 区域。
- 参数
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 账户必须具有正确的权限设置。
- 参数
model_name – SageMaker 模型的名称。
model_uri –
The location, in URI format, of the MLflow model to deploy to SageMaker. For example
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see Referencing Artifacts.
execution_role_arn –
一个 IAM 角色,该角色授予 SageMaker 服务访问指定的 Docker 镜像和包含 MLflow 模型构件的 S3 存储桶的权限。如果未指定,则使用当前假定的角色。此执行角色在创建 SageMaker 模型(从指定的 MLflow 模型)时传递给 SageMaker 服务。它作为 SageMaker CreateModel API 调用的
ExecutionRoleArn参数传递。此角色不会用于任何其他调用。有关 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 配置。此参数的可接受值与 SageMaker boto3 客户端的 create_model 方法中的
VpcConfig参数完全相同。有关更多信息,请参阅 https://docs.aws.amazon.com/sagemaker/latest/dg/API_VpcConfig.html。flavor – The name of the flavor of the model to use for deployment. Must be either
Noneor one of mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS. IfNone, a flavor is automatically selected from the model’s available flavors. If the specified flavor is not present or not supported for deployment, an exception will be thrown。
- mlflow.sagemaker.run_local(name, model_uri, flavor=None, config=None)[source]
在兼容 SageMaker 的 Docker 容器中本地运行模型服务。
请注意,本地部署的模型无法通过其他部署 API(例如
update_deployment、delete_deployment等)进行管理。- 参数
name – 本地服务应用程序的名称。
model_uri –
要本地部署的 MLflow 模型的位置(URI 格式)。例如
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see Referencing Artifacts.
flavor – The name of the flavor of the model to use for deployment. Must be either
Noneor one of mlflow.sagemaker.SUPPORTED_DEPLOYMENT_FLAVORS. IfNone, a flavor is automatically selected from the model’s available flavors. If the specified flavor is not present or not supported for deployment, an exception will be thrown。config –
Configuration parameters. The supported parameters are
image:用于模型服务的 Docker 镜像的名称。默认为"mlflow-pyfunc"。
port:在本地主机上公开模型服务器的端口。默认为
5000。
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", }, )
- 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 批量转换作业。
- 参数
job_name – 已部署的 SageMaker 批量转换作业的名称。
region_name – 部署批量转换作业的 AWS 区域名称。
assume_role_arn – 要假定的 IAM 跨账户角色的名称,以便将 SageMaker 部署到另一个 AWS 账户。如果未指定,SageMaker 将部署到当前活动的 AWS 账户。
archive – 如果为
True,则会保留与指定批量转换作业关联的资源,例如其关联的模型和模型构件。如果为False,则会删除这些资源。要使用archive=False,必须以同步方式(synchronous=True)执行terminate_transform_job()。synchronous – 如果为 True,则此函数将阻塞,直到终止过程成功或遇到不可恢复的失败。如果为 False,则此函数将在启动终止过程后立即返回。它不会等待终止过程完成;在这种情况下,调用者负责通过本地 SageMaker API 或 AWS 控制台监控终止过程的状态。
timeout_seconds – 如果 synchronous 为 True,则如果未获得明确的结果(成功或失败),终止过程将在指定秒数后返回。函数返回后,调用者负责通过本地 SageMaker API 或 AWS 控制台监控终止过程的状态。如果 synchronous 为 False,则忽略此参数。