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
enable_lora() must create LoRA A/B variables sized to the built kernel, so it requires the layer to already be built. Calling it on a freshly constructed Dense (no input shape seen yet) raises immediately.
Source
Thrown at keras/src/layers/core/dense.py:265
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
# 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(View on GitHub (pinned to 7a34a03db6)
Solutions
- Build the model first: model.build(input_shape) or model(x) once, then enable LoRA.
- Guard injection code with if not layer.built: build or skip.
- For Sequential models, call model.build() with the expected input shape before iterating.
Example fix
# before dense = keras.layers.Dense(64) dense.enable_lora(8) # not built yet # after dense = keras.layers.Dense(64) dense.build((None, 128)) dense.enable_lora(8)
Defensive patterns
Strategy: validation
Validate before calling
model.build(input_shape) # or model(x) once
for layer in model.layers:
if isinstance(layer, keras.layers.Dense) and layer.built:
layer.enable_lora(rank) Type guard
def ready_for_lora(layer) -> bool:
return layer.built and not getattr(layer, 'lora_enabled', False) Prevention
- Build models before LoRA injection
- Guard injection loops with layer.built
When it happens
Trigger: Calling dense.enable_lora(rank) before layer.build(input_shape) or before the layer has processed a batch — e.g. iterating model.layers immediately after Model() construction.
Common situations: LoRA-injection utilities that run before model.build()/first forward call; functional models where layers construct before input shapes propagate.
Related errors
- You must build the layer before accessing `kernel`.
- Lora is incompatible with kernel constraints. In order to en
- lora is already enabled. This can only be done once per laye
- lora is not currently supported with GPTQ quantization.
- Currently, `_float8_call` doesn't support LoRA
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/d058f52b1d504b6f.
Report an issue: GitHub.