Skip to content

Commit d49d746

Browse files
SkafteNickiBorda
authored andcommitted
Replace exit with `sys.exit (#21052)
* replace exit with sys.exit * fix in upgrade checkpoint (cherry picked from commit 64d3112)
1 parent ee7b1fb commit d49d746

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/lightning/fabric/utilities/consolidate_checkpoint.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import sys
23
from argparse import ArgumentParser, Namespace
34
from pathlib import Path
45

@@ -40,23 +41,23 @@ def _parse_cli_args() -> Namespace:
4041
def _process_cli_args(args: Namespace) -> Namespace:
4142
if not _TORCH_GREATER_EQUAL_2_3:
4243
_log.error("Processing distributed checkpoints requires PyTorch >= 2.3.")
43-
exit(1)
44+
sys.exit(1)
4445

4546
checkpoint_folder = Path(args.checkpoint_folder)
4647
if not checkpoint_folder.exists():
4748
_log.error(f"The provided checkpoint folder does not exist: {checkpoint_folder}")
48-
exit(1)
49+
sys.exit(1)
4950
if not checkpoint_folder.is_dir():
5051
_log.error(
5152
f"The provided checkpoint path must be a folder, containing the checkpoint shards: {checkpoint_folder}"
5253
)
53-
exit(1)
54+
sys.exit(1)
5455
if not (checkpoint_folder / _METADATA_FILENAME).is_file():
5556
_log.error(
5657
"Only FSDP-sharded checkpoints saved with Lightning are supported for consolidation. The provided folder"
5758
f" is not in that format: {checkpoint_folder}"
5859
)
59-
exit(1)
60+
sys.exit(1)
6061

6162
if args.output_file is None:
6263
output_file = checkpoint_folder.with_suffix(checkpoint_folder.suffix + ".consolidated")
@@ -67,7 +68,7 @@ def _process_cli_args(args: Namespace) -> Namespace:
6768
"The path for the converted checkpoint already exists. Choose a different path by providing"
6869
f" `--output_file` or move/delete the file first: {output_file}"
6970
)
70-
exit(1)
71+
sys.exit(1)
7172

7273
return Namespace(checkpoint_folder=checkpoint_folder, output_file=output_file)
7374

src/lightning/pytorch/trainer/call.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import logging
1515
import signal
16+
import sys
1617
from copy import deepcopy
1718
from typing import Any, Callable, Optional, Union
1819

@@ -62,7 +63,7 @@ def _call_and_handle_interrupt(trainer: "pl.Trainer", trainer_fn: Callable, *arg
6263
launcher = trainer.strategy.launcher
6364
if isinstance(launcher, _SubprocessScriptLauncher):
6465
launcher.kill(_get_sigkill_signal())
65-
exit(1)
66+
sys.exit(1)
6667

6768
except BaseException as exception:
6869
_interrupt(trainer, exception)

src/lightning/pytorch/utilities/upgrade_checkpoint.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import glob
1515
import logging
16+
import sys
1617
from argparse import ArgumentParser, Namespace
1718
from pathlib import Path
1819
from shutil import copyfile
@@ -35,7 +36,7 @@ def _upgrade(args: Namespace) -> None:
3536
f"The path {path} does not exist. Please provide a valid path to a checkpoint file or a directory"
3637
f" containing checkpoints ending in {extension}."
3738
)
38-
exit(1)
39+
sys.exit(1)
3940

4041
if path.is_file():
4142
files = [path]
@@ -46,7 +47,7 @@ def _upgrade(args: Namespace) -> None:
4647
f"No checkpoint files with extension {extension} were found in {path}."
4748
f" HINT: Try setting the `--extension` option to specify the right file extension to look for."
4849
)
49-
exit(1)
50+
sys.exit(1)
5051

5152
_log.info("Creating a backup of the existing checkpoint files before overwriting in the upgrade process.")
5253
for file in files:

0 commit comments

Comments
 (0)