keras-team/keras · error · AttributeError
You must build the layer before accessing `kernel`.
Error message
You must build the layer before accessing `kernel`.
What it means
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.
Source
Thrown at keras/src/layers/core/einsum_dense.py:242
shape=tuple(bias_shape),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
dtype=self.dtype,
trainable=True,
)
else:
self.bias = None
self.built = True
if self.lora_rank:
self.enable_lora(self.lora_rank, lora_alpha=self.lora_alpha)
@property
def kernel(self):
from keras.src.quantizers import gptq_core
if not self.built:
raise AttributeError(
"You must build the layer before accessing `kernel`."
)
mode = self.quantization_mode
is_gptq = mode == "gptq"
is_awq = mode == "awq"
is_int4 = mode == "int4"
gptq_calibrated = bool(getattr(self, "is_gptq_calibrated", False))
awq_calibrated = bool(getattr(self, "is_awq_calibrated", False))
gptq_bits = (
gptq_core.get_weight_bits_for_layer(self, None) if is_gptq else None
)
# Decide the source tensor first (packed vs already-quantized vs plain
# kernel)
if is_gptq and gptq_calibrated and gptq_bits not in (2, 4):
# calibrated GPTQ, not a packed bit-width, no unpacking needed
kernel = self.quantized_kernelView on GitHub (pinned to 7a34a03db6)
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.
Example fix
# before
layer = keras.layers.EinsumDense('ab,bc->ac', output_shape=(None, 64))
print(layer.kernel.shape) # AttributeError
# after
layer = keras.layers.EinsumDense('ab,bc->ac', output_shape=(None, 64))
layer.build((None, 32))
print(layer.kernel.shape) Defensive patterns
Strategy: validation
Validate before calling
if not layer.built:
layer.build(input_shape)
k = layer.kernel Type guard
def safe_kernel(layer):
return layer.kernel if layer.built else None Prevention
- Build before reading weights
- Use compute_output_shape for shape logic
When it happens
Trigger: Reading layer.kernel (or .kernel.shape) on an EinsumDense that hasn't seen an input shape — before layer(input) or layer.build(input_shape).
Common situations: Weight-initialization or inspection code right after construction; custom loading logic reading kernel before build; functional models probed before inputs resolve.
Related errors
- You must build the layer before accessing `kernel`.
- Cannot enable lora on a layer that isn't yet built.
- Lora is incompatible with kernel constraints. In order to en
- Cannot enable lora on a layer that isn't yet built.
- lora is already enabled. This can only be done once per laye
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/7fb0bf584725fb88.
Report an issue: GitHub.