{"record":{"id":"26907ee506c72363","repo":"Lightning-AI/pytorch-lightning","slug":"error-while-merging-hparams-the-keys-inconsisten","errorCode":null,"errorMessage":"Error while merging hparams: the keys {inconsistent_keys} are present in both the LightningModule's and LightningDataModule's hparams but have different values.","messagePattern":"Error while merging hparams: the keys (.+?) are present in both the LightningModule's and LightningDataModule's hparams but have different values\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/loggers/utilities.py","lineNumber":83,"sourceCode":"\n    hparams_initial = None\n    if pl_module._log_hyperparams and datamodule_log_hyperparams:\n        datamodule_hparams = trainer.datamodule.hparams_initial\n        lightning_hparams = pl_module.hparams_initial\n        inconsistent_keys = []\n        for key in lightning_hparams.keys() & datamodule_hparams.keys():\n            if key == \"_class_path\":\n                # Skip LightningCLI's internal hparam\n                continue\n            lm_val, dm_val = lightning_hparams[key], datamodule_hparams[key]\n            if (\n                type(lm_val) != type(dm_val)  # noqa: E721\n                or (isinstance(lm_val, Tensor) and id(lm_val) != id(dm_val))\n                or lm_val != dm_val\n            ):\n                inconsistent_keys.append(key)\n        if inconsistent_keys:\n            raise RuntimeError(\n                f\"Error while merging hparams: the keys {inconsistent_keys} are present \"\n                \"in both the LightningModule's and LightningDataModule's hparams \"\n                \"but have different values.\"\n            )\n        hparams_initial = {**lightning_hparams, **datamodule_hparams}\n    elif pl_module._log_hyperparams:\n        hparams_initial = pl_module.hparams_initial\n    elif datamodule_log_hyperparams:\n        hparams_initial = trainer.datamodule.hparams_initial\n\n    # Don't log LightningCLI's internal hparam\n    if hparams_initial is not None:\n        hparams_initial = {k: v for k, v in hparams_initial.items() if k != \"_class_path\"}\n\n    for logger in trainer.loggers:\n        if hparams_initial is not None:\n            logger.log_hyperparams(hparams_initial)\n        logger.log_graph(pl_module)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/loggers/utilities.py#L65-L101","documentation":"At the start of training, Lightning merges the LightningModule's and LightningDataModule's hyperparameters for logging; if a key exists in both but with different values (or different types, or non-identical tensors), it raises this RuntimeError because the ambiguity can't be resolved.","triggerScenarios":"Defining hparams like batch_size=32 on the LightningModule (e.g. via save_hyperparameters) and batch_size=64 on the LightningDataModule, then fitting with a datamodule — comparison uses type equality and value equality (identity for Tensors).","commonSituations":"Refactoring so both classes expose the same hparam name from argparse/config with stale defaults; passing different values to model and datamodule constructors from a config where they should be shared.","solutions":["Make the values identical (single source of truth: pass the same config value to both)","Remove the duplicated key from one of the two classes' saved hparams (usually the datamodule)","For tensors, pass the same tensor object to both or don't save it as an hparam"],"exampleFix":"# before\nmodel = LitModel(batch_size=32)\ndm = LitDataModule(batch_size=64)  # same key, different value\ntrainer.fit(model, datamodule=dm)\n# after\nbs = cfg.batch_size\nmodel = LitModel(batch_size=bs)\ndm = LitDataModule(batch_size=bs)\ntrainer.fit(model, datamodule=dm)","handlingStrategy":"validation","validationCode":"lm_h = dict(model.hparams); dm_h = dict(datamodule.hparams)\noverlap = set(lm_h) & set(dm_h)\nbad = [k for k in overlap and (type(lm_h[k]) != type(dm_h[k]) or lm_h[k] != dm_h[k])]\nassert not bad, f\"Mismatched shared hparams: {bad}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive both model and datamodule hparams from one config object","Avoid save_hyperparameters of the same key in both classes"],"tags":["lightning","hparams","merge-conflict","datamodule"],"backgroundTag":"duplicate-config-keys","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}