{"record":{"id":"f61d59f952c44c96","repo":"keras-team/keras","slug":"lora-is-incompatible-with-kernel-constraints-in-o-f61d59","errorCode":null,"errorMessage":"Lora is incompatible with kernel constraints. In order to enable lora on this layer, remove the `kernel_constraint` argument.","messagePattern":"Lora is incompatible with kernel constraints\\. In order to enable lora on this layer, remove the `kernel_constraint` argument\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/dense.py","lineNumber":259,"sourceCode":"            x = ops.add(x, self.bias)\n        if self.activation is not None:\n            x = self.activation(x)\n        return x\n\n    def compute_output_shape(self, input_shape):\n        output_shape = list(input_shape)\n        output_shape[-1] = self.units\n        return tuple(output_shape)\n\n    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","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/dense.py#L241-L277","documentation":"Dense.enable_lora() refuses to run when kernel_constraint is set: LoRA fine-tuning wraps the base kernel and applies its own update path, so a constrained kernel plus LoRA would apply the constraint incorrectly. The check fires before any LoRA state is created.","triggerScenarios":"Constructing Dense(kernel_constraint='max_norm' or keras.constraints.MaxNorm()) and later calling dense.enable_lora(rank, ...), typically from a LoRA-injection utility that walks all Dense layers.","commonSituations":"Applying blanket enable_lora() over a model where some layers were configured with regularization constraints; porting Keras 2 models that set kernel_constraint globally.","solutions":["Remove the kernel_constraint argument from Dense layers you want to LoRA-finetune.","In a LoRA injection loop, skip constrained layers: if layer.kernel_constraint: continue.","Apply regularization via activity_regularizer or externally instead of kernel_constraint."],"exampleFix":"# before\ndense = keras.layers.Dense(64, kernel_constraint=keras.constraints.MaxNorm(3))\ndense.build(x.shape)\ndense.enable_lora(8)\n\n# after\ndense = keras.layers.Dense(64)\ndense.build(x.shape)\ndense.enable_lora(8)","handlingStrategy":"validation","validationCode":"for layer in model.layers:\n    if isinstance(layer, keras.layers.Dense) and layer.kernel_constraint is None:\n        layer.enable_lora(rank)","typeGuard":"def lora_compatible(layer) -> bool:\n    return (getattr(layer, 'kernel_constraint', None) is None and layer.built\n            and not getattr(layer, 'lora_enabled', False)\n            and getattr(layer, 'quantization_mode', None) != 'gptq')","tryCatchPattern":null,"preventionTips":["Don't set kernel_constraint on LoRA-target layers","Check all enable_lora preconditions before calling"],"tags":["keras","lora","fine-tuning","constraints"],"backgroundTag":"lora-enable-precondition-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}