Skip to content

feat(visualizer): add master_only switch to record images on every rank - #1705

Open
dsfwmmy wants to merge 1 commit into
open-mmlab:mainfrom
dsfwmmy:fix/add-image-all-ranks
Open

dsfwmmy wants to merge 1 commit into
open-mmlab:mainfrom
dsfwmmy:fix/add-image-all-ranks

Conversation

@dsfwmmy

@dsfwmmy dsfwmmy commented Nov 20, 2026

Copy link
Copy Markdown

Motivation

Fixes #1081.

Visualizer.add_image was decorated with @master_only, so during distributed
training only the master process wrote images to the vis backends and the
predictions of the other ranks were lost. Simply deleting the decorator is not
enough, because every rank would then write to the same file: LocalVisBackend
derives the file name from name and step only, so the ranks would overwrite
each other.

This PR makes recording images from every rank an explicit, opt-in behaviour and
makes LocalVisBackend able to store the images of different ranks side by side.

Modification

Usage:

visualizer = dict(
    type='Visualizer',
    vis_backends=[dict(type='LocalVisBackend')],
    save_dir='vis',
    master_only=False)  # every rank records its own image

BC-breaking

No. master_only defaults to True and rank-0 file names are unchanged.

Limitation

Only LocalVisBackend gets rank-aware file names in this PR. Remote backends
(TensorBoard, WandB, MLflow, ...) still share one experiment between ranks, so
master_only=False is only recommended together with LocalVisBackend for now.
I can extend this in a follow-up if that is preferable.

Tests

Added unit tests:

  • tests/test_visualizer/test_visualizer.py::TestVisualizer::test_master_only
    asserts that add_image is skipped on a non-main process by default and
    reaches every backend once master_only=False.
  • tests/test_visualizer/test_vis_backend.py::TestLocalVisBackend::test_add_image_non_main_process
    asserts the _rank suffix of the saved file.

Results on Windows 11 / Python 3.13 / torch 2.11:

pytest tests/test_visualizer -q -k "not MLflow and not DVCLive"
52 passed, 14 skipped        # before/after this PR: same 13 MLflow/DVCLive
                             # failures, they need optional deps that are not
                             # installed in this environment
pytest ...::test_master_only ...::test_add_image_non_main_process
2 passed

flake8 and isort --check-only are clean on the touched files. yapf could
not be verified locally because the pinned v0.32.0 does not run on Python 3.13
(lib2to3 was removed); the added code follows the formatting of the
surrounding code.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests.
  3. If there is potential influence on downstream projects: the default is
    unchanged, so MMDetection/MMClassification are not affected unless they opt in.
  4. The documentation has been modified accordingly (docstring of Visualizer).

Refs: open-mmlab#1081

`Visualizer.add_image` was decorated with `@master_only`, so during
distributed training only the master process wrote images to the vis
backends. Removing the decorator alone is not enough, because every rank
would then write to the same file name: `LocalVisBackend` derives it from
`name` and `step` only.

This commit:

- adds a `master_only` argument to `Visualizer` (defaults to True, so the
  behaviour of previous versions is preserved) and gates `add_image` on it
  instead of using the decorator, following the suggestion in open-mmlab#1081
- appends the rank to the file name in `LocalVisBackend.add_image` for
  non-main processes, so the images of different ranks no longer overwrite
  each other
- adds unit tests covering both the switch and the rank suffix
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Visualizer do not need master_only when saving image

2 participants