forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RankFilter to skip logging when the rank is not meeting criteria (P…
…roject-MONAI#6243) Partially fixes Project-MONAI#6189 Fixes Project-MONAI#6230 ### Description The RankFilter class is a convenient filter that extends the Filter class in the Python logging module. The purpose is to control which log records are processed based on the rank in a distributed environment. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Mingxin Zheng <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
87 additions
and
2 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
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
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,53 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import torch.distributed as dist | ||
|
||
from monai.utils import RankFilter | ||
from tests.utils import DistCall, DistTestCase | ||
|
||
|
||
class DistributedRankFilterTest(DistTestCase): | ||
def setUp(self): | ||
self.log_dir = tempfile.TemporaryDirectory() | ||
|
||
@DistCall(nnodes=1, nproc_per_node=2) | ||
def test_rankfilter(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") | ||
|
||
dist.barrier() | ||
if dist.get_rank() == 0: | ||
with open(log_filename) as file: | ||
lines = [line.rstrip() for line in file] | ||
log_message = " ".join(lines) | ||
assert log_message.count("test_warnings") == 1 | ||
|
||
def tearDown(self) -> None: | ||
self.log_dir.cleanup() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |