-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: [Bug] Pipeline parameters (ParameterInteger, ParameterString) fail in ModelTrain (5504) #5724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
aviruthen
wants to merge
4
commits into
aws:master
from
aviruthen:fix/bug-pipeline-parameters-parameterinteger-5504-3
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c517f24
fix: [Bug] Pipeline parameters (ParameterInteger, ParameterString) fa…
aviruthen 109214a
fix: address review comments (iteration #1)
aviruthen 0d1195d
fix: address review comments (iteration #2)
aviruthen e3b2d82
fix: address review comments (iteration #3)
aviruthen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
sagemaker-core/tests/unit/core/modules/test_utils_safe_serialize.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # 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 safe_serialize in sagemaker.core.modules.utils with PipelineVariable support. | ||
|
|
||
| Verifies that safe_serialize correctly handles PipelineVariable objects | ||
| (e.g., ParameterInteger, ParameterString) by returning them as-is rather | ||
| than attempting str() conversion which would raise TypeError. | ||
|
|
||
| See: https://github.com/aws/sagemaker-python-sdk/issues/5504 | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
|
aviruthen marked this conversation as resolved.
|
||
| from sagemaker.core.modules.utils import safe_serialize | ||
| from sagemaker.core.helper.pipeline_variable import PipelineVariable | ||
| from sagemaker.core.workflow.parameters import ParameterInteger, ParameterString | ||
|
aviruthen marked this conversation as resolved.
aviruthen marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class TestSafeSerializeWithPipelineVariables: | ||
| """Test safe_serialize handles PipelineVariable objects correctly.""" | ||
|
|
||
| @pytest.mark.parametrize("param", [ | ||
| ParameterInteger(name="MaxDepth", default_value=5), | ||
| ParameterString(name="Algorithm", default_value="xgboost"), | ||
| ]) | ||
| def test_safe_serialize_returns_pipeline_variable_as_is(self, param): | ||
| """PipelineVariable objects should be returned as-is (identity preserved).""" | ||
| result = safe_serialize(param) | ||
| assert result is param | ||
| assert isinstance(result, PipelineVariable) | ||
|
|
||
| def test_pipeline_variable_str_raises_type_error(self): | ||
| """Confirm PipelineVariable.__str__ raises TypeError (the root cause of the bug).""" | ||
| param = ParameterInteger(name="TestParam", default_value=10) | ||
| with pytest.raises(TypeError): | ||
| str(param) | ||
|
|
||
|
|
||
| class TestSafeSerializeBasicTypes: | ||
| """Regression tests: verify basic types still work after PipelineVariable support.""" | ||
|
|
||
| def test_safe_serialize_with_string(self): | ||
| """Strings should be returned as-is without JSON wrapping.""" | ||
| assert safe_serialize("hello") == "hello" | ||
|
|
||
| def test_safe_serialize_with_int(self): | ||
| """Integers should be JSON-serialized to string.""" | ||
| assert safe_serialize(42) == "42" | ||
|
|
||
| def test_safe_serialize_with_dict(self): | ||
| """Dicts should be JSON-serialized.""" | ||
| result = safe_serialize({"key": "val"}) | ||
| assert result == '{"key": "val"}' | ||
|
|
||
| def test_safe_serialize_with_bool(self): | ||
| """Booleans should be JSON-serialized.""" | ||
| assert safe_serialize(True) == "true" | ||
| assert safe_serialize(False) == "false" | ||
|
|
||
| def test_safe_serialize_with_none(self): | ||
| """None should be JSON-serialized to 'null'.""" | ||
| assert safe_serialize(None) == "null" | ||
|
|
||
| def test_safe_serialize_with_custom_object(self): | ||
| """Custom objects should fall back to str().""" | ||
|
|
||
| class CustomObj: | ||
| def __str__(self): | ||
| return "custom" | ||
|
|
||
| assert safe_serialize(CustomObj()) == "custom" | ||
|
aviruthen marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # 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 safe_serialize with PipelineVariable support. | ||
|
|
||
| Verifies that safe_serialize in sagemaker.train.utils correctly handles | ||
| PipelineVariable objects (e.g., ParameterInteger, ParameterString) by | ||
| returning them as-is rather than attempting str() conversion which would | ||
| raise TypeError. | ||
|
|
||
| See: https://github.com/aws/sagemaker-python-sdk/issues/5504 | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
|
aviruthen marked this conversation as resolved.
|
||
| from sagemaker.train.utils import safe_serialize | ||
| from sagemaker.core.helper.pipeline_variable import PipelineVariable | ||
| from sagemaker.core.workflow.parameters import ParameterInteger, ParameterString | ||
|
|
||
|
|
||
| class TestSafeSerializeWithPipelineVariables: | ||
| """Test safe_serialize handles PipelineVariable objects correctly.""" | ||
|
|
||
| @pytest.mark.parametrize("param", [ | ||
|
aviruthen marked this conversation as resolved.
|
||
| ParameterInteger(name="MaxDepth", default_value=5), | ||
| ParameterString(name="Algorithm", default_value="xgboost"), | ||
| ]) | ||
| def test_safe_serialize_returns_pipeline_variable_as_is(self, param): | ||
| """PipelineVariable objects should be returned as-is (identity preserved).""" | ||
| result = safe_serialize(param) | ||
| assert result is param | ||
| assert isinstance(result, PipelineVariable) | ||
|
|
||
| def test_pipeline_variable_str_raises_type_error(self): | ||
| """Confirm PipelineVariable.__str__ raises TypeError (the root cause of the bug).""" | ||
| param = ParameterInteger(name="TestParam", default_value=10) | ||
| with pytest.raises(TypeError): | ||
| str(param) | ||
|
|
||
|
|
||
| class TestSafeSerializeBasicTypes: | ||
| """Regression tests: verify basic types still work after PipelineVariable support.""" | ||
|
|
||
| def test_safe_serialize_with_string(self): | ||
| """Strings should be returned as-is without JSON wrapping.""" | ||
| assert safe_serialize("hello") == "hello" | ||
| assert safe_serialize("12345") == "12345" | ||
|
|
||
| def test_safe_serialize_with_int(self): | ||
| """Integers should be JSON-serialized to string.""" | ||
| assert safe_serialize(42) == "42" | ||
|
|
||
| def test_safe_serialize_with_float(self): | ||
| """Floats should be JSON-serialized to string.""" | ||
| assert safe_serialize(3.14) == "3.14" | ||
|
|
||
| def test_safe_serialize_with_dict(self): | ||
| """Dicts should be JSON-serialized.""" | ||
| result = safe_serialize({"key": "val"}) | ||
| assert result == '{"key": "val"}' | ||
|
|
||
| def test_safe_serialize_with_bool(self): | ||
| """Booleans should be JSON-serialized.""" | ||
| assert safe_serialize(True) == "true" | ||
| assert safe_serialize(False) == "false" | ||
|
|
||
| def test_safe_serialize_with_none(self): | ||
| """None should be JSON-serialized to 'null'.""" | ||
| assert safe_serialize(None) == "null" | ||
|
|
||
| def test_safe_serialize_with_list(self): | ||
| """Lists should be JSON-serialized.""" | ||
| assert safe_serialize([1, 2, 3]) == "[1, 2, 3]" | ||
|
|
||
| def test_safe_serialize_with_custom_object(self): | ||
| """Custom objects should fall back to str().""" | ||
|
|
||
| class CustomObj: | ||
| def __str__(self): | ||
| return "custom" | ||
|
|
||
| assert safe_serialize(CustomObj()) == "custom" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.