keras-team/keras · error · ValueError

lora is already enabled. This can only be done once per laye

Error message

lora is already enabled. This can only be done once per layer.

What it means

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.

Source

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

    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
        # the layer has been int4-quantized, `self._kernel` stores a *packed*
        # representation whose first dimension is `ceil(input_dim/2)`. We
        # saved the true, *unpacked* input dimension in `self._orig_input_dim`
        # during quantization. Use it if available; otherwise fall back to the
        # first dimension of `self.kernel`.
        if self.quantization_mode == "int4" and hasattr(
            self, "_orig_input_dim"
        ):
            input_dim_for_lora = self._orig_input_dim
        else:

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Guard injection with if not layer.lora_enabled: layer.enable_lora(...).
  2. For different rank/alpha, rebuild or reload the model fresh rather than re-enabling.
  3. Make LoRA-injection functions idempotent by checking the flag first.

Example fix

# before
for layer in model.layers:
    if isinstance(layer, keras.layers.Dense):
        layer.enable_lora(8)  # raises on 2nd run

# after
for layer in model.layers:
    if isinstance(layer, keras.layers.Dense) and not layer.lora_enabled:
        layer.enable_lora(8)
Defensive patterns

Strategy: validation

Validate before calling

for layer in model.layers:
    if isinstance(layer, keras.layers.Dense) and not layer.lora_enabled:
        layer.enable_lora(rank)

Type guard

def needs_lora(layer) -> bool:
    return not getattr(layer, 'lora_enabled', False)

Prevention

When it happens

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

Common situations: 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.

Related errors


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