Lightning-AI/pytorch-lightning · error · MisconfigurationException

cluster_environment set through both strategy class and plug

Error message

cluster_environment set through both strategy class and plugins, choose one

What it means

A strategy instance with a cluster_environment attached conflicts with an explicit Trainer(plugins=[<ClusterEnvironment>]) setting. The connector enforces single ownership of the cluster environment configuration.

Source

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

                if self._accelerator_flag != "auto":
                    raise MisconfigurationException(
                        "accelerator set through both strategy class and accelerator flag, choose one"
                    )
                self._accelerator_flag = self._strategy_flag._accelerator
            if self._strategy_flag._precision_plugin:
                # [RFC] handle precision plugin set up conflict?
                if self._precision_plugin_flag:
                    raise MisconfigurationException("precision set through both strategy class and plugins, choose one")
                self._precision_plugin_flag = self._strategy_flag._precision_plugin
            if self._strategy_flag._checkpoint_io:
                if self.checkpoint_io:
                    raise MisconfigurationException(
                        "checkpoint_io set through both strategy class and plugins, choose one"
                    )
                self.checkpoint_io = self._strategy_flag._checkpoint_io
            if getattr(self._strategy_flag, "cluster_environment", None):
                if self._cluster_environment_flag:
                    raise MisconfigurationException(
                        "cluster_environment set through both strategy class and plugins, choose one"
                    )
                self._cluster_environment_flag = getattr(self._strategy_flag, "cluster_environment")

            if hasattr(self._strategy_flag, "parallel_devices") and self._strategy_flag.parallel_devices:
                if self._strategy_flag.parallel_devices[0].type == "cpu":
                    if self._accelerator_flag and self._accelerator_flag not in ("auto", "cpu"):
                        raise MisconfigurationException(
                            f"CPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,"
                            f" but accelerator set to {self._accelerator_flag}, please choose one device type"
                        )
                    self._accelerator_flag = "cpu"
                if self._strategy_flag.parallel_devices[0].type == "cuda":
                    if self._accelerator_flag and self._accelerator_flag not in ("auto", "cuda", "gpu"):
                        raise MisconfigurationException(
                            f"GPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,"
                            f" but accelerator set to {self._accelerator_flag}, please choose one device type"
                        )

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Provide the cluster environment in only one location (strategy or plugins)
  2. For SLURM, often neither is needed since it is auto-detected via env var detection

Example fix

# before
strategy = DDPStrategy(cluster_environment=SLURMEnvironment())
trainer = Trainer(strategy=strategy, plugins=[SLURMEnvironment()])
# after
strategy = DDPStrategy()
trainer = Trainer(strategy=strategy, plugins=[SLURMEnvironment()])
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.plugins import ClusterEnvironment
if isinstance(strategy, Strategy) and getattr(strategy, "cluster_environment", None):
    plugins = [p for p in plugins if not isinstance(p, ClusterEnvironment)]
trainer = Trainer(strategy=strategy, plugins=plugins)

Type guard

def cluster_env_conflict(strategy, plugins) -> bool:
    from lightning.pytorch.plugins import ClusterEnvironment
    return bool(getattr(strategy, "cluster_environment", None)) and any(isinstance(p, ClusterEnvironment) for p in (plugins or []))

Prevention

When it happens

Trigger: Trainer(strategy=DDPStrategy(cluster_environment=SLURMEnvironment()), plugins=[SLURMEnvironment()]) — any strategy with cluster_environment set plus a cluster env plugin/flag.

Common situations: HPC/SLURM setups where a shared strategy factory pre-binds SLURMEnvironment and the training script also adds it to plugins.

Related errors


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