Skip to content

Commit f6b7579

Browse files
authored
[TVMC] Fix logging in TVMC (#14175)
Three logger related changes in this patch: * Currently we don't set the output stream on the Python logger, so it defaults to sys.stderr, which means we only get some logger output when the command fails. So set the output stream to sys.stdout * Currently we can add -v flag to anywhere in the command line for tvmc compile, but only between tvmc and run/tune for run and tune. Unify the behaviour such that we can add the flag anywhere on the command line. * Set the effective upper bound of -vs to 3 as 4 could result in NOTSET which would not output anything.
1 parent 5d05092 commit f6b7579

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

python/tvm/driver/tvmc/autotuner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def add_tune_parser(subparsers, _, json_params):
8787
required=True,
8888
help="output file to store the tuning records for the tuning process",
8989
)
90+
parser.add_argument("-v", "--verbose", action="count", default=0, help="increase verbosity.")
9091
parser.add_argument(
9192
"--parallel",
9293
default=4,

python/tvm/driver/tvmc/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ def _main(argv):
8585
parser.add_argument("-h", "--help", action="help", help="show this help message and exit.")
8686

8787
args = parser.parse_args(argv)
88-
if args.verbose > 4:
89-
args.verbose = 4
88+
if args.verbose > 3:
89+
args.verbose = 3
9090

91+
# See the meaning of the logging levels at
92+
# https://docs.python.org/3/library/logging.html#logging-levels
93+
logging.basicConfig(stream=sys.stdout)
9194
logging.getLogger("TVMC").setLevel(40 - args.verbose * 10)
9295

9396
if args.version:

python/tvm/driver/tvmc/runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def add_run_parser(subparsers, main_parser, json_params):
109109
"Profiling may also have an impact on inference time, "
110110
"making it take longer to be generated. (non-micro devices only)",
111111
)
112+
parser.add_argument("-v", "--verbose", action="count", default=0, help="increase verbosity.")
112113
parser.add_argument(
113114
"--end-to-end",
114115
action="store_true",

tests/python/driver/tvmc/test_command_line.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import platform
1919
import pytest
2020
import shutil
21+
import logging
22+
import sys
2123

2224
from pytest_lazyfixture import lazy_fixture
2325
from unittest import mock
@@ -26,6 +28,7 @@
2628
from tvm.driver.tvmc.main import _main
2729
from tvm.driver.tvmc.model import TVMCException
2830
from tvm.driver.tvmc import compiler
31+
from unittest.mock import MagicMock
2932

3033

3134
@pytest.mark.skipif(
@@ -210,3 +213,62 @@ def test_tvmc_compile_input_model(mock_compile_model, tmpdir_factory, model):
210213
_main(run_arg)
211214

212215
mock_compile_model.assert_called_once()
216+
217+
218+
def test_tvmc_logger(caplog, tmpdir_factory, keras_simple):
219+
pytest.importorskip("tensorflow")
220+
tmpdir = tmpdir_factory.mktemp("out")
221+
222+
# TUNE
223+
log_path = os.path.join(tmpdir, "records.json")
224+
tune_cmd = f"tvmc tune --target llvm -vvvv --output {log_path} " f"--trials 2 {keras_simple}"
225+
226+
tuning_args = tune_cmd.split(" ")[1:]
227+
_main(tuning_args)
228+
229+
# Check that we log during tvmc tune
230+
for log_str in ("DEBUG", "INFO", "WARNING", "TVMC"):
231+
assert log_str in caplog.text
232+
233+
caplog.clear()
234+
235+
# COMPILE
236+
module_file = os.path.join(tmpdir, "m.tar")
237+
compile_cmd = f"tvmc compile --target 'llvm' {keras_simple} -vvvv --output {module_file}"
238+
239+
compile_args = compile_cmd.split(" ")[1:]
240+
_main(compile_args)
241+
242+
# Check that we log during tvmc compile
243+
for log_str in ("DEBUG", "WARNING", "TVMC"):
244+
assert log_str in caplog.text
245+
246+
caplog.clear()
247+
248+
# RUN
249+
run_cmd = f"tvmc run -vvvv {module_file}"
250+
251+
run_args = run_cmd.split(" ")[1:]
252+
_main(run_args)
253+
254+
# Check that we log during tvmc run
255+
for log_str in ("DEBUG", "TVMC"):
256+
assert log_str in caplog.text
257+
258+
259+
# Unfortunately pytest seems to intercept the logging output, so we can't test whether it
260+
# actually writes the logging output to sys.stdout, but we can test that we call
261+
# logging.basicConfig with the correct arguments
262+
def test_tvmc_logger_set_basicConfig(monkeypatch, tmpdir_factory, keras_simple):
263+
pytest.importorskip("tensorflow")
264+
mock_basicConfig = MagicMock()
265+
monkeypatch.setattr(logging, "basicConfig", mock_basicConfig)
266+
267+
# Run a random tvmc command
268+
tmpdir = tmpdir_factory.mktemp("out")
269+
module_file = os.path.join(tmpdir, "m.tar")
270+
compile_cmd = f"tvmc compile --target 'llvm' {keras_simple} -vvvv --output {module_file}"
271+
compile_args = compile_cmd.split(" ")[1:]
272+
_main(compile_args)
273+
274+
mock_basicConfig.assert_called_with(stream=sys.stdout)

0 commit comments

Comments
 (0)