{"record":{"id":"129938b64b84e12e","repo":"Lightning-AI/pytorch-lightning","slug":"you-set-trainer-sync-batchnorm-true-and-provide","errorCode":null,"errorMessage":"You set `Trainer(sync_batchnorm=True)` and provided a `{plugin.__class__.__name__}` plugin, but this is not allowed. Choose one or the other.","messagePattern":"You set `Trainer\\(sync_batchnorm=True\\)` and provided a `(.+?)` plugin, but this is not allowed\\. Choose one or the other\\.","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/accelerator_connector.py","lineNumber":240,"sourceCode":"        self._accelerator_flag = accelerator\n\n        precision_flag = _convert_precision_to_unified_args(precision)\n\n        if plugins:\n            plugins_flags_types: dict[str, int] = Counter()\n            for plugin in plugins:\n                if isinstance(plugin, Precision):\n                    self._precision_plugin_flag = plugin\n                    plugins_flags_types[Precision.__name__] += 1\n                elif isinstance(plugin, CheckpointIO):\n                    self.checkpoint_io = plugin\n                    plugins_flags_types[CheckpointIO.__name__] += 1\n                elif isinstance(plugin, ClusterEnvironment):\n                    self._cluster_environment_flag = plugin\n                    plugins_flags_types[ClusterEnvironment.__name__] += 1\n                elif isinstance(plugin, LayerSync):\n                    if sync_batchnorm and not isinstance(plugin, TorchSyncBatchNorm):\n                        raise MisconfigurationException(\n                            f\"You set `Trainer(sync_batchnorm=True)` and provided a `{plugin.__class__.__name__}`\"\n                            \" plugin, but this is not allowed. Choose one or the other.\"\n                        )\n                    self._layer_sync = plugin\n                    plugins_flags_types[TorchSyncBatchNorm.__name__] += 1\n                else:\n                    raise MisconfigurationException(\n                        f\"Found invalid type for plugin {plugin}. Expected one of: Precision, \"\n                        \"CheckpointIO, ClusterEnvironment, or LayerSync.\"\n                    )\n\n            duplicated_plugin_key = [k for k, v in plugins_flags_types.items() if v > 1]\n            if duplicated_plugin_key:\n                raise MisconfigurationException(\n                    f\"Received multiple values for {', '.join(duplicated_plugin_key)} flags in `plugins`.\"\n                    \" Expected one value for each type at most.\"\n                )\n","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/accelerator_connector.py#L222-L258","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove `sync_batchnorm=True` and keep only your LayerSync plugin, or remove the plugin and keep the flag (Lightning then uses TorchSyncBatchNorm).","If you intended the standard behavior, pass `plugins=[TorchSyncBatchNorm()]` with `sync_batchnorm=False`, or just the flag with no plugin.","Audit shared Trainer factory functions for double configuration."],"exampleFix":"# before\ntrainer = Trainer(sync_batchnorm=True, plugins=[MyLayerSync()])\n# after (pick one)\ntrainer = Trainer(sync_batchnorm=True)\n# or\ntrainer = Trainer(plugins=[MyLayerSync()])","handlingStrategy":"validation","validationCode":"from lightning.pytorch.plugins.layer_sync import LayerSync, TorchSyncBatchNorm\n\ndef check_sync_bn(sync_batchnorm: bool, plugins: list):\n    layer_syncs = [p for p in plugins if isinstance(p, LayerSync)]\n    if sync_batchnorm and any(not isinstance(p, TorchSyncBatchNorm) for p in layer_syncs):\n        raise ValueError('Choose either sync_batchnorm=True or a custom LayerSync plugin, not both')\n    return plugins","typeGuard":"def sync_bn_config_ok(sync_batchnorm: bool, plugins) -> bool:\n    ls = [p for p in (plugins or []) if isinstance(p, LayerSync)]\n    return not sync_batchnorm or all(isinstance(p, TorchSyncBatchNorm) for p in ls)","tryCatchPattern":"except MisconfigurationException as e: if 'sync_batchnorm' in str(e): rebuild Trainer with sync_batchnorm=False, keeping the plugin","preventionTips":["Configure sync batchnorm in exactly one place (flag OR plugin).","Write a build_trainer(**kwargs) factory that asserts this invariant.","Avoid copy-pasting plugin lists between projects."],"tags":["pytorch-lightning","sync-batchnorm","plugins","configuration-conflict"],"backgroundTag":"duplicate-conflicting-config","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}