keras-team/keras · error · ValueError

Cannot enable lora on a layer that isn't yet built.

Error message

Cannot enable lora on a layer that isn't yet built.

What it means

EinsumDense.enable_lora() requires the layer to already be built (its kernel shape known) because the LoRA A/B matrices are created against the actual kernel dimensions. Calling it on an unbuilt layer raises this ValueError immediately so LoRA state is never half-initialized.

Source

Thrown at keras/src/layers/core/einsum_dense.py:341

        if self.activation is not None:
            x = self.activation(x)
        return x

    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 appropriate (unpacked) kernel shape for LoRA.
        if self.quantization_mode == "int4":
            # INT4 weights are stored in a flattened 2D layout that loses
            # the original N-dimensional structure required by the einsum
            # equation. We use `original_kernel_shape`` to ensure LoRA adapters
            # operate in the correct logical dimension space.
            kernel_shape_for_lora = tuple(self.original_kernel_shape)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Call the model on a dummy input once, or layer.build(input_shape), so the layer is built, then enable_lora.
  2. For Functional/Sequential models, run model.build(input_shape) before enabling LoRA.
  3. If loading from a checkpoint, enable LoRA after weights are loaded (loading guarantees build).

Example fix

# before
layer = keras.layers.EinsumDense('ab,bc->ac', output_dim=64)
layer.enable_lora(rank=8)  # ValueError: not yet built

# after
layer = keras.layers.EinsumDense('ab,bc->ac', output_dim=64)
layer.build(input_shape=(None, 128))
layer.enable_lora(rank=8)
Defensive patterns

Strategy: validation

Validate before calling

if not layer.built:
    layer.build(input_shape)
layer.enable_lora(rank)

Prevention

When it happens

Trigger: layer = keras.layers.EinsumDense(...); layer.enable_lora(4) before any input has triggered build(); enabling LoRA on a freshly constructed model that was never called on data or build()ed explicitly.

Common situations: Script order mistakes — enabling LoRA right after model construction instead of after building; functional models where you forgot model.build(input_shape) or a dummy forward pass before PEFT setup; migrating from libraries that implicitly build layers.

Related errors


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