Skip to content

Commit c0da746

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 4f70c8b commit c0da746

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
@@ -508,22 +508,22 @@ def _resolve_model_package_arn(model_package) -> Optional[str]:
508508

509509

510510
def _parse_context_length(value) -> int:
511-
"""Parse a context length value like '8K', '32K', '128K' into an integer (e.g., 8192).
512-
513-
Returns 0 if value is None or unparseable.
514-
"""
511+
"""Parse a context length value like '8K', '32K', '128K' into an integer (e.g., 8192)."""
515512
if not value:
516513
return 0
517514
value = str(value).strip().upper()
518-
if value.endswith("K"):
519-
try:
520-
return int(value[:-1]) * 1024
521-
except ValueError:
522-
return 0
515+
if not value.endswith("K"):
516+
raise ValueError(
517+
f"Invalid sequence_length '{value}'. "
518+
f"Expected a value ending in 'K', e.g. '8K' or '128K'."
519+
)
523520
try:
524-
return int(value)
521+
return int(value[:-1]) * 1024
525522
except ValueError:
526-
return 0
523+
raise ValueError(
524+
f"Invalid sequence_length '{value}'. "
525+
f"Expected a numeric value followed by 'K', e.g. '8K' or '128K'."
526+
)
527527

528528

529529
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)