Skip to content

Commit fe2f885

Browse files
author
Dewen Qi
committed
fix: Include deprecated JsonGet into PipelineVariable
1 parent b1a530a commit fe2f885

13 files changed

+58
-41
lines changed

src/sagemaker/estimator.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
get_config_value,
7575
name_from_base,
7676
)
77-
from sagemaker.workflow.entities import PipelineVariable
77+
from sagemaker.workflow import is_pipeline_variable
7878

7979
logger = logging.getLogger(__name__)
8080

@@ -600,7 +600,7 @@ def _json_encode_hyperparameters(hyperparameters: Dict[str, Any]) -> Dict[str, A
600600
current_hyperparameters = hyperparameters
601601
if current_hyperparameters is not None:
602602
hyperparameters = {
603-
str(k): (v.to_string() if isinstance(v, PipelineVariable) else json.dumps(v))
603+
str(k): (v.to_string() if is_pipeline_variable(v) else json.dumps(v))
604604
for (k, v) in current_hyperparameters.items()
605605
}
606606
return hyperparameters
@@ -1811,7 +1811,7 @@ def _get_train_args(cls, estimator, inputs, experiment_config):
18111811
current_hyperparameters = estimator.hyperparameters()
18121812
if current_hyperparameters is not None:
18131813
hyperparameters = {
1814-
str(k): (v.to_string() if isinstance(v, PipelineVariable) else str(v))
1814+
str(k): (v.to_string() if is_pipeline_variable(v) else str(v))
18151815
for (k, v) in current_hyperparameters.items()
18161816
}
18171817

src/sagemaker/model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from sagemaker.utils import unique_name_from_base
3838
from sagemaker.async_inference import AsyncInferenceConfig
3939
from sagemaker.predictor_async import AsyncPredictor
40-
from sagemaker.workflow.entities import PipelineVariable
40+
from sagemaker.workflow import is_pipeline_variable
4141

4242
LOGGER = logging.getLogger("sagemaker")
4343

@@ -444,7 +444,7 @@ def _upload_code(self, key_prefix: str, repack: bool = False) -> None:
444444
)
445445

446446
if repack and self.model_data is not None and self.entry_point is not None:
447-
if isinstance(self.model_data, PipelineVariable):
447+
if is_pipeline_variable(self.model_data):
448448
# model is not yet there, defer repacking to later during pipeline execution
449449
return
450450

src/sagemaker/parameter.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import json
1717

18-
from sagemaker.workflow.entities import PipelineVariable
18+
from sagemaker.workflow import is_pipeline_variable
1919

2020

2121
class ParameterRange(object):
@@ -72,10 +72,10 @@ def as_tuning_range(self, name):
7272
return {
7373
"Name": name,
7474
"MinValue": str(self.min_value)
75-
if not isinstance(self.min_value, PipelineVariable)
75+
if not is_pipeline_variable(self.min_value)
7676
else self.min_value.to_string(),
7777
"MaxValue": str(self.max_value)
78-
if not isinstance(self.max_value, PipelineVariable)
78+
if not is_pipeline_variable(self.max_value)
7979
else self.max_value.to_string(),
8080
"ScalingType": self.scaling_type,
8181
}
@@ -110,9 +110,7 @@ def __init__(self, values): # pylint: disable=super-init-not-called
110110
This input will be converted into a list of strings.
111111
"""
112112
values = values if isinstance(values, list) else [values]
113-
self.values = [
114-
str(v) if not isinstance(v, PipelineVariable) else v.to_string() for v in values
115-
]
113+
self.values = [str(v) if not is_pipeline_variable(v) else v.to_string() for v in values]
116114

117115
def as_tuning_range(self, name):
118116
"""Represent the parameter range as a dictionary.

src/sagemaker/processing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
from sagemaker.local import LocalSession
3535
from sagemaker.utils import base_name_from_image, get_config_value, name_from_base
3636
from sagemaker.session import Session
37+
from sagemaker.workflow import is_pipeline_variable
3738
from sagemaker.workflow.properties import Properties
3839
from sagemaker.workflow.parameters import Parameter
39-
from sagemaker.workflow.entities import Expression, PipelineVariable
40+
from sagemaker.workflow.entities import Expression
4041
from sagemaker.dataset_definition.inputs import S3Input, DatasetDefinition
4142
from sagemaker.apiutils._base_types import ApiObject
4243
from sagemaker.s3 import S3Uploader
4344

44-
4545
logger = logging.getLogger(__name__)
4646

4747

@@ -233,7 +233,7 @@ def _normalize_args(
233233
kms_key (str): The ARN of the KMS key that is used to encrypt the
234234
user code file (default: None).
235235
"""
236-
if code and isinstance(code, PipelineVariable):
236+
if code and is_pipeline_variable(code):
237237
raise ValueError(
238238
"code argument has to be a valid S3 URI or local file path "
239239
+ "rather than a pipeline variable"

src/sagemaker/tensorflow/model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from sagemaker.deprecations import removed_kwargs
2222
from sagemaker.predictor import Predictor
2323
from sagemaker.serializers import JSONSerializer
24-
from sagemaker.workflow.entities import PipelineVariable
24+
from sagemaker.workflow import is_pipeline_variable
2525

2626

2727
class TensorFlowPredictor(Predictor):
@@ -333,7 +333,7 @@ def prepare_container_def(self, instance_type=None, accelerator_type=None):
333333

334334
# If self.model_data is pipeline variable, model is not yet there.
335335
# So defer repacking to later during pipeline execution
336-
if self.entry_point and not isinstance(self.model_data, PipelineVariable):
336+
if self.entry_point and not is_pipeline_variable(self.model_data):
337337
key_prefix = sagemaker.fw_utils.model_code_key_prefix(
338338
self.key_prefix, self.name, image_uri
339339
)

src/sagemaker/tuner.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
IntegerParameter,
3939
ParameterRange,
4040
)
41-
from sagemaker.workflow.entities import PipelineVariable
4241

4342
from sagemaker.session import Session
4443
from sagemaker.utils import base_from_name, base_name_from_image, name_from_base
44+
from sagemaker.workflow import is_pipeline_variable
4545

4646
AMAZON_ESTIMATOR_MODULE = "sagemaker"
4747
AMAZON_ESTIMATOR_CLS_NAMES = {
@@ -362,7 +362,7 @@ def _prepare_static_hyperparameters(
362362
"""Prepare static hyperparameters for one estimator before tuning."""
363363
# Remove any hyperparameter that will be tuned
364364
static_hyperparameters = {
365-
str(k): str(v) if not isinstance(v, PipelineVariable) else v.to_string()
365+
str(k): str(v) if not is_pipeline_variable(v) else v.to_string()
366366
for (k, v) in estimator.hyperparameters().items()
367367
}
368368
for hyperparameter_name in hyperparameter_ranges.keys():

src/sagemaker/workflow/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@
2020
from sagemaker.workflow.properties import Properties
2121

2222
PipelineNonPrimitiveInputTypes = Union[ExecutionVariable, Expression, Parameter, Properties]
23+
24+
25+
def is_pipeline_variable(var: object) -> bool:
26+
"""Check if the variable is a pipeline variable
27+
28+
Args:
29+
var (object): The variable to be verified.
30+
Returns:
31+
bool: True if it is, False otherwise.
32+
"""
33+
34+
# Currently Expression is on top of all kinds of pipeline variables
35+
# as well as PipelineExperimentConfigProperty and PropertyFile
36+
# TODO: We should deprecate the Expression and replace it with PipelineVariable
37+
return isinstance(var, Expression)

src/sagemaker/workflow/clarify_check_step.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
from sagemaker.model_monitor.model_monitoring import _MODEL_MONITOR_S3_PATH
3838
from sagemaker.processing import ProcessingInput, ProcessingOutput, ProcessingJob
3939
from sagemaker.utils import name_from_base
40-
from sagemaker.workflow import PipelineNonPrimitiveInputTypes
41-
from sagemaker.workflow.entities import RequestType, PipelineVariable
40+
from sagemaker.workflow import PipelineNonPrimitiveInputTypes, is_pipeline_variable
41+
from sagemaker.workflow.entities import RequestType
4242
from sagemaker.workflow.properties import Properties
4343
from sagemaker.workflow.steps import Step, StepTypeEnum, CacheConfig
4444
from sagemaker.workflow.check_job_config import CheckJobConfig
@@ -193,16 +193,15 @@ def __init__(
193193
+ "DataBiasCheckConfig, ModelBiasCheckConfig or ModelExplainabilityCheckConfig"
194194
)
195195

196-
if isinstance(
197-
clarify_check_config.data_config.s3_analysis_config_output_path, PipelineVariable
198-
):
196+
if is_pipeline_variable(clarify_check_config.data_config.s3_analysis_config_output_path):
199197
raise RuntimeError(
200198
"s3_analysis_config_output_path cannot be of type "
201199
+ "ExecutionVariable/Expression/Parameter/Properties"
202200
)
203201

204-
if not clarify_check_config.data_config.s3_analysis_config_output_path and isinstance(
205-
clarify_check_config.data_config.s3_output_path, PipelineVariable
202+
if (
203+
not clarify_check_config.data_config.s3_analysis_config_output_path
204+
and is_pipeline_variable(clarify_check_config.data_config.s3_output_path)
206205
):
207206
raise RuntimeError(
208207
"`s3_output_path` cannot be of type ExecutionVariable/Expression/Parameter"

src/sagemaker/workflow/condition_step.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
from sagemaker.workflow.step_collections import StepCollection
2727
from sagemaker.workflow.utilities import list_to_request
2828
from sagemaker.workflow.entities import (
29-
Expression,
3029
RequestType,
30+
PipelineVariable,
3131
)
3232
from sagemaker.workflow.properties import (
3333
Properties,
@@ -95,7 +95,7 @@ def properties(self):
9595

9696

9797
@attr.s
98-
class JsonGet(Expression): # pragma: no cover
98+
class JsonGet(PipelineVariable): # pragma: no cover
9999
"""Get JSON properties from PropertyFiles.
100100
101101
Attributes:

src/sagemaker/workflow/conditions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
import attr
2424

25+
from sagemaker.workflow import is_pipeline_variable
2526
from sagemaker.workflow.entities import (
2627
DefaultEnumMeta,
2728
Entity,
2829
Expression,
2930
PrimitiveType,
3031
RequestType,
31-
PipelineVariable,
3232
)
3333
from sagemaker.workflow.execution_variables import ExecutionVariable
3434
from sagemaker.workflow.parameters import Parameter
@@ -262,6 +262,6 @@ def primitive_or_expr(
262262
Returns:
263263
Either the expression of the value or the primitive value.
264264
"""
265-
if isinstance(value, PipelineVariable):
265+
if is_pipeline_variable(value):
266266
return value.expr
267267
return value

src/sagemaker/workflow/pipeline.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,20 @@ def interpolate(
339339
Args:
340340
request_obj (RequestType): The request dict.
341341
callback_output_to_step_map (Dict[str, str]): A dict of output name -> step name.
342+
lambda_output_to_step_map (Dict[str, str]): A dict of output name -> step name.
342343
343344
Returns:
344345
RequestType: The request dict with Parameter values replaced by their expression.
345346
"""
346-
request_obj_copy = deepcopy(request_obj)
347-
return _interpolate(
348-
request_obj_copy,
349-
callback_output_to_step_map=callback_output_to_step_map,
350-
lambda_output_to_step_map=lambda_output_to_step_map,
351-
)
347+
try:
348+
request_obj_copy = deepcopy(request_obj)
349+
return _interpolate(
350+
request_obj_copy,
351+
callback_output_to_step_map=callback_output_to_step_map,
352+
lambda_output_to_step_map=lambda_output_to_step_map,
353+
)
354+
except TypeError as type_err:
355+
raise TypeError("Not able to interpolate Pipeline definition: %s" % type_err)
352356

353357

354358
def _interpolate(

src/sagemaker/workflow/quality_check_step.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from sagemaker import s3
2323
from sagemaker.model_monitor import ModelMonitor
2424
from sagemaker.processing import ProcessingOutput, ProcessingJob, Processor, ProcessingInput
25-
from sagemaker.workflow import PipelineNonPrimitiveInputTypes
25+
from sagemaker.workflow import PipelineNonPrimitiveInputTypes, is_pipeline_variable
2626

27-
from sagemaker.workflow.entities import RequestType, PipelineVariable
27+
from sagemaker.workflow.entities import RequestType
2828
from sagemaker.workflow.properties import (
2929
Properties,
3030
)
@@ -279,7 +279,7 @@ def _generate_baseline_job_inputs(self):
279279
_CONTAINER_BASE_PATH, _CONTAINER_INPUT_PATH, _BASELINE_DATASET_INPUT_NAME
280280
)
281281
)
282-
if isinstance(baseline_dataset, PipelineVariable):
282+
if is_pipeline_variable(baseline_dataset):
283283
baseline_dataset_input = ProcessingInput(
284284
source=self.quality_check_config.baseline_dataset,
285285
destination=baseline_dataset_des,

tests/integ/sagemaker/workflow/test_workflow_with_clarify.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
from sagemaker.processing import ProcessingInput, ProcessingOutput
3434
from sagemaker.session import get_execution_role
3535
from sagemaker.workflow.conditions import ConditionLessThanOrEqualTo
36-
from sagemaker.workflow.condition_step import ConditionStep
37-
from sagemaker.workflow.functions import JsonGet
36+
from sagemaker.workflow.condition_step import ConditionStep, JsonGet
37+
3838
from sagemaker.workflow.parameters import (
3939
ParameterInteger,
4040
ParameterString,
@@ -237,8 +237,9 @@ def test_workflow_with_clarify(
237237
property_files=[property_file],
238238
)
239239

240+
# Keep the deprecated JsonGet in test to verify it's compatible with new changes
240241
cond_left = JsonGet(
241-
step_name=step_process.name,
242+
step=step_process,
242243
property_file="BiasOutput",
243244
json_path="post_training_bias_metrics.facets.F1[0].metrics[0].value",
244245
)

0 commit comments

Comments
 (0)