forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model_trainer_pipeline_variable.py
More file actions
178 lines (157 loc) · 7.51 KB
/
test_model_trainer_pipeline_variable.py
File metadata and controls
178 lines (157 loc) · 7.51 KB
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
# 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.
"""Tests for PipelineVariable support in ModelTrainer (GH#5524).
Verifies that ModelTrainer fields accept PipelineVariable objects
(e.g., ParameterString) in addition to their concrete types, following
the existing V3 pattern established by SourceCode and OutputDataConfig.
See: https://github.com/aws/sagemaker-python-sdk/issues/5524
"""
from __future__ import absolute_import
import pytest
from pydantic import ValidationError
from unittest.mock import patch, MagicMock
from sagemaker.core.helper.session_helper import Session
from sagemaker.core.helper.pipeline_variable import PipelineVariable, StrPipeVar
from sagemaker.core.workflow.parameters import ParameterString
from sagemaker.train.model_trainer import ModelTrainer, Mode
from sagemaker.train.configs import (
Compute,
StoppingCondition,
OutputDataConfig,
)
from sagemaker.train.defaults import DEFAULT_INSTANCE_TYPE
DEFAULT_IMAGE = "000000000000.dkr.ecr.us-west-2.amazonaws.com/dummy-image:latest"
DEFAULT_BUCKET = "sagemaker-us-west-2-000000000000"
DEFAULT_ROLE = "arn:aws:iam::000000000000:role/test-role"
DEFAULT_BUCKET_PREFIX = "sample-prefix"
DEFAULT_REGION = "us-west-2"
DEFAULT_COMPUTE = Compute(instance_type=DEFAULT_INSTANCE_TYPE, instance_count=1)
DEFAULT_STOPPING = StoppingCondition(max_runtime_in_seconds=3600)
DEFAULT_OUTPUT = OutputDataConfig(
s3_output_path=f"s3://{DEFAULT_BUCKET}/{DEFAULT_BUCKET_PREFIX}/test-job",
)
@pytest.fixture(scope="module", autouse=True)
def modules_session():
with patch("sagemaker.train.Session", spec=Session) as session_mock:
session_instance = session_mock.return_value
session_instance.default_bucket.return_value = DEFAULT_BUCKET
session_instance.get_caller_identity_arn.return_value = DEFAULT_ROLE
session_instance.default_bucket_prefix = DEFAULT_BUCKET_PREFIX
session_instance.boto_session = MagicMock(spec="boto3.session.Session")
session_instance.boto_region_name = DEFAULT_REGION
yield session_instance
class TestModelTrainerPipelineVariableAcceptance:
"""Test that ModelTrainer fields accept PipelineVariable objects."""
def test_training_image_accepts_parameter_string(self):
"""ModelTrainer.training_image should accept ParameterString (GH#5524)."""
param = ParameterString(name="TrainingImage", default_value=DEFAULT_IMAGE)
trainer = ModelTrainer(
training_image=param,
base_job_name="pipeline-test-job", # Required: PipelineVariable can't generate job name
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.training_image is param
def test_algorithm_name_accepts_parameter_string(self):
"""ModelTrainer.algorithm_name should accept ParameterString."""
param = ParameterString(name="AlgorithmName", default_value="my-algo-arn")
trainer = ModelTrainer(
algorithm_name=param,
base_job_name="pipeline-test-job", # Required: PipelineVariable can't generate job name
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.algorithm_name is param
def test_training_input_mode_accepts_parameter_string(self):
"""ModelTrainer.training_input_mode should accept ParameterString."""
param = ParameterString(name="InputMode", default_value="File")
trainer = ModelTrainer(
training_image=DEFAULT_IMAGE,
training_input_mode=param,
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.training_input_mode is param
def test_environment_values_accept_parameter_string(self):
"""ModelTrainer.environment dict values should accept ParameterString."""
param = ParameterString(name="DatasetVersion", default_value="v1")
trainer = ModelTrainer(
training_image=DEFAULT_IMAGE,
environment={"DATASET_VERSION": param, "STATIC_VAR": "hello"},
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.environment["DATASET_VERSION"] is param
assert trainer.environment["STATIC_VAR"] == "hello"
class TestModelTrainerRealValuesStillWork:
"""Regression tests: verify that passing real values still works after the change."""
def test_training_image_accepts_real_string(self):
"""ModelTrainer.training_image should still accept a plain string."""
trainer = ModelTrainer(
training_image=DEFAULT_IMAGE,
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.training_image == DEFAULT_IMAGE
def test_algorithm_name_accepts_real_string(self):
"""ModelTrainer.algorithm_name should still accept a plain string."""
trainer = ModelTrainer(
algorithm_name="arn:aws:sagemaker:us-west-2:000000000000:algorithm/my-algo",
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.algorithm_name == "arn:aws:sagemaker:us-west-2:000000000000:algorithm/my-algo"
def test_training_input_mode_accepts_real_string(self):
"""ModelTrainer.training_input_mode should still accept a plain string."""
trainer = ModelTrainer(
training_image=DEFAULT_IMAGE,
training_input_mode="Pipe",
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.training_input_mode == "Pipe"
def test_environment_accepts_real_string_values(self):
"""ModelTrainer.environment should still accept plain string values."""
trainer = ModelTrainer(
training_image=DEFAULT_IMAGE,
environment={"KEY1": "value1", "KEY2": "value2"},
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)
assert trainer.environment == {"KEY1": "value1", "KEY2": "value2"}
def test_training_image_rejects_invalid_type(self):
"""ModelTrainer.training_image should still reject invalid types (e.g., int)."""
with pytest.raises(ValidationError):
ModelTrainer(
training_image=12345,
role=DEFAULT_ROLE,
compute=DEFAULT_COMPUTE,
stopping_condition=DEFAULT_STOPPING,
output_data_config=DEFAULT_OUTPUT,
)