Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6894 update rank filter #6895

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions monai/utils/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ def __init__(self, rank: int | None = None, filter_fn: Callable = lambda rank: r
if dist.is_available() and dist.is_initialized():
self.rank: int = rank if rank is not None else dist.get_rank()
else:
warnings.warn(
"The torch.distributed is either unavailable and uninitiated when RankFilter is instantiated. "
"If torch.distributed is used, please ensure that the RankFilter() is called "
"after torch.distributed.init_process_group() in the script."
)
if torch.cuda.is_available() and torch.cuda.device_count() > 1:
warnings.warn(
"The torch.distributed is either unavailable and uninitiated when RankFilter is instantiated.\n"
"If torch.distributed is used, please ensure that the RankFilter() is called\n"
"after torch.distributed.init_process_group() in the script.\n"
)
self.rank = 0

def filter(self, *_args):
return self.filter_fn(self.rank)
16 changes: 15 additions & 1 deletion tests/test_rankfilter_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ def test_rankfilter(self):
with open(log_filename) as file:
lines = [line.rstrip() for line in file]
log_message = " ".join(lines)
assert log_message.count("test_warnings") == 1
self.assertEqual(log_message.count("test_warnings"), 1)

def test_rankfilter_single_proc(self):
logger = logging.getLogger(__name__)
log_filename = os.path.join(self.log_dir.name, "records.log")
h1 = logging.FileHandler(filename=log_filename)
h1.setLevel(logging.WARNING)
logger.addHandler(h1)
logger.addFilter(RankFilter())
logger.warning("test_warnings")

with open(log_filename) as file:
lines = [line.rstrip() for line in file]
log_message = " ".join(lines)
self.assertEqual(log_message.count("test_warnings"), 1)

def tearDown(self) -> None:
self.log_dir.cleanup()
Expand Down