keras-team/keras · error · ValueError

Lora is incompatible with kernel constraints. In order to en

Error message

Lora is incompatible with kernel constraints. In order to enable lora on this layer, remove the `kernel_constraint` argument.

What it means

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.

Source

Thrown at keras/src/layers/core/dense.py:259

            x = ops.add(x, self.bias)
        if self.activation is not None:
            x = self.activation(x)
        return x

    def compute_output_shape(self, input_shape):
        output_shape = list(input_shape)
        output_shape[-1] = self.units
        return tuple(output_shape)

    def enable_lora(
        self,
        rank,
        lora_alpha=None,
        a_initializer="he_uniform",
        b_initializer="zeros",
    ):
        if self.kernel_constraint:
            raise ValueError(
                "Lora is incompatible with kernel constraints. "
                "In order to enable lora on this layer, remove the "
                "`kernel_constraint` argument."
            )
        if not self.built:
            raise ValueError(
                "Cannot enable lora on a layer that isn't yet built."
            )
        if self.lora_enabled:
            raise ValueError(
                "lora is already enabled. This can only be done once per layer."
            )
        if self.quantization_mode == "gptq":
            raise NotImplementedError(
                "lora is not currently supported with GPTQ quantization."
            )
        self._tracker.unlock()
        # Determine the correct input dimension for the LoRA A matrix. When

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Remove the kernel_constraint argument from Dense layers you want to LoRA-finetune.
  2. In a LoRA injection loop, skip constrained layers: if layer.kernel_constraint: continue.
  3. Apply regularization via activity_regularizer or externally instead of kernel_constraint.

Example fix

# before
dense = keras.layers.Dense(64, kernel_constraint=keras.constraints.MaxNorm(3))
dense.build(x.shape)
dense.enable_lora(8)

# after
dense = keras.layers.Dense(64)
dense.build(x.shape)
dense.enable_lora(8)
Defensive patterns

Strategy: validation

Validate before calling

for layer in model.layers:
    if isinstance(layer, keras.layers.Dense) and layer.kernel_constraint is None:
        layer.enable_lora(rank)

Type guard

def lora_compatible(layer) -> bool:
    return (getattr(layer, 'kernel_constraint', None) is None and layer.built
            and not getattr(layer, 'lora_enabled', False)
            and getattr(layer, 'quantization_mode', None) != 'gptq')

Prevention

When it happens

Trigger: 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.

Common situations: Applying blanket enable_lora() over a model where some layers were configured with regularization constraints; porting Keras 2 models that set kernel_constraint globally.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/f61d59f952c44c96. Report an issue: GitHub.