File size: 26,067 Bytes
476455e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import
import os
import tarfile
import boto3
import numpy
import pytest
import tempfile
import stopit
import tests.integ.lock as lock
from tests.integ import DATA_DIR
from mock import Mock, ANY
from sagemaker import image_uris
from sagemaker.model import Model
from sagemaker.transformer import Transformer
from sagemaker.processing import ProcessingInput, ProcessingOutput, ScriptProcessor
from sagemaker.sklearn.processing import SKLearnProcessor
from sagemaker.workflow.pipeline import Pipeline
from sagemaker.workflow.steps import TrainingStep, ProcessingStep, TransformStep
from sagemaker.workflow.model_step import ModelStep
from sagemaker.workflow.parameters import ParameterInteger, ParameterString
from sagemaker.workflow.condition_step import ConditionStep
from sagemaker.workflow.fail_step import FailStep
from sagemaker.workflow.conditions import ConditionLessThanOrEqualTo
from sagemaker.workflow.functions import JsonGet, PropertyFile, Join
from sagemaker.workflow.pipeline_context import LocalPipelineSession
from sagemaker.local import LocalSession, LocalSagemakerRuntimeClient, LocalSagemakerClient
from sagemaker.mxnet import MXNet
# endpoint tests all use the same port, so we use this lock to prevent concurrent execution
LOCK_PATH = os.path.join(tempfile.gettempdir(), "sagemaker_test_local_mode_lock")
DATA_PATH = os.path.join(DATA_DIR, "iris", "data")
DEFAULT_REGION = "us-west-2"
class LocalNoS3Session(LocalSession):
"""
This Session sets local_code: True regardless of any config file settings
"""
def __init__(self):
super(LocalSession, self).__init__()
def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client, **kwargs):
self.boto_session = boto3.Session(region_name=DEFAULT_REGION)
if self.config is None:
self.config = {"local": {"local_code": True, "region_name": DEFAULT_REGION}}
self._region_name = DEFAULT_REGION
self.sagemaker_client = LocalSagemakerClient(self)
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
self.local_mode = True
class LocalPipelineNoS3Session(LocalPipelineSession):
"""
This Session sets local_code: True regardless of any config file settings
"""
def __init__(self):
super(LocalPipelineSession, self).__init__()
def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client, **kwargs):
self.boto_session = boto3.Session(region_name=DEFAULT_REGION)
if self.config is None:
self.config = {"local": {"local_code": True, "region_name": DEFAULT_REGION}}
self._region_name = DEFAULT_REGION
self.sagemaker_client = LocalSagemakerClient(self)
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
self.local_mode = True
@pytest.fixture(scope="module")
def sagemaker_local_session_no_local_code(boto_session):
return LocalSession(boto_session=boto_session, disable_local_code=True)
@pytest.fixture(scope="module")
def sklearn_image_uri(
sklearn_latest_version,
sklearn_latest_py_version,
cpu_instance_type,
sagemaker_session,
):
return image_uris.retrieve(
"sklearn",
sagemaker_session.boto_region_name,
version=sklearn_latest_version,
py_version=sklearn_latest_py_version,
instance_type=cpu_instance_type,
)
@pytest.fixture(scope="module")
def mxnet_model(
sagemaker_local_session, mxnet_inference_latest_version, mxnet_inference_latest_py_version
):
def _create_model(output_path):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
instance_count=1,
instance_type="local",
output_path=output_path,
framework_version=mxnet_inference_latest_version,
py_version=mxnet_inference_latest_py_version,
sagemaker_session=sagemaker_local_session,
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
mx.fit({"train": train_input, "test": test_input})
model = mx.create_model(1)
return model
return _create_model
@pytest.mark.local_mode
def test_local_mode_serving_from_s3_model(
sagemaker_local_session,
mxnet_model,
mxnet_inference_latest_version,
mxnet_inference_latest_py_version,
):
path = "s3://%s" % sagemaker_local_session.default_bucket()
s3_model = mxnet_model(path)
s3_model.sagemaker_session = sagemaker_local_session
predictor = None
with lock.lock(LOCK_PATH):
try:
predictor = s3_model.deploy(initial_instance_count=1, instance_type="local")
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
if predictor:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_local_mode_serving_from_local_model(tmpdir, sagemaker_local_session, mxnet_model):
predictor = None
with lock.lock(LOCK_PATH):
try:
path = "file://%s" % (str(tmpdir))
model = mxnet_model(path)
model.sagemaker_session = sagemaker_local_session
predictor = model.deploy(initial_instance_count=1, instance_type="local")
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
if predictor:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_local_mode(
sagemaker_local_session, mxnet_training_latest_version, mxnet_training_latest_py_version
):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
py_version=mxnet_training_latest_py_version,
instance_count=1,
instance_type="local",
sagemaker_session=sagemaker_local_session,
framework_version=mxnet_training_latest_version,
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
mx.fit({"train": train_input, "test": test_input})
endpoint_name = mx.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
predictor = mx.deploy(1, "local", endpoint_name=endpoint_name)
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_distributed_local_mode(
sagemaker_local_session, mxnet_training_latest_version, mxnet_training_latest_py_version
):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
py_version=mxnet_training_latest_py_version,
instance_count=2,
instance_type="local",
sagemaker_session=sagemaker_local_session,
framework_version=mxnet_training_latest_version,
distribution={"parameter_server": {"enabled": True}},
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
mx.fit({"train": train_input, "test": test_input})
@pytest.mark.local_mode
def test_mxnet_local_data_local_script(
mxnet_training_latest_version, mxnet_training_latest_py_version
):
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "mnist.py")
local_no_s3_session = LocalNoS3Session()
local_no_s3_session.boto_session.resource = Mock(
side_effect=local_no_s3_session.boto_session.resource
)
local_no_s3_session.boto_session.client = Mock(
side_effect=local_no_s3_session.boto_session.client
)
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
instance_count=1,
instance_type="local",
framework_version=mxnet_training_latest_version,
py_version=mxnet_training_latest_py_version,
sagemaker_session=local_no_s3_session,
)
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
mx.fit({"train": train_input, "test": test_input})
endpoint_name = mx.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
predictor = mx.deploy(1, "local", endpoint_name=endpoint_name)
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
# check if no boto_session s3 calls were made
with pytest.raises(AssertionError):
local_no_s3_session.boto_session.resource.assert_called_with("s3", region_name=ANY)
with pytest.raises(AssertionError):
local_no_s3_session.boto_session.client.assert_called_with("s3", region_name=ANY)
finally:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_local_training_env(mxnet_training_latest_version, mxnet_training_latest_py_version):
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "check_env.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
instance_count=1,
instance_type="local",
framework_version=mxnet_training_latest_version,
py_version=mxnet_training_latest_py_version,
sagemaker_session=LocalNoS3Session(),
environment={"MYVAR": "HELLO_WORLD"},
)
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
mx.fit({"train": train_input, "test": test_input})
@pytest.mark.local_mode
def test_mxnet_training_failure(
sagemaker_local_session, mxnet_training_latest_version, mxnet_training_latest_py_version, tmpdir
):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "failure_script.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
framework_version=mxnet_training_latest_version,
py_version=mxnet_training_latest_py_version,
instance_count=1,
instance_type="local",
sagemaker_session=sagemaker_local_session,
code_location="s3://{}".format(sagemaker_local_session.default_bucket()),
output_path="file://{}".format(tmpdir),
)
with pytest.raises(RuntimeError):
mx.fit()
with tarfile.open(os.path.join(str(tmpdir), "output.tar.gz")) as tar:
tar.getmember("failure")
@pytest.mark.local_mode
def test_local_transform_mxnet(
sagemaker_local_session,
tmpdir,
mxnet_inference_latest_version,
mxnet_inference_latest_py_version,
cpu_instance_type,
):
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "check_env.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
instance_count=1,
instance_type="local",
framework_version=mxnet_inference_latest_version,
py_version=mxnet_inference_latest_py_version,
sagemaker_session=sagemaker_local_session,
environment={"MYVAR": "HELLO_WORLD"},
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
mx.fit({"train": train_input, "test": test_input})
transform_input_path = os.path.join(data_path, "transform")
transform_input_key_prefix = "integ-test-data/mxnet_mnist/transform"
transform_input = mx.sagemaker_session.upload_data(
path=transform_input_path, key_prefix=transform_input_key_prefix
)
output_path = "file://%s" % (str(tmpdir))
transformer = mx.transformer(
1,
"local",
assemble_with="Line",
max_payload=1,
strategy="SingleRecord",
output_path=output_path,
)
with lock.lock(LOCK_PATH):
transformer.transform(transform_input, content_type="text/csv", split_type="Line")
transformer.wait()
assert os.path.exists(os.path.join(str(tmpdir), "data.csv.out"))
@pytest.mark.local_mode
def test_local_processing_sklearn(sagemaker_local_session_no_local_code, sklearn_latest_version):
script_path = os.path.join(DATA_DIR, "dummy_script.py")
input_file_path = os.path.join(DATA_DIR, "dummy_input.txt")
sklearn_processor = SKLearnProcessor(
framework_version=sklearn_latest_version,
role="SageMakerRole",
instance_type="local",
instance_count=1,
command=["python3"],
sagemaker_session=sagemaker_local_session_no_local_code,
)
sklearn_processor.run(
code=script_path,
inputs=[ProcessingInput(source=input_file_path, destination="/opt/ml/processing/inputs/")],
wait=False,
logs=False,
)
job_description = sklearn_processor.latest_job.describe()
assert len(job_description["ProcessingInputs"]) == 2
assert job_description["ProcessingResources"]["ClusterConfig"]["InstanceCount"] == 1
assert job_description["ProcessingResources"]["ClusterConfig"]["InstanceType"] == "local"
assert job_description["AppSpecification"]["ContainerEntrypoint"] == [
"python3",
"/opt/ml/processing/input/code/dummy_script.py",
]
assert job_description["RoleArn"] == "<no_role>"
@pytest.mark.local_mode
def test_local_processing_script_processor(sagemaker_local_session, sklearn_image_uri):
input_file_path = os.path.join(DATA_DIR, "dummy_input.txt")
script_processor = ScriptProcessor(
role="SageMakerRole",
image_uri=sklearn_image_uri,
command=["python3"],
instance_count=1,
instance_type="local",
volume_size_in_gb=30,
volume_kms_key=None,
max_runtime_in_seconds=3600,
base_job_name="test-script-processor",
env={"DUMMY_ENVIRONMENT_VARIABLE": "dummy-value"},
tags=[{"Key": "dummy-tag", "Value": "dummy-tag-value"}],
sagemaker_session=sagemaker_local_session,
)
script_processor.run(
code=os.path.join(DATA_DIR, "dummy_script.py"),
inputs=[
ProcessingInput(
source=input_file_path,
destination="/opt/ml/processing/input/container/path/",
input_name="dummy_input",
s3_data_type="S3Prefix",
s3_input_mode="File",
s3_data_distribution_type="FullyReplicated",
s3_compression_type="None",
)
],
outputs=[
ProcessingOutput(
source="/opt/ml/processing/output/container/path/",
output_name="dummy_output",
s3_upload_mode="EndOfJob",
)
],
arguments=["-v"],
wait=True,
logs=True,
)
job_description = script_processor.latest_job.describe()
assert job_description["ProcessingInputs"][0]["InputName"] == "dummy_input"
assert job_description["ProcessingInputs"][1]["InputName"] == "code"
assert job_description["ProcessingJobName"].startswith("test-script-processor")
assert job_description["ProcessingJobStatus"] == "Completed"
assert job_description["ProcessingOutputConfig"]["Outputs"][0]["OutputName"] == "dummy_output"
assert job_description["ProcessingResources"]["ClusterConfig"]["InstanceCount"] == 1
assert job_description["ProcessingResources"]["ClusterConfig"]["InstanceType"] == "local"
assert job_description["ProcessingResources"]["ClusterConfig"]["VolumeSizeInGB"] == 30
assert job_description["AppSpecification"]["ContainerArguments"] == ["-v"]
assert job_description["AppSpecification"]["ContainerEntrypoint"] == [
"python3",
"/opt/ml/processing/input/code/dummy_script.py",
]
assert job_description["AppSpecification"]["ImageUri"] == sklearn_image_uri
assert job_description["Environment"] == {"DUMMY_ENVIRONMENT_VARIABLE": "dummy-value"}
@pytest.mark.local_mode
def test_local_pipeline_with_processing_step(sklearn_latest_version, local_pipeline_session):
string_container_arg = ParameterString(name="ProcessingContainerArg", default_value="foo")
sklearn_processor = SKLearnProcessor(
framework_version=sklearn_latest_version,
role="SageMakerRole",
instance_type="local",
instance_count=1,
command=["python3"],
sagemaker_session=local_pipeline_session,
)
script_path = os.path.join(DATA_DIR, "dummy_script.py")
input_file_path = os.path.join(DATA_DIR, "dummy_input.txt")
processing_args = sklearn_processor.run(
code=script_path,
inputs=[ProcessingInput(source=input_file_path, destination="/opt/ml/processing/inputs/")],
arguments=["--container_arg", string_container_arg],
)
processing_step = ProcessingStep(
name="sklearn_processor_local_pipeline", step_args=processing_args
)
pipeline = Pipeline(
name="local_pipeline_processing",
steps=[processing_step],
sagemaker_session=local_pipeline_session,
parameters=[string_container_arg],
)
pipeline.create("SageMakerRole", "pipeline for sdk integ testing")
with lock.lock(LOCK_PATH):
execution = pipeline.start()
pipeline_execution_describe_result = execution.describe()
assert pipeline_execution_describe_result["PipelineArn"] == "local_pipeline_processing"
assert pipeline_execution_describe_result["PipelineExecutionStatus"] == "Succeeded"
pipeline_execution_list_steps_result = execution.list_steps()
assert len(pipeline_execution_list_steps_result["PipelineExecutionSteps"]) == 1
assert (
pipeline_execution_list_steps_result["PipelineExecutionSteps"][0]["StepName"]
== "sklearn_processor_local_pipeline"
)
assert (
pipeline_execution_list_steps_result["PipelineExecutionSteps"][0]["StepStatus"]
== "Succeeded"
)
@pytest.mark.local_mode
def test_local_pipeline_with_training_and_transform_steps(
mxnet_training_latest_version,
mxnet_inference_latest_version,
mxnet_training_latest_py_version,
tmpdir,
):
session = LocalPipelineNoS3Session()
instance_count = ParameterInteger(name="InstanceCountParam")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "check_env.py")
output_path = "file://%s" % (str(tmpdir))
# define Estimator
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
instance_count=instance_count,
instance_type="local",
framework_version=mxnet_training_latest_version,
py_version=mxnet_training_latest_py_version,
sagemaker_session=session,
output_path=output_path,
environment={"MYVAR": "HELLO_WORLD"},
)
# define training step
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
training_args = mx.fit({"train": train_input, "test": test_input})
training_step = TrainingStep(name="mxnet_mnist_training", step_args=training_args)
# define model
inference_image_uri = image_uris.retrieve(
framework="mxnet",
region=DEFAULT_REGION,
version=mxnet_inference_latest_version,
instance_type="local",
image_scope="inference",
)
model = Model(
image_uri=inference_image_uri,
model_data=training_step.properties.ModelArtifacts.S3ModelArtifacts,
sagemaker_session=session,
role="SageMakerRole",
)
# define create model step
model_step_args = model.create(instance_type="local", accelerator_type="local")
model_step = ModelStep(name="mxnet_mnist_model", step_args=model_step_args)
# define transformer
transformer = Transformer(
model_name=model_step.properties.ModelName,
instance_type="local",
instance_count=instance_count,
output_path=output_path,
assemble_with="Line",
max_payload=1,
strategy="SingleRecord",
sagemaker_session=session,
)
# define transform step
transform_input = "file://" + os.path.join(data_path, "transform")
transform_args = transformer.transform(
transform_input, content_type="text/csv", split_type="Line"
)
transform_step = TransformStep(name="mxnet_mnist_transform", step_args=transform_args)
pipeline = Pipeline(
name="local_pipeline_training_transform",
parameters=[instance_count],
steps=[training_step, model_step, transform_step],
sagemaker_session=session,
)
pipeline.create("SageMakerRole", "pipeline for sdk integ testing")
with lock.lock(LOCK_PATH):
execution = pipeline.start(parameters={"InstanceCountParam": 1})
assert os.path.exists(os.path.join(str(tmpdir), "model.tar.gz"))
assert os.path.exists(os.path.join(str(tmpdir), "data.csv.out"))
pipeline_execution_describe_result = execution.describe()
assert pipeline_execution_describe_result["PipelineArn"] == "local_pipeline_training_transform"
assert pipeline_execution_describe_result["PipelineExecutionStatus"] == "Succeeded"
pipeline_execution_list_steps_result = execution.list_steps()
assert len(pipeline_execution_list_steps_result["PipelineExecutionSteps"]) == 3
@pytest.mark.local_mode
def test_local_pipeline_with_eval_cond_fail_steps(sklearn_image_uri, local_pipeline_session):
processor = ScriptProcessor(
image_uri=sklearn_image_uri,
role="SageMakerRole",
instance_count=1,
instance_type="local",
sagemaker_session=local_pipeline_session,
command=["python3"],
)
evaluation_report = PropertyFile(
name="EvaluationReport", output_name="evaluation", path="evaluation.json"
)
base_dir = os.path.join(DATA_DIR, "mxnet_mnist")
mx_mnist_model_data = os.path.join(base_dir, "model.tar.gz")
test_input = os.path.join(base_dir, "test")
eval_step = ProcessingStep(
name="mxnet_mnist_eval",
processor=processor,
inputs=[
ProcessingInput(
source=mx_mnist_model_data,
destination="/opt/ml/processing/model",
),
ProcessingInput(
source=test_input,
destination="/opt/ml/processing/test",
),
],
outputs=[
ProcessingOutput(output_name="evaluation", source="/opt/ml/processing/evaluation"),
],
code=os.path.join(base_dir, "code/evaluation.py"),
property_files=[evaluation_report],
)
f1_score = JsonGet(
step_name=eval_step.name,
property_file=evaluation_report,
json_path="metrics.f1.value",
)
fail_step = FailStep(
name="mxnet_mnist_fail", error_message=Join(on=":", values=["F1 score too low", f1_score])
)
cond_lte = ConditionLessThanOrEqualTo(
left=f1_score,
right=0.8,
)
cond_step = ConditionStep(
name="mxnet_mnist_condition",
conditions=[cond_lte],
if_steps=[fail_step],
else_steps=[],
)
pipeline = Pipeline(
name="local_pipeline_training_transform",
steps=[eval_step, cond_step],
sagemaker_session=local_pipeline_session,
)
pipeline.create("SageMakerRole", "pipeline for sdk integ testing")
with lock.lock(LOCK_PATH):
execution = pipeline.start()
pipeline_execution_describe_result = execution.describe()
assert pipeline_execution_describe_result["PipelineArn"] == "local_pipeline_training_transform"
assert pipeline_execution_describe_result["PipelineExecutionStatus"] == "Failed"
pipeline_execution_list_steps_result = execution.list_steps()
assert len(pipeline_execution_list_steps_result["PipelineExecutionSteps"]) == 3
for step in pipeline_execution_list_steps_result["PipelineExecutionSteps"]:
if step["StepName"] == "mxnet_mnist_eval":
assert step["StepStatus"] == "Succeeded"
elif step["StepName"] == "mxnet_mnist_condition":
assert step["StepStatus"] == "Succeeded"
assert step["Metadata"]["Condition"]["Outcome"] is True
else:
assert step["StepStatus"] == "Failed"
assert step["FailureReason"] == "F1 score too low:0.7"
|