{"record":{"id":"680f65acea5120dc","repo":"keras-team/keras","slug":"lora-is-already-enabled-this-can-only-be-done-onc-680f65","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/dense.py","lineNumber":269,"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 correct input dimension for the LoRA A matrix. When\n        # the layer has been int4-quantized, `self._kernel` stores a *packed*\n        # representation whose first dimension is `ceil(input_dim/2)`. We\n        # saved the true, *unpacked* input dimension in `self._orig_input_dim`\n        # during quantization. Use it if available; otherwise fall back to the\n        # first dimension of `self.kernel`.\n        if self.quantization_mode == \"int4\" and hasattr(\n            self, \"_orig_input_dim\"\n        ):\n            input_dim_for_lora = self._orig_input_dim\n        else:","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/dense.py#L251-L287","documentation":"enable_lora() is one-shot per layer: once LoRA variables exist, a second call would create duplicate A/B matrices and corrupt the low-rank update, so it is rejected.","triggerScenarios":"Calling dense.enable_lora(...) twice, commonly when a LoRA injection utility is re-run on the same model (retry logic, notebook re-execution, or wrapping an already LoRA-injected model).","commonSituations":"Re-running a fine-tuning setup cell; applying 'enable LoRA on all Dense' to a checkpoint that already has LoRA; loops without an idempotency guard.","solutions":["Guard injection with if not layer.lora_enabled: layer.enable_lora(...).","For different rank/alpha, rebuild or reload the model fresh rather than re-enabling.","Make LoRA-injection functions idempotent by checking the flag first."],"exampleFix":"# before\nfor layer in model.layers:\n    if isinstance(layer, keras.layers.Dense):\n        layer.enable_lora(8)  # raises on 2nd run\n\n# after\nfor layer in model.layers:\n    if isinstance(layer, keras.layers.Dense) and not layer.lora_enabled:\n        layer.enable_lora(8)","handlingStrategy":"validation","validationCode":"for layer in model.layers:\n    if isinstance(layer, keras.layers.Dense) and not layer.lora_enabled:\n        layer.enable_lora(rank)","typeGuard":"def needs_lora(layer) -> bool:\n    return not getattr(layer, 'lora_enabled', False)","tryCatchPattern":null,"preventionTips":["Make LoRA injection idempotent","Never re-run injection on already-injected models"],"tags":["keras","lora","idempotency","duplicate-call"],"backgroundTag":"lora-enable-precondition-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}