Skip to content

Commit 1f26aee

Browse files
xgchenaChoiByungWook
authored andcommitted
fix: Remove workarounds and apply fixes to Clarify and MM integ tests (#541)
1 parent eea124f commit 1f26aee

File tree

3 files changed

+43
-56
lines changed

3 files changed

+43
-56
lines changed

tests/integ/test_clarify.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import pandas as pd
2222
import pytest
23+
import statistics
2324
import tempfile
2425

2526
from sagemaker import s3
@@ -32,7 +33,7 @@
3233
SHAPConfig,
3334
)
3435

35-
from sagemaker.amazon.linear_learner import LinearLearner
36+
from sagemaker.amazon.linear_learner import LinearLearner, LinearLearnerPredictor
3637
from sagemaker import utils
3738
from tests import integ
3839
from tests.integ import timeout
@@ -80,11 +81,13 @@ def model_name(sagemaker_session, cpu_instance_type, training_set):
8081
cpu_instance_type,
8182
predictor_type="binary_classifier",
8283
sagemaker_session=sagemaker_session,
84+
disable_profiler=True,
8385
)
8486
ll.binary_classifier_model_selection_criteria = "accuracy"
8587
ll.early_stopping_tolerance = 0.0001
8688
ll.early_stopping_patience = 3
8789
ll.num_models = 1
90+
ll.epochs = 1
8891
ll.num_calibration_samples = 1
8992

9093
features, label = training_set
@@ -106,8 +109,6 @@ def clarify_processor(sagemaker_session, cpu_instance_type):
106109
instance_type=cpu_instance_type,
107110
sagemaker_session=sagemaker_session,
108111
)
109-
# TODO: remove once container ready.
110-
processor.image_uri = "678264136642.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xai-analyzer:1.0"
111112
return processor
112113

113114

@@ -146,8 +147,15 @@ def model_config(model_name):
146147

147148

148149
@pytest.fixture(scope="module")
149-
def model_predicted_label_config():
150-
return ModelPredictedLabelConfig(label="predicted_label")
150+
def model_predicted_label_config(sagemaker_session, model_name, training_set):
151+
predictor = LinearLearnerPredictor(
152+
model_name,
153+
sagemaker_session=sagemaker_session,
154+
)
155+
result = predictor.predict(training_set[0].astype(np.float32))
156+
predictions = [float(record.label["score"].float32_tensor.values[0]) for record in result]
157+
probability_threshold = statistics.median(predictions)
158+
return ModelPredictedLabelConfig(label="score", probability_threshold=probability_threshold)
151159

152160

153161
@pytest.fixture(scope="module")
@@ -166,11 +174,7 @@ def shap_config():
166174
)
167175

168176

169-
@pytest.mark.skipif(
170-
integ.test_region() != "us-west-2",
171-
reason="Image is not yet available in certain regions.",
172-
)
173-
def test_pre_training_bias(clarify_processor, data_config, data_bias_config):
177+
def test_pre_training_bias(clarify_processor, data_config, data_bias_config, sagemaker_session):
174178
with timeout.timeout(minutes=CLARIFY_DEFAULT_TIMEOUT_MINUTES):
175179
clarify_processor.run_pre_training_bias(
176180
data_config,
@@ -179,7 +183,8 @@ def test_pre_training_bias(clarify_processor, data_config, data_bias_config):
179183
wait=True,
180184
)
181185
analysis_result_json = s3.S3Downloader.read_file(
182-
data_config.s3_output_path + "/analysis.json"
186+
data_config.s3_output_path + "/analysis.json",
187+
sagemaker_session,
183188
)
184189
analysis_result = json.loads(analysis_result_json)
185190
assert (
@@ -192,12 +197,13 @@ def test_pre_training_bias(clarify_processor, data_config, data_bias_config):
192197
)
193198

194199

195-
@pytest.mark.skipif(
196-
integ.test_region() != "us-west-2",
197-
reason="Image is not yet available in certain regions.",
198-
)
199200
def test_post_training_bias(
200-
clarify_processor, data_config, data_bias_config, model_config, model_predicted_label_config
201+
clarify_processor,
202+
data_config,
203+
data_bias_config,
204+
model_config,
205+
model_predicted_label_config,
206+
sagemaker_session,
201207
):
202208
with timeout.timeout(minutes=CLARIFY_DEFAULT_TIMEOUT_MINUTES):
203209
clarify_processor.run_post_training_bias(
@@ -209,7 +215,8 @@ def test_post_training_bias(
209215
wait=True,
210216
)
211217
analysis_result_json = s3.S3Downloader.read_file(
212-
data_config.s3_output_path + "/analysis.json"
218+
data_config.s3_output_path + "/analysis.json",
219+
sagemaker_session,
213220
)
214221
analysis_result = json.loads(analysis_result_json)
215222
assert (
@@ -222,11 +229,7 @@ def test_post_training_bias(
222229
)
223230

224231

225-
@pytest.mark.skipif(
226-
integ.test_region() != "us-west-2",
227-
reason="Image is not yet available in certain regions.",
228-
)
229-
def test_shap(clarify_processor, data_config, model_config, shap_config):
232+
def test_shap(clarify_processor, data_config, model_config, shap_config, sagemaker_session):
230233
with timeout.timeout(minutes=CLARIFY_DEFAULT_TIMEOUT_MINUTES):
231234
clarify_processor.run_explainability(
232235
data_config,
@@ -237,7 +240,8 @@ def test_shap(clarify_processor, data_config, model_config, shap_config):
237240
wait=True,
238241
)
239242
analysis_result_json = s3.S3Downloader.read_file(
240-
data_config.s3_output_path + "/analysis.json"
243+
data_config.s3_output_path + "/analysis.json",
244+
sagemaker_session,
241245
)
242246
analysis_result = json.loads(analysis_result_json)
243247
assert (

tests/integ/test_clarify_model_monitor.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,15 @@
6666

6767
CRON = "cron(*/5 * * * ? *)"
6868
UPDATED_CRON = CronExpressionGenerator.daily()
69-
MAX_RUNTIME_IN_SECONDS = 45 * 60
70-
UPDATED_MAX_RUNTIME_IN_SECONDS = 60 * 60
69+
MAX_RUNTIME_IN_SECONDS = 30 * 60
70+
UPDATED_MAX_RUNTIME_IN_SECONDS = 25 * 60
7171
ROLE = "SageMakerRole"
7272
INSTANCE_COUNT = 1
7373
INSTANCE_TYPE = "ml.c5.xlarge"
7474
VOLUME_SIZE_IN_GB = 100
7575
START_TIME_OFFSET = "-PT1H"
7676
END_TIME_OFFSET = "-PT0H"
7777
TEST_TAGS = [{"Key": "integration", "Value": "test"}]
78-
TEST_ENV = {"CLOUDWATCH_METRICS_DIRECTORY": "/tmp"}
79-
80-
# TODO: Use the same skipit mark as in test_model_monitor.py
81-
TEST_REGSION = "us-west-2"
82-
# TODO: Remove test image override once once 1p-registration went through.
83-
# NOTE: The test account only has the image in us-west-2 and us-east-2
84-
TEST_IMAGE_URI = "678264136642.dkr.ecr.{}.amazonaws.com/sagemaker-xai-analyzer:1.0".format(
85-
TEST_REGSION
86-
)
8778

8879

8980
@pytest.yield_fixture(scope="module")
@@ -203,10 +194,8 @@ def bias_monitor(sagemaker_session):
203194
volume_size_in_gb=VOLUME_SIZE_IN_GB,
204195
max_runtime_in_seconds=MAX_RUNTIME_IN_SECONDS,
205196
sagemaker_session=sagemaker_session,
206-
env=TEST_ENV,
207197
tags=TEST_TAGS,
208198
)
209-
monitor.image_uri = TEST_IMAGE_URI
210199
return monitor
211200

212201

@@ -245,8 +234,8 @@ def scheduled_bias_monitor(
245234

246235

247236
@pytest.mark.skipif(
248-
tests.integ.test_region() != TEST_REGSION,
249-
reason="Image is not yet available in certain regions.",
237+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
238+
reason="ModelMonitoring is not yet supported in this region.",
250239
)
251240
def test_bias_monitor(sagemaker_session, scheduled_bias_monitor, endpoint_name, ground_truth_input):
252241
monitor = scheduled_bias_monitor
@@ -297,8 +286,8 @@ def test_bias_monitor(sagemaker_session, scheduled_bias_monitor, endpoint_name,
297286

298287

299288
@pytest.mark.skipif(
300-
tests.integ.test_region() != TEST_REGSION,
301-
reason="Image is not yet available in certain regions.",
289+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
290+
reason="ModelMonitoring is not yet supported in this region.",
302291
)
303292
def test_run_bias_monitor(
304293
scheduled_bias_monitor, sagemaker_session, endpoint_name, ground_truth_input, upload_actual_data
@@ -316,8 +305,8 @@ def test_run_bias_monitor(
316305

317306

318307
@pytest.mark.skipif(
319-
tests.integ.test_region() != TEST_REGSION,
320-
reason="Image is not yet available in certain regions.",
308+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
309+
reason="ModelMonitoring is not yet supported in this region.",
321310
)
322311
def test_run_bias_monitor_baseline(
323312
sagemaker_session,
@@ -336,10 +325,8 @@ def test_run_bias_monitor_baseline(
336325
volume_size_in_gb=VOLUME_SIZE_IN_GB,
337326
max_runtime_in_seconds=MAX_RUNTIME_IN_SECONDS,
338327
sagemaker_session=sagemaker_session,
339-
env=TEST_ENV,
340328
tags=TEST_TAGS,
341329
)
342-
monitor.image_uri = TEST_IMAGE_URI
343330

344331
baselining_job_name = utils.unique_name_from_base("bias-baselining-job")
345332
print("Creating baselining job: {}".format(baselining_job_name))
@@ -396,10 +383,8 @@ def explainability_monitor(sagemaker_session):
396383
volume_size_in_gb=VOLUME_SIZE_IN_GB,
397384
max_runtime_in_seconds=MAX_RUNTIME_IN_SECONDS,
398385
sagemaker_session=sagemaker_session,
399-
env=TEST_ENV,
400386
tags=TEST_TAGS,
401387
)
402-
monitor.image_uri = TEST_IMAGE_URI
403388
return monitor
404389

405390

@@ -429,8 +414,8 @@ def scheduled_explainability_monitor(
429414

430415

431416
@pytest.mark.skipif(
432-
tests.integ.test_region() != TEST_REGSION,
433-
reason="Image is not yet available in certain regions.",
417+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
418+
reason="ModelMonitoring is not yet supported in this region.",
434419
)
435420
def test_explainability_monitor(sagemaker_session, scheduled_explainability_monitor, endpoint_name):
436421
monitor = scheduled_explainability_monitor
@@ -479,8 +464,8 @@ def test_explainability_monitor(sagemaker_session, scheduled_explainability_moni
479464

480465

481466
@pytest.mark.skipif(
482-
tests.integ.test_region() != TEST_REGSION,
483-
reason="Image is not yet available in certain regions.",
467+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
468+
reason="ModelMonitoring is not yet supported in this region.",
484469
)
485470
def test_run_explainability_monitor(
486471
scheduled_explainability_monitor,
@@ -501,8 +486,8 @@ def test_run_explainability_monitor(
501486

502487

503488
@pytest.mark.skipif(
504-
tests.integ.test_region() != TEST_REGSION,
505-
reason="Image is not yet available in certain regions.",
489+
tests.integ.test_region() in tests.integ.NO_MODEL_MONITORING_REGIONS,
490+
reason="ModelMonitoring is not yet supported in this region.",
506491
)
507492
def test_run_explainability_monitor_baseline(
508493
sagemaker_session, shap_config, data_config, model_config, endpoint_name, upload_actual_data
@@ -514,10 +499,8 @@ def test_run_explainability_monitor_baseline(
514499
volume_size_in_gb=VOLUME_SIZE_IN_GB,
515500
max_runtime_in_seconds=MAX_RUNTIME_IN_SECONDS,
516501
sagemaker_session=sagemaker_session,
517-
env=TEST_ENV,
518502
tags=TEST_TAGS,
519503
)
520-
monitor.image_uri = TEST_IMAGE_URI
521504

522505
baselining_job_name = utils.unique_name_from_base("explainability-baselining-job")
523506
print("Creating baselining job: {}".format(baselining_job_name))

tests/integ/test_model_quality_monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
CRON = "cron(*/5 * * * ? *)"
5353
UPDATED_CRON = CronExpressionGenerator.daily()
54-
MAX_RUNTIME_IN_SECONDS = 45 * 60
55-
UPDATED_MAX_RUNTIME_IN_SECONDS = 60 * 60
54+
MAX_RUNTIME_IN_SECONDS = 30 * 60
55+
UPDATED_MAX_RUNTIME_IN_SECONDS = 25 * 60
5656
ROLE = "SageMakerRole"
5757
INSTANCE_COUNT = 1
5858
INSTANCE_TYPE = "ml.c5.xlarge"

0 commit comments

Comments
 (0)