Skip to content

Commit fa89781

Browse files
mohamedzeidan2021Mohamed Zeidan
andauthored
fix: accept a PipelineVariable for HyperparameterTuner random_seed (#6320)
* fix: accept pipeline variable for HyperparameterTuner random_seed HyperParameterTuningJobConfig.random_seed was typed Optional[int]; assigning a pipeline variable (e.g. ParameterInteger) raised ValidationError under the shape's validate_assignment. Type it Optional[IntPipeVar] (via the codegen PIPE_VAR_OVERRIDES source of truth + the generated shapes.py), matching sibling fields like instance_count. Also broaden the ModelTrainer-facing tuner random_seed signature to Optional[Union[int, PipelineVariable]]. Fixes #5614 (random_seed part), #6171 * docs: note random_seed accepts a PipelineVariable in tuner docstrings --------- Co-authored-by: Mohamed Zeidan <zeidmo@amazon.com>
1 parent 7e84ec9 commit fa89781

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎sagemaker-core/src/sagemaker/core/shapes/shapes.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7438,7 +7438,7 @@ class HyperParameterTuningJobConfig(Base):
74387438
parameter_ranges: Optional[ParameterRanges] = Unassigned()
74397439
training_job_early_stopping_type: Optional[StrPipeVar] = Unassigned()
74407440
tuning_job_completion_criteria: Optional[TuningJobCompletionCriteria] = Unassigned()
7441-
random_seed: Optional[int] = Unassigned()
7441+
random_seed: Optional[IntPipeVar] = Unassigned()
74427442

74437443

74447444
class HyperParameterAlgorithmSpecification(Base):

‎sagemaker-core/src/sagemaker/core/tools/constants.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,9 @@
147147
"ProcessingInstancePreference": {
148148
"InstanceCount": "IntPipeVar",
149149
},
150+
# RandomSeed accepts a pipeline variable (e.g. a ParameterInteger) so tuning pipelines
151+
# can parameterize reproducibility (issue #5614 / #6171).
152+
"HyperParameterTuningJobConfig": {
153+
"RandomSeed": "IntPipeVar",
154+
},
150155
}

‎sagemaker-train/src/sagemaker/train/tuner.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __init__(
106106
completion_criteria_config: Optional[TuningJobCompletionCriteria] = None,
107107
early_stopping_type: Union[str, PipelineVariable] = "Off",
108108
model_trainer_name: Optional[str] = None,
109-
random_seed: Optional[int] = None,
109+
random_seed: Optional[Union[int, PipelineVariable]] = None,
110110
autotune: bool = False,
111111
hyperparameters_to_keep_static: Optional[List[str]] = None,
112112
):
@@ -172,7 +172,8 @@ def __init__(
172172
model_trainer_name (str): A unique name to identify a model_trainer within the
173173
hyperparameter tuning job, when more than one model_trainer is used with
174174
the same tuning job (default: None).
175-
random_seed (int): An initial value used to initialize a pseudo-random number generator.
175+
random_seed (int or PipelineVariable): An initial value used to initialize a pseudo-random
176+
number generator.
176177
Setting a random seed will make the hyperparameter tuning search strategies to
177178
produce more consistent configurations for the same tuning job.
178179
autotune (bool): Whether the parameter ranges or other unset settings of a tuning job
@@ -1081,7 +1082,8 @@ def create(
10811082
Can be either 'Auto' or 'Off' (default: 'Off'). If set to 'Off', early stopping
10821083
will not be attempted. If set to 'Auto', early stopping of some training jobs may
10831084
happen, but is not guaranteed to.
1084-
random_seed (int): An initial value used to initialize a pseudo-random number generator.
1085+
random_seed (int or PipelineVariable): An initial value used to initialize a pseudo-random
1086+
number generator.
10851087
Setting a random seed will make the hyperparameter tuning search strategies to
10861088
produce more consistent configurations for the same tuning job.
10871089
autotune (bool): Whether the parameter ranges or other unset settings of a tuning job

‎sagemaker-train/tests/unit/train/test_tuner.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,30 @@ def test_init_with_random_seed(self, mock_model_trainer, hyperparameter_ranges):
261261

262262
assert tuner.random_seed == 42
263263

264+
def test_random_seed_accepts_pipeline_variable(self, mock_model_trainer, hyperparameter_ranges):
265+
"""Regression for #5614 / #6171.
266+
267+
``random_seed`` must accept a pipeline variable (e.g. a ParameterInteger). Building the
268+
tuning job config assigns it to ``HyperParameterTuningJobConfig.random_seed`` under
269+
``validate_assignment=True``; when that field was typed ``Optional[int]`` this raised
270+
``ValidationError: 1 validation error for HyperParameterTuningJobConfig``.
271+
"""
272+
from sagemaker.core.workflow.parameters import ParameterInteger
273+
274+
seed = ParameterInteger(name="RandomState", default_value=42)
275+
tuner = HyperparameterTuner(
276+
model_trainer=mock_model_trainer,
277+
objective_metric_name="accuracy",
278+
hyperparameter_ranges=hyperparameter_ranges,
279+
max_jobs=2,
280+
max_parallel_jobs=1,
281+
random_seed=seed,
282+
)
283+
284+
assert tuner.random_seed is seed
285+
config = tuner._build_tuning_job_config()
286+
assert config.random_seed is seed
287+
264288
def test_init_with_autotune(self, mock_model_trainer):
265289
"""Test initialization with autotune enabled."""
266290
tuner = HyperparameterTuner(

0 commit comments

Comments
 (0)