Skip to content

Commit 9865719

Browse files
committed
fix: reject bare integer inputs in _parse_context_length
Bare integers like "128" were silently parsed as token counts (128), causing every recipe to pass the >= filter and silently selecting the shortest recipe instead of failing. Now raises ValueError with an actionable message for any input not ending in 'K'.
1 parent f3d623d commit 9865719

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

‎sagemaker-train/src/sagemaker/train/common_utils/finetune_utils.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,22 +569,22 @@ def _resolve_model_package_arn(model_package) -> Optional[str]:
569569

570570

571571
def _parse_context_length(value) -> int:
572-
"""Parse a context length value like '8K', '32K', '128K' into an integer (e.g., 8192).
573-
574-
Returns 0 if value is None or unparseable.
575-
"""
572+
"""Parse a context length value like '8K', '32K', '128K' into an integer (e.g., 8192)."""
576573
if not value:
577574
return 0
578575
value = str(value).strip().upper()
579-
if value.endswith("K"):
580-
try:
581-
return int(value[:-1]) * 1024
582-
except ValueError:
583-
return 0
576+
if not value.endswith("K"):
577+
raise ValueError(
578+
f"Invalid sequence_length '{value}'. "
579+
f"Expected a value ending in 'K', e.g. '8K' or '128K'."
580+
)
584581
try:
585-
return int(value)
582+
return int(value[:-1]) * 1024
586583
except ValueError:
587-
return 0
584+
raise ValueError(
585+
f"Invalid sequence_length '{value}'. "
586+
f"Expected a numeric value followed by 'K', e.g. '8K' or '128K'."
587+
)
588588

589589

590590
def _get_fine_tuning_options_and_model_arn(model_name: str, customization_technique: str, training_type, sagemaker_session,

‎sagemaker-train/tests/unit/train/common_utils/test_finetune_utils.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,8 @@ def test__parse_context_length_with_lowercase(self):
10541054
assert _parse_context_length("8k") == 8192
10551055

10561056
def test__parse_context_length_with_integer(self):
1057-
assert _parse_context_length("4096") == 4096
1057+
with pytest.raises(ValueError, match="Invalid sequence_length '4096'"):
1058+
_parse_context_length("4096")
10581059

10591060
def test__parse_context_length_with_none(self):
10601061
assert _parse_context_length(None) == 0

0 commit comments

Comments
 (0)