From bf1ab6197d87ad603019e691f3aaafba2c18c139 Mon Sep 17 00:00:00 2001 From: Roxen-AS Date: Tue, 24 Nov 2026 21:09:24 +0530 Subject: [PATCH] fix(core): allow pipeline variable for TransformResources instance_count Add TransformResources to PIPE_VAR_OVERRIDES for InstanceCount so that TransformResources.instance_count accepts IntPipeVar (e.g. ParameterInteger), allowing parameterized instance counts when constructing transform steps in pipelines. Fixes #6577. Signed-off-by: Roxen-AS --- .../src/sagemaker/core/shapes/shapes.py | 2 +- .../src/sagemaker/core/tools/constants.py | 3 ++ sagemaker-core/tests/unit/test_transformer.py | 50 +++++++++++++++++++ .../tests/unit/tools/test_shapes_extractor.py | 10 ++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/sagemaker-core/src/sagemaker/core/shapes/shapes.py b/sagemaker-core/src/sagemaker/core/shapes/shapes.py index 62c239862d..3a91f003e5 100644 --- a/sagemaker-core/src/sagemaker/core/shapes/shapes.py +++ b/sagemaker-core/src/sagemaker/core/shapes/shapes.py @@ -1970,7 +1970,7 @@ class TransformResources(Base): """ instance_type: StrPipeVar - instance_count: int + instance_count: IntPipeVar volume_kms_key_id: Optional[StrPipeVar] = Unassigned() transform_ami_version: Optional[StrPipeVar] = Unassigned() diff --git a/sagemaker-core/src/sagemaker/core/tools/constants.py b/sagemaker-core/src/sagemaker/core/tools/constants.py index 16f7a8e453..1edb5bd783 100644 --- a/sagemaker-core/src/sagemaker/core/tools/constants.py +++ b/sagemaker-core/src/sagemaker/core/tools/constants.py @@ -159,6 +159,9 @@ "ProcessingInstancePreference": { "InstanceCount": "IntPipeVar", }, + "TransformResources": { + "InstanceCount": "IntPipeVar", + }, # RandomSeed accepts a pipeline variable (e.g. a ParameterInteger) so tuning pipelines # can parameterize reproducibility (issue #5614 / #6171). "HyperParameterTuningJobConfig": { diff --git a/sagemaker-core/tests/unit/test_transformer.py b/sagemaker-core/tests/unit/test_transformer.py index e8c954ec71..bed1e70a1d 100644 --- a/sagemaker-core/tests/unit/test_transformer.py +++ b/sagemaker-core/tests/unit/test_transformer.py @@ -641,6 +641,56 @@ def test_load_config(self, mock_session): assert config["output_config"]["s3_output_path"] == "s3://bucket/output" assert config["resource_config"]["instance_count"] == 2 + def test_instance_count_accepts_pipeline_variable(self, mock_session): + """Regression for #6577. + + ``instance_count`` must accept a pipeline variable (e.g. a ParameterInteger). + Building the transform job request instantiates ``TransformResources`` with + ``instance_count``; when that field was typed ``int``, pydantic raised + a ValidationError. + """ + from sagemaker.core.workflow.parameters import ParameterInteger + from sagemaker.core.shapes import TransformResources + + count_param = ParameterInteger(name="InstanceCount", default_value=2) + resources = TransformResources( + instance_type="ml.m5.xlarge", + instance_count=count_param, + ) + assert resources.instance_count is count_param + + transformer = Transformer( + model_name="test-model", + instance_count=count_param, + instance_type="ml.m5.xlarge", + output_path="s3://bucket/output", + sagemaker_session=mock_session, + ) + assert transformer.instance_count is count_param + + config = transformer._load_config( + data="s3://bucket/input", + data_type="S3Prefix", + content_type="text/csv", + compression_type=None, + split_type="Line", + ) + request = transformer._get_transform_request( + job_name="test-job", + model_name="test-model", + strategy=None, + max_concurrent_transforms=None, + max_payload=None, + env=None, + input_config=config["input_config"], + output_config=config["output_config"], + resource_config=config["resource_config"], + experiment_config=None, + tags=None, + data_processing=None, + ) + assert request["TransformResources"].instance_count is count_param + class TestTransformerTransformWithTags: """Regression tests for tags handling in Transformer.transform() (issue #6090).""" diff --git a/sagemaker-core/tests/unit/tools/test_shapes_extractor.py b/sagemaker-core/tests/unit/tools/test_shapes_extractor.py index 35b1b28b97..f7753394b1 100644 --- a/sagemaker-core/tests/unit/tools/test_shapes_extractor.py +++ b/sagemaker-core/tests/unit/tools/test_shapes_extractor.py @@ -360,8 +360,10 @@ class TestInstancePreferencesPipeVarOverrides: _MODEL = { "TrainingInstanceCount": _COUNT, "ProcessingInstanceCount": _COUNT, + "TransformInstanceCount": _COUNT, "TrainingInstanceType": _STRING, "ProcessingInstanceType": _STRING, + "TransformInstanceType": _STRING, "ResourceConfig": { "type": "structure", "members": { @@ -392,6 +394,13 @@ class TestInstancePreferencesPipeVarOverrides: "InstanceCount": {"shape": "ProcessingInstanceCount"}, }, }, + "TransformResources": { + "type": "structure", + "members": { + "InstanceType": {"shape": "TransformInstanceType"}, + "InstanceCount": {"shape": "TransformInstanceCount"}, + }, + }, # Control: same integer shape, no override registered -> must stay int. "UnrelatedConfig": { "type": "structure", @@ -421,6 +430,7 @@ def extractor(self, tmp_path): ("ProcessingClusterConfig", "instance_count"), ("ProcessingClusterConfig", "selected_instance_count"), ("ProcessingInstancePreference", "instance_count"), + ("TransformResources", "instance_count"), ], ) def test_count_members_generate_as_int_pipe_var(self, extractor, shape, member):