{"record":{"id":"d058f52b1d504b6f","repo":"keras-team/keras","slug":"cannot-enable-lora-on-a-layer-that-isn-t-yet-built-d058f5","errorCode":null,"errorMessage":"Cannot enable lora on a layer that isn't yet built.","messagePattern":"Cannot enable lora on a layer that isn't yet built\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/dense.py","lineNumber":265,"sourceCode":"        output_shape = list(input_shape)\n        output_shape[-1] = self.units\n        return tuple(output_shape)\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 correct input dimension for the LoRA A matrix. When\n        # the layer has been int4-quantized, `self._kernel` stores a *packed*\n        # representation whose first dimension is `ceil(input_dim/2)`. We\n        # saved the true, *unpacked* input dimension in `self._orig_input_dim`\n        # during quantization. Use it if available; otherwise fall back to the\n        # first dimension of `self.kernel`.\n        if self.quantization_mode == \"int4\" and hasattr(","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/dense.py#L247-L283","documentation":"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.","triggerScenarios":"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.","commonSituations":"LoRA-injection utilities that run before model.build()/first forward call; functional models where layers construct before input shapes propagate.","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."],"exampleFix":"# before\ndense = keras.layers.Dense(64)\ndense.enable_lora(8)  # not built yet\n\n# after\ndense = keras.layers.Dense(64)\ndense.build((None, 128))\ndense.enable_lora(8)","handlingStrategy":"validation","validationCode":"model.build(input_shape)  # or model(x) once\nfor layer in model.layers:\n    if isinstance(layer, keras.layers.Dense) and layer.built:\n        layer.enable_lora(rank)","typeGuard":"def ready_for_lora(layer) -> bool:\n    return layer.built and not getattr(layer, 'lora_enabled', False)","tryCatchPattern":null,"preventionTips":["Build models before LoRA injection","Guard injection loops with layer.built"],"tags":["keras","lora","lazy-build","precondition"],"backgroundTag":"lora-enable-precondition-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}