Skip to content

Commit febee60

Browse files
wasim-buildsmohamedzeidan2021Mohamed Zeidan
authored
fix/issue 5897 crlf sm train (#6088)
* fix: enforce LF newlines for sm_train.sh on Windows (Fixes #5897) * test(train): cover LF line endings for sm_train.sh Adds the unit test from #5907 plus an OS-independent regression guard. The byte-level check from #5907 (no CRLF in the written sm_train.sh) only fails on Windows, since POSIX text-mode open() never translates \n. CI runs on Linux, so that test passes with or without the fix. The second test inspects the open() call for sm_train.sh and asserts newline="\n", mirroring the approach merged in #6313 for the mlops repack launcher, so the guard holds on any host OS. --------- Co-authored-by: Mohamed Zeidan <81834882+mohamedzeidan2021@users.noreply.github.com> Co-authored-by: Mohamed Zeidan <zeidmo@amazon.com>
1 parent 2e2ffc0 commit febee60

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎sagemaker-train/src/sagemaker/train/model_trainer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ def _prepare_train_script(
13911391
execute_driver=execute_driver,
13921392
)
13931393

1394-
with open(os.path.join(tmp_dir.name, TRAIN_SCRIPT), "w") as f:
1394+
with open(os.path.join(tmp_dir.name, TRAIN_SCRIPT), "w", newline="\n") as f:
13951395
f.write(train_script)
13961396

13971397
@classmethod

‎sagemaker-train/tests/unit/train/test_model_trainer.py‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,93 @@ def mock_upload_data(path, bucket, key_prefix, extra_args=None):
16171617
assert kwargs["tensor_board_output_config"].local_path == "/opt/ml/output/tensorboard"
16181618

16191619

1620+
@patch("sagemaker.train.model_trainer.TrainingJob")
1621+
@patch("sagemaker.train.model_trainer.TemporaryDirectory")
1622+
def test_prepare_train_script_uses_lf_line_endings(
1623+
mock_tmp_dir, mock_training_job, modules_session
1624+
):
1625+
"""Test that _prepare_train_script generates sm_train.sh with LF line endings only."""
1626+
modules_session.upload_data.return_value = (
1627+
f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/test"
1628+
)
1629+
1630+
tmp_dir = tempfile.TemporaryDirectory()
1631+
tmp_dir._cleanup = False
1632+
tmp_dir.cleanup = lambda: None
1633+
mock_tmp_dir.return_value = tmp_dir
1634+
1635+
try:
1636+
model_trainer = ModelTrainer(
1637+
sagemaker_session=modules_session,
1638+
training_image=DEFAULT_IMAGE,
1639+
source_code=DEFAULT_SOURCE_CODE,
1640+
role=DEFAULT_ROLE,
1641+
)
1642+
1643+
model_trainer.train()
1644+
1645+
train_script_path = os.path.join(tmp_dir.name, TRAIN_SCRIPT)
1646+
assert os.path.exists(train_script_path)
1647+
1648+
with open(train_script_path, "rb") as f:
1649+
content = f.read()
1650+
1651+
# Verify no CRLF line endings exist
1652+
assert b"\r\n" not in content, "sm_train.sh contains CRLF line endings; expected LF only"
1653+
# Verify LF line endings are present
1654+
assert b"\n" in content, "sm_train.sh does not contain any LF line endings"
1655+
finally:
1656+
shutil.rmtree(tmp_dir.name, ignore_errors=True)
1657+
1658+
1659+
@patch("sagemaker.train.model_trainer.TrainingJob")
1660+
@patch("sagemaker.train.model_trainer.TemporaryDirectory")
1661+
def test_prepare_train_script_opened_with_lf_newline(
1662+
mock_tmp_dir, mock_training_job, modules_session
1663+
):
1664+
"""Regression for #5897: sm_train.sh must be opened with ``newline="\\n"``.
1665+
1666+
On Windows, text-mode ``open(..., "w")`` translates ``\\n`` to ``\\r\\n``, which breaks
1667+
the bash script inside the Linux training container. Byte-level assertions alone pass
1668+
on POSIX hosts even without the fix, so this test inspects the ``open`` call itself and
1669+
therefore holds independently of the test host OS.
1670+
"""
1671+
modules_session.upload_data.return_value = (
1672+
f"s3://{DEFAULT_BUCKET}/{DEFAULT_BASE_NAME}-job/input/test"
1673+
)
1674+
1675+
tmp_dir = tempfile.TemporaryDirectory()
1676+
tmp_dir._cleanup = False
1677+
tmp_dir.cleanup = lambda: None
1678+
mock_tmp_dir.return_value = tmp_dir
1679+
1680+
train_script_newlines = []
1681+
real_open = open
1682+
1683+
def tracking_open(file, mode="r", *args, **kwargs):
1684+
if str(file).endswith(TRAIN_SCRIPT) and "w" in mode:
1685+
train_script_newlines.append(kwargs.get("newline"))
1686+
return real_open(file, mode, *args, **kwargs)
1687+
1688+
try:
1689+
model_trainer = ModelTrainer(
1690+
sagemaker_session=modules_session,
1691+
training_image=DEFAULT_IMAGE,
1692+
source_code=DEFAULT_SOURCE_CODE,
1693+
role=DEFAULT_ROLE,
1694+
)
1695+
1696+
with patch("builtins.open", side_effect=tracking_open):
1697+
model_trainer.train()
1698+
1699+
assert train_script_newlines == ["\n"], (
1700+
"sm_train.sh must be opened with newline='\\n' to keep LF endings, "
1701+
f"got {train_script_newlines}"
1702+
)
1703+
finally:
1704+
shutil.rmtree(tmp_dir.name, ignore_errors=True)
1705+
1706+
16201707
@patch("sagemaker.train.model_trainer.TrainingJob")
16211708
def test_input_merge(mock_training_job, modules_session):
16221709
model_input = InputData(channel_name="model", data_source="s3://bucket/model/model.tar.gz")

0 commit comments

Comments
 (0)