File size: 46,287 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 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 | # 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.
"""The `Step` definitions for SageMaker Pipelines Workflows."""
from __future__ import absolute_import
import abc
import warnings
from enum import Enum
from typing import Dict, List, Set, Union, Optional, Any, TYPE_CHECKING
from urllib.parse import urlparse
import attr
from sagemaker import Session
from sagemaker.estimator import EstimatorBase, _TrainingJob
from sagemaker.inputs import CreateModelInput, TrainingInput, TransformInput, FileSystemInput
from sagemaker.model import Model
from sagemaker.pipeline import PipelineModel
from sagemaker.processing import (
ProcessingInput,
ProcessingJob,
ProcessingOutput,
Processor,
)
from sagemaker.transformer import Transformer, _TransformJob
from sagemaker.tuner import HyperparameterTuner, _TuningJob
from sagemaker.workflow.conditions import Condition
from sagemaker.workflow import is_pipeline_variable
from sagemaker.workflow.entities import (
DefaultEnumMeta,
Entity,
RequestType,
)
from sagemaker.workflow.pipeline_context import _JobStepArguments
from sagemaker.workflow.properties import (
PropertyFile,
Properties,
)
from sagemaker.workflow.entities import PipelineVariable
from sagemaker.workflow.functions import Join, JsonGet
from sagemaker.workflow.retry import RetryPolicy
if TYPE_CHECKING:
from sagemaker.workflow.step_collections import StepCollection
class StepTypeEnum(Enum, metaclass=DefaultEnumMeta):
"""Enum of `Step` types."""
CONDITION = "Condition"
CREATE_MODEL = "Model"
PROCESSING = "Processing"
REGISTER_MODEL = "RegisterModel"
TRAINING = "Training"
TRANSFORM = "Transform"
CALLBACK = "Callback"
TUNING = "Tuning"
LAMBDA = "Lambda"
QUALITY_CHECK = "QualityCheck"
CLARIFY_CHECK = "ClarifyCheck"
EMR = "EMR"
FAIL = "Fail"
@attr.s
class Step(Entity):
"""Pipeline `Step` for SageMaker Pipelines Workflows.
Attributes:
name (str): The name of the `Step`.
display_name (str): The display name of the `Step`.
description (str): The description of the `Step`.
step_type (StepTypeEnum): The type of the `Step`.
depends_on (List[Union[str, Step, StepCollection]]): The list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that the current `Step`
depends on.
"""
name: str = attr.ib(factory=str)
display_name: Optional[str] = attr.ib(default=None)
description: Optional[str] = attr.ib(default=None)
step_type: StepTypeEnum = attr.ib(factory=StepTypeEnum.factory)
depends_on: Optional[List[Union[str, "Step", "StepCollection"]]] = attr.ib(default=None)
@property
@abc.abstractmethod
def arguments(self) -> RequestType:
"""The arguments to the particular `Step` service call."""
@property
def step_only_arguments(self) -> RequestType:
"""The arguments to this Step only.
Compound Steps such as the ConditionStep will have to
override this method to return arguments pertaining to only that step.
"""
return self.arguments
@property
@abc.abstractmethod
def properties(self):
"""The properties of the particular `Step`."""
def to_request(self) -> RequestType:
"""Gets the request structure for workflow service calls."""
request_dict = {
"Name": self.name,
"Type": self.step_type.value,
"Arguments": self.arguments,
}
if self.depends_on:
request_dict["DependsOn"] = self._resolve_depends_on(self.depends_on)
if self.display_name:
request_dict["DisplayName"] = self.display_name
if self.description:
request_dict["Description"] = self.description
return request_dict
def add_depends_on(self, step_names: List[Union[str, "Step", "StepCollection"]]):
"""Add `Step` names or `Step` instances to the current `Step` depends on list."""
if not step_names:
return
if not self.depends_on:
self.depends_on = []
self.depends_on.extend(step_names)
@property
def ref(self) -> Dict[str, str]:
"""Gets a reference dictionary for `Step` instances."""
return {"Name": self.name}
@staticmethod
def _resolve_depends_on(
depends_on_list: List[Union[str, "Step", "StepCollection"]]
) -> List[str]:
"""Resolve the `Step` depends on list."""
from sagemaker.workflow.step_collections import StepCollection
depends_on = []
for step in depends_on_list:
# As for StepCollection, the names of its sub steps will be interpolated
# when generating the pipeline definition
if isinstance(step, (Step, StepCollection)):
depends_on.append(step.name)
elif isinstance(step, str):
depends_on.append(step)
else:
raise ValueError(f"Invalid input step type: {type(step)}")
return depends_on
def _find_step_dependencies(
self, step_map: Dict[str, Union["Step", "StepCollection"]]
) -> List[str]:
"""Find the all step names this step is dependent on."""
step_dependencies = set()
if self.depends_on:
step_dependencies.update(self._find_dependencies_in_depends_on_list(step_map))
step_dependencies.update(
self._find_dependencies_in_step_arguments(self.step_only_arguments, step_map)
)
return list(step_dependencies)
def _find_dependencies_in_depends_on_list(
self, step_map: Dict[str, Union["Step", "StepCollection"]]
) -> Set[str]:
"""Find dependency steps referenced in the depends-on field of this step."""
# import here to prevent circular import
from sagemaker.workflow.step_collections import StepCollection
dependencies = set()
for step in self.depends_on:
if isinstance(step, Step):
dependencies.add(step.name)
elif isinstance(step, StepCollection):
dependencies.add(step.steps[-1].name)
elif isinstance(step, str):
# step could be the name of a `Step` or a `StepCollection`
dependencies.add(self._get_step_name_from_str(step, step_map))
return dependencies
def _find_dependencies_in_step_arguments(
self, obj: Any, step_map: Dict[str, Union["Step", "StepCollection"]]
):
"""Find the step dependencies referenced in the arguments of this step."""
dependencies = set()
if isinstance(obj, dict):
for value in obj.values():
if isinstance(value, (PipelineVariable, Condition)):
for referenced_step in value._referenced_steps:
dependencies.add(self._get_step_name_from_str(referenced_step, step_map))
if isinstance(value, JsonGet):
self._validate_json_get_function(value, step_map)
dependencies.update(self._find_dependencies_in_step_arguments(value, step_map))
elif isinstance(obj, list):
for item in obj:
if isinstance(item, (PipelineVariable, Condition)):
for referenced_step in item._referenced_steps:
dependencies.add(self._get_step_name_from_str(referenced_step, step_map))
if isinstance(item, JsonGet):
self._validate_json_get_function(item, step_map)
dependencies.update(self._find_dependencies_in_step_arguments(item, step_map))
return dependencies
def _validate_json_get_function(
self, json_get: JsonGet, step_map: Dict[str, Union["Step", "StepCollection"]]
):
"""Validate the JsonGet function inputs."""
property_file_reference = json_get.property_file
processing_step = step_map[json_get.step_name]
property_file = None
if isinstance(property_file_reference, str):
if not isinstance(processing_step, ProcessingStep):
raise ValueError(
f"Invalid JsonGet function {json_get.expr} in step '{self.name}'. JsonGet "
f"function can only be evaluated on processing step outputs."
)
for file in processing_step.property_files:
if file.name == property_file_reference:
property_file = file
break
elif isinstance(property_file_reference, PropertyFile):
property_file = property_file_reference
if property_file is None:
raise ValueError(
f"Invalid JsonGet function {json_get.expr} in step '{self.name}'. Property file "
f"reference '{property_file_reference}' is undefined in step "
f"'{processing_step.name}'."
)
property_file_output = None
if "ProcessingOutputConfig" in processing_step.arguments:
for output in processing_step.arguments["ProcessingOutputConfig"]["Outputs"]:
if output["OutputName"] == property_file.output_name:
property_file_output = output
if property_file_output is None:
raise ValueError(
f"Processing output name '{property_file.output_name}' defined in property file "
f"'{property_file.name}' not found in processing step '{processing_step.name}'."
)
@staticmethod
def _get_step_name_from_str(
str_input: str, step_map: Dict[str, Union["Step", "StepCollection"]]
) -> str:
"""Convert a Step or StepCollection name input to step name."""
from sagemaker.workflow.step_collections import StepCollection
if str_input not in step_map:
raise ValueError(f"Step {str_input} is undefined.")
if isinstance(step_map[str_input], StepCollection):
return step_map[str_input].steps[-1].name
return str_input
@staticmethod
def _trim_experiment_config(request_dict: Dict):
"""For job steps, trim the experiment config to keep the trial component display name."""
if request_dict.get("ExperimentConfig", {}).get("TrialComponentDisplayName"):
request_dict["ExperimentConfig"] = {
"TrialComponentDisplayName": request_dict["ExperimentConfig"][
"TrialComponentDisplayName"
]
}
else:
request_dict.pop("ExperimentConfig", None)
@attr.s
class CacheConfig:
"""Configuration class to enable caching in SageMaker Pipelines Workflows.
If caching is enabled, the pipeline attempts to find a previous execution of a `Step`
that was called with the same arguments. `Step` caching only considers successful execution.
If a successful previous execution is found, the pipeline propagates the values
from the previous execution rather than recomputing the `Step`.
When multiple successful executions exist within the timeout period,
it uses the result for the most recent successful execution.
Attributes:
enable_caching (bool): To enable `Step` caching. Defaults to `False`.
expire_after (str): If `Step` caching is enabled, a timeout also needs to defined.
It defines how old a previous execution can be to be considered for reuse.
Value should be an ISO 8601 duration string. Defaults to `None`.
Examples::
'p30d' # 30 days
'P4DT12H' # 4 days and 12 hours
'T12H' # 12 hours
"""
enable_caching: bool = attr.ib(default=False)
expire_after = attr.ib(
default=None, validator=attr.validators.optional(attr.validators.instance_of(str))
)
@property
def config(self):
"""Configures `Step` caching for SageMaker Pipelines Workflows."""
config = {"Enabled": self.enable_caching}
if self.expire_after is not None:
config["ExpireAfter"] = self.expire_after
return {"CacheConfig": config}
class ConfigurableRetryStep(Step):
"""`ConfigurableRetryStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_type: StepTypeEnum,
display_name: str = None,
description: str = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: List[RetryPolicy] = None,
):
super().__init__(
name=name,
display_name=display_name,
step_type=step_type,
description=description,
depends_on=depends_on,
)
self.retry_policies = [] if not retry_policies else retry_policies
def add_retry_policy(self, retry_policy: RetryPolicy):
"""Add a policy to the current `ConfigurableRetryStep` retry policies list."""
if not retry_policy:
return
if not self.retry_policies:
self.retry_policies = []
self.retry_policies.append(retry_policy)
def to_request(self) -> RequestType:
"""Gets the request structure for `ConfigurableRetryStep`."""
step_dict = super().to_request()
if self.retry_policies:
step_dict["RetryPolicies"] = self._resolve_retry_policy(self.retry_policies)
return step_dict
@staticmethod
def _resolve_retry_policy(retry_policy_list: List[RetryPolicy]) -> List[RequestType]:
"""Resolve the `ConfigurableRetryStep` retry policy list."""
return [retry_policy.to_request() for retry_policy in retry_policy_list]
class TrainingStep(ConfigurableRetryStep):
"""`TrainingStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_args: _JobStepArguments = None,
estimator: EstimatorBase = None,
display_name: str = None,
description: str = None,
inputs: Union[TrainingInput, dict, str, FileSystemInput] = None,
cache_config: CacheConfig = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: List[RetryPolicy] = None,
):
"""Construct a `TrainingStep`, given an `EstimatorBase` instance.
In addition to the `EstimatorBase` instance, the other arguments are those
that are supplied to the `fit` method of the `sagemaker.estimator.Estimator`.
Args:
name (str): The name of the `TrainingStep`.
step_args (_JobStepArguments): The arguments for the `TrainingStep` definition.
estimator (EstimatorBase): A `sagemaker.estimator.EstimatorBase` instance.
display_name (str): The display name of the `TrainingStep`.
description (str): The description of the `TrainingStep`.
inputs (Union[str, dict, TrainingInput, FileSystemInput]): Information
about the training data. This can be one of three types:
* (str) the S3 location where training data is saved, or a file:// path in
local mode.
* (dict[str, str] or dict[str, sagemaker.inputs.TrainingInput]) If using multiple
channels for training data, you can specify a dictionary mapping channel names to
strings or :func:`~sagemaker.inputs.TrainingInput` objects.
* (sagemaker.inputs.TrainingInput) - channel configuration for S3 data sources
that can provide additional information as well as the path to the training
dataset.
See :func:`sagemaker.inputs.TrainingInput` for full details.
* (sagemaker.inputs.FileSystemInput) - channel configuration for
a file system data source that can provide additional information as well as
the path to the training dataset.
cache_config (CacheConfig): A `sagemaker.workflow.steps.CacheConfig` instance.
depends_on (List[Union[str, Step, StepCollection]]): A list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that this `TrainingStep`
depends on.
retry_policies (List[RetryPolicy]): A list of retry policies.
"""
super(TrainingStep, self).__init__(
name, StepTypeEnum.TRAINING, display_name, description, depends_on, retry_policies
)
if not (step_args is not None) ^ (estimator is not None):
raise ValueError("Either step_args or estimator need to be given.")
if step_args:
from sagemaker.workflow.utilities import validate_step_args_input
validate_step_args_input(
step_args=step_args,
expected_caller={Session.train.__name__},
error_message="The step_args of TrainingStep must be obtained from estimator.fit().",
)
self.step_args = step_args.args if step_args else None
self.estimator = estimator
self.inputs = inputs
self._properties = Properties(step_name=name, shape_name="DescribeTrainingJobResponse")
self.cache_config = cache_config
if self.cache_config:
if (self.step_args and "ProfilerConfig" in self.step_args) or (
self.estimator is not None and not self.estimator.disable_profiler
):
msg = (
"Profiling is enabled on the provided estimator. "
"The default profiler rule includes a timestamp "
"which will change each time the pipeline is "
"upserted, causing cache misses. If profiling "
"is not needed, set disable_profiler to True on the estimator."
)
warnings.warn(msg)
if not self.step_args:
warnings.warn(
(
'We are deprecating the instantiation of TrainingStep using "estimator".'
'Instead, simply using "step_args".'
),
DeprecationWarning,
)
self.job_name = None
if estimator and (estimator.source_dir or estimator.entry_point):
# By default, `Estimator` will upload the local code to an S3 path
# containing a timestamp. This causes cache misses whenever a
# pipeline is updated, even if the underlying script hasn't changed.
# To avoid this, hash the contents of the training script and include it
# in the `job_name` passed to the `Estimator`, which will be used
# instead of the timestamped path.
if not is_pipeline_variable(estimator.source_dir) and not is_pipeline_variable(
estimator.entry_point
):
self.job_name = self._generate_code_upload_path()
@property
def arguments(self) -> RequestType:
"""The arguments dictionary that is used to call `create_training_job`.
NOTE: The `CreateTrainingJob` request is not quite the args list that workflow needs.
The `TrainingJobName` and `ExperimentConfig` attributes cannot be included.
"""
if self.step_args:
request_dict = self.step_args
else:
self.estimator._prepare_for_training(self.job_name)
train_args = _TrainingJob._get_train_args(
self.estimator, self.inputs, experiment_config=dict()
)
request_dict = self.estimator.sagemaker_session._get_train_request(**train_args)
if "HyperParameters" in request_dict:
request_dict["HyperParameters"].pop("sagemaker_job_name", None)
request_dict.pop("TrainingJobName", None)
Step._trim_experiment_config(request_dict)
return request_dict
@property
def properties(self):
"""A `Properties` object representing the `DescribeTrainingJobResponse` data model."""
return self._properties
def to_request(self) -> RequestType:
"""Updates the request dictionary with cache configuration."""
request_dict = super().to_request()
if self.cache_config:
request_dict.update(self.cache_config.config)
return request_dict
def _generate_code_upload_path(self) -> str or None:
"""Generate an upload path for local training scripts based on their content."""
from sagemaker.workflow.utilities import hash_files_or_dirs
if self.estimator.source_dir:
source_dir_url = urlparse(self.estimator.source_dir)
if source_dir_url.scheme == "" or source_dir_url.scheme == "file":
code_hash = hash_files_or_dirs(
[self.estimator.source_dir] + self.estimator.dependencies
)
return f"{self.name}-{code_hash}"[:1024]
elif self.estimator.entry_point:
entry_point_url = urlparse(self.estimator.entry_point)
if entry_point_url.scheme == "" or entry_point_url.scheme == "file":
code_hash = hash_files_or_dirs(
[self.estimator.entry_point] + self.estimator.dependencies
)
return f"{self.name}-{code_hash}"[:1024]
return None
class CreateModelStep(ConfigurableRetryStep):
"""`CreateModelStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_args: Optional[dict] = None,
model: Optional[Union[Model, PipelineModel]] = None,
inputs: Optional[CreateModelInput] = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: Optional[List[RetryPolicy]] = None,
display_name: Optional[str] = None,
description: Optional[str] = None,
):
"""Construct a `CreateModelStep`, given an `sagemaker.model.Model` instance.
In addition to the `Model` instance, the other arguments are those that are supplied to
the `_create_sagemaker_model` method of the `sagemaker.model.Model._create_sagemaker_model`.
Args:
name (str): The name of the `CreateModelStep`.
step_args (dict): The arguments for the `CreateModelStep` definition (default: None).
model (Model or PipelineModel): A `sagemaker.model.Model`
or `sagemaker.pipeline.PipelineModel` instance (default: None).
inputs (CreateModelInput): A `sagemaker.inputs.CreateModelInput` instance.
(default: None).
depends_on (List[Union[str, Step, StepCollection]]): A list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that this `CreateModelStep`
depends on (default: None).
retry_policies (List[RetryPolicy]): A list of retry policies (default: None).
display_name (str): The display name of the `CreateModelStep` (default: None).
description (str): The description of the `CreateModelStep` (default: None).
"""
super(CreateModelStep, self).__init__(
name, StepTypeEnum.CREATE_MODEL, display_name, description, depends_on, retry_policies
)
if not (step_args is None) ^ (model is None):
raise ValueError(
"step_args and model are mutually exclusive. Either of them should be provided."
)
self.step_args = step_args
self.model = model
self.inputs = inputs or CreateModelInput()
self._properties = Properties(step_name=name, shape_name="DescribeModelOutput")
warnings.warn(
(
"We are deprecating the use of CreateModelStep. "
"Instead, please use the ModelStep, which simply takes in the step arguments "
"generated by model.create(). For more, see: "
"https://sagemaker.readthedocs.io/en/stable/"
"amazon_sagemaker_model_building_pipeline.html#model-step"
),
DeprecationWarning,
)
@property
def arguments(self) -> RequestType:
"""The arguments dictionary that is used to call `create_model`.
NOTE: The `CreateModelRequest` is not quite the args list that workflow needs.
`ModelName` cannot be included in the arguments.
"""
if self.step_args:
request_dict = self.step_args
else:
if isinstance(self.model, PipelineModel):
request_dict = self.model.sagemaker_session._create_model_request(
name="",
role=self.model.role,
container_defs=self.model.pipeline_container_def(self.inputs.instance_type),
vpc_config=self.model.vpc_config,
enable_network_isolation=self.model.enable_network_isolation,
)
else:
request_dict = self.model.sagemaker_session._create_model_request(
name="",
role=self.model.role,
container_defs=self.model.prepare_container_def(
instance_type=self.inputs.instance_type,
accelerator_type=self.inputs.accelerator_type,
),
vpc_config=self.model.vpc_config,
enable_network_isolation=self.model.enable_network_isolation(),
)
request_dict.pop("ModelName", None)
return request_dict
@property
def properties(self):
"""A `Properties` object representing the `DescribeModelResponse` data model."""
return self._properties
class TransformStep(ConfigurableRetryStep):
"""`TransformStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_args: _JobStepArguments = None,
transformer: Transformer = None,
inputs: TransformInput = None,
display_name: str = None,
description: str = None,
cache_config: CacheConfig = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: List[RetryPolicy] = None,
):
"""Constructs a `TransformStep`, given a `Transformer` instance.
In addition to the `Transformer` instance, the other arguments are those
that are supplied to the `transform` method of the `sagemaker.transformer.Transformer`.
Args:
name (str): The name of the `TransformStep`.
step_args (_JobStepArguments): The arguments for the `TransformStep` definition.
transformer (Transformer): A `sagemaker.transformer.Transformer` instance.
inputs (TransformInput): A `sagemaker.inputs.TransformInput` instance.
cache_config (CacheConfig): A `sagemaker.workflow.steps.CacheConfig` instance.
display_name (str): The display name of the `TransformStep`.
description (str): The description of the `TransformStep`.
depends_on (List[Union[str, Step, StepCollection]]): A list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that this `TransformStep`
depends on.
retry_policies (List[RetryPolicy]): A list of retry policies.
"""
super(TransformStep, self).__init__(
name, StepTypeEnum.TRANSFORM, display_name, description, depends_on, retry_policies
)
if not (step_args is not None) ^ (transformer is not None):
raise ValueError("either step_args or transformer need to be given, but not both.")
if step_args:
from sagemaker.workflow.utilities import validate_step_args_input
validate_step_args_input(
step_args=step_args,
expected_caller={Session.transform.__name__},
error_message="The step_args of TransformStep must be obtained "
"from transformer.transform().",
)
self.step_args = step_args.args if step_args else None
self.transformer = transformer
self.inputs = inputs
self.cache_config = cache_config
self._properties = Properties(step_name=name, shape_name="DescribeTransformJobResponse")
if not self.step_args:
if inputs is None:
raise ValueError("Inputs can't be None when transformer is given.")
warnings.warn(
(
'We are deprecating the instantiation of TransformStep using "transformer".'
'Instead, simply using "step_args".'
),
DeprecationWarning,
)
@property
def arguments(self) -> RequestType:
"""The arguments dictionary that is used to call `create_transform_job`.
NOTE: The `CreateTransformJob` request is not quite the args list that workflow needs.
`TransformJobName` and `ExperimentConfig` cannot be included in the arguments.
"""
if self.step_args:
request_dict = self.step_args
else:
transform_args = _TransformJob._get_transform_args(
transformer=self.transformer,
data=self.inputs.data,
data_type=self.inputs.data_type,
content_type=self.inputs.content_type,
compression_type=self.inputs.compression_type,
split_type=self.inputs.split_type,
input_filter=self.inputs.input_filter,
output_filter=self.inputs.output_filter,
join_source=self.inputs.join_source,
model_client_config=self.inputs.model_client_config,
experiment_config=dict(),
batch_data_capture_config=self.inputs.batch_data_capture_config,
)
request_dict = self.transformer.sagemaker_session._get_transform_request(
**transform_args
)
request_dict.pop("TransformJobName", None)
Step._trim_experiment_config(request_dict)
return request_dict
@property
def properties(self):
"""A `Properties` object representing the `DescribeTransformJobResponse` data model."""
return self._properties
def to_request(self) -> RequestType:
"""Updates the dictionary with cache configuration."""
request_dict = super().to_request()
if self.cache_config:
request_dict.update(self.cache_config.config)
return request_dict
class ProcessingStep(ConfigurableRetryStep):
"""`ProcessingStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_args: _JobStepArguments = None,
processor: Processor = None,
display_name: str = None,
description: str = None,
inputs: List[ProcessingInput] = None,
outputs: List[ProcessingOutput] = None,
job_arguments: List[str] = None,
code: str = None,
property_files: List[PropertyFile] = None,
cache_config: CacheConfig = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: List[RetryPolicy] = None,
kms_key=None,
):
"""Construct a `ProcessingStep`, given a `Processor` instance.
In addition to the `Processor` instance, the other arguments are those that are supplied to
the `process` method of the `sagemaker.processing.Processor`.
Args:
name (str): The name of the `ProcessingStep`.
step_args (_JobStepArguments): The arguments for the `ProcessingStep` definition.
processor (Processor): A `sagemaker.processing.Processor` instance.
display_name (str): The display name of the `ProcessingStep`.
description (str): The description of the `ProcessingStep`
inputs (List[ProcessingInput]): A list of `sagemaker.processing.ProcessorInput`
instances. Defaults to `None`.
outputs (List[ProcessingOutput]): A list of `sagemaker.processing.ProcessorOutput`
instances. Defaults to `None`.
job_arguments (List[str]): A list of strings to be passed into the processing job.
Defaults to `None`.
code (str): This can be an S3 URI or a local path to a file with the framework
script to run. Defaults to `None`.
property_files (List[PropertyFile]): A list of property files that workflow looks
for and resolves from the configured processing output list.
cache_config (CacheConfig): A `sagemaker.workflow.steps.CacheConfig` instance.
depends_on (List[Union[str, Step, StepCollection]]): A list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that this `ProcessingStep`
depends on.
retry_policies (List[RetryPolicy]): A list of retry policies.
kms_key (str): The ARN of the KMS key that is used to encrypt the
user code file. Defaults to `None`.
"""
super(ProcessingStep, self).__init__(
name, StepTypeEnum.PROCESSING, display_name, description, depends_on, retry_policies
)
if not (step_args is not None) ^ (processor is not None):
raise ValueError("either step_args or processor need to be given, but not both.")
if step_args:
from sagemaker.workflow.utilities import validate_step_args_input
validate_step_args_input(
step_args=step_args,
expected_caller={Session.process.__name__},
error_message="The step_args of ProcessingStep must be obtained from processor.run().",
)
self.step_args = step_args.args if step_args else None
self.processor = processor
self.inputs = inputs
self.outputs = outputs
self.job_arguments = job_arguments
self.code = code
self.property_files = property_files or []
self.job_name = None
self.kms_key = kms_key
self.cache_config = cache_config
self._properties = Properties(step_name=name, shape_name="DescribeProcessingJobResponse")
if not self.step_args:
# Examine why run method in `sagemaker.processing.Processor`
# mutates the processor instance by setting the instance's
# arguments attribute. Refactor `Processor.run`, if possible.
self.processor.arguments = job_arguments
if code:
if is_pipeline_variable(code):
raise ValueError(
"code argument has to be a valid S3 URI or local file path "
+ "rather than a pipeline variable"
)
code_url = urlparse(code)
if code_url.scheme == "" or code_url.scheme == "file":
# By default, `Processor` will upload the local code to an S3 path
# containing a timestamp. This causes cache misses whenever a
# pipeline is updated, even if the underlying script hasn't changed.
# To avoid this, hash the contents of the script and include it
# in the `job_name` passed to the `Processor`, which will be used
# instead of the timestamped path.
self.job_name = self._generate_code_upload_path()
warnings.warn(
(
'We are deprecating the instantiation of ProcessingStep using "processor".'
'Instead, simply using "step_args".'
),
DeprecationWarning,
)
@property
def arguments(self) -> RequestType:
"""The arguments dictionary that is used to call `create_processing_job`.
NOTE: The `CreateProcessingJob` request is not quite the args list that workflow needs.
`ProcessingJobName` and `ExperimentConfig` cannot be included in the arguments.
"""
if self.step_args:
request_dict = self.step_args
else:
normalized_inputs, normalized_outputs = self.processor._normalize_args(
job_name=self.job_name,
arguments=self.job_arguments,
inputs=self.inputs,
outputs=self.outputs,
code=self.code,
kms_key=self.kms_key,
)
process_args = ProcessingJob._get_process_args(
self.processor, normalized_inputs, normalized_outputs, experiment_config=dict()
)
request_dict = self.processor.sagemaker_session._get_process_request(**process_args)
request_dict.pop("ProcessingJobName", None)
Step._trim_experiment_config(request_dict)
return request_dict
@property
def properties(self):
"""A `Properties` object representing the `DescribeProcessingJobResponse` data model."""
return self._properties
def to_request(self) -> RequestType:
"""Get the request structure for workflow service calls."""
request_dict = super(ProcessingStep, self).to_request()
if self.cache_config:
request_dict.update(self.cache_config.config)
if self.property_files:
request_dict["PropertyFiles"] = [
property_file.expr for property_file in self.property_files
]
return request_dict
def _generate_code_upload_path(self) -> str:
"""Generate an upload path for local processing scripts based on its contents."""
from sagemaker.workflow.utilities import hash_file
code_hash = hash_file(self.code)
return f"{self.name}-{code_hash}"[:1024]
class TuningStep(ConfigurableRetryStep):
"""`TuningStep` for SageMaker Pipelines Workflows."""
def __init__(
self,
name: str,
step_args: _JobStepArguments = None,
tuner: HyperparameterTuner = None,
display_name: str = None,
description: str = None,
inputs=None,
job_arguments: List[str] = None,
cache_config: CacheConfig = None,
depends_on: Optional[List[Union[str, Step, "StepCollection"]]] = None,
retry_policies: List[RetryPolicy] = None,
):
"""Construct a `TuningStep`, given a `HyperparameterTuner` instance.
In addition to the `HyperparameterTuner` instance, the other arguments are those
that are supplied to the `fit` method of the `sagemaker.tuner.HyperparameterTuner`.
Args:
name (str): The name of the `TuningStep`.
step_args (_JobStepArguments): The arguments for the `TuningStep` definition.
tuner (HyperparameterTuner): A `sagemaker.tuner.HyperparameterTuner` instance.
display_name (str): The display name of the `TuningStep`.
description (str): The description of the `TuningStep`.
inputs: Information about the training data. Please refer to the
`fit()` method of the associated estimator, as this can take
any of the following forms:
* (str) - The S3 location where training data is saved.
* (dict[str, str] or dict[str, sagemaker.inputs.TrainingInput]) -
If using multiple channels for training data, you can specify
a dictionary mapping channel names to strings or
:func:`~sagemaker.inputs.TrainingInput` objects.
* (sagemaker.inputs.TrainingInput) - Channel configuration for S3 data sources
that can provide additional information about the training dataset.
See :func:`sagemaker.inputs.TrainingInput` for full details.
* (sagemaker.session.FileSystemInput) - channel configuration for
a file system data source that can provide additional information as well as
the path to the training dataset.
* (sagemaker.amazon.amazon_estimator.RecordSet) - A collection of
Amazon :class:~`Record` objects serialized and stored in S3.
For use with an estimator for an Amazon algorithm.
* (sagemaker.amazon.amazon_estimator.FileSystemRecordSet) -
Amazon SageMaker channel configuration for a file system data source for
Amazon algorithms.
* (list[sagemaker.amazon.amazon_estimator.RecordSet]) - A list of
:class:~`sagemaker.amazon.amazon_estimator.RecordSet` objects,
where each instance is a different channel of training data.
* (list[sagemaker.amazon.amazon_estimator.FileSystemRecordSet]) - A list of
:class:~`sagemaker.amazon.amazon_estimator.FileSystemRecordSet` objects,
where each instance is a different channel of training data.
job_arguments (List[str]): A list of strings to be passed into the processing job.
Defaults to `None`.
cache_config (CacheConfig): A `sagemaker.workflow.steps.CacheConfig` instance.
depends_on (List[Union[str, Step, StepCollection]]): A list of `Step`/`StepCollection`
names or `Step` instances or `StepCollection` instances that this `TuningStep`
depends on.
retry_policies (List[RetryPolicy]): A list of retry policies.
"""
super(TuningStep, self).__init__(
name, StepTypeEnum.TUNING, display_name, description, depends_on, retry_policies
)
if not (step_args is not None) ^ (tuner is not None):
raise ValueError("either step_args or tuner need to be given, but not both.")
if step_args:
from sagemaker.workflow.utilities import validate_step_args_input
validate_step_args_input(
step_args=step_args,
expected_caller={Session.create_tuning_job.__name__},
error_message="The step_args of TuningStep must be obtained from tuner.fit().",
)
self.step_args = step_args.args if step_args else None
self.tuner = tuner
self.inputs = inputs
self.job_arguments = job_arguments
self._properties = Properties(
step_name=name,
shape_names=[
"DescribeHyperParameterTuningJobResponse",
"ListTrainingJobsForHyperParameterTuningJobResponse",
],
)
self.cache_config = cache_config
if not self.step_args:
warnings.warn(
(
'We are deprecating the instantiation of TuningStep using "tuner".'
'Instead, simply using "step_args".'
),
DeprecationWarning,
)
@property
def arguments(self) -> RequestType:
"""The arguments dictionary that is used to call `create_hyper_parameter_tuning_job`.
NOTE: The `CreateHyperParameterTuningJob` request is not quite the
args list that workflow needs.
The `HyperParameterTuningJobName` attribute cannot be included.
"""
if self.step_args:
request_dict = self.step_args
else:
if self.tuner.estimator is not None:
self.tuner.estimator._prepare_for_training()
else:
for _, estimator in self.tuner.estimator_dict.items():
estimator._prepare_for_training()
self.tuner._prepare_for_tuning()
tuner_args = _TuningJob._get_tuner_args(self.tuner, self.inputs)
request_dict = self.tuner.sagemaker_session._get_tuning_request(**tuner_args)
request_dict.pop("HyperParameterTuningJobName", None)
return request_dict
@property
def properties(self):
"""A `Properties` object
A `Properties` object representing `DescribeHyperParameterTuningJobResponse` and
`ListTrainingJobsForHyperParameterTuningJobResponse` data model.
"""
return self._properties
def to_request(self) -> RequestType:
"""Updates the dictionary with cache configuration."""
request_dict = super().to_request()
if self.cache_config:
request_dict.update(self.cache_config.config)
return request_dict
def get_top_model_s3_uri(self, top_k: int, s3_bucket: str, prefix: str = "") -> Join:
"""Get the model artifact S3 URI from the top performing training jobs.
Args:
top_k (int): The index of the top performing training job
tuning step stores up to 50 top performing training jobs.
A valid top_k value is from 0 to 49. The best training job
model is at index 0.
s3_bucket (str): The S3 bucket to store the training job output artifact.
prefix (str): The S3 key prefix to store the training job output artifact.
"""
values = ["s3:/", s3_bucket]
if prefix != "" and prefix is not None:
values.append(prefix)
return Join(
on="/",
values=values
+ [
self.properties.TrainingJobSummaries[top_k].TrainingJobName,
"output/model.tar.gz",
],
)
|