Cleaner Console Outputs for Scripts with Multiple Workers#
The console_outputs=
argument can be set to False
to suppress helptext and
error message printing.
This is useful in PyTorch for distributed training scripts, where you only want to print the helptext from the main process:
# HuggingFace Accelerate.
args = tyro.cli(Args, console_outputs=accelerator.is_main_process)
# PyTorch DDP.
args = tyro.cli(Args, console_outputs=(rank == 0))
# PyTorch Lightning.
args = tyro.cli(Args, console_outputs=trainer.is_global_zero)
1import dataclasses
2
3import tyro
4
5
6@dataclasses.dataclass
7class Args:
8 """Description.
9 This should show up in the helptext!"""
10
11 field1: int
12 """A field."""
13
14 field2: int = 3
15 """A numeric field, with a default value."""
16
17
18if __name__ == "__main__":
19 args = tyro.cli(Args, console_outputs=False)
20 print(args)
python 04_additional/14_suppress_console_outputs.py --help