Lightning-AI/pytorch-lightning · error · MisconfigurationException

You set `Trainer(sync_batchnorm=True)` and provided a `{plug

Error message

You set `Trainer(sync_batchnorm=True)` and provided a `{plugin.__class__.__name__}` plugin, but this is not allowed. Choose one or the other.

What it means

`Trainer(sync_batchnorm=True)` installs Lightning's own TorchSyncBatchNorm layer sync. If the plugins list also contains a different LayerSync implementation, the two conflict and MisconfigurationException is raised, telling you to pick one mechanism.

Source

Thrown at src/lightning/pytorch/trainer/connectors/accelerator_connector.py:240

        self._accelerator_flag = accelerator

        precision_flag = _convert_precision_to_unified_args(precision)

        if plugins:
            plugins_flags_types: dict[str, int] = Counter()
            for plugin in plugins:
                if isinstance(plugin, Precision):
                    self._precision_plugin_flag = plugin
                    plugins_flags_types[Precision.__name__] += 1
                elif isinstance(plugin, CheckpointIO):
                    self.checkpoint_io = plugin
                    plugins_flags_types[CheckpointIO.__name__] += 1
                elif isinstance(plugin, ClusterEnvironment):
                    self._cluster_environment_flag = plugin
                    plugins_flags_types[ClusterEnvironment.__name__] += 1
                elif isinstance(plugin, LayerSync):
                    if sync_batchnorm and not isinstance(plugin, TorchSyncBatchNorm):
                        raise MisconfigurationException(
                            f"You set `Trainer(sync_batchnorm=True)` and provided a `{plugin.__class__.__name__}`"
                            " plugin, but this is not allowed. Choose one or the other."
                        )
                    self._layer_sync = plugin
                    plugins_flags_types[TorchSyncBatchNorm.__name__] += 1
                else:
                    raise MisconfigurationException(
                        f"Found invalid type for plugin {plugin}. Expected one of: Precision, "
                        "CheckpointIO, ClusterEnvironment, or LayerSync."
                    )

            duplicated_plugin_key = [k for k, v in plugins_flags_types.items() if v > 1]
            if duplicated_plugin_key:
                raise MisconfigurationException(
                    f"Received multiple values for {', '.join(duplicated_plugin_key)} flags in `plugins`."
                    " Expected one value for each type at most."
                )

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Remove `sync_batchnorm=True` and keep only your LayerSync plugin, or remove the plugin and keep the flag (Lightning then uses TorchSyncBatchNorm).
  2. If you intended the standard behavior, pass `plugins=[TorchSyncBatchNorm()]` with `sync_batchnorm=False`, or just the flag with no plugin.
  3. Audit shared Trainer factory functions for double configuration.

Example fix

# before
trainer = Trainer(sync_batchnorm=True, plugins=[MyLayerSync()])
# after (pick one)
trainer = Trainer(sync_batchnorm=True)
# or
trainer = Trainer(plugins=[MyLayerSync()])
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.plugins.layer_sync import LayerSync, TorchSyncBatchNorm

def check_sync_bn(sync_batchnorm: bool, plugins: list):
    layer_syncs = [p for p in plugins if isinstance(p, LayerSync)]
    if sync_batchnorm and any(not isinstance(p, TorchSyncBatchNorm) for p in layer_syncs):
        raise ValueError('Choose either sync_batchnorm=True or a custom LayerSync plugin, not both')
    return plugins

Type guard

def sync_bn_config_ok(sync_batchnorm: bool, plugins) -> bool:
    ls = [p for p in (plugins or []) if isinstance(p, LayerSync)]
    return not sync_batchnorm or all(isinstance(p, TorchSyncBatchNorm) for p in ls)

Try / catch

except MisconfigurationException as e: if 'sync_batchnorm' in str(e): rebuild Trainer with sync_batchnorm=False, keeping the plugin

Prevention

When it happens

Trigger: Passing `Trainer(sync_batchnorm=True, plugins=[SomeLayerSyncSubclass()])` where the plugin is a LayerSync but not TorchSyncBatchNorm (e.g. a custom or Apex-style sync-BN plugin). Passing TorchSyncBatchNorm() itself is allowed and deduplicated.

Common situations: Config files that both enable the sync_batchnorm flag and list a custom LayerSync plugin from a shared template; migrating setups that used plugin-based sync batchnorm before the flag existed.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/129938b64b84e12e. Report an issue: GitHub.