{"record":{"id":"67fe565612800ba5","repo":"keras-team/keras","slug":"lora-is-already-enabled-this-can-only-be-done-onc-67fe56","errorCode":null,"errorMessage":"lora is already enabled. This can only be done once per layer.","messagePattern":"lora is already enabled\\. This can only be done once per layer\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/einsum_dense.py","lineNumber":345,"sourceCode":"    def enable_lora(\n        self,\n        rank,\n        lora_alpha=None,\n        a_initializer=\"he_uniform\",\n        b_initializer=\"zeros\",\n    ):\n        if self.kernel_constraint:\n            raise ValueError(\n                \"Lora is incompatible with kernel constraints. \"\n                \"In order to enable lora on this layer, remove the \"\n                \"`kernel_constraint` argument.\"\n            )\n        if not self.built:\n            raise ValueError(\n                \"Cannot enable lora on a layer that isn't yet built.\"\n            )\n        if self.lora_enabled:\n            raise ValueError(\n                \"lora is already enabled. This can only be done once per layer.\"\n            )\n        if self.quantization_mode == \"gptq\":\n            raise NotImplementedError(\n                \"lora is not currently supported with GPTQ quantization.\"\n            )\n        self._tracker.unlock()\n        # Determine the appropriate (unpacked) kernel shape for LoRA.\n        if self.quantization_mode == \"int4\":\n            # INT4 weights are stored in a flattened 2D layout that loses\n            # the original N-dimensional structure required by the einsum\n            # equation. We use `original_kernel_shape`` to ensure LoRA adapters\n            # operate in the correct logical dimension space.\n            kernel_shape_for_lora = tuple(self.original_kernel_shape)\n        else:\n            kernel_shape_for_lora = self.kernel.shape\n\n        # LoRA weights should be float32 to avoid the risk of underflow or","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L327-L363","documentation":"EinsumDense.enable_lora() is intentionally one-shot: once lora_enabled is True it cannot be enabled again, because the LoRA A/B kernels and tracker state already exist. A second call raises this ValueError to prevent duplicating LoRA parameters.","triggerScenarios":"Calling layer.enable_lora(...) twice on the same layer object; a training loop that re-runs a setup function each epoch or restart; re-invoking a LoRA-setup helper after resuming from a checkpoint that already had LoRA enabled.","commonSituations":"Notebook re-execution of a setup cell; idempotency-unaware training scripts that call enable_lora on every resume; a setup function called per-fold or per-run on the same layer.","solutions":["Guard the call: if not layer.lora_enabled: layer.enable_lora(...).","Move enable_lora into one-time model construction code so it is not re-run on resume.","If you intended a fresh LoRA, rebuild the layer/model instead of re-enabling on the existing one."],"exampleFix":"# before\nfor epoch in range(epochs):\n    layer.enable_lora(rank=8)  # second iteration raises\n\n# after\nif not layer.lora_enabled:\n    layer.enable_lora(rank=8)","handlingStrategy":"type-guard","validationCode":"if not layer.lora_enabled:\n    layer.enable_lora(rank)","typeGuard":"def lora_ready(layer) -> bool:\n    return layer.built and not layer.lora_enabled and layer.kernel_constraint is None","tryCatchPattern":null,"preventionTips":["Make setup code idempotent: check lora_enabled first.","Call enable_lora once at model construction, not inside the training loop."],"tags":["keras","lora","einsum-dense","idempotency","peft"],"backgroundTag":"lora-already-enabled","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}