Skip to content

Commit

Permalink
Update the SageMaker Model Monitoring module with model bias monitori…
Browse files Browse the repository at this point in the history
…ng. (#126)

Co-authored-by: Patrick Cloke <[email protected]>
  • Loading branch information
clokep and Patrick Cloke authored Jun 20, 2024
1 parent 1b36eee commit b2fd14d
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### **Added**

- added `ray-on-eks`, and `manifests/ray-on-eks` manifests
- Added a `sagemaker-model-monitoring-module` module with an example of data quality and model quality monitoring of a SageMaker Endpoint.
- Added a `sagemaker-model-monitoring-module` module with an example of data quality, model quality, and model bias monitoring of a SageMaker Endpoint.
- Added an option to enable data capture in the `sagemaker-endpoint-module`.

### **Changed**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ See deployment steps in the [Deployment Guide](DEPLOYMENT.md).
| [SageMaker Custom Kernel Module](modules/sagemaker/sagemaker-custom-kernel/README.md) | Builds custom kernel for SageMaker Studio from a Dockerfile |
| [SageMaker Model Package Group Module](modules/sagemaker/sagemaker-model-package-group/README.md) | Creates a SageMaker Model Package Group to register and version SageMaker Machine Learning (ML) models and setups an Amazon EventBridge Rule to send model package group state change events to an Amazon EventBridge Bus |
| [SageMaker Model Package Promote Pipeline Module](modules/sagemaker/sagemaker-model-package-promote-pipeline/README.md) | Deploy a Pipeline to promote SageMaker Model Packages in a multi-account setup. The pipeline can be triggered through an EventBridge rule in reaction of a SageMaker Model Package Group state event change (Approved/Rejected). Once the pipeline is triggered, it will promote the latest approved model package, if one is found. |
| [SageMaker Model Monitoring Module](modules/sagemaker/sagemaker-model-monitoring-module/README.md) | Deploy data quality & model quality monitoring jobs which run against a SageMaker Endpoint. |
| [SageMaker Model Monitoring Module](modules/sagemaker/sagemaker-model-monitoring-module/README.md) | Deploy data quality, model quality, and model bias monitoring jobs which run against a SageMaker Endpoint. |

### Mlflow Modules

Expand Down
21 changes: 18 additions & 3 deletions modules/sagemaker/sagemaker-model-monitoring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## Description

This module creates a SageMaker Model Monitoring jobs for (optionally) data quality and model quality.
This module creates SageMaker Model Monitoring jobs for (optionally) data quality, model bias, and model quality.
It requires a deployed model endpoint and the proper check steps
for each monitoring job:

* Data Quality: [QualityCheck step](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-quality-check)
* Model Quality: [QualityCheck step](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-quality-check)
* Model Bias: [ClarifyCheck step](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-clarify-check)

### Architecture

Expand All @@ -30,6 +31,11 @@ One or more of:

- `enable-data-quality-monitor`: True to enable the data quality monitoring job.
- `enable-model-quality-monitor`: True to enable the model quality monitoring job.
- `enable-model-bias-monitor`: True to enable the model bias monitoring job.

#### Required for some jobs

- `ground-truth-prefix`: The S3 prefix in `model-artifacts-bucket-arn` which contains the [ground truth](https://docs.aws.amazon.com/sagemaker/latest/dg/model-monitor-model-quality-merge.html) for captured data. Required if `enable-model-quality-monitor` or `enable-model-bias-monitor` is true.

#### Optional

Expand Down Expand Up @@ -68,15 +74,24 @@ N/A

###### Required

- `ground-truth-prefix`: The S3 prefix in `model-artifacts-bucket-arn` which contains the [ground truth](https://docs.aws.amazon.com/sagemaker/latest/dg/model-monitor-model-quality-merge.html) for captured data.
- `model-quality-problem-type`: The machine learning problem type of the model that the monitoring job monitors.
- `model-quality-inference-attribute`: The attribute of the input data that represents the ground truth label.

###### Optional

- `model-quality-inference-attribute`: The attribute of the input data that represents the ground truth label.
- `model-quality-probability-attribute`: In a classification problem, the attribute that represents the class probability.
- `model-quality-probability-threshold-attribute`: The threshold for the class probability to be evaluated as a positive result.

##### Model Bias Monitoring Job Parameters

###### Optional

- `model-bias-checkstep-analysis-config-prefix`: The S3 prefix in `model-artifacts-bucket-arn` which contains the output from the [Clarify Check Step](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-clarify-check) used for model bias.
- `model-bias-features-attribute`: The attributes of the input data that are the input features.
- `model-bias-inference-attribute`: The attribute of the input data that represents the ground truth label.
- `model-bias-probability-attribute`: In a classification problem, the attribute that represents the class probability.
- `model-bias-probability-threshold-attribute`: The threshold for the class probability to be evaluated as a positive result.

### Sample manifest declaration

```yaml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from typing import Any, List, Optional

from aws_cdk import aws_sagemaker as sagemaker
from constructs import Construct


class ModelBiasConstruct(Construct):
"""
CDK construct to define a SageMaker model bias job definition.
It defines both the model bias job, corresponding schedule, and configuration.
"""

def __init__(
self,
scope: Construct,
construct_id: str,
clarify_image_uri: str,
endpoint_name: str,
model_bucket_name: str,
model_bias_checkstep_output_prefix: str,
model_bias_checkstep_analysis_config_prefix: Optional[str],
model_bias_output_prefix: str,
ground_truth_prefix: str,
kms_key_id: str,
model_monitor_role_arn: str,
security_group_id: str,
subnet_ids: List[str],
instance_count: int,
instance_type: str,
instance_volume_size_in_gb: int,
max_runtime_in_seconds: int,
features_attribute: Optional[str],
inference_attribute: Optional[str],
probability_attribute: Optional[str],
probability_threshold_attribute: Optional[int],
schedule_expression: str,
**kwargs: Any,
) -> None:
super().__init__(scope, construct_id, **kwargs)

# To match the defaults in SageMaker.
if model_bias_checkstep_analysis_config_prefix is None:
model_bias_checkstep_analysis_config_prefix = model_bias_checkstep_output_prefix

model_bias_job_definition = sagemaker.CfnModelBiasJobDefinition(
self,
"ModelBiasJobDefinition",
job_resources=sagemaker.CfnModelBiasJobDefinition.MonitoringResourcesProperty(
cluster_config=sagemaker.CfnModelBiasJobDefinition.ClusterConfigProperty(
instance_count=instance_count,
instance_type=instance_type,
volume_size_in_gb=instance_volume_size_in_gb,
volume_kms_key_id=kms_key_id,
)
),
model_bias_app_specification=sagemaker.CfnModelBiasJobDefinition.ModelBiasAppSpecificationProperty(
config_uri=f"s3://{model_bucket_name}/{model_bias_checkstep_analysis_config_prefix}/analysis_config.json",
image_uri=clarify_image_uri,
),
model_bias_job_input=sagemaker.CfnModelBiasJobDefinition.ModelBiasJobInputProperty(
ground_truth_s3_input=sagemaker.CfnModelBiasJobDefinition.MonitoringGroundTruthS3InputProperty(
s3_uri=f"s3://{model_bucket_name}/{ground_truth_prefix}"
),
endpoint_input=sagemaker.CfnModelBiasJobDefinition.EndpointInputProperty(
endpoint_name=endpoint_name,
local_path="/opt/ml/processing/input/model_bias_input",
features_attribute=features_attribute,
inference_attribute=inference_attribute,
probability_attribute=probability_attribute,
probability_threshold_attribute=probability_threshold_attribute,
),
),
model_bias_job_output_config=sagemaker.CfnModelBiasJobDefinition.MonitoringOutputConfigProperty(
monitoring_outputs=[
sagemaker.CfnModelBiasJobDefinition.MonitoringOutputProperty(
s3_output=sagemaker.CfnModelBiasJobDefinition.S3OutputProperty(
local_path="/opt/ml/processing/output/model_bias_output",
s3_uri=f"s3://{model_bucket_name}/{model_bias_output_prefix}",
s3_upload_mode="EndOfJob",
)
)
],
kms_key_id=kms_key_id,
),
job_definition_name=f"{endpoint_name}-model-bias-def",
role_arn=model_monitor_role_arn,
model_bias_baseline_config=sagemaker.CfnModelBiasJobDefinition.ModelBiasBaselineConfigProperty(
constraints_resource=sagemaker.CfnModelBiasJobDefinition.ConstraintsResourceProperty(
s3_uri=f"s3://{model_bucket_name}/{model_bias_checkstep_output_prefix}/analysis.json"
)
),
stopping_condition=sagemaker.CfnModelBiasJobDefinition.StoppingConditionProperty(
max_runtime_in_seconds=max_runtime_in_seconds
),
network_config=sagemaker.CfnModelBiasJobDefinition.NetworkConfigProperty(
enable_inter_container_traffic_encryption=False,
enable_network_isolation=False,
vpc_config=sagemaker.CfnModelBiasJobDefinition.VpcConfigProperty(
security_group_ids=[security_group_id], subnets=subnet_ids
),
),
)

model_bias_monitor_schedule = sagemaker.CfnMonitoringSchedule(
self,
"ModelBiasMonitoringSchedule",
monitoring_schedule_config=sagemaker.CfnMonitoringSchedule.MonitoringScheduleConfigProperty(
monitoring_job_definition_name=model_bias_job_definition.job_definition_name,
monitoring_type="ModelBias",
schedule_config=sagemaker.CfnMonitoringSchedule.ScheduleConfigProperty(
schedule_expression=schedule_expression,
),
),
monitoring_schedule_name=f"{endpoint_name}-model-bias",
)
model_bias_monitor_schedule.add_depends_on(model_bias_job_definition)
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ModuleSettings(CdkBaseSettings):
ground_truth_prefix: str = Field(default="")
enable_data_quality_monitor: bool = Field(default=True)
enable_model_quality_monitor: bool = Field(default=True)
enable_model_bias_monitor: bool = Field(default=True)

# Data quality monitoring options.
data_quality_checkstep_output_prefix: str = Field(default="")
Expand All @@ -49,6 +50,7 @@ class ModuleSettings(CdkBaseSettings):
data_quality_instance_volume_size_in_gb: int = Field(default=20, ge=1)
data_quality_max_runtime_in_seconds: int = Field(default=3600, ge=1)
data_quality_schedule_expression: str = Field(default="cron(0 * ? * * *)")

# Model quality monitoring options.
model_quality_checkstep_output_prefix: str = Field(default="")
model_quality_output_prefix: str = Field(default="")
Expand All @@ -62,6 +64,20 @@ class ModuleSettings(CdkBaseSettings):
model_quality_probability_threshold_attribute: Optional[int] = Field(default=None)
model_quality_schedule_expression: str = Field(default="cron(0 * ? * * *)")

# Model bias monitoring options.
model_bias_checkstep_output_prefix: str = Field(default="")
model_bias_checkstep_analysis_config_prefix: Optional[str] = Field(default=None)
model_bias_output_prefix: str = Field(default="")
model_bias_instance_count: int = Field(default=1, ge=1)
model_bias_instance_type: str = Field(default="ml.m5.large")
model_bias_instance_volume_size_in_gb: int = Field(default=20, ge=1)
model_bias_max_runtime_in_seconds: int = Field(default=1800, ge=1)
model_bias_features_attribute: Optional[str] = Field(default=None)
model_bias_inference_attribute: Optional[str] = Field(default=None)
model_bias_probability_attribute: Optional[str] = Field(default=None)
model_bias_probability_threshold_attribute: Optional[int] = Field(default=None)
model_bias_schedule_expression: str = Field(default="cron(0 * ? * * *)")

tags: Optional[Dict[str, str]] = Field(default=None)


Expand Down
Loading

0 comments on commit b2fd14d

Please sign in to comment.