{"record":{"id":"997cb5b2e9987516","repo":"Lightning-AI/pytorch-lightning","slug":"attribute-is-neither-stored-in-the-model-namespa","errorCode":null,"errorMessage":"{attribute} is neither stored in the model namespace nor the `hparams` namespace/dict, nor the datamodule.","messagePattern":"(.+?) is neither stored in the model namespace nor the `hparams` namespace/dict, nor the datamodule\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/parsing.py","lineNumber":304,"sourceCode":"    Checks for attribute in model namespace, the old hparams namespace/dict, and the datamodule.\n\n    \"\"\"\n    return _lightning_get_first_attr_holder(model, attribute) is not None\n\n\ndef lightning_getattr(model: \"pl.LightningModule\", attribute: str) -> Optional[Any]:\n    \"\"\"Special getattr for Lightning. Checks for attribute in model namespace, the old hparams namespace/dict, and the\n    datamodule.\n\n    Raises:\n        AttributeError:\n            If ``model`` doesn't have ``attribute`` in any of\n            model namespace, the hparams namespace/dict, and the datamodule.\n\n    \"\"\"\n    holder = _lightning_get_first_attr_holder(model, attribute)\n    if holder is None:\n        raise AttributeError(\n            f\"{attribute} is neither stored in the model namespace\"\n            \" nor the `hparams` namespace/dict, nor the datamodule.\"\n        )\n\n    if isinstance(holder, dict):\n        return holder[attribute]\n    return getattr(holder, attribute)\n\n\ndef lightning_setattr(model: \"pl.LightningModule\", attribute: str, value: Any) -> None:\n    \"\"\"Special setattr for Lightning. Checks for attribute in model namespace and the old hparams namespace/dict. Will\n    also set the attribute on datamodule, if it exists.\n\n    Raises:\n        AttributeError:\n            If ``model`` doesn't have ``attribute`` in any of\n            model namespace, the hparams namespace/dict, and the datamodule.\n","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/parsing.py#L286-L322","documentation":"lightning_getattr looks for the attribute in three places: the model namespace, model.hparams (namespace or dict), and the datamodule. If none holds it, AttributeError is raised. Used by batch-size finders and auto-scale logic to read 'batch_size'.","triggerScenarios":"Running trainer.tuner.scale_batch_size or lr_find on a model that stores batch size under a different name (e.g. self.bs or only in the dataloader method) and has no datamodule attribute.","commonSituations":"Auto batch-size scaling where the LightningModule uses self.batch_size locally but never assigns it as instance attribute, or save_hyperparameters wasn't called so hparams is empty.","solutions":["Set the attribute on the LightningModule: self.batch_size = 32 in __init__","Or save it into hparams: self.save_hyperparameters({'batch_size': 32})","Or attach it to the datamodule and pass the datamodule to the Trainer"],"exampleFix":"# before\nclass M(LightningModule):\n    def __init__(self, bs=32):\n        super().__init__()\n        # batch size never stored\n# after\nclass M(LightningModule):\n    def __init__(self, batch_size=32):\n        super().__init__()\n        self.batch_size = batch_size","handlingStrategy":"validation","validationCode":"from lightning.pytorch.utilities.parsing import lightning_hasattr  # if available\nhas = hasattr(model, 'batch_size') or 'batch_size' in getattr(model, 'hparams', {}) or hasattr(dm, 'batch_size')\nassert has, 'batch_size not found anywhere'","typeGuard":"def has_tunable_attr(model, name='batch_size', dm=None) -> bool:\n    return any(hasattr(o, name) or name in getattr(o, 'hparams', {})\n               for o in (model, dm) if o is not None)","tryCatchPattern":"try:\n    val = lightning_getattr(model, 'batch_size')\nexcept AttributeError:\n    val = default_bs","preventionTips":["Always store batch_size as an instance attribute or hparam","Keep naming consistent ('batch_size', not 'bs')"],"tags":["attribute-lookup","batch-size","tuner","lightning"],"backgroundTag":"missing-attribute","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}