-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add file arg to tqdm utils / callbacks
Summary: # Context tqdm by default prints to stderr. This can be unintuitive location for the progress bar to appear # This Diff Adds `file` arg so users can pass `sys.stdout` in they want to print there instead Reviewed By: gunchu Differential Revision: D53718992
- Loading branch information
1 parent
bbb696a
commit 3e0732e
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import sys | ||
import unittest | ||
from io import StringIO | ||
from unittest.mock import MagicMock, patch | ||
|
||
from torchtnt.utils.tqdm import create_progress_bar | ||
|
||
|
||
class TQDMTest(unittest.TestCase): | ||
@patch("sys.stdout", new_callable=StringIO) | ||
@patch("sys.stderr", new_callable=StringIO) | ||
def test_tqdm_file(self, mock_stderr: MagicMock, mock_stdout: MagicMock) -> None: | ||
""" | ||
Test the file argument to create_progress_bar | ||
""" | ||
|
||
create_progress_bar( | ||
dataloader=["foo", "bar"], | ||
desc="foo", | ||
num_epochs_completed=0, | ||
num_steps_completed=0, | ||
max_steps=None, | ||
max_steps_per_epoch=None, | ||
file=None, | ||
) | ||
self.assertIn( | ||
"foo 0: 0%| | 0/2 [00:00<?, ?it/s]", mock_stderr.getvalue() | ||
) | ||
# ensure nothing written to stdout | ||
self.assertEqual(mock_stdout.getvalue(), "") | ||
|
||
create_progress_bar( | ||
dataloader=["foo", "bar"], | ||
desc="foo", | ||
num_epochs_completed=0, | ||
num_steps_completed=0, | ||
max_steps=None, | ||
max_steps_per_epoch=None, | ||
file=sys.stdout, | ||
) | ||
self.assertIn( | ||
"foo 0: 0%| | 0/2 [00:00<?, ?it/s]", mock_stdout.getvalue() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters