Skip to content

Commit d3171d4

Browse files
author
Mourya Baddam
committed
fix: Apply default experiment config for pipelines only in regions with SageMaker Experiments
1 parent be0d6b5 commit d3171d4

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

src/sagemaker/workflow/_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@
6565
--source_dir "${var_source_dir}"
6666
"""
6767

68+
# Static list of regions where SageMaker Experiments is Available.
69+
# Note: Experiments is not expanding to new regions, so this list is static.
70+
EXPERIMENTS_REGIONS = frozenset([
71+
"us-east-1", # US East (N. Virginia)
72+
"us-east-2", # US East (Ohio)
73+
"us-west-1", # US West (N. California)
74+
"us-west-2", # US West (Oregon)
75+
"ca-central-1", # Canada (Central)
76+
"eu-west-1", # Europe (Ireland)
77+
"eu-west-2", # Europe (London)
78+
"eu-west-3", # Europe (Paris)
79+
"eu-central-1", # Europe (Frankfurt)
80+
"eu-north-1", # Europe (Stockholm)
81+
"eu-south-1", # Europe (Milan)
82+
"eu-south-2", # Europe (Spain)
83+
"ap-northeast-1", # Asia Pacific (Tokyo)
84+
"ap-northeast-2", # Asia Pacific (Seoul)
85+
"ap-northeast-3", # Asia Pacific (Osaka)
86+
"ap-southeast-1", # Asia Pacific (Singapore)
87+
"ap-southeast-2", # Asia Pacific (Sydney)
88+
"ap-southeast-3", # Asia Pacific (Jakarta)
89+
"ap-south-1", # Asia Pacific (Mumbai)
90+
"ap-east-1", # Asia Pacific (Hong Kong)
91+
"sa-east-1", # South America (São Paulo)
92+
"af-south-1", # Africa (Cape Town)
93+
"me-south-1", # Middle East (Bahrain)
94+
"il-central-1", # Israel (Tel Aviv)
95+
"cn-north-1", # China (Beijing)
96+
"cn-northwest-1", # China (Ningxia)
97+
])
6898

6999
class _RepackModelStep(TrainingStep):
70100
"""Repacks model artifacts with custom inference entry points.

src/sagemaker/workflow/pipeline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from sagemaker.s3_utils import s3_path_join
3535
from sagemaker.session import Session
3636
from sagemaker.utils import resolve_value_from_config, retry_with_backoff, format_tags, Tags
37+
from sagemaker.workflow._utils import EXPERIMENTS_REGIONS
3738
from sagemaker.workflow.callback_step import CallbackOutput, CallbackStep
3839
from sagemaker.workflow._event_bridge_client_helper import (
3940
EventBridgeSchedulerHelper,
@@ -99,6 +100,8 @@ def __init__(
99100
the same name already exists. By default, pipeline name is used as
100101
experiment name and execution id is used as the trial name.
101102
If set to None, no experiment or trial will be created automatically.
103+
Note: The default experiment config is only applied in regions where
104+
SageMaker Experiments is Available.
102105
steps (Sequence[Union[Step, StepCollection, StepOutput]]): The list of the
103106
non-conditional steps associated with the pipeline. Any steps that are within the
104107
`if_steps` or `else_steps` of a `ConditionStep` cannot be listed in the steps of a
@@ -127,6 +130,12 @@ def __init__(
127130
self.sagemaker_session.boto_session.client("scheduler"),
128131
)
129132

133+
# Apply default experiment config only in regions where Experiments is available
134+
if pipeline_experiment_config is _DEFAULT_EXPERIMENT_CFG:
135+
region = self.sagemaker_session.boto_region_name
136+
if region not in EXPERIMENTS_REGIONS:
137+
self.pipeline_experiment_config = None
138+
130139
@property
131140
def latest_pipeline_version_id(self):
132141
"""Retrieves the latest version id of this pipeline"""

tests/unit/sagemaker/workflow/test_pipeline.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,99 @@ def test_pipeline_disable_experiment_config(sagemaker_session_mock):
791791
)
792792

793793

794+
@patch("sagemaker.workflow.pipeline.EXPERIMENTS_REGIONS", {"us-east-1", "us-west-2"})
795+
def test_pipeline_init_with_default_experiment_config_in_supported_region(sagemaker_session_mock):
796+
"""Test that default experiment config is preserved in regions where Experiments is available."""
797+
sagemaker_session_mock.boto_region_name = "us-east-1"
798+
799+
pipeline = Pipeline(
800+
name="MyPipeline",
801+
steps=[CustomStep(name="MyStep", input_data="input")],
802+
sagemaker_session=sagemaker_session_mock,
803+
)
804+
805+
# Default experiment config should be preserved
806+
assert pipeline.pipeline_experiment_config is not None
807+
assert (
808+
pipeline.pipeline_experiment_config.experiment_name == ExecutionVariables.PIPELINE_NAME
809+
)
810+
assert (
811+
pipeline.pipeline_experiment_config.trial_name
812+
== ExecutionVariables.PIPELINE_EXECUTION_ID
813+
)
814+
815+
816+
@patch("sagemaker.workflow.pipeline.EXPERIMENTS_REGIONS", {"us-east-1", "us-west-2"})
817+
def test_pipeline_init_with_default_experiment_config_in_unsupported_region(
818+
sagemaker_session_mock,
819+
):
820+
"""Test that default experiment config is disabled in regions where Experiments is not available."""
821+
sagemaker_session_mock.boto_region_name = "ap-southeast-4" # Not in EXPERIMENTS_REGIONS
822+
823+
pipeline = Pipeline(
824+
name="MyPipeline",
825+
steps=[CustomStep(name="MyStep", input_data="input")],
826+
sagemaker_session=sagemaker_session_mock,
827+
)
828+
829+
# Default experiment config should be set to None
830+
assert pipeline.pipeline_experiment_config is None
831+
832+
833+
@patch("sagemaker.workflow.pipeline.EXPERIMENTS_REGIONS", {"us-east-1", "us-west-2"})
834+
def test_pipeline_init_with_explicit_experiment_config_in_unsupported_region(
835+
sagemaker_session_mock,
836+
):
837+
"""Test that explicitly set experiment config is preserved even in unsupported regions."""
838+
sagemaker_session_mock.boto_region_name = "ap-southeast-4" # Not in EXPERIMENTS_REGIONS
839+
840+
explicit_config = PipelineExperimentConfig("MyExperiment", "MyTrial")
841+
pipeline = Pipeline(
842+
name="MyPipeline",
843+
pipeline_experiment_config=explicit_config,
844+
steps=[CustomStep(name="MyStep", input_data="input")],
845+
sagemaker_session=sagemaker_session_mock,
846+
)
847+
848+
# Explicitly set experiment config should be preserved
849+
assert pipeline.pipeline_experiment_config is not None
850+
assert pipeline.pipeline_experiment_config.experiment_name == "MyExperiment"
851+
assert pipeline.pipeline_experiment_config.trial_name == "MyTrial"
852+
853+
854+
@patch("sagemaker.workflow.pipeline.EXPERIMENTS_REGIONS", {"us-east-1", "us-west-2"})
855+
def test_pipeline_init_with_none_experiment_config_in_supported_region(sagemaker_session_mock):
856+
"""Test that explicitly setting experiment config to None is respected in supported regions."""
857+
sagemaker_session_mock.boto_region_name = "us-east-1"
858+
859+
pipeline = Pipeline(
860+
name="MyPipeline",
861+
pipeline_experiment_config=None,
862+
steps=[CustomStep(name="MyStep", input_data="input")],
863+
sagemaker_session=sagemaker_session_mock,
864+
)
865+
866+
# Explicitly set None should be preserved
867+
assert pipeline.pipeline_experiment_config is None
868+
869+
870+
@patch("sagemaker.workflow.pipeline.EXPERIMENTS_REGIONS", {"us-east-1"})
871+
def test_pipeline_definition_without_experiment_config_in_unsupported_region(
872+
sagemaker_session_mock,
873+
):
874+
"""Test that pipeline definition has no experiment config in unsupported regions."""
875+
sagemaker_session_mock.boto_region_name = "ap-southeast-4" # Not in EXPERIMENTS_REGIONS
876+
877+
pipeline = Pipeline(
878+
name="MyPipeline",
879+
steps=[CustomStep(name="MyStep", input_data="input")],
880+
sagemaker_session=sagemaker_session_mock,
881+
)
882+
883+
definition = json.loads(pipeline.definition())
884+
assert definition["PipelineExperimentConfig"] is None
885+
886+
794887
def test_pipeline_list_executions(sagemaker_session_mock):
795888
sagemaker_session_mock.sagemaker_client.list_pipeline_executions.return_value = {
796889
"PipelineExecutionSummaries": [Mock()],

0 commit comments

Comments
 (0)