{"record":{"id":"278ddfa4f47b25cd","repo":"keras-team/keras","slug":"lora-is-incompatible-with-kernel-constraints-in-o-278ddf","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/einsum_dense.py","lineNumber":335,"sourceCode":"        return tuple(full_output_shape)\n\n    def call(self, inputs, training=None):\n        x = ops.einsum(self.equation, inputs, self.kernel)\n        if self.bias is not None:\n            x = ops.add(x, self.bias)\n        if self.activation is not None:\n            x = self.activation(x)\n        return x\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 appropriate (unpacked) kernel shape for LoRA.","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L317-L353","documentation":"EinsumDense.enable_lora() refuses to enable LoRA when the layer was constructed with a kernel_constraint argument. Constraints (e.g. UnitNorm, MaxNorm) are applied to the kernel variable and are incompatible with the LoRA A/B factorization Keras maintains. The check runs before any LoRA state is created, so the layer is left unchanged.","triggerScenarios":"Calling layer.enable_lora(rank=...) on an EinsumDense (or a subclass such as an attention projection) that was created with kernel_constraint=... (e.g. keras.constraints.UnitNorm()).","commonSituations":"Copy-pasting a layer definition that included a norm constraint while adding PEFT/LoRA fine-tuning; loading a legacy model with constrained projections and trying to LoRA-ize it; config files that set kernel_constraint globally.","solutions":["Remove the kernel_constraint argument from the EinsumDense constructor (and from any config that sets it), then call enable_lora again.","If a norm-bounded kernel is required, apply normalization outside the layer (e.g. a separate normalization layer) so the kernel itself is unconstrained.","If you loaded the layer from a saved model, rebuild it without the constraint and transfer weights before enabling LoRA."],"exampleFix":"# before\nlayer = keras.layers.EinsumDense('ab,bc->ac', output_dim=64, kernel_constraint='unit_norm')\nlayer.enable_lora(rank=8)  # ValueError\n\n# after\nlayer = keras.layers.EinsumDense('ab,bc->ac', output_dim=64)\nlayer.enable_lora(rank=8)","handlingStrategy":"validation","validationCode":"if getattr(layer, 'kernel_constraint', None) is not None:\n    raise RuntimeError(f'{layer.name} has kernel_constraint; remove before enable_lora')\nlayer.enable_lora(rank)","typeGuard":null,"tryCatchPattern":"try:\n    layer.enable_lora(rank)\nexcept ValueError as e:\n    if 'kernel_constraint' in str(e):\n        # rebuild layer without constraint\n        ...","preventionTips":["Do not set kernel_constraint on layers you plan to LoRA-tune.","Assert layer.kernel_constraint is None before calling enable_lora."],"tags":["keras","lora","einsum-dense","kernel-constraint","peft"],"backgroundTag":"lora-unsupported-layer-config","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}