Skip to content

Commit 6e40c21

Browse files
authored
[Build] Added --pdb flag to build.py, drop into pdb on error (mlc-ai#1017)
This commit adds an optional `--pdb` flag to the `build.py` script. If passed, any exception raised that would otherwise terminate the script will first enter a pdb post-mortem, allowing the error to be inspected.
1 parent ad3a6b9 commit 6e40c21

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

mlc_llm/build.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
"""Script for building/compiling models."""
2+
import contextlib
3+
import sys
4+
25
from mlc_llm import core
36

7+
8+
@contextlib.contextmanager
9+
def debug_on_except():
10+
try:
11+
yield
12+
finally:
13+
if sys.exc_info() == (None, None, None):
14+
return
15+
16+
import traceback
17+
18+
try:
19+
import ipdb as pdb
20+
except ImportError:
21+
import pdb
22+
23+
traceback.print_exc()
24+
pdb.post_mortem()
25+
26+
427
def main():
528
"""Main method for building model from command line."""
629
empty_args = core.convert_build_args_to_argparser() # Create new ArgumentParser
730
parsed_args = empty_args.parse_args() # Parse through command line
8-
# Post processing of arguments
9-
parsed_args = core._parse_args(parsed_args) # pylint: disable=protected-access
10-
core.build_model_from_args(parsed_args)
31+
32+
with contextlib.ExitStack() as stack:
33+
# Enter an exception-catching context before post-processing
34+
# the arguments, in case the post-processing itself raises an
35+
# exception.
36+
if parsed_args.pdb:
37+
stack.enter_context(debug_on_except())
38+
39+
# Post processing of arguments
40+
parsed_args = core._parse_args(parsed_args) # pylint: disable=protected-access
41+
42+
core.build_model_from_args(parsed_args)
43+
1144

1245
if __name__ == "__main__":
1346
main()

mlc_llm/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ class BuildArgs:
236236
"action": "store_true",
237237
},
238238
)
239+
pdb: bool = field(
240+
default=False,
241+
metadata={
242+
"help": ("If set, drop into a pdb debugger on error"),
243+
"action": "store_true",
244+
},
245+
)
239246

240247

241248
def convert_build_args_to_argparser() -> argparse.ArgumentParser:

0 commit comments

Comments
 (0)