{"record":{"id":"7fb0bf584725fb88","repo":"keras-team/keras","slug":"you-must-build-the-layer-before-accessing-kernel-7fb0bf","errorCode":null,"errorMessage":"You must build the layer before accessing `kernel`.","messagePattern":"You must build the layer before accessing `kernel`\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/einsum_dense.py","lineNumber":242,"sourceCode":"                shape=tuple(bias_shape),\n                initializer=self.bias_initializer,\n                regularizer=self.bias_regularizer,\n                constraint=self.bias_constraint,\n                dtype=self.dtype,\n                trainable=True,\n            )\n        else:\n            self.bias = None\n        self.built = True\n        if self.lora_rank:\n            self.enable_lora(self.lora_rank, lora_alpha=self.lora_alpha)\n\n    @property\n    def kernel(self):\n        from keras.src.quantizers import gptq_core\n\n        if not self.built:\n            raise AttributeError(\n                \"You must build the layer before accessing `kernel`.\"\n            )\n\n        mode = self.quantization_mode\n        is_gptq = mode == \"gptq\"\n        is_awq = mode == \"awq\"\n        is_int4 = mode == \"int4\"\n        gptq_calibrated = bool(getattr(self, \"is_gptq_calibrated\", False))\n        awq_calibrated = bool(getattr(self, \"is_awq_calibrated\", False))\n        gptq_bits = (\n            gptq_core.get_weight_bits_for_layer(self, None) if is_gptq else None\n        )\n\n        # Decide the source tensor first (packed vs already-quantized vs plain\n        # kernel)\n        if is_gptq and gptq_calibrated and gptq_bits not in (2, 4):\n            # calibrated GPTQ, not a packed bit-width, no unpacking needed\n            kernel = self.quantized_kernel","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L224-L260","documentation":"EinsumDense.kernel is a property over the weight variable, which is only created when build() runs (weights are lazy, derived from input shape and equation resolution). Accessing .kernel before build raises AttributeError since no variable exists.","triggerScenarios":"Reading layer.kernel (or .kernel.shape) on an EinsumDense that hasn't seen an input shape — before layer(input) or layer.build(input_shape).","commonSituations":"Weight-initialization or inspection code right after construction; custom loading logic reading kernel before build; functional models probed before inputs resolve.","solutions":["Build the layer first: layer.build(input_shape) or run a forward pass, then access .kernel.","Use compute_output_shape / equation shape utilities instead of reading the kernel for shape logic.","For whole models, call model.build(input_shape) before inspecting layer weights."],"exampleFix":"# before\nlayer = keras.layers.EinsumDense('ab,bc->ac', output_shape=(None, 64))\nprint(layer.kernel.shape)  # AttributeError\n\n# after\nlayer = keras.layers.EinsumDense('ab,bc->ac', output_shape=(None, 64))\nlayer.build((None, 32))\nprint(layer.kernel.shape)","handlingStrategy":"validation","validationCode":"if not layer.built:\n    layer.build(input_shape)\nk = layer.kernel","typeGuard":"def safe_kernel(layer):\n    return layer.kernel if layer.built else None","tryCatchPattern":null,"preventionTips":["Build before reading weights","Use compute_output_shape for shape logic"],"tags":["keras","einsum-dense","lazy-build","attribute-error"],"backgroundTag":"keras-layer-not-built-yet","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}